1
+ # -*- coding: utf-8 -*-
2
+ """
3
+
4
+ @author: FaizMohammad
5
+ """
6
+
7
+ #importing all the required Modules
8
+ import numpy as np
9
+ import pandas as pd
10
+ import matplotlib .pyplot as plt
11
+ from sklearn .datasets import load_iris
12
+ from sklearn .metrics import confusion_matrix ,accuracy_score
13
+ from sklearn .cross_validation import train_test_split
14
+ from sklearn .tree import DecisionTreeClassifier
15
+
16
+
17
+ #insert iris_data from sklearn.datasets and separate features and target
18
+ #let's take X = features and y = Target
19
+
20
+ iris = load_iris ()
21
+ X = iris .data
22
+ y = iris .target
23
+
24
+ #Now let's divide the data for traning and testing with test-size= 20%
25
+ X_train ,X_test ,y_train ,y_test = train_test_split (X ,y ,test_size = 0.2 ,random_state = 0 )
26
+
27
+ #Select Decision tree Classifier algorithm for model fitting
28
+ classifier = DecisionTreeClassifier ()
29
+ classifier .fit (X_train ,y_train )
30
+
31
+ #Now check the Model prediction with testing data
32
+ predict = classifier .predict (X_test )
33
+
34
+ #and at last check the Model Accuracy
35
+ cm = accuracy_score (pred ,y_test )
36
+
37
+ #plot (X VS y )graph where X-label is sepal length(cm) and y-label is sepal width(cm)
38
+ feature1 = 0
39
+ feature2 = 1
40
+ formatter = plt .FuncFormatter (lambda i , * args : iris .target_names [int (i )])
41
+ plt .figure (figsize = (5 ,4 ))
42
+ plt .scatter (X [:,feature1 ],X [:,feature2 ],c = y )
43
+ plt .colorbar (ticks = [0 ,1 ,2 ],format = formatter )
44
+ plt .xlabel (iris .feature_names [0 ])
45
+ plt .ylabel (iris .feature_names [1 ])
46
+ plt .show ()
0 commit comments