Precision

编程入门 行业动态 更新时间:2024-10-04 13:26:55
Precision_score和accuracy_score显示值错误(Precision_score and accuracy_score showing value error)

我是这台机器学习的新手,并使用这个波士顿数据集进行预测。 除了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行,in

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)

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

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

I'm new to this machine learning and using this boston dataset for predictions. Everything except the result for precision_score and accuracy_score is working fine . This is what i have done :

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'))

Now i get the following error:

File "LinearRegression.py", line 33, in

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

line 172, in accuracy_score

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

File "/usr/local/lib/python2.7/dist-packages/sklearn/metrics/classification.py", line 89, in _check_targets

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

最满意答案

您正在使用线性回归模型

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))

这将解决您的问题。

You are using a linear Regression model as

clf = LinearRegression()

which predicts continuous values. eg: 1.2, 1.3

Whereas accuracy_score(y_test, y_predict) expects boolean values. 1 or 0 (true or false) or categorical values like 1,2,3,4 etc.. Where the numbers act as categories.

That's why you are getting an error.

How to solve this?

Since you are trying to predict Price on boston data which is a continuous value. I recommend you change your error measure from accuracy to RMSE or MSE

Replace:

print(accuracy_score(y_test, y_predict))

with:

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

That will solve your problem.

更多推荐

本文发布于:2023-07-09 10:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Precision

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!