【问题标题】:Precision_score and accuracy_score showing value errorPrecision_score 和 accuracy_score 显示值错误
【发布时间】:2017-07-16 04:26:10
【问题描述】:

我是这个机器学习的新手,并且使用这个波士顿数据集进行预测。除了precision_score 和accuracy_score 的结果之外,一切都运行良好。这就是我所做的:

import pandas as pd 
import sklearn 
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing,cross_validation, svm
from sklearn.datasets import load_boston
import numpy as np
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, classification_report, confusion_matrix

boston = load_boston()
df = pd.DataFrame(boston.data)
df.columns= boston.feature_names
df['Price']= boston.target

X = np.array(df.drop(['Price'],axis=1), dtype=np.float64)
X = preprocessing.scale(X)

y = np.array(df['Price'], dtype=np.float64)

print (len(X[:,6:7]),len(y))

X_train,X_test,y_train,y_test=cross_validation.train_test_split(X,y,test_size=0.30)

clf =LinearRegression()
clf.fit(X_train,y_train)
y_predict = clf.predict(X_test)

print(y_predict,len(y_predict))
print (accuracy_score(y_test, y_predict))
print(precision_score(y_test, y_predict,average = 'macro'))

现在我收到以下错误:

文件“LinearRegression.py”,第 33 行,在

 accuracy = accuracy_score(y_test, y_predict)    File "/usr/local/lib/python2.7/dist-packages/sklearn/metrics/classification.py",

第 172 行,在 accuracy_score 中

 y_type, y_true, y_pred = _check_targets(y_true, y_pred)

文件 “/usr/local/lib/python2.7/dist-packages/sklearn/metrics/classification.py”, 第 89 行,在 _check_targets 中

 raise ValueError("{0} is not supported".format(y_type))

 ValueError: continuous is not supported

【问题讨论】:

    标签: python machine-learning scikit-learn linear-regression


    【解决方案1】:

    您正在使用线性回归模型

    clf = LinearRegression()
    

    预测连续值。例如:1.2、1.3

    accuracy_score(y_test, y_predict) 需要布尔值。 1 或 0(真或假)或分类值,如 1、2、3、4 等。其中数字充当类别。

    这就是您收到错误的原因。

    如何解决这个问题?

    由于您试图在波士顿数据上预测Price,这是一个连续值。我建议您将错误度量从准确度更改为 RMSE 或 MSE

    替换:

    print(accuracy_score(y_test, y_predict))
    

    与:

    from sklearn.metrics import mean_squared_error
    print(mean_squared_error(y_test, y_predict))
    

    这会解决你的问题。

    【讨论】:

    • 但即使我将分类器更改为 Svm 或 RandomForestClassifier 我得到相同的结果。他们是否以相同的方式进行预测?
    • @harshi RandomForestClassifier 预测 1 和 0 或 0、1、2、3、4 等类别。在这里,您需要谨慎预测价格,可能是 1.2 或 0.24。它们是连续值。
    • @harshi 阅读了有关分类问题和回归问题之间差异的更多信息。这会有所帮助:quora.com/…
    • 我发现我可以使用 'clf.score(X_test, y_test)' 来获得准确度。但是计算 RMSE 并不能为我提供准确度。我如何计算precision_score?
    • @harshi clf.score() 方法不会让您获得准确性。 LinearRegression 返回分数中的 R 方系数。 See documentation。我认为您关于分类和回归的概念不清楚。
    猜你喜欢
    • 2019-06-30
    • 2015-02-22
    • 2021-12-07
    • 2018-11-11
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2019-11-16
    相关资源
    最近更新 更多