来自Python的样本数量不一致错误

时间:2018-06-21 19:44:28

标签: python pandas machine-learning scikit-learn kaggle

我正在Spyder IDE上进行泰坦尼克号比赛。代码几乎不完整,但是我一次只做一个步骤(这是我第一次构建学习模型)。现在,尝试运行我的代码时,日志中出现Found input variables with inconsistent numbers of samples: [891, 183]错误。这是我到目前为止的内容:

import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error

train_path = "C:\\Users\\Omar\\Downloads\\Titanic Data\\train.csv"
train_data = pd.read_csv(train_path)
columns_of_interest = ['Survived','Pclass', 'Sex', 'Age']
filtered_titanic_data = train_data.dropna(axis=0)

x = train_data[columns_of_interest]
y = filtered_titanic_data.Survived

train_x, val_x, train_y, val_y = train_test_split(x, y, random_state=0)

titanic_model = DecisionTreeRegressor()
titanic_model.fit(train_x, train_y)

val_predictions = titanic_model.predict(val_x)

print(filtered_titanic_data)

Idk是否来自excel文件或参数。如果这是一个简单的问题,很抱歉。我无法实施其他人的解决方案。

1 个答案:

答案 0 :(得分:1)

错误是因为您正在从过滤数据中获取标签,并从未过滤数据中获取x

更改以下行

x = train_data[columns_of_interest]

x = filtered_titanic_data[columns_of_interest]