尝试在 GaussianNB 上计算精度时出错
Getting errors when trying to calculate accuracy on GaussianNB
我正在尝试获得我创建的模型的准确性。我的代码看起来像这样
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
import numpy as np
data = 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)
whih 给出了错误:
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 ]]
预测
[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
我正在尝试获得我创建的模型的准确性。我的代码看起来像这样
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
import numpy as np
data = 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)
whih 给出了错误:
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 ]]
预测
[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