簡體   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