ValueError:找到样本数量不一致的输入变量:[15996,8]

时间:2019-03-20 12:13:27

标签: python pandas csv

我已经使用熊猫加载了数据集并打印了前5行

df = pd.read_csv(r'filename')print(df.shape)
print(df.shape)

打印出数据集中的行数和列数

  

(15997,8)

print(df.head(5))

Just to show what the results look like

然后,如果我拆分数据集

X = df.iloc[1:].values
y = df.iloc[0].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.4,random_state=42, stratify=y)

我收到此错误

  

ValueError:找到样本数量不一致的输入变量:[15996,8]

我需要X值从数据集中排除第一行,而y值仅包括第一行

2 个答案:

答案 0 :(得分:2)

对于 df.iloc ,第一个索引用作键,因此您将在行上拆分。第二个索引用于列。试试这个:-

X = data.iloc[:,1:]
Y = data.iloc[:,0]

答案 1 :(得分:0)

尝试。

X = data.iloc[:,1:]
y = data.iloc[:,0]

由于数据框具有两个维度,因此必须在两个维度上进行切片才能创建子帧。