-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment2.py
120 lines (88 loc) · 3.79 KB
/
assignment2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from openml.apiconnector import APIConnector
import numpy as np
from pandas import Series,DataFrame
import pandas as pd
import random
import matplotlib.pyplot as plt
import re
import sklearn.tree
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.externals.six import StringIO
import dot_parser
import pydot
from sklearn import tree
from sklearn.externals.six import StringIO
from IPython.display import Image
from sklearn import cross_validation
from sklearn.tree import ExtraTreeClassifier
apikey = 'b6da739f426042fa9785167b29887d1a'
connector = APIConnector(apikey=apikey)
dataset = connector.download_dataset(10)
train = dataset.get_dataset()
train = pd.DataFrame(train,columns=['lymphatics','block_of_affere','bl_of_lymph_c','bl_of_lymph_s' ,
'by_pass' , 'extravasates' , 'regeneration_of' ,'early_uptake_in' ,'lym_nodes_dimin'
,'lym_nodes_enlar' ,'changes_in_lym' ,'defect_in_node' ,'changes_in_node' ,'changes_in_stru'
,'special_forms' ,'dislocation_of','exclusion_of_no','no_of_nodes_in' , 'class'])
train.rename(columns={'class':'target'}, inplace=True)
target_counts = train.target.value_counts()
target_counts
train.target.hist(bins=4)
plt.show()
target = train['target']
X = train.iloc[:,:-1]
#feature importance by Random Forest
rf = RandomForestClassifier(n_estimators=10)
rf.fit(X, target)
feat_imp = pd.Series(rf.feature_importances_, index=X.columns)
feat_imp.sort_values(inplace=True)
ax = feat_imp.tail(19).plot(kind='barh', figsize=(13,10), title='Feature importance')
plt.show()
#feature importance by decision tree
clf = tree.DecisionTreeClassifier(min_samples_leaf=3,max_depth=8)
clf = clf.fit(X, target)
feat_imp = pd.Series(clf.feature_importances_, index=X.columns)
feat_imp.sort_values(inplace=True)
ax = feat_imp.tail(19).plot(kind='barh', figsize=(13,10), title='Feature importance by Decision tree')
plt.show()
#removing the small classes
train = train.query('target == 1 or target==2')
target = train['target']
X = train.iloc[:,:-1]
clf = tree.DecisionTreeClassifier(min_samples_leaf=3,max_depth=8)
clf = clf.fit(X, target)
scores = cross_validation.cross_val_score(clf, X, target, cv=5)
print "scores of decision tree with depth max depth 8 and min sample leaf 3" , scores
print scores.mean()
print scores.std()
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,
feature_names=X.columns,
class_names=[str('metastases'),str('malign_lymph')],
filled=True, rounded=True,
special_characters=True)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
clf = tree.DecisionTreeClassifier(min_samples_leaf=5,max_depth=6)
clf = clf.fit(X, target)
scores = cross_validation.cross_val_score(clf, X, target, cv=5)
print "scores of decision tree with depth max depth 6 and min sample leaf 5" , scores
print scores.mean()
print scores.std()
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,
feature_names=X.columns,
class_names=[str('metastases'),str('malign_lymph')],
filled=True, rounded=True,
special_characters=True)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
print "Extra tree classifier"
clf = sklearn.tree.ExtraTreeClassifier(min_samples_leaf=3 ,max_depth=8 )
scores = cross_validation.cross_val_score(clf, X, target, cv=5)
print scores
print scores.mean()
print scores.std()
clf = sklearn.ensemble.ExtraTreesClassifier(n_estimators=10 ,min_samples_leaf=3 ,max_depth=8 )
scores = cross_validation.cross_val_score(clf, X, target, cv=5)
scores