0% found this document useful (0 votes)
8 views10 pages

Decision Tree

Python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Decision Tree

Python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
2610972026, 19:48 Unttledt - Jupyter Notebook In [1]: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import warnings warnings. filterwarnings(" ignore") In [2]: from sklearn.datasets import load_iris In [3]: iris=load_iris() In [4]: iris > out[4]: {‘data’: array([[5. (4.9, (4.7, [4.6, (Ss. , (5.4, [4.6, [s., [4.4, [4.9, (5.4, [4.8, [4.8, (4.3, [s.8, (5.7, (5.4, [5.1, (5.7, » 3.5, 1.4, 0.2], 229 9SS9S999999999990H - bNb oa RO DEL pub aRE ERR ERR RRR PUUURaYuUuue Nowe www WUbbGRE RODD RORY DES ronos In [5]: iris.data (7.9, (6.4, (6.3, [6.1, (7.7, (6.3, [6.4, [6., [6.9, (6.7, [6.9, (5.8, (6.8, [6.7, (6.7, (6.3, [6.5, (6.2, (5.9, BB! ame me Rib - f Yunnan kunanunas’ localhost 8888inotebooks/Unitle.ipyn7™kemel_name=python3 sn0 2610972026, 19:48 Untitled - Jupyter Notebook need dep petal_width 02 02 02 02 02 23 19 20 23 & indep var species setosa setosa sotosa setosa virginica virginica virginica virginica In [6]: iris.target out{6]: array([@, ®, @ ®, @ ®, @ 2, 2 2% 2, 2, @ 2, 2, 2, 2, 2, 2, 2, e, 2, @, 2 2 2 1, 1, 1, 1, 4441,1,1,1,1,1,1, LLLL4143411,1, 2, 2, 2, 2, 2, 25 2, 25 2, 2s 2, 2, 2, 25 2, 25 2, 2; 2, 2s In [14]: ns. load_dataset(" In [13]: #for performing each & every algo we In [15]: df out[15]: sepal_length sepal_width petal_length ° 54 35 14 1 49 3.0 14 2 47 32 13 3 46 34 15 4 50 36 14 145 a7 30 62 146 63 25 50 147 6s 30 62 148 62 34 54 149 59 30 5A 150 rows * 5 columns In [16]: xedf.iloc[: :1] ris. target localhost 8888inotebooks/Unitle.ipyn7™kemel_name=python3 18 virginica 210 2610972026, 19:48 In [17]: x out [17 145 148 147 148 149 Be 49 47 46 50 67 63 65 62 59 150 rows * 4 columns Tn [28]: y out[18]: In [19 array([@, from sklearn.model, 35 3.0 32 34 36 3.0 25 3.0 34 3.0 |_selection import train_test_split x_train,x_test,y_train,y test = train_test_split( Untitled - Jupyter Notebook 14 14 13 15 14 52 50 62 84 8A x,y, test_size=0.33,random_state=42) localhost 8888inotebooks/Unitle.ipyn7™kemel_name=python3 sepal_length sepal_width petal_length petal_width 02 02 02 02 02 23 19 20 23 18 30 2610972026, 19:48 Unttledt - Jupyter Notebook In [20]: x_train out [20 sepal_length sepal_width petal_length petal_width 96 57 29 42 13 105 76 3.0 66 24 65 56 30 45 15 ° 54 35 14 02 122 17 28 67 20 n Cs 28 40 13 106 49 25 45 1 14 58 40 12 02 2 58 26 40 12 102 7. 3.0 59 24 100 rows x 4 columns localhost 8888inotebooks/Unitle.ipyn7™kemel_name=python3 40 2610972026, 19:48 Untitled - Jupytor Notobook In [21 xtest localhost 8888inotebooks/Unitle.ipyn7™kemel_name=python3 510 2610972026, 19:48 out[21]: Ay 18 18 7 76 a at 68 82 10 2 36 19 56 108 6 55 132 2 sar 26 128 431 14s 108 143 45 30 2 18 65 " 2 148 5 2 sepal_longth sepal_width petal_| 6 57 77 50 68 54 56 69 62 58 65 48 55 49 51 63 6s 56 57 64 47 6 50 64 79 67 87 68 48 48 46 57 67 48 44 63 64 52 28 38 26 29 28 3a 29 a4 22 ar 32 30 35 a4 38 a3 30 25 28 28 32 30 3a 28 38 30 25 32 30 34 36 44 a4 34 32 25 32 a5 localhost 8888inotebooks/Unitle.ipyn7™kemel_name=python3 Untitled - Jupyter Notebook 69 45 48 15 36 54 45 39 54 14 13 15 15 47 58 39 45 56 16 49 16 56 64 52 58 59 14 16 10 15 44 16 13 50 45 18 23 18 14 oa 13 23 15 12 20 oa 02 o4 03 16 22 1 13 22 02 18 oa 24 20 23 18 23 03 02 02 oa 14 02 02 19 18 02 a0 2610972026, 19:48 Untitled - Jupytor Notobook sepal_length sepal_width petal_length petal_width 4 5.0 36 14 02 32 52 4a 18 o4 142 58 27 54 19 85 60 34 45 16 86 67 34 47 15 6 54 39 13 04 0 54 37 15 02 a 55 24 37 10 133 63 28 54 15 137 64 a4 55 18 15 66 30 44 14 109 72 36 64 25 In [22]: y_train out [22]: array([1, 2, 1, @, 2, 1, ®, @, @, 1, 2, @, ®, 8, 1, @, 1, 2, 4, 2, @ 2,2, 4, 1 @. 1 L, 2, 1, @, @, 2, 2, @, @, @ 1, 2, @ 2, 2, @ 1, 1, 2, 1, 2, @ 2, 1, 2,1, 1, 1, ®, 1, 1, @ 1, 2, 2, ® 1, 2, 2, @ 2, % 4, 2, 2, 1,2, 4, 1, 2, 2, @ 4, 2, @ 1, 2]) In [23]: y_test out[23]: array({1, @, 2, 1, 1, @ 1, 2, 1, 1, 2, @ ® 8 @ 1, 2, 1,1, 2, 8 2, ®, 2, 2, 2, 2, 2, @ @, @ @, 1, 2 ® 2, 1, 0, @ 2, 2, 1, 1, a, ®, 1, 2, 2, 1, 2)) In [24]: from sklearn.tree import DecisiontreeClassifier In [41]: 1=DecisionTreeClassifier() In [42]: Outl42]: |. pecisionTreeClassifier | f becisiontreeciassifier() SS > In [43]: a.fit(x_train,y_train) Outl43]: pecisionTreeClassifier | adeps://ecteit- Ce : (tens: // sits es/generated/sklearn.tree.Decisiontry DecisionTreeClassifier() ee > localhost 8888inotebooks/Unitle.ipyn7™kemel_name=python3 70 2610972026, 19:48 Unttledt - Jupyter Notebook In [44]: from sklearn import tree plt.figure(Figsize=(15,10)) tree.plot_tree(a, filled=True) out[44]: [Text(o.5416666666666666, 0.9285714285714286, ‘x[3] <= @.8\ngini = 0.666\n samples = 100\nvalue = [31, 35, 34]'), Text (0.4583333333333333, @.7857142857142857, ‘gini = @.0\nsamples = 31\nv alue = (31, @, @]'), Text (0.5, @.8571428571428572, ‘True '), Text (0.625, @.7857142857142857, ‘x[3] <= 1.75\ngini = 0.5\nsamples value = [0, 35, 34]'), Text (0. 5833333333333333, @.8571428571428572, ‘ False’), Text (0.4166666666666667, @.6428571428571429, ‘x[2] <= 5.35\ngini = 0.188 \nsamples = 38\nvalue = [@, 34, 4]'), Text (0.3333333333333333, 0.5, ‘x[3] <= 1.65\ngini = @.105\nsamples = 36\n value = [@, 34, 2]'), 69\n Text (0, 16666666666666666, 0.35714285714285715, ‘x[2] <= 4.95\ngini = 0.05 7\nsanples = 34\nvalue = [8, 33, 1]"), Text (@.08333333333333333, @.21428571428571427, ‘gini = @.@\nsamples = 32 \nvalue = [@, 32, @]"), Text(@.25, @.21428571428571427, ‘x[2] <= 5.05\ngini = @.5\nsamples = 2\nv alue = [@, 1, 1)'), Text (0, 16666666656666666, 0.07142857142857142, ‘gini = @.@\nsamples = 1\n value = [®, 8 1]'), Text (@.3333333333333333, 0.07142857142857142, ‘gini = @.0\nsamples = 1\nv alue = [@, 1, @]'), Text(@.5, @.35714285714285715, 'x[@] <= 5.8\ngini = @.5\nsamples = 2\nval ue = (2, 1, 1]'), Text (0. 4166666666666667, 0.21428571428571427, ‘gini = @.0\nsamples = 1\nv alue = [@, ®, 1]'), Text (@.5833333333333334, @.21428571428571427, ‘gini = @.0\nsamples = 1\nv alue = [@, 1, @]'), Text(@.5, 0.5, ‘gini = @.0\nsanples = 2\nvalue = [8, ®, 2]'), Text (@.8333333333333334, 0.6428571428571429, 'x[2] <= 4.85\ngini = 0.062 \nsamples = 31\nvalue = [@, 1, 30]'), Text (0.75, 8.5, 'x[@] <= 5.95\ngini = @.444\nsamples = 3\nvalue = [0, 1, 2)"), Text(0.6666666666666666, @.35714285714285715, ‘gini = @.0\nsamples = 1\nv alue = [8, 1, 0]'), Text (@.8333333333333334, @.35714285714285715, ‘gini = @.0\nsamples = 2\nv alue = [@, ®, 2]'), Text (0.9166666666666666, 0.5, ‘gini = @.0\nsamples = 28\nvalue = [0, @, 2 8)")] localhost 8888inotebooks/Unitle.ipyn7™kemel_name=python3 ano 2610972026, 19:48 Untitled - Jupyter Notebook x= 3 samples -predict(x_test) In [45 y_pred: In [46]: y_pred array([1, ®, 2, 1, 1, ® 1, 2, 1, 1, 2, ® ® @ @ 1, 2, 1, 1, 2 @, 2, 2, 2, 2, 2, 0 0, @, 8, 1, 0, 8 2, 1, 8, 2, a, 2, 1, @, 1, 1, 2, 1, 2)) In [47]: from sklearn.metrics import accuracy_score,classification_report In [48]: score=accuracy_score(y_pred,y_test) print(score) @.98 In [49]: classification_report(y_pred,y test) out[49]: * precision recall fi1-score —support\n\n 1.00 1.00 1.00 19\n 1 1.00 0.97 16\n 2 0.94 1.00 0.97 accuracy 0.98 5@\n macro avg 0.98 0.98 5@\nweighted avg 0.98 0.98 5@\n" In [Js localhost 8888inotebooks/Unitle.ipyn7™kemel_name=python3 @ 2.94 15\n\n 0.98 0.98 eno 2610972026, 19:48 Unttledt - Jupyter Notebook localhost 8888inotebooks/Unitle.ipyn7™kemel_name=python3 1010

You might also like