Classification metrics can't handle a mix of binary and continuous targets
All your commented-out models are not classifiers but regression models, for which accuracy is meaningless.
You get the error because these regression models do not produce binary outcomes, but continuous (float) numbers (as all regression models do); so, when scikit-learn attempts to calculate the accuracy by comparing a binary number (true label) with a float (predicted value), it not unexpectedly gives an error. And this cause is clearly hinted at the error message itself:
Classification metrics can't handle a mix of binary and continuous target
Confusion Matrix ValueError: Classification metrics can't handle a mix of binary and continuous targets
The model outputs the predicted probabilities, you need to transform them back to class labels before calculating the classification metrics, see below.
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
tf.random.set_seed(0)
# generate the data
X, y = make_classification(n_classes=2, n_features=4, n_informative=4, n_redundant=0, random_state=42)
# split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# build the model
model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(4,)))
model.add(Dropout(0.1))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(16, input_dim=1, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(12, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(1, activation='sigmoid'))
# fit the model
model.compile(loss='binary_crossentropy', optimizer='RMSprop', metrics=['accuracy', 'AUC'])
model.fit(X_train, y_train, epochs=100, batch_size=64, validation_data=(X_test, y_test), validation_batch_size=64, verbose=0)
# extract the predicted probabilities
p_pred = model.predict(X_test)
p_pred = p_pred.flatten()
print(p_pred.round(2))
# [1. 0.01 0.91 0.87 0.06 0.95 0.24 0.58 0.78 ...
# extract the predicted class labels
y_pred = np.where(p_pred > 0.5, 1, 0)
print(y_pred)
# [1 0 1 1 0 1 0 1 1 0 0 0 0 1 1 0 1 0 0 0 0 ...
print(confusion_matrix(y_test, y_pred))
# [[13 1]
# [ 2 9]]
print(classification_report(y_test, y_pred))
# precision recall f1-score support
# 0 0.87 0.93 0.90 14
# 1 0.90 0.82 0.86 11
# accuracy 0.88 25
# macro avg 0.88 0.87 0.88 25
# weighted avg 0.88 0.88 0.88 25
ValueError: "metrics can't handle a mix of binary and continuous targets" with no source
You are trying to use a regression algorithm (DecisionTreeRegressor
) for a binary classification problem; the regression model, as expected, gives continuous outputs, but the accuracy_score
, where the error actually happens:
File "C:/Users/Omar/Downloads/Kaggle Competition/Titanic.py", line 58, in <module>
print(accuracy_score(val_y, val_predictions))
expects binary ones, hence the error.
For starters, change your model to
from sklearn.tree import DecisionTreeClassifier
titanic_model = DecisionTreeClassifier()
Python Sklearn "ValueError: Classification metrics can't handle a mix of multiclass-multioutput and binary targets" error
You probably want to get the accuracy for the predictions
together with the column Survived
of the test_survived
dataframe:
from sklearn.metrics import accuracy_score
accuracy=accuracy_score(test_survived['Survived'],predictions)
print(accuracy)
Your error occured, because the accuracy_score() only takes two 1-dimensional arrays, one as the ground truth labels and the other as the predicted labels. But you provided a 2-dimensional "array" (the dataframe) and the 1-dimensional predictions, hence it assumed that your first input is a multiclass-output.
The documentation is also very resourceful for this.
Related Topics
How to Call a Classes Method from Another Class Without Initialising the First Class
How to Remove a Pandas Dataframe from Another Dataframe
How to Update Sqlalchemy Orm Object by a Python Dict
Python Executable Not Finding Libpython Shared Library
Python: Element Is Not Attached to the Page Document
Typeerror: Missing 1 Required Positional Argument: 'Self'
Finding the Value of the Min and Max Pixel
Only Reading First N Rows of CSV File With CSV Reader in Python
How to Convert Column With String Type to Int Form in Pyspark Data Frame
Overlay a Smaller Image on a Larger Image Python Opencv
Regex That Matches a Number With Commas for Every Three Digits
What Causes a Python Segmentation Fault
Possible to Get User Input Without Inserting a New Line
Splitting Dictionary Items into Smaller Dictionaries Based on Condition
Making a Discord Bot Change Playing Status Every 10 Seconds
How to Download Outlook Attachment from Python Script