Python ValueError:找到样本数不一致的输入变量:

Python ValueError:找到样本数不一致的输入变量:,python,pandas,scikit-learn,Python,Pandas,Scikit Learn,运行下面的代码时出现值错误,我认为可能是由于iloc代码将数据拆分为x和y,但看不出我做错了什么: if st.checkbox('Select Multiple Columns'): new_data = st.multiselect( "Select the target columns. Please note, the target variable should be the la

运行下面的代码时出现值错误,我认为可能是由于iloc代码将数据拆分为x和y,但看不出我做错了什么:

            if st.checkbox('Select Multiple Columns'):
                new_data = st.multiselect(
                    "Select the target columns. Please note, the target variable should be the last column selected",
                    df.columns)
                df1 = df[new_data]
                st.dataframe(df1)

                # dividing data into X and Y varibles
                x = df1.iloc[:, :-1]
                y = df1.iloc[:-1]

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=seed)

            clf.fit(X_train, y_train)

            y_pred = clf.predict(X_test)
            st.write('Prediction:', y_pred)
我得到的错误如下:

ValueError:找到样本数不一致的输入变量:[196195] 回溯:

数据集的片段:

1/1/20  X   2020    206457
1/1/20  X   2021    70571
1/1/20  X   2022    46918
1/1/20  X   2023    36492
1/1/20  X   2024    0
1/1/20  X   2025    0
1/1/20  X   2020    286616
1/1/20  X   2021    134276
1/1/20  X   2022    87674
1/1/20  X   2023    240
1/1/20  X   2024    0
1/1/20  X   2025    0


检查您的2条语句代码:

x = df1.iloc[:, :-1]
y = df1.iloc[:-1]
x和y在
df1上的切片方式不同。整行为x,少行为y。因此,样本数量不一致:[196195]==>196代表x;195为y

请注意,
iloc[]
的第一个参数是行切片,而第二个参数是列切片


您有x切片所有行,少一列(不含最后一列),而y切片仅含一个参数,且仅对行(不含最后一行)进行切片,并且在不指定第二个参数的列切片的情况下对所有列进行切片。

检查这两条语句的代码:

x = df1.iloc[:, :-1]
y = df1.iloc[:-1]
x和y在
df1上的切片方式不同。整行为x,少行为y。因此,样本数量不一致:[196195]==>196代表x;195为y

请注意,
iloc[]
的第一个参数是行切片,而第二个参数是列切片


x对所有行进行切片,少一列(不含最后一列),而y仅对一个参数进行切片,且仅对行进行切片(不含最后一行),它对所有列进行切片,而不在第二个参数上指定列切片。

x和y的切片方式不同。x在整行上,而y在少一行上。x和y的切片方式不同。整行为x,少行为y。