该模块可以把数据前处理+建模
整合起来
好处:
步骤1: 定义前处理步骤
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
# Preprocessing for numerical data 数字数据插值
numerical_transformer = SimpleImputer(strategy='constant')
# Preprocessing for categorical data 文字特征处理,插值+编码转换
categorical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='most_frequent')),
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
# Bundle preprocessing for numerical and categorical data
# 上面两者合并起来,形成完整的数据处理流程
preprocessor = ColumnTransformer(
transformers=[
('num', numerical_transformer, numerical_cols),
('cat', categorical_transformer, categorical_cols)
])
步骤2: 定义模型
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor(n_estimators=100, random_state=0)
步骤3: 创建和评估管道
我们使用Pipeline类来定义将预处理和建模步骤捆绑在一起的管道。
管道会在生成预测之前自动对数据进行预处理(如果没有管道,我们必须在进行预测之前先对数据进行预处理)。
# Bundle preprocessing and modeling code in a pipeline
# 将 前处理管道 + 模型管道,再次叠加形成新管道
my_pipeline = Pipeline(steps=[('preprocessor', preprocessor),
('model', model)
])
# Preprocessing of training data, fit model
my_pipeline.fit(X_train, y_train)
# Preprocessing of validation data, get predictions
preds = my_pipeline.predict(X_valid)
# 用定义好的pipeline 对test进行预测,提交,代码很简洁,不易出错
preds_test = my_pipeline.predict(X_test)
# Save test predictions to file
output = pd.DataFrame({'Id': X_test.index,
'SalePrice': preds_test})
output.to_csv('submission.csv', index=False)
You advanced 5,020 places on the leaderboard! Your submission scored 16459.13640, which is an improvement of your previous score of 16619.07644. Great job! 误差有点提升,哈哈,加油!?
交叉验证可以更好的验证模型,把数据分成几份(Folds),依次选取一份作为验证集,其余的用来训练,显然交叉验证会花费更多的时间
如何选择是否使用:
较小
的数据集,不需要太多的计算负担,则应运行交叉验证
较大
的数据集,单个验证集就足够了,因为数据足够多了,交叉验证花费的时间成本变大
from sklearn.ensemble import RandomForestRegressor
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
my_pipeline = Pipeline(steps=[
('preprocessor', SimpleImputer()),
('model', RandomForestRegressor(n_estimators=50,random_state=0))
])
from sklearn.model_selection import cross_val_score
# Multiply by -1 since sklearn calculates *negative* MAE
scores = -1 * cross_val_score(my_pipeline, X, y,
cv=5,
scoring='neg_mean_absolute_error')
print("MAE scores:\n", scores)
print("Average MAE score (across experiments):")
print(scores.mean())
# 树的棵数不同情况下,交叉验证的得分均值
def get_score(n_estimators):
"""Return the average MAE over 3 CV folds of random forest model.
Keyword argument:
n_estimators -- the number of trees in the forest
"""
my_pipeline = Pipeline(steps=[
('preprocessing',SimpleImputer()),
('model',RandomForestRegressor(n_estimators=n_estimators,random_state=0))
])
scores = -1*cross_val_score(my_pipeline,X,y,cv=3,scoring='neg_mean_absolute_error')
return scores.mean()
results = {}
for i in range(1,9):# 获取树的棵树是50,100,。。。,400时,模型的效果
results[50*i] = get_score(50*i)
# 可视化不同参数下的模型效果
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(list(results.keys()), list(results.values()))
plt.show()
n_estimators_best = min(results, key=results.get) #最合适的参数