I am new to modeling, and I am practicing building a logistic regression model. I would like to create a confusion matrix, but my code doesn't seem to work.

Here is the code for the model (which works):

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

#train, test = train_test_split(hp[age], test_size=0.3)

#from sklearn import preprocessing
X = hp['age'].values.reshape((32561,1))
#X = hp[['age','hours-per-week']].values
y = hp['evalinvest'].values

LogReg = LogisticRegression()


Here is where I am having diffculty:

# Confusion Matrix
import numpy as np
from sklearn.metrics import *
CM = confusion_matrix(X,y)
print ("\n\nConfusion matrix:\n", CM)

It runs and outputs results, but I don't feel like it is correct.

Confusion matrix:

 [[  0   0   0 ...,   0   0   0]
 [  0   0   0 ...,   0   0   0]
 [385  10   0 ...,   0   0   0]
 [  1   0   0 ...,   0   0   0]
 [  3   0   0 ...,   0   0   0]
 [ 33  10   0 ...,   0   0   0]]

Then, when I run the following code, it doesn't work:

tn, fp, fn, tp = CM.ravel()
print ("\nTP, TN, FP, FN:", tp, ",", tn, ",", fp, ",", fn)


ValueError                                Traceback (most recent call last)
<ipython-input-68-dca3ebbdc69a> in <module>()
----> 1 tn, fp, fn, tp = CM.ravel()
      2 print ("\nTP, TN, FP, FN:", tp, ",", tn, ",", fp, ",", fn)

ValueError: too many values to unpack (expected 4)

1 Answer 1


I think you are confused on many levels here.

  • Logistic regression is a classifier. It gives you the probability that given an $x_{i}$, the probability it belongs to a class or classes.
  • So you will be having your evalinvest as classes, not continuous values.
  • When you call predict method on top of trained LogisticRegression model, it predicts class for each $x_{i}$ in your test set. So you expect a single array of class labels they belong to.
  • sklearn.metrics.confusion_matrix function takes in the original class values to the ones that are predicted by the model you had trained and returns what $x_{i}$ has been classified into what class. So it returns an n(c) x n(c) array where n(c) is the number of classes. So the diagonal of the matrix indicates the number of elements of class i being classified as class i. And an ideal model should be expected to get this numbers good. The mistake which you had made you sent in your X (train) matrix into it and y (labels) into it, which is wrong. You are supposed to send in y_true(true labels) and y_pred (predicted labels from the model). Check the documentation for more.
  • Ideally the function should have thrown an error. But unfortunately you had only one row in your X and one in y and they both are of same size and it passed assert it had to make.
  • And np.ravel converts an multi-dimensional array into 1D array. So if you doing a binary classification, you would be having a 2x2 matrix, whose flattened array would have been four elements and the assignment would have worked. But the ravelling you had made releases $n(c)^2$ elements. So there you go ValueError. See the docs

I hope my other answer on confusion matrices may clear things a little more.

Hope it clears some mistakes you are making.

  • $\begingroup$ Thank you very much for your detailed explanation, i really appreciate it. I am a newbie in the DS world, so still has a lot of confusions on how to use these technics. I will need more helps on this, so appreciate in advance for your future help as well. $\endgroup$
    – user633599
    Sep 12, 2018 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.