整理了 pandas 库的常用指令

  1. iloc 切片,索引

1
print(train_data.iloc[0:4, [0, 1, 2, 3, -3, -2, -1]])

  1. concat 拼接

1
all_features = pd.concat((train_data.iloc[:, 1:-1], test_data.iloc[:, 1:]))

  1. 处理数值

1
2
3
4
5
6
7
8
numeric_features = all_features.dtypes[all_features.dtypes != 'object'].index
# 获取所有 数值类 的特征的列名
# Index(['MSSubClass', 'LotFrontage', ...])
all_features[numeric_features] = all_features[numeric_features].apply(
lambda x: (x - x.mean()) / (x.std()))
# 归一化处理,z-score
# 在标准化数据之后,所有均值消失,因此我们可以将缺失值设置为0
all_features[numeric_features] = all_features[numeric_features].fillna(0)

  1. 处理离散值

1
2
3
4
# One-hot encoding
# “Dummy_na=True”将“na”(缺失值)视为有效的特征值,并为其创建指示符特征
all_features = pd.get_dummies(all_features, dummy_na=True)
all_features.shape

  1. 提取 numpy 并转换为 tensor

1
2
3
4
5
n_train = train_data.shape[0]
train_features = torch.tensor(all_features[:n_train].values, dtype=torch.float32)
test_features = torch.tensor(all_features[n_train:].values, dtype=torch.float32)
train_labels = torch.tensor(
train_data.SalePrice.values.reshape(-1, 1), dtype=torch.float32)