@@ -1244,121 +1244,124 @@ def calculate_accuracy(calcacc):
1244
1244
def plot_confusion_matrix (pltcm ):
1245
1245
ConfusionMatrix_out .clear_output ()
1246
1246
with ConfusionMatrix_out :
1247
- if Tree_state_widget .value == 'No expert tree interactions' :
1248
- tree_orig = tree .DecisionTreeClassifier (criterion = inter_3 .widget .kwargs ['criterion' ], splitter = inter_3 .widget .kwargs ['splitter' ], max_depth = inter_3 .widget .kwargs ['max_depth' ],
1249
- min_samples_split = inter_3 .widget .kwargs ['min_samples_split' ], min_samples_leaf = inter_3 .widget .kwargs ['min_samples_leaf' ],
1250
- min_weight_fraction_leaf = 0.0 , max_features = inter_3 .widget .kwargs ['max_features' ], random_state = inter_3 .widget .kwargs ['random_state' ],
1251
- max_leaf_nodes = inter_3 .widget .kwargs ['max_leaf_nodes' ], min_impurity_decrease = inter_3 .widget .kwargs ['min_impurity_decrease' ],
1252
- min_impurity_split = None , class_weight = None ,ccp_alpha = 0.0 )
1253
- tree_orig .fit (self .Data_dict ['x' ],self .Data_dict ['y' ])
1254
- if self .Train_test_splitting .value == True :
1255
- CM_train = confusion_matrix (self .Data_dict ['y' ], tree_orig .predict (self .Data_dict ['x' ]), labels = self .classes_dict ['Classes Labels' ])
1256
- CM_train_display = ConfusionMatrixDisplay (CM_train , self .classes_dict ['Classes Labels' ])
1257
- CM_test = confusion_matrix (self .Data_dict ['w' ], tree_orig .predict (self .Data_dict ['z' ]), labels = self .classes_dict ['Classes Labels' ])
1258
- CM_test_display = ConfusionMatrixDisplay (CM_test , self .classes_dict ['Classes Labels' ])
1259
- CM_display_dict = {'Train' : CM_train_display , 'Test' : CM_test_display }
1260
- f , axes = plt .subplots (1 , 2 , figsize = (20 , 5 ), sharey = 'row' )
1261
- for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1262
- CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1263
- CM_display_dict .ax_ .set_title (key )
1264
- CM_display_dict .im_ .colorbar .remove ()
1265
- CM_display_dict .ax_ .set_xlabel ('Predicted' )
1266
- if i != 0 :
1267
- CM_display_dict .ax_ .set_ylabel ('' )
1268
- plt .subplots_adjust (wspace = 0.40 , hspace = 0.1 )
1269
- f .colorbar (CM_display_dict .im_ , ax = axes )
1270
- plt .show ()
1271
- elif self .Train_test_splitting .value == False :
1272
- CM = confusion_matrix (self .Data_dict ['y' ], tree_orig .predict (self .Data_dict ['x' ]), labels = self .classes_dict ['Classes Labels' ])
1273
- CM_display = ConfusionMatrixDisplay (CM , self .classes_dict ['Classes Labels' ])
1274
- CM_display_dict = {'Confusion Matrix' : CM_display }
1275
- f , axes = plt .subplots (1 , 1 , figsize = (20 , 5 ))
1276
- for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1277
- CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1278
- CM_display_dict .ax_ .set_title (key )
1279
- CM_display_dict .im_ .colorbar .remove ()
1280
- CM_display_dict .ax_ .set_xlabel ('Predicted' )
1281
- f .colorbar (CM_display_dict .im_ , ax = axes )
1282
- plt .show ()
1283
- elif Tree_state_widget .value == 'Tree was last modified' :
1284
- if self .Train_test_splitting .value == True :
1285
- #Make predictions on train set:
1286
- predicted_iDT_train = iDT .classify (base .Last_modified_tree , self .Data_dict ['x' ])
1287
- predicted_iDT_train .classify ()
1288
- #Make predictions on test set:
1289
- predicted_iDT_test = iDT .classify (base .Last_modified_tree , self .Data_dict ['z' ])
1290
- predicted_iDT_test .classify ()
1291
- #Calculate confusion Matrices:
1292
- CM_train = confusion_matrix (self .Data_dict ['y' ], predicted_iDT_train .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1293
- CM_train_display = ConfusionMatrixDisplay (CM_train , self .classes_dict ['Classes Labels' ])
1294
- CM_test = confusion_matrix (self .Data_dict ['w' ], predicted_iDT_test .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1295
- CM_test_display = ConfusionMatrixDisplay (CM_test , self .classes_dict ['Classes Labels' ])
1296
- CM_display_dict = {'Train' : CM_train_display , 'Test' : CM_test_display }
1297
- f , axes = plt .subplots (1 , 2 , figsize = (20 , 5 ), sharey = 'row' )
1298
- for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1299
- CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1300
- CM_display_dict .ax_ .set_title (key )
1301
- CM_display_dict .im_ .colorbar .remove ()
1302
- CM_display_dict .ax_ .set_xlabel ('Predicted' )
1303
- if i != 0 :
1304
- CM_display_dict .ax_ .set_ylabel ('' )
1305
- plt .subplots_adjust (wspace = 0.40 , hspace = 0.1 )
1306
- f .colorbar (CM_display_dict .im_ , ax = axes )
1307
- plt .show ()
1308
- elif self .Train_test_splitting .value == False :
1309
- predicted_iDT = iDT .classify (base .Last_modified_tree , self .Data_dict ['x' ])
1310
- predicted_iDT .classify ()
1311
- #Calculate confusion Matrices:
1312
- CM = confusion_matrix (self .Data_dict ['y' ], predicted_iDT .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1313
- CM_display = ConfusionMatrixDisplay (CM , self .classes_dict ['Classes Labels' ])
1314
- CM_display_dict = {'Confusion Matrix' : CM_display }
1315
- f , axes = plt .subplots (1 , 1 , figsize = (20 , 5 ))
1316
- for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1317
- CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1318
- CM_display_dict .ax_ .set_title (key )
1319
- CM_display_dict .im_ .colorbar .remove ()
1320
- CM_display_dict .ax_ .set_xlabel ('Predicted' )
1321
- f .colorbar (CM_display_dict .im_ , ax = axes )
1322
- plt .show ()
1323
- elif Tree_state_widget .value == 'Tree was last pruned' :
1324
- if self .Train_test_splitting .value == True :
1325
- #Make predictions on train set:
1326
- predicted_iDT_train = iDT .classify (base_prn .Last_Pruned_tree , self .Data_dict ['x' ])
1327
- predicted_iDT_train .classify ()
1328
- #Make predictions on test set:
1329
- predicted_iDT_test = iDT .classify (base_prn .Last_Pruned_tree , self .Data_dict ['z' ])
1330
- predicted_iDT_test .classify ()
1331
- #Calculate confusion Matrices:
1332
- CM_train = confusion_matrix (self .Data_dict ['y' ], predicted_iDT_train .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1333
- CM_train_display = ConfusionMatrixDisplay (CM_train , self .classes_dict ['Classes Labels' ])
1334
- CM_test = confusion_matrix (self .Data_dict ['w' ], predicted_iDT_test .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1335
- CM_test_display = ConfusionMatrixDisplay (CM_test , self .classes_dict ['Classes Labels' ])
1336
- CM_display_dict = {'Train' : CM_train_display , 'Test' : CM_test_display }
1337
- f , axes = plt .subplots (1 , 2 , figsize = (20 , 5 ), sharey = 'row' )
1338
- for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1339
- CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1340
- CM_display_dict .ax_ .set_title (key )
1341
- CM_display_dict .im_ .colorbar .remove ()
1342
- CM_display_dict .ax_ .set_xlabel ('Predicted' )
1343
- if i != 0 :
1344
- CM_display_dict .ax_ .set_ylabel ('' )
1345
- plt .subplots_adjust (wspace = 0.40 , hspace = 0.1 )
1346
- f .colorbar (CM_display_dict .im_ , ax = axes )
1347
- plt .show ()
1348
- elif self .Train_test_splitting .value == False :
1349
- predicted_iDT = iDT .classify (base_prn .Last_Pruned_tree , self .Data_dict ['x' ])
1350
- predicted_iDT .classify ()
1351
- CM = confusion_matrix (self .Data_dict ['y' ], predicted_iDT .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1352
- CM_display = ConfusionMatrixDisplay (CM , self .classes_dict ['Classes Labels' ])
1353
- CM_display_dict = {'Confusion Matrix' : CM_display }
1354
- f , axes = plt .subplots (1 , 1 , figsize = (20 , 5 ))
1355
- for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1356
- CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1357
- CM_display_dict .ax_ .set_title (key )
1358
- CM_display_dict .im_ .colorbar .remove ()
1359
- CM_display_dict .ax_ .set_xlabel ('Predicted' )
1360
- f .colorbar (CM_display_dict .im_ , ax = axes )
1361
- plt .show ()
1247
+ if len (self .classes_dict ['Classes Labels' ]) == 0 :
1248
+ print ("You havent assigned labels to classes. To plot a confusion matrix please first assign labels to classes using the appropriate tool in the Preprocessing Stage tab." )
1249
+ else :
1250
+ if Tree_state_widget .value == 'No expert tree interactions' :
1251
+ tree_orig = tree .DecisionTreeClassifier (criterion = inter_3 .widget .kwargs ['criterion' ], splitter = inter_3 .widget .kwargs ['splitter' ], max_depth = inter_3 .widget .kwargs ['max_depth' ],
1252
+ min_samples_split = inter_3 .widget .kwargs ['min_samples_split' ], min_samples_leaf = inter_3 .widget .kwargs ['min_samples_leaf' ],
1253
+ min_weight_fraction_leaf = 0.0 , max_features = inter_3 .widget .kwargs ['max_features' ], random_state = inter_3 .widget .kwargs ['random_state' ],
1254
+ max_leaf_nodes = inter_3 .widget .kwargs ['max_leaf_nodes' ], min_impurity_decrease = inter_3 .widget .kwargs ['min_impurity_decrease' ],
1255
+ min_impurity_split = None , class_weight = None ,ccp_alpha = 0.0 )
1256
+ tree_orig .fit (self .Data_dict ['x' ],self .Data_dict ['y' ])
1257
+ if self .Train_test_splitting .value == True :
1258
+ CM_train = confusion_matrix (self .Data_dict ['y' ], tree_orig .predict (self .Data_dict ['x' ]), labels = self .classes_dict ['Classes Labels' ])
1259
+ CM_train_display = ConfusionMatrixDisplay (CM_train , self .classes_dict ['Classes Labels' ])
1260
+ CM_test = confusion_matrix (self .Data_dict ['w' ], tree_orig .predict (self .Data_dict ['z' ]), labels = self .classes_dict ['Classes Labels' ])
1261
+ CM_test_display = ConfusionMatrixDisplay (CM_test , self .classes_dict ['Classes Labels' ])
1262
+ CM_display_dict = {'Train' : CM_train_display , 'Test' : CM_test_display }
1263
+ f , axes = plt .subplots (1 , 2 , figsize = (20 , 5 ), sharey = 'row' )
1264
+ for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1265
+ CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1266
+ CM_display_dict .ax_ .set_title (key )
1267
+ CM_display_dict .im_ .colorbar .remove ()
1268
+ CM_display_dict .ax_ .set_xlabel ('Predicted' )
1269
+ if i != 0 :
1270
+ CM_display_dict .ax_ .set_ylabel ('' )
1271
+ plt .subplots_adjust (wspace = 0.40 , hspace = 0.1 )
1272
+ f .colorbar (CM_display_dict .im_ , ax = axes )
1273
+ plt .show ()
1274
+ elif self .Train_test_splitting .value == False :
1275
+ CM = confusion_matrix (self .Data_dict ['y' ], tree_orig .predict (self .Data_dict ['x' ]), labels = self .classes_dict ['Classes Labels' ])
1276
+ CM_display = ConfusionMatrixDisplay (CM , self .classes_dict ['Classes Labels' ])
1277
+ CM_display_dict = {'Confusion Matrix' : CM_display }
1278
+ f , axes = plt .subplots (1 , 1 , figsize = (20 , 5 ))
1279
+ for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1280
+ CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1281
+ CM_display_dict .ax_ .set_title (key )
1282
+ CM_display_dict .im_ .colorbar .remove ()
1283
+ CM_display_dict .ax_ .set_xlabel ('Predicted' )
1284
+ f .colorbar (CM_display_dict .im_ , ax = axes )
1285
+ plt .show ()
1286
+ elif Tree_state_widget .value == 'Tree was last modified' :
1287
+ if self .Train_test_splitting .value == True :
1288
+ #Make predictions on train set:
1289
+ predicted_iDT_train = iDT .classify (base .Last_modified_tree , self .Data_dict ['x' ])
1290
+ predicted_iDT_train .classify ()
1291
+ #Make predictions on test set:
1292
+ predicted_iDT_test = iDT .classify (base .Last_modified_tree , self .Data_dict ['z' ])
1293
+ predicted_iDT_test .classify ()
1294
+ #Calculate confusion Matrices:
1295
+ CM_train = confusion_matrix (self .Data_dict ['y' ], predicted_iDT_train .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1296
+ CM_train_display = ConfusionMatrixDisplay (CM_train , self .classes_dict ['Classes Labels' ])
1297
+ CM_test = confusion_matrix (self .Data_dict ['w' ], predicted_iDT_test .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1298
+ CM_test_display = ConfusionMatrixDisplay (CM_test , self .classes_dict ['Classes Labels' ])
1299
+ CM_display_dict = {'Train' : CM_train_display , 'Test' : CM_test_display }
1300
+ f , axes = plt .subplots (1 , 2 , figsize = (20 , 5 ), sharey = 'row' )
1301
+ for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1302
+ CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1303
+ CM_display_dict .ax_ .set_title (key )
1304
+ CM_display_dict .im_ .colorbar .remove ()
1305
+ CM_display_dict .ax_ .set_xlabel ('Predicted' )
1306
+ if i != 0 :
1307
+ CM_display_dict .ax_ .set_ylabel ('' )
1308
+ plt .subplots_adjust (wspace = 0.40 , hspace = 0.1 )
1309
+ f .colorbar (CM_display_dict .im_ , ax = axes )
1310
+ plt .show ()
1311
+ elif self .Train_test_splitting .value == False :
1312
+ predicted_iDT = iDT .classify (base .Last_modified_tree , self .Data_dict ['x' ])
1313
+ predicted_iDT .classify ()
1314
+ #Calculate confusion Matrices:
1315
+ CM = confusion_matrix (self .Data_dict ['y' ], predicted_iDT .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1316
+ CM_display = ConfusionMatrixDisplay (CM , self .classes_dict ['Classes Labels' ])
1317
+ CM_display_dict = {'Confusion Matrix' : CM_display }
1318
+ f , axes = plt .subplots (1 , 1 , figsize = (20 , 5 ))
1319
+ for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1320
+ CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1321
+ CM_display_dict .ax_ .set_title (key )
1322
+ CM_display_dict .im_ .colorbar .remove ()
1323
+ CM_display_dict .ax_ .set_xlabel ('Predicted' )
1324
+ f .colorbar (CM_display_dict .im_ , ax = axes )
1325
+ plt .show ()
1326
+ elif Tree_state_widget .value == 'Tree was last pruned' :
1327
+ if self .Train_test_splitting .value == True :
1328
+ #Make predictions on train set:
1329
+ predicted_iDT_train = iDT .classify (base_prn .Last_Pruned_tree , self .Data_dict ['x' ])
1330
+ predicted_iDT_train .classify ()
1331
+ #Make predictions on test set:
1332
+ predicted_iDT_test = iDT .classify (base_prn .Last_Pruned_tree , self .Data_dict ['z' ])
1333
+ predicted_iDT_test .classify ()
1334
+ #Calculate confusion Matrices:
1335
+ CM_train = confusion_matrix (self .Data_dict ['y' ], predicted_iDT_train .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1336
+ CM_train_display = ConfusionMatrixDisplay (CM_train , self .classes_dict ['Classes Labels' ])
1337
+ CM_test = confusion_matrix (self .Data_dict ['w' ], predicted_iDT_test .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1338
+ CM_test_display = ConfusionMatrixDisplay (CM_test , self .classes_dict ['Classes Labels' ])
1339
+ CM_display_dict = {'Train' : CM_train_display , 'Test' : CM_test_display }
1340
+ f , axes = plt .subplots (1 , 2 , figsize = (20 , 5 ), sharey = 'row' )
1341
+ for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1342
+ CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1343
+ CM_display_dict .ax_ .set_title (key )
1344
+ CM_display_dict .im_ .colorbar .remove ()
1345
+ CM_display_dict .ax_ .set_xlabel ('Predicted' )
1346
+ if i != 0 :
1347
+ CM_display_dict .ax_ .set_ylabel ('' )
1348
+ plt .subplots_adjust (wspace = 0.40 , hspace = 0.1 )
1349
+ f .colorbar (CM_display_dict .im_ , ax = axes )
1350
+ plt .show ()
1351
+ elif self .Train_test_splitting .value == False :
1352
+ predicted_iDT = iDT .classify (base_prn .Last_Pruned_tree , self .Data_dict ['x' ])
1353
+ predicted_iDT .classify ()
1354
+ CM = confusion_matrix (self .Data_dict ['y' ], predicted_iDT .predicted_classes , labels = self .classes_dict ['Classes Labels' ])
1355
+ CM_display = ConfusionMatrixDisplay (CM , self .classes_dict ['Classes Labels' ])
1356
+ CM_display_dict = {'Confusion Matrix' : CM_display }
1357
+ f , axes = plt .subplots (1 , 1 , figsize = (20 , 5 ))
1358
+ for i , (key , CM_display_dict ) in enumerate (CM_display_dict .items ()):
1359
+ CM_display_dict .plot (ax = axes [i ], xticks_rotation = 45 )
1360
+ CM_display_dict .ax_ .set_title (key )
1361
+ CM_display_dict .im_ .colorbar .remove ()
1362
+ CM_display_dict .ax_ .set_xlabel ('Predicted' )
1363
+ f .colorbar (CM_display_dict .im_ , ax = axes )
1364
+ plt .show ()
1362
1365
1363
1366
#Assign the above function to calculate accuracy button click
1364
1367
ConfusionMatrix_button .on_click (plot_confusion_matrix )
0 commit comments