@@ -199,7 +199,9 @@ define([
199
199
sizeOfClusters, silhouetteScore, ari, nmi,
200
200
clusteredIndex, featureData2, targetData2
201
201
} = this . state ;
202
+ // add import code for display and Markdown
202
203
let needDisplay = false ;
204
+ let needMarkdown = false ;
203
205
204
206
//====================================================================
205
207
// Classfication
@@ -208,8 +210,9 @@ define([
208
210
if ( confusion_matrix ) {
209
211
code = new com_String ( ) ;
210
212
code . appendLine ( "# Confusion Matrix" ) ;
213
+ code . appendLine ( "display(Markdown('### Confusion Matrix'))" ) ;
211
214
code . appendFormat ( 'display(pd.crosstab({0}, {1}, margins=True))' , targetData , predictData ) ;
212
- needDisplay = true ;
215
+ needMarkdown = true ;
213
216
codeCells . push ( code . toString ( ) ) ;
214
217
}
215
218
if ( report ) {
@@ -221,29 +224,25 @@ define([
221
224
if ( accuracy ) {
222
225
code = new com_String ( ) ;
223
226
code . appendLine ( "# Accuracy" ) ;
224
- code . appendFormat ( 'display(metrics.accuracy_score({0}, {1}))' , targetData , predictData ) ;
225
- needDisplay = true ;
227
+ code . appendFormat ( "print('Accuracy: {}'.format(metrics.accuracy_score({0}, {1})))" , targetData , predictData ) ;
226
228
codeCells . push ( code . toString ( ) ) ;
227
229
}
228
230
if ( precision ) {
229
231
code = new com_String ( ) ;
230
232
code . appendLine ( "# Precision" ) ;
231
- code . appendFormat ( "display(metrics.precision_score({0}, {1}, average='weighted'))" , targetData , predictData ) ;
232
- needDisplay = true ;
233
+ code . appendFormat ( "print('Precision: {}'.format(metrics.precision_score({0}, {1}, average='weighted')))" , targetData , predictData ) ;
233
234
codeCells . push ( code . toString ( ) ) ;
234
235
}
235
236
if ( recall ) {
236
237
code = new com_String ( ) ;
237
238
code . appendLine ( "# Recall" ) ;
238
- code . appendFormat ( "display(metrics.recall_score({0}, {1}, average='weighted'))" , targetData , predictData ) ;
239
- needDisplay = true ;
239
+ code . appendFormat ( "print('Recall: {}'.format(metrics.recall_score({0}, {1}, average='weighted')))" , targetData , predictData ) ;
240
240
codeCells . push ( code . toString ( ) ) ;
241
241
}
242
242
if ( f1_score ) {
243
243
code = new com_String ( ) ;
244
244
code . appendLine ( "# F1-score" ) ;
245
- code . appendFormat ( "display(metrics.f1_score({0}, {1}, average='weighted'))" , targetData , predictData ) ;
246
- needDisplay = true ;
245
+ code . appendFormat ( "print('F1-score: {}'.format(metrics.f1_score({0}, {1}, average='weighted')))" , targetData , predictData ) ;
247
246
codeCells . push ( code . toString ( ) ) ;
248
247
}
249
248
// if (roc_curve) {
@@ -278,15 +277,13 @@ define([
278
277
if ( r_squared ) {
279
278
code = new com_String ( ) ;
280
279
code . appendLine ( "# R square" ) ;
281
- code . appendFormat ( 'display(metrics.r2_score({0}, {1}))' , targetData , predictData ) ;
282
- needDisplay = true ;
280
+ code . appendFormat ( "print('R square: {}'.format(metrics.r2_score({0}, {1})))" , targetData , predictData ) ;
283
281
codeCells . push ( code . toString ( ) ) ;
284
282
}
285
283
if ( mae ) {
286
284
code = new com_String ( ) ;
287
285
code . appendLine ( "# MAE(Mean Absolute Error)" ) ;
288
- code . appendFormat ( 'display(metrics.mean_absolute_error({0}, {1}))' , targetData , predictData ) ;
289
- needDisplay = true ;
286
+ code . appendFormat ( "print('MAE: {}'.format(metrics.mean_absolute_error({0}, {1})))" , targetData , predictData ) ;
290
287
codeCells . push ( code . toString ( ) ) ;
291
288
}
292
289
if ( mape ) {
@@ -295,24 +292,24 @@ define([
295
292
code . appendLine ( 'def MAPE(y_test, y_pred):' ) ;
296
293
code . appendLine ( ' return np.mean(np.abs((y_test - pred) / y_test)) * 100' ) ;
297
294
code . appendLine ( ) ;
298
- code . appendFormat ( 'display(MAPE({0}, {1}))' , targetData , predictData ) ;
299
- needDisplay = true ;
295
+ code . appendFormat ( "print('MAPE: {}'.format(MAPE({0}, {1})))" , targetData , predictData ) ;
300
296
codeCells . push ( code . toString ( ) ) ;
301
297
}
302
298
if ( rmse ) {
303
299
code = new com_String ( ) ;
304
300
code . appendLine ( "# RMSE(Root Mean Squared Error)" ) ;
305
- code . appendFormat ( 'display(metrics.mean_squared_error({0}, {1})**0.5)' , targetData , predictData ) ;
306
- needDisplay = true ;
301
+ code . appendFormat ( "print('RMSE: {}'.format(metrics.mean_squared_error({0}, {1})**0.5))" , targetData , predictData ) ;
307
302
codeCells . push ( code . toString ( ) ) ;
308
303
}
309
304
if ( scatter_plot ) {
310
305
code = new com_String ( ) ;
311
306
code . appendLine ( '# Regression plot' ) ;
307
+ code . appendLine ( "display(Markdown('### Regression plot'))" ) ;
312
308
code . appendFormatLine ( 'plt.scatter({0}, {1})' , targetData , predictData ) ;
313
309
code . appendFormatLine ( "plt.xlabel('{0}')" , targetData ) ;
314
310
code . appendFormatLine ( "plt.ylabel('{0}')" , predictData ) ;
315
311
code . append ( 'plt.show()' ) ;
312
+ needMarkdown = true ;
316
313
codeCells . push ( code . toString ( ) ) ;
317
314
}
318
315
}
@@ -327,23 +324,28 @@ define([
327
324
if ( silhouetteScore ) {
328
325
code = new com_String ( ) ;
329
326
code . appendLine ( "# Silhouette score" ) ;
330
- code . appendFormat ( "print(f 'Silhouette score: {metrics.cluster.silhouette_score({0}, {1})}' )" , featureData2 , clusteredIndex ) ;
327
+ code . appendFormat ( "print('Silhouette score: {}'.format( metrics.cluster.silhouette_score({0}, {1})) )" , featureData2 , clusteredIndex ) ;
331
328
codeCells . push ( code . toString ( ) ) ;
332
329
}
333
330
if ( ari ) {
334
331
code = new com_String ( ) ;
335
332
code . appendLine ( "# ARI(Adjusted Rand score)" ) ;
336
- code . appendFormat ( "print(f 'ARI: {metrics.cluster.adjusted_rand_score({0}, {1})}' )" , targetData2 , clusteredIndex ) ;
333
+ code . appendFormat ( "print('ARI: {}'.format( metrics.cluster.adjusted_rand_score({0}, {1})) )" , targetData2 , clusteredIndex ) ;
337
334
codeCells . push ( code . toString ( ) ) ;
338
335
}
339
336
if ( nmi ) {
340
337
code = new com_String ( ) ;
341
338
code . appendLine ( "# NMI(Normalized Mutual Info Score)" ) ;
342
- code . appendFormat ( "print(f 'NM: {metrics.cluster.normalized_mutual_info_score({0}, {1})}' )" , targetData2 , clusteredIndex ) ;
339
+ code . appendFormat ( "print('NM: {}'.format( metrics.cluster.normalized_mutual_info_score({0}, {1})) )" , targetData2 , clusteredIndex ) ;
343
340
codeCells . push ( code . toString ( ) ) ;
344
341
}
345
342
}
346
- if ( needDisplay === true ) {
343
+ if ( needMarkdown === true ) {
344
+ codeCells = [
345
+ "from IPython.display import display, Markdown" ,
346
+ ...codeCells
347
+ ] ;
348
+ } else if ( needDisplay === true ) {
347
349
codeCells = [
348
350
"from IPython.display import display" ,
349
351
...codeCells
0 commit comments