0 ratings0% found this document useful (0 votes) 20 views9 pagesDecision Tree
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
sav eae
import pandas 35 aypd
Amport nunsy 25 nye
from sklearn.tree import DectsionTreeclassi fen
from sklearn nodel_selection dnport train test split
from sklearn.netrics import confusion nacrix
from sklearninctrics dnpert accuracy_score
from sklearn.netrics import classification report
import natplotisb.ayplot as ayplot
from sklearn import tree
from skiearn.ensenole Inport RandenForest¢iassifter
ssydatannypd.read_csv("C:/Users/hp/Oounoads /data csv")
sydata, info)
Rangelndex: S69 entries, @ to S6B
Data colunns (total 33 colums)
clue Hon-mull Count type
ie 59 nonnull inte
agnosis Se9 nonsnll object,
radius nean Ses on-ruldFlstea
ectre nean Sco non-null Floated
perinetar-nean 565 non-null floated
brea aes 5s non-null Floated
Snoothness_nean 565 non-null floated
Comoctnessinean ——-SeD non-null Floated
concavity. naan 565 non-null floats
Concave polnts_tean—SeD non-null Floated
symetryaean 565 non-null floats
Stactaldnension_pean 569 non-null floated
radius. se 565 non-null floates
texture se 565 non-null floats
perinetarse 565 non-null floated
tease 565 non-null floates
snocthness_se Se5 non-nail Floatet
Conpactnesi se 565 non-null flootet
toneaity 58 Fister
floates
Syme se ‘Homessavnaa. sae
a
2
2
2
23
28
2
28
2
Fr
a
32
evr:
fractal_dinension_se
rodiue_poret
texture worst
perineter worst
area_worst
Enoathness worst
compactness uorst
concavity worst
concave paints worst
symetry worst
fractal dinension worst
Unnared: 32
memory usage: 146.84 XB
sydata.deop("id",
syaata. info)
569
369
569
569
569
569
569
569
569
569
569
non-null
fon-nuld
ron-nuld
fon-nuld
ron-rull
fon-nuld
non-null
fon-nuld
ron-nuld
fon-nuld
oa-ruld
@ non-null
floate4(31), intsa(2), ebject(2)
"uonaned: 32"), axiseL, inpla
Rangetndex:
loatsa
floatet
Floatsa
Floats
Floatsa
‘floatet
floats
floated
floats
#loatet
Hloatss
Floats
Non-Mull Count type
569 entries, @ to 568
Data coluns (total 31 colums):
elu
diagnosis
radius_nean
texture ean
perineter_nean
snoothness. ean
compactness mean
concavity. nen
concave paints mean
symetry scan
‘ractal_dinension mean
redius_ se
texture_se
perinetér_se
snoathness_ se
compactness se
concavity se
concave points se
569
569
569
569
569
569
569
569
569
569
559
569
569
569
559
569
569
569
569
on-nul
ron-nuld
fon-nuld
ron-ruld
fon-null
ogra
fon-nul
fon-rall
mon-nul
ron-rull
fon-rull
fon-nul
fon-null
fon-null
fon-rull
fon-rull
roa-rull
ron-ruld
fon-nuld
ail
(sig ain on RATE [gst
object
Floatsa
Floats
Floatsa
Floats
‘loatoa
floated
loatoa
floated
Floated
Aloatss
Floatea
Floated
Floated
Aloatss
Floated
Aoatss
Floatsa
floatet
Floated
floatetsav eae con Ye
21 radius worst 569 non-null fleatsa
22 texture worst 569 non-null floated
23 perineter worst 569 non-null fleatsa
24 breaworst 5369 non-null fleatsa
25 Snoothness worst 569 non-null fleatsa
25 compactness worst 569 non-null floated
27 concavity worst 568 non-null floats
2B concave paints worst 569 non-null floatsd
29 symetry worst 568 non-null floats
5 fractal. dinension worst $69 non-null floatéd
types: #loatea(se), object(1)
memory usage: 137.94 KB
4 Separating the target vartable
X = mydata.values[:, 2]
Y= mydatavalues{:, &]
Ktrain, xzest,¥ train, Y_test = train sest_split(x, ¥, test_size
Yirainisie
455
4 Function t0 perform training with giniindex.
fee train using gial(%,¥)
¥ creating the clossifter object
‘clf_gind = DecistontreeClassifier(criterson
ni" randon_state = 100,max dept
4 performing training
ce gin EK,
return clf_gink
# Function to perform training with entropy.
def Carin using. entropy (X,Y)
1 bectston tree with entropy
cLfLentropy * Decistontreeclassifier(criterion » "entropy", randon_state = 18¢ymax_depth = 28, nin_sanples_lsaf = 3)
4 performing training
eLf_entropy- 4806)
_eading Nat afaoutg/ConmontTMl fone ontsav eae
# Function to perform training with random forest classifier,
def tarin_using efe(A¥yn est)
rfc = RanconForestClassirien(n_estinatorsen_est, randon_stater
fee fitOK ¥)
return rf
1+ Function to make predictions
det prediction(x test, clf object)
# Predicton on test
Yyapred = elf object.predict(X test)
Dprint( "Predicted vaiues:")
print(y pred)
return y_pred
det cal_aceuracy(y_test, y_pred)
prlat("confusian Matcie: "confusion matebx(y_test, y_preé))
print ("Accuracy : ",accunacy_score(y.test,y- pred) +108)
Drint("Repart : ",classifieation, renert(y_ test, y-pred))
‘ranyaatacoluans.values
ene['h, 8")
fet gen tree(ci#)
Fig, sx08 » yplot.subplots(nrous = Lyncols = 2, flgsize = (747), spie500)
‘Eree.plot_tree(elf, feature nanes ~ fn,class_sanee-cn, filled ~ true)
nypiot. show)
‘Fipssaveftgt"snagenane.prg')
cle gin » erotn_using pint (X train, V_tratn)
Prediction using gint
print("Results Using Gini Tadex:")
Yopred_gint = predietion(X test, elf_gint)
cal_aceuracy(¥_test, y_pred_gini)
fenctrea(el ins)
_eading Mat liopoiConmeniTM foneiTRonestsPredicted values
c
3° tm
ae
me
ae
aoe
18 Ww
Confusion Matrix:
(433]]
Accuracy
Report:
8
"
accuracy
cro ave
weighted avg.
oe ee
%
Bas
a
m]
(1683)
93.85964912280701
precision
0.96
ean
0.93
2.94
recall
_eading ataaoupuiConmeniTMt oneness
aa
ane
mon OW
an
Fi-seore support
n
8
aa
ua
uaclt_entropy = tarin_using_entropy( train, ¥_train)
# prediction using entropy
print(*Results Using tntropy:*)
Yuprea_entropy = preaiction(X test, elf_entropy)
al sccuracy(_test, y_pres_entrony)
enstree(elt_entrony)
Results Using Entropy
Predicted values
(at kw a ae on ow sow oa!
ON Ee woe ae
a
eee
aN
aN
om
~
a
~
a
~Confusion Matrix: [178 2]
T3491]
Accuracy : 96.49122807017544
Report: ‘precision recall. fi-score support
8 0.96 0990.97 n
Meme 083 019s a
accuracy 0.96
macro avg 0.97 0.96 0.96 td
weighted avg 0.970.950.9518
_eading Mat aop ConmanITM faneiTRonstsclé_rfe = tarin_using efc(X train, Y_train, 28)
4 Prediction using entropy
prine(Results Using Fntrony:*)
Ypres rfe = prediction(x test, clf_rfe)
cal aceuracy(Y_test, y predr#e)
Results Using Entropy:
Predicted values
(at nw a at ow
Bee ee
ee eae
See
see WB Bt Ow)
Confusion Matrix: [[78 1]
T3491]
Accuracy : 96.49122807017544
Report precision recall. fl-score
8 0.96 0990.97 n
M098 0.93 8.95 8
aa
supportsav eae con Ye
macroyg 8.57, 096 us
weighted avg 0.97.
4 view the feature scores
colefois"]
feature scorescrypd.Sentes(clf_rfc. Feature teportances.index=col).sort_values(ascendingsFalse)
Feature scores,
area_se 0.182502
concave points_nean 2.169849,
Concave points.worst 0.112208
radius worst 000198
concavity-nean 0.083746,
perineter_worst 074382
Fadlus_se 0.045875
area_vorst 0.033503,
perineter_mean 0.022796,
kexture worst 0.022662
radius. ean 0.016896,
fractal dinension worst 0.016442
fractal_dimension_ce 0.013438
symetry worst 0.012875
concavity worst ecena3e
conpactness_se 2.011388
farea.nean 010982
texture mean 2.010007
penineter-se 0009439,
compactness worst 2.006927
swoothness se 2.006619
‘fractal dinension 2.005590
texture se 2085804
symetry_nean 2.002044
snoothness.nean 0.003662
symetry £2 2.003557,
concavity. se 2.003287
conpactness_nean 2002886,
concave poiats se 0.002658,
snoothness worst 0.002522
type: floates
_eading at anoupuiConmeniTMl faneiTX ont