如何将用户输入转换为独立的虚拟变量

时间:2019-08-02 07:12:57

标签: python pandas prediction dummy-variable

我有一个如下所示的数据集

Plant   Brand   Volume
A         HL      12
B         AG      14
A         FP      50000
G         TT      08

为了训练模型,我使用pd.get_dummies方法制作了它们(Plant-Brand) 它扩展为186列,如下所示;

Plant_A    Plant_B ...  Brand_HL    Brand_AG   ...
   1          0            1           0
   0          1            0           1
   1          0            0           0
   0          0            0           0

我的模型运行良好,但是我需要在用户输入下使用此模型

用户应仅手动提供plantID,Brand和Volume 我需要转换这些值并以相同的格式写入(186列),以便预测独立的值

y_pred = model.predict(X_user)

如何将4维转换为186?有什么方法可以快速做到这一点?

预先感谢

1 个答案:

答案 0 :(得分:0)

您可以使用:

np_array = np.asarray(df) # df is your pandas DataFrame

np_array.resize((new_num_rows, new_num_columns))

但是您必须记住,old_num_rows * old_num_columns应该等于new_num_rows * new_num_columns