将 model.predict() 的结果与原始 pandas DataFrame 合并?

新手上路,请多包涵

我正在尝试将 predict 方法的结果与 pandas.DataFrame 对象中的原始数据合并。

 from sklearn.datasets import load_iris
from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
import pandas as pd
import numpy as np

data = load_iris()

# bear with me for the next few steps... I'm trying to walk you through
# how my data object landscape looks... i.e. how I get from raw data
# to matrices with the actual data I have, not the iris dataset
# put feature matrix into columnar format in dataframe
df = pd.DataFrame(data = data.data)

# add outcome variable
df['class'] = data.target

X = np.matrix(df.loc[:, [0, 1, 2, 3]])
y = np.array(df['class'])

# finally, split into train-test
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = 0.8)

model = DecisionTreeClassifier()

model.fit(X_train, y_train)

# I've got my predictions now
y_hats = model.predict(X_test)

要将这些预测与原来的 df 合并,我试试这个:

 df['y_hats'] = y_hats

但这提出了:

ValueError:值的长度与索引的长度不匹配

我知道我可以将 df 拆分为 train_dftest_df 这个问题将得到解决,但实际上我需要按照上面的路径创建矩阵 Xy (我的实际问题是一个文本分类问题,在这个问题中,我在拆分为训练和测试之前对 整个 特征矩阵进行了归一化)。我怎样才能将这些预测值与我的 df 中的适当行对齐,因为 y_hats 数组是零索引的,并且似乎所有关于 哪些 行的信息都包含在 X_testy_test 丢失了吗?或者我会被降级为先将数据帧拆分为训练测试,然后再构建特征矩阵吗?我只想用数据框中的 np.nan 值填充 train 中包含的行。

原文由 blacksite 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

你的 y_hats 长度只会是测试数据的长度 (20%),因为你在 X_test 上预测过。一旦您的模型得到验证并且您对测试预测感到满意(通过检查模型在 X_test 预测上与 X_test 真实值相比的准确性),您应该在完整数据集 (X) 上重新运行预测。将这两行添加到底部:

 y_hats2 = model.predict(X)

df['y_hats'] = y_hats2

根据您的评论进行 编辑,这是一个更新的结果,它返回数据集,并在测试数据集中的位置附加了预测

from sklearn.datasets import load_iris
from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
import pandas as pd
import numpy as np

data = load_iris()

# bear with me for the next few steps... I'm trying to walk you through
# how my data object landscape looks... i.e. how I get from raw data
# to matrices with the actual data I have, not the iris dataset
# put feature matrix into columnar format in dataframe
df = pd.DataFrame(data = data.data)

# add outcome variable
df_class = pd.DataFrame(data = data.target)

# finally, split into train-test
X_train, X_test, y_train, y_test = train_test_split(df,df_class, train_size = 0.8)

model = DecisionTreeClassifier()

model.fit(X_train, y_train)

# I've got my predictions now
y_hats = model.predict(X_test)

y_test['preds'] = y_hats

df_out = pd.merge(df,y_test[['preds']],how = 'left',left_index = True, right_index = True)

原文由 flyingmeatball 发布,翻译遵循 CC BY-SA 3.0 许可协议

我有同样的问题(几乎)

我这样修的

...
.
.
.
X_train, X_test, y_train, y_test = train_test_split(df,df_class, train_size = 0.8)

model = DecisionTreeClassifier()

model.fit(X_train, y_train)

y_hats = model.predict(X_test)

y_hats  = pd.DataFrame(y_hats)

df_out = X_test.reset_index()
df_out["Actual"] = y_test.reset_index()["Columns_Name"]
df_out["Prediction"] = y_hats.reset_index()[0]

y_test['preds'] = y_hats

df_out = pd.merge(df,y_test[['preds']],how = 'left',left_index = True, right_index = True)

原文由 asmgx 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题