Questions tagged [confusion-matrix]
A confusion matrix is a contingency table of correct and incorrect classifications used to evaluate the performance of a classification algorithm in machine learning.
confusion-matrix
1,099
questions
99
votes
19
answers
264k
views
How to write a confusion matrix
I wrote a confusion matrix calculation code in Python:
def conf_mat(prob_arr, input_arr):
# confusion matrix
conf_arr = [[0, 0], [0, 0]]
for i in range(len(prob_arr)):
if int(...
60
votes
9
answers
100k
views
How to normalize a confusion matrix?
I calculated a confusion matrix for my classifier using confusion_matrix() from scikit-learn. The diagonal elements of the confusion matrix represent the number of points for which the predicted label ...
45
votes
8
answers
51k
views
How to plot confusion matrix with string axis rather than integer in python
I am following a previous thread on how to plot confusion matrix in Matplotlib. The script is as follows:
from numpy import *
import matplotlib.pyplot as plt
from pylab import *
conf_arr = [[33,2,0,0,...
36
votes
3
answers
49k
views
How to interpret scikit's learn confusion matrix and classification report?
I have a sentiment analysis task, for this Im using this corpus the opinions have 5 classes (very neg, neg, neu, pos, very pos), from 1 to 5. So I do the classification as follows:
from sklearn....
32
votes
1
answer
29k
views
caret train() predicts very different then predict.glm()
I am trying to estimate a logistic regression, using the 10-fold cross-validation.
#import libraries
library(car); library(caret); library(e1071); library(verification)
#data import and preparation
...
29
votes
8
answers
49k
views
R how to visualize confusion matrix using the caret package
I'd like to visualize the data I've put in the confusion matrix. Is there a function I could simply put the confusion matrix and it would visualize it (plot it)?
Example what I'd like to do(Matrix$...
29
votes
2
answers
23k
views
Adjust size of ConfusionMatrixDisplay (ScikitLearn)
How to set the size of the figure ploted by ScikitLearn's Confusion Matrix?
import numpy as np
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
cm = confusion_matrix(np.arange(25), ...
25
votes
3
answers
72k
views
confusion matrix error "Classification metrics can't handle a mix of multilabel-indicator and multiclass targets"
I am getting a
Classification metrics can't handle a mix of multilabel-indicator and multiclass targets
error when I try to use confusion matrix.
I am doing my first deep learning project. I am new ...
24
votes
3
answers
77k
views
R logistic regression area under curve
I am performing logistic regression using this page. My code is as below.
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
mylogit <- glm(admit ~ gre, data = mydata, family = "...
20
votes
6
answers
48k
views
Plot confusion matrix in R using ggplot
I have two confusion matrices with calculated values as true positive (tp), false positives (fp), true negatives(tn) and false negatives (fn), corresponding to two different methods. I want to ...
20
votes
1
answer
22k
views
Accuracy, precision, and recall for multi-class model
How do I calculate accuracy, precision and recall for each class from a confusion matrix?
I am using the embedded dataset iris; the confusion matrix is as below:
prediction setosa versicolor ...
19
votes
1
answer
36k
views
How to sum all the arrays inside a list of arrays?
I am working with the confusion matrix. So for each loop I have an array (confusion matrix). As I am doing 10 loops, I end up with 10 arrays. I want to sum all of them.
So I decided that for each ...
18
votes
3
answers
36k
views
True Positive Rate and False Positive Rate (TPR, FPR) for Multi-Class Data in python [duplicate]
How do you compute the true- and false- positive rates of a multi-class classification problem? Say,
y_true = [1, -1, 0, 0, 1, -1, 1, 0, -1, 0, 1, -1, 1, 0, 0, -1, 0]
y_prediction = [-1, -...
18
votes
5
answers
32k
views
Sci-kit learn how to print labels for confusion matrix?
So I'm using sci-kit learn to classify some data. I have 13 different class values/categorizes to classify the data to. Now I have been able to use cross validation and print the confusion matrix. ...
17
votes
5
answers
86k
views
calculate accuracy and precision of confusion matrix
Is there any tool / R package available to calculate accuracy and precision of a confusion matrix?
The formula and data structure are here.
17
votes
2
answers
24k
views
Plot Confusion Matrix for multilabel Classifcation Python
I'm looking for someone who can help me to plot my Confusion Matrix. I need this for a term paper at the university. However I have very little experience in programming.
In the pictures you can see ...
17
votes
3
answers
35k
views
Plot Confusion Matrix with scikit-learn without a Classifier
I have a confusion matrix created with sklearn.metrics.confusion_matrix.
Now, I would like to plot it with sklearn.metrics.plot_confusion_matrix, but the first parameter is the trained classifier, as ...
17
votes
3
answers
81k
views
ImportError: cannot import name 'plot_confusion_matrix' from 'sklearn.metrics'
I am attempting to run below code.
from sklearn.metrics import plot_confusion_matrix
And I am receiving below error.
---------------------------------------------------------------------------
...
15
votes
3
answers
27k
views
How to change plot_confusion_matrix default figure size in sklearn.metrics package
I tried to plot confusion matrix with Jupyter notebook using sklearn.metrics.plot_confusion_matrix package, but the default figure size is a little bit small. I have added plt.figure(figsize=(20, 20)) ...
15
votes
1
answer
14k
views
How to make sklearn.metrics.confusion_matrix() to always return TP, TN, FP, FN?
I am using sklearn.metrics.confusion_matrix(y_actual, y_predict) to extract tn, fp, fn, tp and most of the time it works perfectly.
from sklearn.metrics import confusion_matrix
y_actual, y_predict = ...
14
votes
3
answers
43k
views
Multi-class multi-label confusion matrix with Sklearn
I am working with a multi-class multi-label output from my classifier. The total number of classes is 14 and instances can have multiple classes associated. For example:
y_true = np.array([[0,0,1], [...
14
votes
5
answers
42k
views
Confusion matrix and test accuracy for PyTorch Transfer Learning tutorial
Following the Pytorch Transfer learning tutorial, I am interested in reporting only train and test accuracy as well as confusion matrix (say using sklearn confusionmatrix). How can I do that? The ...
14
votes
1
answer
32k
views
How to retrieve overall accuracy value from confusionMatrix in R?
In R caret library, if I got a confusion matrix like this below, if there a way to retrieve the overall accuracy 0.992? I can't get this single value out, since I need to store this value and use it ...
14
votes
3
answers
18k
views
How to get confusion matrix when using model.fit_generator
I am using model.fit_generator to train and get results for my binary (two class) model because I am giving input images directly from my folder. How to get confusion matrix in this case (TP, TN, FP, ...
12
votes
3
answers
33k
views
R package caret confusionMatrix with missing categories
I am using the function confusionMatrix in the R package caret to calculate some statistics for some data I have. I have been putting my predictions as well as my actual values into the table ...
12
votes
2
answers
53k
views
how to create confusion matrix for classification in tensorflow
I have CNN model which has 4 output nodes, and I am trying to compute the confusion matrix so that i can know the individual class accuracy. I am able to compute the overall accuracy.
In the link here,...
12
votes
3
answers
8k
views
Why scikit learn confusion matrix is reversed?
I have 3 questions:
1)
The confusion matrix for sklearn is as follows:
TN | FP
FN | TP
While when I'm looking at online resources, I find it like this:
TP | FP
FN | TN
Which one should I ...
11
votes
1
answer
11k
views
Scikit - changing the threshold to create multiple confusion matrixes
I'm building a classifier that goes through lending club data, and selects the best X loans. I've trained a Random Forest, and created the usual ROC curves, Confusion Matrices, etc.
The confusion ...
11
votes
1
answer
12k
views
What's the correct way to compute a confusion matrix for object detection?
I am trying to compute a confusion matrix for my object detection model. However, I seem to stumble across some pitfalls. My current approach is to compare each predicted box with each ground truth ...
10
votes
3
answers
24k
views
Plotly: How to make an annotated confusion matrix using a heatmap?
I like to use Plotly to visualize everything, I'm trying to visualize a confusion matrix by Plotly, this is my code:
def plot_confusion_matrix(y_true, y_pred, class_names):
confusion_matrix = ...
10
votes
3
answers
23k
views
How compute confusion matrix for multiclass classification in Scikit?
I have a multiclass classification task. When I run my script based on the scikit example as the follows:
classifier = OneVsRestClassifier(GradientBoostingClassifier(n_estimators=70, max_depth=3, ...
10
votes
2
answers
9k
views
How to get rid of white lines in confusion matrix?
Does anyone know why these white lines are quartering my confusion matrix? I've changed many of the parameters but cannot figure it out. The only thing that makes them go away is if I don't label the ...
10
votes
2
answers
18k
views
How to calculate multiclass overall accuracy, sensitivity and specificity?
Can anyone explain how to calculate the accuracy, sensitivity and specificity of multi-class dataset?
10
votes
2
answers
8k
views
Confusion Matrix with number of classified/misclassified instances on it (Python/Matplotlib)
I am plotting a confusion matrix with matplotlib with the following code:
from numpy import *
import matplotlib.pyplot as plt
from pylab import *
conf_arr = [[33,2,0,0,0,0,0,0,0,1,3], [3,31,0,0,0,0,...
10
votes
1
answer
8k
views
Tensorflow confusion matrix using one-hot code
I have multi-class classification using RNN and here is my main code for RNN:
def RNN(x, weights, biases):
x = tf.unstack(x, input_size, 1)
lstm_cell = rnn.BasicLSTMCell(num_unit, forget_bias=...
9
votes
3
answers
15k
views
How to get precision, recall and f-measure from confusion matrix in Python [duplicate]
I'm using Python and have some confusion matrixes. I'd like to calculate precisions and recalls and f-measure by confusion matrixes in multiclass classification. My result logs don't contain y_true ...
9
votes
5
answers
11k
views
Saving output of confusionMatrix as a .csv table
I have a following code resulting in a table-like output
lvs <- c("normal", "abnormal")
truth <- factor(rep(lvs, times = c(86, 258)),
levels = rev(lvs))
pred <- factor(
...
9
votes
4
answers
20k
views
Is there any function to calculate Precision and Recall using Matlab?
I have problem about calculating the precision and recall for classifier in matlab. I use fisherIris data (that consists of 150 datapoints, 50-setosa, 50-versicolor, 50-virginica). I have classified ...
9
votes
2
answers
4k
views
Why is my confusion matrix returning only one number?
I'm doing a binary classification. Whenever my prediction equals the ground truth, I find sklearn.metrics.confusion_matrix to return a single value. Isn't there a problem?
from sklearn.metrics import ...
9
votes
1
answer
12k
views
Understanding multi-label classifier using confusion matrix
I have a multi-label classification problem with 12 classes. I'm using slim of Tensorflow to train the model using the models pretrained on ImageNet. Here are the percentages of presence of each class ...
8
votes
1
answer
13k
views
Scikit-learn Change Threshold in Confusion Matrix
I need to have multiple confusion matrix at a different threshold for a binary classifier.
I have look up everywhere but could not find an easy implementation for this.
Can anyone provide a way to ...
8
votes
8
answers
30k
views
Plot confusion matrix sklearn with multiple labels
I am plotting a confusion matrix for a multiple labelled data, where labels look like:
label1: 1, 0, 0, 0
label2: 0, 1, 0, 0
label3: 0, 0, 1, 0
label4: 0, 0, 0, 1
I am able to ...
8
votes
3
answers
11k
views
Seaborn Confusion Matrix (heatmap) 2 color schemes (correct diagonal vs wrong rest)
Background
In a confusion matrix, the diagonal represents the cases that the predicted label matches the correct label. So the diagonal is good, while all other cells are bad. To clarify what is good ...
8
votes
5
answers
7k
views
plot_confusion_matrix without estimator
I'm trying to use plot_confusion_matrix,
from sklearn.metrics import confusion_matrix
y_true = [1, 1, 0, 1]
y_pred = [1, 1, 0, 0]
confusion_matrix(y_true, y_pred)
Output:
array([[1, 0],
[1,...
8
votes
4
answers
11k
views
Confusion matrix error when array dimensions are of size 3
This code:
from pandas_ml import ConfusionMatrix
y_actu = [1,2]
y_pred = [1,2]
cm = ConfusionMatrix(y_actu, y_pred)
cm.print_stats()
prints:
population: 2
P: 1
N: 1
PositiveTest: 1
NegativeTest: 1
TP:...
8
votes
2
answers
14k
views
How to extract False Positive, False Negative from a confusion matrix of multiclass classification
I am classifying mnist data using following Keras code. From confusion_matrix command of sklearn.metrics i got confusion matrix and from TruePositive= sum(numpy.diag(cm1)) command i am able to get ...
8
votes
2
answers
11k
views
R Confusion Matrix sensitivity and specificity labeling
I am using R v3.3.2 and Caret 6.0.71 (i.e. latest versions) to construct a logistic regression classifier. I am using the confusionMatrix function to create stats for judging its performance.
...
8
votes
3
answers
3k
views
Mean of non-diagonal elements of each row numpy
I essentially have a confusion matrix of size n x n with all my diagonal elements being 1.
For every row, I wish to calculate its mean, excluding the 1, i.e. excluding the diagonal value. Is there a ...
8
votes
2
answers
2k
views
Create_Analytics in RTextTools
I trying to classify Text documents into number of categories.
My below code works fine
matrix[[i]] <- create_matrix(trainingdata[[i]][,1], language="english",removeNumbers=FALSE,stemWords=FALSE,...
8
votes
1
answer
5k
views
R, Confusion Matrix in percent
In R how to get Confusion Matrix in percent (or fraction of 1).
The "caret" package provides useful function but shows absolute number of samples.
library(caret)
data(iris)
T <- iris$Species
P <...