提交 aa75893a 编写于 作者: S ShusenTang

fix bug(#46) in feature preprocessing

上级 62bee2ff
此差异已折叠。
......@@ -83,8 +83,8 @@ all_features = pd.concat((train_data.iloc[:, 1:-1], test_data.iloc[:, 1:]))
numeric_features = all_features.dtypes[all_features.dtypes != 'object'].index
all_features[numeric_features] = all_features[numeric_features].apply(
lambda x: (x - x.mean()) / (x.std()))
# 标准化后,每个特征的均值变为0,所以可以直接用0来替换缺失值
all_features = all_features.fillna(0)
# 标准化后,每个数值特征的均值变为0,所以可以直接用0来替换缺失值
all_features[numeric_features] = all_features[numeric_features].fillna(0)
```
接下来将离散数值转成指示特征。举个例子,假设特征MSZoning里面有两个不同的离散值RL和RM,那么这一步转换将去掉MSZoning特征,并新加两个特征MSZoning\_RL和MSZoning\_RM,其值为0或1。如果一个样本原来在MSZoning里的值为RL,那么有MSZoning\_RL=1且MSZoning\_RM=0。
......@@ -92,12 +92,12 @@ all_features = all_features.fillna(0)
``` python
# dummy_na=True将缺失值也当作合法的特征值并为其创建指示特征
all_features = pd.get_dummies(all_features, dummy_na=True)
all_features.shape # (2919, 354)
all_features.shape # (2919, 331)
```
可以看到这一步转换将特征数从79增加到了354
可以看到这一步转换将特征数从79增加到了331
最后,通过`values`属性得到NumPy格式的数据,并转成`NDArray`方便后面的训练。
最后,通过`values`属性得到NumPy格式的数据,并转成`Tensor`方便后面的训练。
``` python
n_train = train_data.shape[0]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册