【发布时间】:2022-01-01 05:57:35
【问题描述】:
我正在使用来自以下链接的测试和训练数据集:
https://www.kaggle.com/emrearslan123/house-price-prediction/data
import pandas as pd
from sklearn.preprocessin import StandardScaler
import numpy as np
df = pd.read_csv('./train.csv')
test = pd.read_csv('./test.csv')
important_num_cols = list(df.corr()["SalePrice"][(df.corr()["SalePrice"]>0.50) |
(df.corr()["SalePrice"]<-0.50)].index)
cat_cols = ["MSZoning", "Utilities","BldgType","Heating","KitchenQual","SaleCondition","LandSlope"]
important_cols = important_num_cols + cat_cols
df = df[important_cols]
important_cols.remove('SalePrice')
test = test[important_cols]
#the following lines correspond to na values in the test dataset
#while df has no null values
test['TotalBsmtSF'] = test['TotalBsmtSF'].fillna(test['TotalBsmtSF'].mode()[0])
test['GarageCars'] = test['GarageCars'].fillna(test['GarageCars'].mode()[0])
test['GarageArea'] = test['GarageArea'].fillna(test['GarageArea'].mean())
test['MSZoning'] = test['MSZoning'].fillna(test['MSZoning'].mode()[0])
test['Utilities'] = test['Utilities'].fillna(test['Utilities'].mode()[0])
test['KitchenQual'] = test['KitchenQual'].fillna(test['KitchenQual'].mode()[0])
y = df['SalePrice']
X = df.drop('SalePrice', axis=1)
所以基本上我的训练数据集 X 与我的测试数据集具有相同数量的特征。但是,当我在两个数据集上应用一个热编码 pd.get_dummies 时,我的训练数据比我的测试数据多 3 个特征/列。
X = pd.get_dummies(X, columns=cat_cols)
test = pd.get_dummies(test, columns=cat_cols)
这怎么可能?
【问题讨论】:
标签: python jupyter-notebook one-hot-encoding