Computer Vision News - March 2020
3 Summary A Step Towards Explainability 25 X_train, X_test, y_train, y_test = train_test_split(dt.drop('target', 1), dt['target'], test_size = .2, random_state=10) #split the data model = RandomForestClassifier(max_depth=5) model.fit(X_train, y_train) estimator = model.estimators_[1] feature_names = [i for i in X_train.columns] y_train_str = y_train.astype('str') y_train_str[y_train_str == '0'] = 'no disease' y_train_str[y_train_str == '1'] = 'disease' y_train_str = y_train_str.values # code inspiration from http://bit.ly/2vepSlp export_graphviz(estimator, out_file='tree.dot', feature_names = feature_names, class_names = y_train_str, rounded = True, proportion = True, label='root', precision = 2, filled = True) from subprocess import call call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600']) from IPython.display import Image Image(filename = explain_tree.png') This will produce the explainability tree we will use next time to talk about the model and the way it predicts the results. Let’s now evaluate the model, to see if it makes sense to analyse further: y_predict = model.predict(X_test) y_pred_quant = model.predict_proba(X_test)[:, 1] y_pred_bin = model.predict(X_test) confusion_matrix = confusion_matrix(y_test, y_pred_bin) confusion_matrix total=sum(sum(confusion_matrix)) sensitivity = confusion_matrix[0,0]/(confusion_matrix[0,0]+confusion_matrix[1,0]) print ('Sensitivity : ', sensitivity ) specificity = confusion_matrix[1,1]/(confusion_matrix[1,1]+confusion_matrix[0,1]) print ('Specificity : ', specificity) fpr, tpr, thresholds = roc_curve(y_test, y_pred_quant) fig, ax = plt.subplots() ax.plot(fpr, tpr) ax.plot([0, 1], [0, 1], transform=ax.transAxes, ls="--", c=".3") plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.0]) plt.rcParams['font.size'] = 12 plt.title('ROC curve for diabetes classifier') plt.xlabel('False Positive Rate (1 - Specificity)') plt.ylabel('True Positive Rate (Sensitivity)') plt.grid(True) auc(fpr, tpr)
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=