我正在尝试在Sklearn python中使用train_test_split
方法,但是训练和测试集的长度不相等。
当我尝试在Jupyter笔记本中运行以下代码段时,它显示
“ ValueError:找到数量不一致的输入变量 样本:[111,38]“
import pandas as pd
df=pd.read_csv("iris.data")
df.head(5)
x=df[['5.1','3.5','1.4','0.2']].values
y=df[['Iris-setosa']].values
from sklearn.model_selection import train_test_split
x_train,y_train,x_test,y_test=train_test_split(x,y,random_state=42)
print("train set: ",x_train.shape,y_train.shape)
print("test set: ",x_test.shape,y_test.shape)
from sklearn.neighbors import KNeighborsClassifier
k=4
neigh=KNeighborsClassifier(n_neighbors=k).fit(x_train,y_train)
neigh
答案 0 :(得分:2)
更改行:
x_train,y_train,x_test,y_test=train_test_split(x,y,random_state=42)
收件人:
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=42)
你很好!
请注意,您的数据框没有标题。因此,(i)要正确加载它,并且(ii)防止在您拟合模型时出现警告,您应该将代码放在顶部:
df = pd.read_csv("iris.data", header=None)
x=df.iloc[:,[0,1,2,3]].values
y=df.iloc[:,[4]].values.ravel()