![]() The class can be used such as: labels = ĬonfusionMatrix = ConfusionMatrix(labels)ĬonfusionMatrix.update("kraken", "velociraptor")ĬonfusionMatrix.update("velociraptor", "velociraptor") I wrote a simple class to build a confusion matrix without the need to depend on a machine learning library. Once you have your list of predicted classes and a list of expected classes you can use code like Torsten Marek's to create the confusion array and calculate the accuracy. You can get one score, by choosing the first one in that list, but how you handle that depends on what you're classifying. This handles the case where two classes have the same scores. ]Ĭhoosing a classification would be something like: for instance_scores in prob_arr : Normalize - optional boolean for matrix - a 2-dimensional list of pairwise counts Predicted - a list of integers or strings for predicted classes Generate a confusion matrix for multiple - a list of integers or strings for known classes I noticed that a new Python library about Confusion Matrix named P圜M is out: maybe you can have a look.Ī Dependency Free Multiclass Confusion Matrix # A Simple Confusion Matrix Implementationĭef confusionmatrix(actual, predicted, normalize = False): LR-: Negative likelihood ratio 0 0.75 0.75 You can binarize a confusion matrix, get class statistics such as TP, TN, FP, FN, ACC, TPR, FPR, FNR, TNR (SPC), LR+, LR-, DOR, PPV, FDR, FOR, NPV and some overall statistics In : from pandas_ml import ConfusionMatrix With this package confusion matrix can be pretty-printed, plot. You might also be interested by this project and its Pip package Or plot normalized confusion matrix using: plot_confusion_matrix(df_conf_norm) Plt.yticks(tick_marks, df_confusion.index) Plt.xticks(tick_marks, df_lumns, rotation=45) Plt.matshow(df_confusion, cmap=cmap) # imshow You can plot this confusion_matrix using import matplotlib.pyplot as pltĭef plot_confusion_matrix(df_confusion, title='Confusion matrix', cmap=plt.cm.gray_r): You can also get a normalized confusion matrix using: df_confusion = pd.crosstab(y_actu, y_pred)ĭf_conf_norm = df_confusion.div(df_confusion.sum(axis=1), axis="index") You will get also sum for each row and column: Predicted 0 1 2 All If you add margins=True like df_confusion = pd.crosstab(y_actu, y_pred, rownames=, colnames=, margins=True) ![]() You will get a (nicely labeled) Pandas DataFrame: Predicted 0 1 2 Y_pred = pd.Series(, name='Predicted')ĭf_confusion = pd.crosstab(y_actu, y_pred) Which output a Numpy array array(,īut you can also create a confusion matrix using Pandas: import pandas as pd Scikit-Learn provides a confusion_matrix function from trics import confusion_matrix How can I make this for multiple classes?įor example, for a data set with three classes, it should return:, ,]. If I run this code for a multiple classed data, it doesn't work. What my code is trying to do is: I get prob_arr and input_arr and for each class (1 and 2) I check if they are misclassified or not.īut my code only works for two classes. Input_arr is the original class labels for a dataset and it is like this: Prob_arr is an array that my classification code returned and a sample array is like this: I wrote a confusion matrix calculation code in Python: def conf_mat(prob_arr, input_arr):Īccuracy = float(conf_arr + conf_arr)/(len(input_arr)) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |