Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4.5 LAB: Using the Decision TreeClassifier() on the iris data Write a program that splits a dataset into training and test set, builds a classification

image text in transcribedimage text in transcribedimage text in transcribed
image text in transcribedimage text in transcribedimage text in transcribed
4.5 LAB: Using the Decision TreeClassifier() on the iris data Write a program that splits a dataset into training and test set, builds a classification tree, and outputs a confusion matrix. The program should do the following: load the iris.csv dataset create a dataframe, x, using the petal_length and sepal_length as features create a dataframe, y, using species split the data into training and test sets with 0.25 test size and random_state = 0 standardize x_train and x_test initialize the decision tree with criterion = "gini", random_state = 100, max_depth=3, min_samples_leaf=5 run the decision tree on x_test generate the confusion matrix The output should be: [[14 0 01 [ 0 13 1] [ 0 1 9]] 430708.3840102 pamp/3 LAB ACTIVITY 4.5.1: LAB: Using the Decision TreeClassifier() on the iris data 0/1 main.py Load default template... 1 # Loads the necessary Libraries 2 import pandas as pd 3 from sklearn. model_selection import train_test_split 4 from sklearn. preprocessing import StandardScaler 5 from sklearn. tree import DecisionTreeClassifier from sklearn import datasets 7 from sklearn import metrics 9 & Load the iris dataset 10 iris = datasets . load_iris( ) 11 12 Asubset of data containing the petal Length and sepal Length 13 x = iris. data 14 Asubset of data containing the species Label 15 y = iris. target 16 17 18 # Split the data into a training set and a test set 19 X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=0) 20 21 # standardize x_train and x_test 22 scaler = StandardScaler() 23 X_train = scaler. fit_transform(X_train) 24 X_test = scaler. fit_transform(X_test) 25 26 Acriterion = "gini", random_state = 109, max_depth=3, min_samples_Leaf=5 27 cart=DecisionTreeClassifier (random_state = 100, criterion="gini", max_depth=3, min_samples_leaf=5) 28 cart. fit(X_train, y_train) 29 30 Muse the model to pridict on test dataset 31 y_pred = cart. predict(X_test) 32 33 # give the confusion matrix using y_test and y_pred 34 #this give the matix of how many correctly identified and how many incorrectly identified. 35 conf= metrics.confusion_matrix(y_test, y_pred) 36 print (conf)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Algebra And Trigonometry Enhanced With Graphing Utilities

Authors: Michael Sullivan, Michael Sullivan III

7th Edition

0134269039, 9780134269030

More Books

Students also viewed these Mathematics questions