步骤
- 获取数据
- 探究数据(可视化+清洗)
- 设计并转换特征和目标变量
- 建立一个模型
- 制作并提交预测
获取数据
探究数据
查看目标函数与理解相关业务
1 | plt.figure() |
### 数据分类-> 数值特征和类别特征 * 方法一
1
select_dtypes(include=[np.number])
- 方法二
1
2
3features = pd.concat([train, test],keys=['train','test'])
numeric_feats = features.dtypes[features.dtypes!="object"].index
categorical_feats = features.dtypes[features.dtypes=="object"].index
查看特征与目标变量的关系
数值特征
- 通过
seaborn
的regplot
函数作箱形图来显示类别特征与目标变量之间的关系(部分举例)1
2
3
4
5
6
7
8
9def jointplot(x,y,**kwargs):
try:
sns.regplot(x=x,y=y)
except Exception:
print(x.value_counts())
numeric_feats = numeric_feats.drop("SalePrice")
f = pd.melt(train, id_vars=['SalePrice'],value_vars=numeric_feats)
g = sns.FacetGrid(f,col='variable',col_wrap=3,sharex=False,sharey=False,size=5)
g = g.map(jointplot,"value","SalePrice")

mark
类别特征
- 通过
seaborn
的boxplot()
函数作箱形图来显示类别特征与目标变量之间的关系(部分举例)1
2
3
4
5
6
7
8
9
10for c in categorical_feats:
train[c] = train[c].astype('category')
if train[c].isnull().any():
train[c] = train[c].cat.add_categories(["Missing"])
train[c] = train[c].fillna("Missing")
def boxplot(x,y,**kwargs):
sns.boxplot(x=x,y=y)
f = pd.melt(train,id_vars=['SalePrice'],value_vars=categorical_feats)
g = sns.FacetGrid(f,col='variable',col_wrap=3,sharex=False,sharey=False,size=5)
g = g.map(boxplot,"value","SalePrice")

mark
整体关系
- 通过
DataFrame.corr()
方法显示列之间的相关性(或关系),可以用来研究特征与目标变量的亲密程度1
2numeric_features = train.select_dtypes(include=[np.number])
corr = numeric_features.corr()
1 | 相关性前5 |
- 通过
seaborn
的heatmap()
函数作热力图显示1
2
3plt.subplots(figsize=(12,10))
corrmat = train.corr()
g = sns.heatmap(train.corr())

mark
缺失值情况
- 通过
isnull().sum()
查看缺失值
训练模型
- Ridge模型

mark
- Lasso模型

mark
源码
参考资料
Getting Started with Kaggle: House Prices Competition Kaggle实践 -- 房价预测top15%方案 scipy.stats.boxcox1p