@@ -38,7 +38,7 @@ class FireRiskModels():
38
38
def __init__ (self ,Modeltype = 'propensity' ,ACS_year = '2016' ):
39
39
40
40
self .SEED = 0
41
- self .type = Modeltype
41
+ self .Modeltype = Modeltype
42
42
43
43
44
44
def train (self ,NFIRS ,ACS ,ACS_variables = None , test_year = 'Current' ,n_years = 5 ):
@@ -83,7 +83,7 @@ def train(self,NFIRS,ACS,ACS_variables =None, test_year='Current',n_years = 5):
83
83
# predict 2017 using 2016-2015 data
84
84
85
85
X_train , y_train ,input1 , Xtrain_years = self .munge_dataset (top10 ,fires ,ACS_data ,ACS .tot_pop , n_years ,test_year_idx - 1 )
86
- self . X_train = X_train
86
+
87
87
88
88
89
89
X_test , y_test ,Input , Xtest_years = self .munge_dataset (top10 ,fires ,ACS_data ,ACS .tot_pop , n_years ,test_year_idx )
@@ -92,6 +92,12 @@ def train(self,NFIRS,ACS,ACS_variables =None, test_year='Current',n_years = 5):
92
92
inference_years = np .append (Xtest_years , str (test_year ))
93
93
self .years_used = np .union1d (model_years , inference_years )
94
94
95
+ # Save Training Data
96
+ self .X_train = X_train
97
+ self .X_test = X_test
98
+ self .y_train = y_train
99
+ self .y_test = y_test
100
+
95
101
96
102
# Note: `Input` is used for manual data validation to ensure munging performed correctly
97
103
@@ -144,14 +150,20 @@ def train(self,NFIRS,ACS,ACS_variables =None, test_year='Current',n_years = 5):
144
150
print (confusion_matrix (y_test , self .test_predictions ))
145
151
print (roc_auc_score (y_test , self .test_prediction_probs [:,1 ]))
146
152
print (classification_report (y_test ,self .test_predictions ))
153
+ self .Classification_report = classification_report (y_test ,self .test_predictions ,output_dict = True )
147
154
#print (log_loss(y_test,self.test_predictions))
148
155
149
156
150
157
#Calculate feature importance for each model
151
158
importances = model .feature_importances_
152
159
indices = np .argsort (importances )[::- 1 ]
160
+
161
+ self .Feature_importances = dict ()
153
162
print ("Feature ranking:" )
154
163
for f in range (len (X_test .columns )):
164
+
165
+ self .Feature_importances [ X_test .columns [indices [f ]] ] = importances [indices [f ]]
166
+
155
167
print ("%d. %s (%f)" % (f + 1 , X_test .columns [indices [f ]], importances [indices [f ]]))
156
168
157
169
@@ -162,6 +174,26 @@ def train(self,NFIRS,ACS,ACS_variables =None, test_year='Current',n_years = 5):
162
174
163
175
def predict (self ,NFIRS ,ACS = [],predict_year = 'Current' ):
164
176
pass
177
+
178
+
179
+ def save (self ,save_path ):
180
+ import json
181
+ # save model
182
+ # note save_path Should be a Pathlib object
183
+
184
+ self .model .save_model ( save_path / f'Fire{ self .Modeltype } Model.json' )
185
+
186
+ # save model metrics
187
+ with open ( save_path / f'Fire{ self .Modeltype } _ClassificationReport.json' , 'w' ) as outfile :
188
+ json .dump (self .Classification_report , outfile )
189
+
190
+ # save Model Data
191
+ self .X_train .to_csv ( save_path / X_train .csv )
192
+ self .X_test .to_csv ( save_path / X_test .csv )
193
+ self .y_train .to_csv ( save_path / y_train .csv )
194
+ self .y_train .to_csv ( save_path / y_train .csv )
195
+
196
+
165
197
166
198
@staticmethod
167
199
def munge_dataset (top10 ,fires ,ACS ,tot_pop , n_years ,test_year_idx ):
0 commit comments