@@ -118,6 +118,7 @@ define([
118
118
}
119
119
120
120
generateCode ( ) {
121
+ let codeCells = [ ] ;
121
122
let code = new com_String ( ) ;
122
123
let {
123
124
modelType, predictData, targetData,
@@ -134,40 +135,56 @@ define([
134
135
//====================================================================
135
136
if ( modelType == 'clf' ) {
136
137
if ( confusion_matrix ) {
138
+ code = new com_String ( ) ;
137
139
code . appendLine ( "# Confusion Matrix" ) ;
138
- code . appendFormatLine ( 'pd.crosstab({0}, {1}, margins=True)' , targetData , predictData ) ;
140
+ code . appendFormat ( 'pd.crosstab({0}, {1}, margins=True)' , targetData , predictData ) ;
141
+ codeCells . push ( code . toString ( ) ) ;
139
142
}
140
143
if ( report ) {
144
+ code = new com_String ( ) ;
141
145
code . appendLine ( "# Classification report" ) ;
142
- code . appendFormatLine ( 'print(metrics.classification_report({0}, {1}))' , targetData , predictData ) ;
146
+ code . appendFormat ( 'print(metrics.classification_report({0}, {1}))' , targetData , predictData ) ;
147
+ codeCells . push ( code . toString ( ) ) ;
143
148
}
144
149
if ( accuracy ) {
150
+ code = new com_String ( ) ;
145
151
code . appendLine ( "# Accuracy" ) ;
146
- code . appendFormatLine ( 'metrics.accuracy_score({0}, {1})' , targetData , predictData ) ;
152
+ code . appendFormat ( 'metrics.accuracy_score({0}, {1})' , targetData , predictData ) ;
153
+ codeCells . push ( code . toString ( ) ) ;
147
154
}
148
155
if ( precision ) {
156
+ code = new com_String ( ) ;
149
157
code . appendLine ( "# Precision" ) ;
150
- code . appendFormatLine ( "metrics.precision_score({0}, {1}, average='weighted')" , targetData , predictData ) ;
158
+ code . appendFormat ( "metrics.precision_score({0}, {1}, average='weighted')" , targetData , predictData ) ;
159
+ codeCells . push ( code . toString ( ) ) ;
151
160
}
152
161
if ( recall ) {
162
+ code = new com_String ( ) ;
153
163
code . appendLine ( "# Recall" ) ;
154
- code . appendFormatLine ( "metrics.recall_score({0}, {1}, average='weighted')" , targetData , predictData ) ;
164
+ code . appendFormat ( "metrics.recall_score({0}, {1}, average='weighted')" , targetData , predictData ) ;
165
+ codeCells . push ( code . toString ( ) ) ;
155
166
}
156
167
if ( f1_score ) {
168
+ code = new com_String ( ) ;
157
169
code . appendLine ( "# F1-score" ) ;
158
- code . appendFormatLine ( "metrics.f1_score({0}, {1}, average='weighted')" , targetData , predictData ) ;
170
+ code . appendFormat ( "metrics.f1_score({0}, {1}, average='weighted')" , targetData , predictData ) ;
171
+ codeCells . push ( code . toString ( ) ) ;
159
172
}
160
173
if ( roc_curve ) {
174
+ code = new com_String ( ) ;
161
175
code . appendLine ( "# ROC Curve" ) ;
162
176
code . appendFormatLine ( "fpr, tpr, thresholds = roc_curve({0}, svc.decision_function({1}}))" , predictData , targetData ) ;
163
177
code . appendLine ( "plt.plot(fpr, tpr, label='ROC Curve')" ) ;
164
178
code . appendLine ( "plt.xlabel('Sensitivity') " ) ;
165
- code . appendLine ( "plt.ylabel('Specificity') " )
179
+ code . append ( "plt.ylabel('Specificity') " )
180
+ codeCells . push ( code . toString ( ) ) ;
166
181
}
167
182
if ( auc ) {
183
+ code = new com_String ( ) ;
168
184
code . appendLine ( "# AUC" ) ;
169
185
code . appendFormatLine ( "fpr, tpr, thresholds = roc_curve({0}, svc.decision_function({1}}))" , predictData , targetData ) ;
170
- code . appendLine ( "metrics.auc(fpr, tpr)" ) ;
186
+ code . append ( "metrics.auc(fpr, tpr)" ) ;
187
+ codeCells . push ( code . toString ( ) ) ;
171
188
}
172
189
}
173
190
@@ -184,30 +201,40 @@ define([
184
201
// code.appendFormatLine('model.intercept_');
185
202
// }
186
203
if ( r_squared ) {
204
+ code = new com_String ( ) ;
187
205
code . appendLine ( "# R square" ) ;
188
- code . appendFormatLine ( 'metrics.r2_score({0}, {1})' , targetData , predictData ) ;
206
+ code . appendFormat ( 'metrics.r2_score({0}, {1})' , targetData , predictData ) ;
207
+ codeCells . push ( code . toString ( ) ) ;
189
208
}
190
209
if ( mae ) {
210
+ code = new com_String ( ) ;
191
211
code . appendLine ( "# MAE(Mean Absolute Error)" ) ;
192
- code . appendFormatLine ( 'metrics.mean_absolute_error({0}, {1})' , targetData , predictData ) ;
212
+ code . appendFormat ( 'metrics.mean_absolute_error({0}, {1})' , targetData , predictData ) ;
213
+ codeCells . push ( code . toString ( ) ) ;
193
214
}
194
215
if ( mape ) {
216
+ code = new com_String ( ) ;
195
217
code . appendLine ( "# MAPE(Mean Absolute Percentage Error)" ) ;
196
218
code . appendLine ( 'def MAPE(y_test, y_pred):' ) ;
197
219
code . appendLine ( ' return np.mean(np.abs((y_test - pred) / y_test)) * 100' ) ;
198
220
code . appendLine ( ) ;
199
- code . appendFormatLine ( 'MAPE({0}, {1})' , targetData , predictData ) ;
221
+ code . appendFormat ( 'MAPE({0}, {1})' , targetData , predictData ) ;
222
+ codeCells . push ( code . toString ( ) ) ;
200
223
}
201
224
if ( rmse ) {
225
+ code = new com_String ( ) ;
202
226
code . appendLine ( "# RMSE(Root Mean Squared Error)" ) ;
203
- code . appendFormatLine ( 'metrics.mean_squared_error({0}, {1})**0.5' , targetData , predictData ) ;
227
+ code . appendFormat ( 'metrics.mean_squared_error({0}, {1})**0.5' , targetData , predictData ) ;
228
+ codeCells . push ( code . toString ( ) ) ;
204
229
}
205
230
if ( scatter_plot ) {
231
+ code = new com_String ( ) ;
206
232
code . appendLine ( '# Regression plot' ) ;
207
233
code . appendFormatLine ( 'plt.scatter({0}, {1})' , targetData , predictData ) ;
208
234
code . appendFormatLine ( "plt.xlabel('{0}')" , targetData ) ;
209
235
code . appendFormatLine ( "plt.ylabel('{1}')" , predictData ) ;
210
- code . appendLine ( 'plt.show()' ) ;
236
+ code . append ( 'plt.show()' ) ;
237
+ codeCells . push ( code . toString ( ) ) ;
211
238
}
212
239
}
213
240
//====================================================================
@@ -219,20 +246,26 @@ define([
219
246
// code.appendFormatLine("print(f'Size of clusters: {np.bincount({0})}')", predictData);
220
247
// }
221
248
if ( silhouetteScore ) {
249
+ code = new com_String ( ) ;
222
250
code . appendLine ( "# Silhouette score" ) ;
223
- code . appendFormatLine ( "print(f'Silhouette score: {metrics.cluster.silhouette_score({0}, {1})}')" , targetData , predictData ) ;
251
+ code . appendFormat ( "print(f'Silhouette score: {metrics.cluster.silhouette_score({0}, {1})}')" , targetData , predictData ) ;
252
+ codeCells . push ( code . toString ( ) ) ;
224
253
}
225
254
if ( ari ) {
255
+ code = new com_String ( ) ;
226
256
code . appendLine ( "# ARI" ) ;
227
- code . appendFormatLine ( "print(f'ARI: {metrics.cluster.adjusted_rand_score({0}, {1})}')" , targetData , predictData ) ;
257
+ code . appendFormat ( "print(f'ARI: {metrics.cluster.adjusted_rand_score({0}, {1})}')" , targetData , predictData ) ;
258
+ codeCells . push ( code . toString ( ) ) ;
228
259
}
229
260
if ( nm ) {
261
+ code = new com_String ( ) ;
230
262
code . appendLine ( "# NM" ) ;
231
- code . appendFormatLine ( "print(f'NM: {metrics.cluster.normalized_mutual_info_score({0}, {1})}')" , targetData , predictData ) ;
263
+ code . appendFormat ( "print(f'NM: {metrics.cluster.normalized_mutual_info_score({0}, {1})}')" , targetData , predictData ) ;
264
+ codeCells . push ( code . toString ( ) ) ;
232
265
}
233
266
}
234
- // FIXME: as seperated cells
235
- return code . toString ( ) ;
267
+ // return as seperated cells
268
+ return codeCells ;
236
269
}
237
270
238
271
}
0 commit comments