Issue
I ran this code in Google Colab:
from sklearn.metrics import confusion_matrix
# Initialize logreg model
logreg = LogisticRegression()
# Fit the model with data
logreg.fit(X_train, y_train)
# Predict model
y_pred = logreg.predict(X_test)
# Evaluate model using confusion matrix
cnf_matrix = confusion_matrix(y_test,y_pred)
print('Confusion Matrix:\n', cnf_matrix)
and it gave me this output
<ipython-input-73-2bdb42bd97ad> in <module>()
6
7 # Fit the model with data
----> 8 logreg.fit(X_train, y_train)
9
10 # Predict model
1 frames
/usr/local/lib/python3.7/dist-packages/sklearn/utils/multiclass.py in check_classification_targets(y)
167 if y_type not in ['binary', 'multiclass', 'multiclass-multioutput',
168 'multilabel-indicator', 'multilabel-sequences']:
--> 169 raise ValueError("Unknown label type: %r" % y_type)
170
171
ValueError: Unknown label type: 'unknown'
How to solve this?
My X variable are numeric columns and y variable is label column. I've tried this code LogisticRegression().fit(X_train, y_train)
but it returned error also.
Solution
Sklearn doesn't recognize Boolean
type target variables. Convert them to numerical to train:
y_train = y_train.astype('int')
If you want your predictions to show boolean values (instead of integers), you could later convert them to Boolean
:
y_pred = y_pred.astype('bool')
Note: if you do decide to convert your predictions, make sure you're not predicting probabilities, but classes (i.e. output is 0
and 1
, not a 2-dimensional matrix of intermediate values; if you do predict probabilities, first convert them to classes, then do boolean conversion).
Answered By - dm2
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.