gpt4 book ai didi

python - 我的准确度是 0.0,我不知道为什么?

转载 作者:行者123 更新时间:2023-12-01 04:25:56 28 4
gpt4 key购买 nike

我得到的准确度为 0.0。我正在使用波士顿住房数据集。

这是我的代码:

import sklearn
from sklearn import datasets
from sklearn import svm, metrics
from sklearn import linear_model, preprocessing
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
boston = datasets.load_boston()

x = boston.data
y = boston.target

train_data, test_data, train_label, test_label = sklearn.model_selection.train_test_split(x, y, test_size=0.2)

model = KNeighborsClassifier()

lab_enc = preprocessing.LabelEncoder()
train_label_encoded = lab_enc.fit_transform(train_label)
test_label_encoded = lab_enc.fit_transform(test_label)

model.fit(train_data, train_label_encoded)
predicted = model.predict(test_data)
accuracy = model.score(test_data, test_label_encoded)
print(accuracy)

如何提高此数据集的准确性?

最佳答案

波士顿数据集用于回归问题。定义在 the docs :

Load and return the boston house-prices dataset (regression).



因此,如果您使用普通编码(例如标签不是来自连续数据的样本),则没有意义。例如,您将 12.3 和 12.4 编码为完全不同的标签,但它们彼此非常接近,如果分类器在实际目标为 12.3 时预测 12.4,则您评估的结果是错误的,但这不是二元情况。在分类中,预测是否正确,但在回归中,它以不同的方式计算,例如均方误差。

这部分不是必需的,但我想给你举一个相同数据集和源代码的例子。将标签四舍五入到零(到最接近零的整数)的简单想法会给你一些直觉。
5.0-5.9 -> 5
6.0-6.9 -> 6
...
50.0-50.9 -> 50

让我们稍微更改一下您的代码。
import numpy as np

def encode_func(labels):
return np.array([int(l) for l in labels])

...

train_label_encoded = encode_func(train_label)
test_label_encoded = encode_func(test_label)

输出将在 10% 左右。

关于python - 我的准确度是 0.0,我不知道为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58667517/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com