繁体   English   中英

ValueError:发现样本数量不一致的输入变量:[100, 300]

[英]ValueError: Found input variables with inconsistent numbers of samples: [100, 300]

我是从 udemy 学习 KNN 的。 数据集从这里下载。

当我尝试运行以下代码时:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dataset = pd.read_csv('Social_Network_Ads.csv')
x = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4:].values

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=0)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)

from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=5, metric='minkowski', p=2)
classifier.fit(x_train, y_train.ravel())

y_pred = classifier.predict(x_train)

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

我得到的错误是:

回溯(最近一次调用):文件“/home/ashutosh/Machine Learning AZ Template Folder/Part 3 - Classification/Section 15 - K-Nearest Neighbors (K-NN)/knn(1).py”,第 24 行,在厘米=混淆_矩阵(y_test,y_pred)

文件“/home/ashutosh/.local/lib/python3.7/site-packages/sklearn/metrics/_classification.py”,第268行,在confusion_matrix y_type, y_true, y_pred = _check_targets(y_true, y_pred)

文件“/home/ashutosh/.local/lib/python3.7/site-packages/sklearn/metrics/_classification.py”,第 80 行,在 _check_targets check_consistent_length(y_true, y_pred)

文件“/home/ashutosh/.local/lib/python3.7/site-packages/sklearn/utils/validation.py”,第 212 行,在 check_consistent_length “样本:%r”% [int(l) for l in lengths ])

ValueError:发现样本数量不一致的输入变量:[100, 300]

换行:

y_pred = classifier.predict(x_train)

到:

y_pred = classifier.predict(x_test)

你可以走了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM