Python 新数据和训练数据集之间的不同形状

Python 新数据和训练数据集之间的不同形状,python,pandas,encoding,classification,shapes,Python,Pandas,Encoding,Classification,Shapes,我有一个数据框,看起来像下面的那个 Spent Products bought Target Variable 0 2300 Car/Mortgage/Leisure 0 1 1500 Car/Education 0 2 150 Groceries 1 3 700 Groceries/Education

我有一个数据框,看起来像下面的那个

    Spent      Products bought         Target Variable               
0   2300       Car/Mortgage/Leisure    0
1   1500       Car/Education           0
2   150        Groceries               1
3   700        Groceries/Education     1  
4   900        Mortgage                1
5   180        Education/Sports        1
6   1800       Car/Mortgage/Others     0
7   900        Sports/Groceries        1
8   1000       Self-Enrichment/Car     1
9   140        Car/Groceries           1
    Spent      Products bought         Target Variable               
0   230        Leisure                 1
1   150        Others                  1
2   100        Groceries               1
3   700        Education               1  
4   900        Mortgage                0
5   180        Education/Sports        1
6   1800       Car/Mortgage            0
7   400        Groceries               1
8   4000       Car                     1
9   140        Car/Groceries           1
我用了
pd.get_dummies
对所有“购买的产品”列进行了热编码现在我的形状是(5000150)

我训练/测试/分割我的数据,然后应用PCA。I
fit_transform
列车组,仅在测试组上应用
transform
。接下来,我使用了一个决策树分类器来预测,这使我获得了90%的准确率

现在问题来了。我有一套新的数据。我知道我的模型是在(,150)的形状上训练的,在**使用
pd进行编码后,这个**新数据只有(150,28)的形状

我知道将新数据与旧数据集合并不是一个解决方案。我有点卡住了,我不知道该怎么解决这个问题。有人有什么意见吗?谢谢

编辑:我尝试对新数据集重新编制索引,但没有成功。在我的培训集中的“购买的产品”列中有更多的独特变量,而在我的新数据集中则更少

新的数据帧看起来更像下面的数据帧

    Spent      Products bought         Target Variable               
0   2300       Car/Mortgage/Leisure    0
1   1500       Car/Education           0
2   150        Groceries               1
3   700        Groceries/Education     1  
4   900        Mortgage                1
5   180        Education/Sports        1
6   1800       Car/Mortgage/Others     0
7   900        Sports/Groceries        1
8   1000       Self-Enrichment/Car     1
9   140        Car/Groceries           1
    Spent      Products bought         Target Variable               
0   230        Leisure                 1
1   150        Others                  1
2   100        Groceries               1
3   700        Education               1  
4   900        Mortgage                0
5   180        Education/Sports        1
6   1800       Car/Mortgage            0
7   400        Groceries               1
8   4000       Car                     1
9   140        Car/Groceries           1

尝试
pd.get_dummies().reindex(train.columns,axis=1)
?你是说在新数据集上?还是在旧数据集上?在新数据集上
reindex
确保您拥有与
train
数据集相同顺序的相同列。刚刚尝试过,但它返回了我预期的结果,即“模型的功能数量必须与输入匹配。模型n_功能为150,输入n_功能为28”
train。列
必须是列车组的列(150)。否则,您应该检查一个热编码器。