【问题标题】:ValueError: "metrics can't handle a mix of binary and continuous targets" with no sourceValueError:“指标无法处理二进制和连续目标的混合”,没有来源
【发布时间】:2018-09-13 16:37:02
【问题描述】:

我是机器学习的初学者,我正在尝试通过解决 Kaggle 的泰坦尼克号问题来学习。据我所知,我已确保指标彼此同步,但我当然将这个问题归咎于自己,而不是 Python。但是,我仍然找不到源代码,Spyder IDE 也无济于事。

这是我的代码:

import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

"""Assigning the train & test datasets' adresses to variables"""
train_path = "C:\\Users\\Omar\\Downloads\\Titanic Data\\train.csv"
test_path = "C:\\Users\\Omar\\Downloads\\Titanic Data\\test.csv"

"""Using pandas' read_csv() function to read the datasets
and then assigning them to their own variables"""
train_data = pd.read_csv(train_path)
test_data = pd.read_csv(test_path)

"""Using pandas' factorize() function to represent genders (male/female)
with binary values (0/1)"""
train_data['Sex'] = pd.factorize(train_data.Sex)[0]
test_data['Sex'] = pd.factorize(test_data.Sex)[0]

"""Replacing missing values in the training and test dataset with 0"""
train_data.fillna(0.0, inplace = True)
test_data.fillna(0.0, inplace = True)

"""Selecting features for training"""
columns_of_interest = ['Pclass', 'Sex', 'Age']

"""Dropping missing/NaN values from the training dataset"""
filtered_titanic_data = train_data.dropna(axis=0)

"""Using the predictory features in the data to handle the x axis"""
x = filtered_titanic_data[columns_of_interest]

"""The survival (what we're trying to find) is the y axis"""
y = filtered_titanic_data.Survived

"""Splitting the train data with test"""
train_x, val_x, train_y, val_y = train_test_split(x, y, random_state=0)

"""Assigning the DecisionTreeRegressor model to a variable"""
titanic_model = DecisionTreeRegressor()

"""Fitting the x and y values with the model"""
titanic_model.fit(train_x, train_y)

"""Predicting the x-axis"""
val_predictions = titanic_model.predict(val_x)

"""Assigning the feature columns from the test to a variable"""
test_x = test_data[columns_of_interest]

"""Predicting the test by feeding its x axis into the model"""
test_predictions = titanic_model.predict(test_x)

"""Printing the prediction"""
print(val_predictions)

"""Checking for the accuracy"""
print(accuracy_score(val_y, val_predictions))

"""Printing the test prediction"""
print(test_predictions)

这是堆栈跟踪:

Traceback (most recent call last):

  File "<ipython-input-3-73797c87986e>", line 1, in <module>
    runfile('C:/Users/Omar/Downloads/Kaggle Competition/Titanic.py', wdir='C:/Users/Omar/Downloads/Kaggle Competition')

  File "C:\Users\Omar\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\Omar\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Omar/Downloads/Kaggle Competition/Titanic.py", line 58, in <module>
    print(accuracy_score(val_y, val_predictions))

  File "C:\Users\Omar\Anaconda3\lib\site-packages\sklearn\metrics\classification.py", line 176, in accuracy_score
    y_type, y_true, y_pred = _check_targets(y_true, y_pred)

  File "C:\Users\Omar\Anaconda3\lib\site-packages\sklearn\metrics\classification.py", line 81, in _check_targets
    "and {1} targets".format(type_true, type_pred))

ValueError: Classification metrics can't handle a mix of binary and continuous targets

【问题讨论】:

  • 由于泰坦尼克号数据很容易获得,这是一个很好的机会让你的代码充分reproducible;因此,下一次,通过明确包含您的(相关)导入(而不是import ...)来这样做将是一个好习惯......
  • 谢谢。我已经添加了导入。
  • 请不要编辑帖子以包含补救措施和没有错误的新结果!它应该保持原样,以供将来可能的其他人帮助(自己编辑回来)!

标签: python machine-learning scikit-learn kaggle


【解决方案1】:

您正在尝试对二元分类问题使用回归算法 (DecisionTreeRegressor);正如预期的那样,回归模型给出了连续的输出,但是accuracy_score,实际上发生了错误:

File "C:/Users/Omar/Downloads/Kaggle Competition/Titanic.py", line 58, in <module>
    print(accuracy_score(val_y, val_predictions)) 

期望二进制,因此错误。

对于初学者,将您的模型更改为

from sklearn.tree import DecisionTreeClassifier

titanic_model = DecisionTreeClassifier()

【讨论】:

    【解决方案2】:

    您正在使用 DecisionTreeRegressor,正如它所说,它是一个回归模型。 Kaggle Titanic 问题是一个分类问题。所以你应该使用 DecisionTreeClassifier。

    至于你的代码为什么会抛出错误,这是因为val_y 具有二进制值(0,1) 而val_predictions 具有连续值,因为您使用了回归模型。

    【讨论】:

      【解决方案3】:

      分类需要离散标签,因为它预测类别(这是标签中的任何一个),而回归适用于连续数据。由于您的输出是类标签,因此您需要执行分类

      【讨论】:

        猜你喜欢
        • 2016-10-27
        • 2019-06-24
        • 2022-01-25
        • 2018-04-21
        • 2021-12-20
        • 2018-12-01
        • 1970-01-01
        • 2017-08-14
        • 2021-12-20
        相关资源
        最近更新 更多