这是我的代码:
import pandas as pa
from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score
def get_accuracy(X_train, y_train, y_test):
perceptron = Perceptron(random_state=241)
perceptron.fit(X_train, y_train)
result = accuracy_score(y_train, y_test)
return result
test_data = pa.read_csv("C:/Users/Roman/Downloads/perceptron-test.csv")
test_data.columns = ["class", "f1", "f2"]
train_data = pa.read_csv("C:/Users/Roman/Downloads/perceptron-train.csv")
train_data.columns = ["class", "f1", "f2"]
accuracy = get_accuracy(train_data[train_data.columns[1:]], train_data[train_data.columns[0]], test_data[test_data.columns[0]])
print(accuracy)
我不明白为什么会收到此错误:
Traceback (most recent call last):
File "C:/Users/Roman/PycharmProjects/data_project-1/lecture_2_perceptron.py", line 35, in <module>
accuracy = get_accuracy(train_data[train_data.columns[1:]],
train_data[train_data.columns[0]], test_data[test_data.columns[0]])
File "C:/Users/Roman/PycharmProjects/data_project-1/lecture_2_perceptron.py", line 22, in get_accuracy
result = accuracy_score(y_train, y_test)
File "C:\Users\Roman\AppData\Roaming\Python\Python35\site-packages\sklearn\metrics\classification.py", line 172, in accuracy_score
y_type, y_true, y_pred = _check_targets(y_true, y_pred)
File "C:\Users\Roman\AppData\Roaming\Python\Python35\site-packages\sklearn\metrics\classification.py", line 72, in _check_targets
check_consistent_length(y_true, y_pred)
File "C:\Users\Roman\AppData\Roaming\Python\Python35\site-packages\sklearn\utils\validation.py", line 176, in check_consistent_length
"%s" % str(uniques))
ValueError: Found arrays with inconsistent numbers of samples: [199 299]
我希望通过获取此类错误,通过方法accuracy_score获得准确性。我用谷歌搜索我找不到任何可以帮助我的东西。谁能解释一下会发生什么?
答案 0 :(得分:1)
sklearn.metrics.accuracy_score()
需要y_true
和y_pred
个参数。也就是说,对于相同的数据集(可能是测试集),它想知道基础事实和模型预测的值。这将允许它评估模型与假设的完美模型相比的表现。
在您的代码中,您将传递两个不同数据集的真实结果变量。这些结果既是真实的,也绝不反映您的模型正确分类观察的能力!
更新get_accuracy()
功能也将X_test
作为参数,我认为这更符合您的预期:
def get_accuracy(X_train, y_train, X_test, y_test):
perceptron = Perceptron(random_state=241)
perceptron.fit(X_train, y_train)
pred_test = perceptron.predict(X_test)
result = accuracy_score(y_test, pred_test)
return result