-
Notifications
You must be signed in to change notification settings - Fork 2
/
make_report.py
129 lines (101 loc) · 5.06 KB
/
make_report.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
120
121
122
123
124
125
126
127
128
129
import os, sys
from rf2steps import RandomForestModel
import rflearning
import numpy as N
def make_clf_grid_report(classifier, col2fit, rep_label='test'):
"""
Creates a report of the hyperparameter search
"""
max_depths = [9,12,13,15,18,22,26,30,40]
nestimators = [30, 50, 70, 80, 100, 150, 200, 250, 300, 400, 600]
#max_depths = [8,20,30]
#nestimators = [10, 20, 30]
## Report info
report_dir = os.path.join('reports/',rep_label)
if not os.path.exists(report_dir):
print 'creating report directory: %s'%report_dir
os.mkdir(report_dir)
reportname = os.path.join(report_dir, 'clf_grid_report.txt')
if os.path.exists(reportname):
if raw_input('Report with this name already exists...\
overwrite?(y/n)') not in ('y', 'Y', 'Yes', 'YES', 'yes'):
print '\n\nAborting!\n\n'
sys.exit(1)
print 'making classifier grid report with {}'.format(classifier)
## Writing the paramenters to the report
frep = open(reportname, 'w')
frep.write('\ndf_file=%s\n'%classifier)
frep.write('\ncolumns=%s\n'%str(col2fit))
frep.write('\nmax_depths=\n%s\n'%str(max_depths))
frep.write('\nn_estimators=\n%s\n'%str(nestimators))
lrn = rflearning.clf_learning(saved_df=classifier)
report_dic = lrn.grid_search(col2fit, waitNshow=False, nestimators=nestimators, max_depths=max_depths)
frep.write('\ngrid score =\n%s\n'%str(report_dic['grid_score']))
## Save figures
save_type='png'
report_dic['fig_grid'].savefig(os.path.join(report_dir, 'clf_fig_gridsearch.%s'%save_type))
report_dic['fig_mdepth'].savefig(os.path.join(report_dir, 'clf_fig_maxdepth.%s'%save_type))
report_dic['fig_nestim'].savefig(os.path.join(report_dir, 'clf_fig_nestim.%s'%save_type))
frep.close()
print '\nFinished writing report file:\n%s'%reportname
def make_clf_score_report(classifier, col2fit, rep_label = 'test'):
"""
Creates a report about the classifier
"""
## Report info
report_dir = os.path.join('reports/',rep_label)
if not os.path.exists(report_dir):
print 'creating report directory: %s'%report_dir
os.mkdir(report_dir)
reportname = os.path.join(report_dir, 'clf_score_report.txt')
if os.path.exists(reportname):
if raw_input('Report with this name already exists...\
overwrite?(y/n)') not in ('y', 'Y', 'Yes', 'YES', 'yes'):
print '\n\nAborting!\n\n'
sys.exit(1)
## hyperparameters
maxdepth = 18
nestimators = 300
#maxdepth = 5
#nestimators = 30
print 'making classifier score report with {}'.format(classifier)
## Writing the paramenters to the report
frep = open(reportname, 'w')
frep.write('\ndf_file=%s\n'%classifier)
frep.write('\ncolumns=%s\n'%str(col2fit))
#rfmodel = RandomForestModel('Data/train_2013.csv', 1000)## For testing
#rfmodel = RandomForestModel(saved_df = classifier)
rfmodel = rflearning.clf_learning(saved_df = classifier)
## Scoring
report_dic = rfmodel.fitNscoreClassifier(col2fit, maxdepth, nestimators, waitNshow=False)
frep.write('\nfeature importances:\n%s\n'%str(report_dic['features_importances']))
frep.write('\n\nCross validation accuracy (std): %f (%f)\n'%(
report_dic['scores_mean'], report_dic['scores_std']))
frep.write('\n ROC area under curve: %f\n'%report_dic['roc_auc'])
##Learning curve
learn_dic = rfmodel.learn_curve(
col2fit, 'accuracy', maxdepth, nestimators, verbose=1, waitNshow=False, nsizes=10)
frep.write('\n train learning scores: %s\n'%str(learn_dic['train_scores']))
frep.write('\n test learning scores: %s\n'%str(learn_dic['test_scores']))
frep.write('\n\n\n\n'+100*'-' + '\n End of report\n')
## Save figures
save_type='png'
report_dic['fig_importance'].savefig(os.path.join(report_dir, 'clf_fig_importance.%s'%save_type))
report_dic['fig_prob'].savefig(os.path.join(report_dir, 'clf_fig_prob.%s'%save_type))
report_dic['fig_roc'].savefig(os.path.join(report_dir, 'clf_fig_roc.%s'%save_type))
learn_dic['fig_learning'].savefig(os.path.join(report_dir, 'clf_fig_learn.%s'%save_type))
frep.close()
print '\nFinished writing report file:\n%s'%reportname
if __name__=="__main__":
coltofit = ['Avg_Reflectivity', 'Range_Reflectivity', 'Nval',
'Avg_DistanceToRadar', 'Avg_RadarQualityIndex', 'Range_RadarQualityIndex',
'Avg_RR1', 'Range_RR1','Avg_RR2', 'Range_RR2',
'Avg_RR3', 'Range_RR3', 'Avg_Zdr', 'Range_Zdr',
'Avg_Composite', 'Range_Composite','Avg_HybridScan', 'Range_HybridScan',
'Avg_Velocity', 'Range_Velocity', 'Avg_LogWaterVolume', 'Range_LogWaterVolume',
'Avg_MassWeightedMean', 'Range_MassWeightedMean',
'Avg_MassWeightedSD', 'Range_MassWeightedSD', 'Avg_RhoHV', 'Range_RhoHV'
]
#make_clf_score_report('saved_df/test30k.h5', coltofit, 'test30k')
make_clf_score_report('saved_df/test700k.h5', coltofit, 'test700k')
#make_clf_grid_report('saved_df/test30k.h5', coltofit, 'test30k')