在尝试计算GaussianNB准确率时遇到错误

我在尝试获取我创建的模型的准确率。我的代码如下所示

from sklearn.datasets import fetch_california_housingfrom sklearn.model_selection import train_test_splitimport numpy as npdata = fetch_california_housing()c = np.array([1 if y > np.median(data['target']) else 0 for y in data['target']])X_train, X_test, c_train, c_test = train_test_split(data['data'], c, random_state=0)gaussian=GaussianNB().fit(X_train, c_train)pred=gaussian.predict(X_test)    metrics.accuracy_score(X_test, pred)

错误是由以下这行代码引发的: metrics.accuracy_score(X_test, pred) 显示的错误信息是

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

我搜索了解决方案,但没有找到任何答案。我看到其他遇到同样问题的人的帖子说,不能使用metric.accuracy…因为这是针对分类问题的。但我的确实是一个分类问题。

我也尝试了另一种方法: pred=score(X_test, pred) 这引发了错误:

TypeError: 'numpy.float64' object is not callable

感谢任何帮助

———————更新————-

X_test

[[   4.1518       22.            5.66307278 ...    4.18059299    32.58       -117.05      ] [   5.7796       32.            6.10722611 ...    3.02097902    33.92       -117.97      ] [   4.3487       29.            5.93071161 ...    2.91011236    38.65       -121.84      ] ... [   3.6296       16.            3.61684211 ...    1.88631579    34.2        -118.61      ] [   5.5133       37.            4.59322034 ...    3.00847458    33.9        -118.34      ] [   4.7639       36.            5.26181818 ...    2.90545455    37.66       -122.44      ]]

Pred

[1 1 1 ... 1 1 1]

回答:

似乎可以用c_test工作:

accuracy_score(c_test, pred) # 0.743798

另一种方法是:

1 - ((c_test != pred).sum() / X_test.shape[0]) # 0.743798

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注