前言:最近开始kaggle的一些竞赛,从一开始的懵懵懂懂,到后面的逐渐熟练,在kaggle上真的学到了很多,特此将自己的一些想法与思考记录下来,希望能和大家一起探讨学习,如果有不对的地方,希望大家可以指出来!
赛题描述:要求购房者描述他们梦dream以求的房子,他们可能不会从地下室天花板的高度或与东西方铁路的距离开始。但是,这场运动会比赛的数据集证明,与价格或卧室或白色栅栏的数量相比,影响价格谈判的因素更多。借助79个解释性变量(几乎)描述了爱荷华州埃姆斯市住宅的方方面面,本次竞赛要求您预测每个房屋的最终价格。链接
这是kaggle新手入门的一道题目,对于初入数据挖掘的同学来说,可以说难度不是很大,只要学过机器学习,熟悉常用的算法工具,例如各种回归算法以及随机森林等树结构的算法,还有pandas以及sklearn的工具使用,那么我们都会得到一个不错的结果。然而如何将成绩进一步的提高,模型的相关优化,是比较困难的,特别是对于我们新手来说,完全就是没有任何的思路。我在kaggle上学习了大神一些的技术方法之后,受益颇多,想在这里将自己的思考记录下来。
总结:对于数据挖掘比赛来说,特征工程是一个比较重要的环节,在训练模型之前,我们需要花费大量的时间精力进行特征工程(包括数据预处理,特征的构造,特征融合以及特征筛选),好的数据处理,会影响后续的训练结果,之前听过这样一句话:“特征工程决定了上限”(忘了是谁说的了。。。)特征构造好了,后续的模型结构我们根据任务场景以及数据的特点进行选择————比如说,常见的回归模型:lasso回归,逻辑回归,KernelRidge等;树结构模型:随机森林,Adaboost,xgboost,lightgbm;SVC,SVM等等。如果考虑数据特点的话,线性回归模型受数据大小影响较大,而树结构是根据特征属性进行分类,鲁棒性较好。当然,我们可以同时考虑这些模型,在后期,可以对模型进行融合,可以进一步提高自己的成绩。
class AveragingModels(BaseEstimator, RegressorMixin, TransformerMixin): # 相当于构造了一个新的estimator,并做回归的任务
def __init__(self, models):
self.models = models
# we define clones of the original models to fit the data in
def fit(self, X, y):
self.models_ = [clone(x) for x in self.models]
# Train cloned base models
for model in self.models_:
model.fit(X, y)
return self
# Now we do the predictions for cloned models and average them
def predict(self, X):
predictions = np.column_stack([
model.predict(X) for model in self.models_
])
return np.mean(predictions, axis=1)
这里构造了一种简单基础模型堆叠平均模型,即将基础模型的预测值作了平均作为最终的预测值。这里比较新颖的是,使用类构造一种新的估算器。
class StackingAveragedModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, base_models, meta_model, n_folds=5):
self.base_models = base_models
self.meta_model = meta_model
self.n_folds = n_folds
# We again fit the data on clones of the original models
def fit(self, X, y):
self.base_models_ = [list() for x in self.base_models]
self.meta_model_ = clone(self.meta_model)
kfold = KFold(n_splits=self.n_folds, shuffle=True, random_state=156)
# Train cloned base models then create out-of-fold predictions
# that are needed to train the cloned meta-model
out_of_fold_predictions = np.zeros((X.shape[0], len(self.base_models)))
for i, model in enumerate(self.base_models):
for train_index, holdout_index in kfold.split(X, y):
instance = clone(model)
self.base_models_[i].append(instance)
instance.fit(X[train_index], y[train_index])
y_pred = instance.predict(X[holdout_index])
out_of_fold_predictions[holdout_index, i] = y_pred
# Now train the cloned meta-model using the out-of-fold predictions as new feature
self.meta_model_.fit(out_of_fold_predictions, y)
return self
# Do the predictions of all base models on the test data and use the averaged predictions as
# meta-features for the final prediction which is done by the meta-model
def predict(self, X):
meta_features = np.column_stack([
np.column_stack([model.predict(X) for model in base_models]).mean(axis=1)
for base_models in self.base_models_])
return self.meta_model_.predict(meta_features)
在这里,我只将我在其中遇到的一些关键东西列举了出来,详细的可以在官网上一些分享的资料进行学习。对自己的整个过程进行了梳理,我觉得还可以在特征构造,以及模型训练过程中的一些参数的调整,像自动寻参的方法进行更多的工作。
https://www.cnblogs.com/king-lps/p/7843395.html
https://www.cnblogs.com/wzdLY/p/9656420.html
https://blog.csdn.net/ICERON/article/details/80243198
https://www.jianshu.com/p/c532424541ad
https://blog.csdn.net/wateryouyo/article/details/53909636
https://wenku.baidu.com/view/96140c8376a20029bd642de3.html
http://blog.sina.com.cn/s/blog_5eb5c9370102vyim.html
原文:https://www.cnblogs.com/wushupei/p/12449235.html