![cover](https://img-blog.csdnimg.cn/c0ee6bfa36ad4b00ae6f45356cff1668.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAbGFnb29uX2xhbGE=,size_20,color_FFFFFF,t_70,g_se,x_16)
HDLSS笔记2进化森林Evolutionary Forest
目录数据情况DemoDemo尝试EF参数Evolutionary Forest"自动特征构建框架(Evolutionary Forest)-可解释性机器学习对比随机森林和Evolutionary Forest直接分类使用Evolutionary Forest构建的特征改进随机森林和XGBoost性能" scikit-learn中的“diabetes”442条样本,特征值10项 Evolutiona
目录
Evolutionary Forest
"自动特征构建框架(Evolutionary Forest)-可解释性机器学习
对比随机森林和Evolutionary Forest直接分类
使用Evolutionary Forest构建的特征改进随机森林和XGBoost性能" scikit-learn中的“diabetes”442条样本,特征值10项 Evolutionary Forest:
https://github.com/zhenlingcn/EvolutionaryForest
文档:
https://evolutionary-forest.readthedocs.io/
GP的自动化特征构建方法,可以提高传统机器学习系统的性能,同时保持类似的可解释性。
提供一个特征构建工具来增强现有的最先进的机器学习算法,特别是基于决策树的算法。
其中GP可参考:
https://www.cnblogs.com/dupuleng/articles/3574324.html
数据情况
load_diabetes 糖尿病数据集:
数据集共442条信息,特征值总共10项, 如下:
# age:年龄
# sex:性别
# bmi = body mass index:身体质量指数,是衡量是否肥胖和标准体重的重要指标,理想BMI(18.5~23.9) = 体重(单位Kg) ÷ 身高的平方 (单位m)
# bp = blood pressure :血压
# s1,s2,s3,s4,s4,s6 (六种血清的化验数据)
Demo
参考:
https://zhuanlan.zhihu.com/p/378444073
基于演化算法(Genetic Programming)的自动特征构建(automated feature engineering)算法Evolutionary Forest,特征工程方面
automated feature engineering based on
scikit-learn包中的一个问题 “diabetes”,其目标是预测一年后该疾病的进展情况。
对比随机森林和Evolutionary Forest
该框架的一个更重要的目标是获得更多优质的可解释特征,从而提高主流机器学习模型的性能。因此,我们可以基于impurity reduction计算特征的重要性,然后根据这些重要性分数对所有特征进行排序
放弃使用原来的特征,只保留构造好的特征,训练基于新特征的随机森林
自动构建的特征大幅度改进了XGBoost和随机森林的性能。基于这个实验的结果,我们可以得出结论,Evolutionary Forest不仅是一种有效的回归模型构建方法,可以构建一个强大的回归模型,也作为一个自动特征生成方法,可以用于生成可解释的特征以及提高现有机器学习系统的性能
Demo尝试
Evolutionary Forest——基于演化算法的自动特征工程框架 - 知乎
prompt终端打开虚拟环境(若有的话)
activate Liver |
或cmd终端打开虚拟环境(在cmd用需要确保anaconda路径加入环境变量,且在cmd中执行过conda init)
conda activate Liver |
安装自动特征构建框架Evolutionary Forest(其中选项-u代表更新到最新版本)
pip install -U evolutionary_forest conda install lightgbm y pip install xgboost pip install catboost pip install deap pip install sympy pip install dill pip install pytest pip install hdfe |
模型训练
import random import numpy as np from lightgbm import LGBMRegressor from sklearn.datasets import load_diabetes from sklearn.ensemble import ExtraTreesRegressor, AdaBoostRegressor, GradientBoostingRegressor, RandomForestRegressor from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split from xgboost import XGBRegressor from catboost import CatBoostRegressor from evolutionary_forest.utils import get_feature_importance, plot_feature_importance, feature_append from evolutionary_forest.forest import cross_val_score, EvolutionaryForestRegressor random.seed(0) np.random.seed(0) X, y = load_diabetes(return_X_y=True) x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) r = RandomForestRegressor() r.fit(x_train, y_train) print(r2_score(y_test, r.predict(x_test))) r = EvolutionaryForestRegressor(max_height=8, normalize=True, select='AutomaticLexicase', mutation_scheme='weight-plus-cross-global', gene_num=10, boost_size=100, n_gen=100, base_learner='DT', verbose=True) r.fit(x_train, y_train) print(r2_score(y_test, r.predict(x_test))) |
基于impurity reduction(提升纯度)计算特征的重要性,然后根据这些重要性分数对所有特征进行排序(显示前15个)
feature_importance_dict = get_feature_importance(r) plot_feature_importance(feature_importance_dict) |
(和原文中的不太一样,原文中最高是10左右)
利用构造好的新特征,改进现有模型的性能:
code_importance_dict = get_feature_importance(r, simple_version=False) new_X = feature_append(r, X, list(code_importance_dict.keys())[:20], only_new_features=True) new_train = feature_append(r, x_train, list(code_importance_dict.keys())[:20], only_new_features=True) new_test = feature_append(r, x_test, list(code_importance_dict.keys())[:20], only_new_features=True) new_r = RandomForestRegressor() new_r.fit(new_train, y_train) print(r2_score(y_test, new_r.predict(new_test))) |
基于新特征的随机森林0.32616589809620355
新特征应用于其他机器学习
regressor_list = ['RF', 'ET', 'AdaBoost', 'GBDT', 'DART', 'XGBoost', 'LightGBM', 'CatBoost'] scores_base = [] scores = [] for regr in regressor_list: regressor = { 'RF': RandomForestRegressor(n_jobs=1, n_estimators=100), 'ET': ExtraTreesRegressor(n_estimators=100), 'AdaBoost': AdaBoostRegressor(n_estimators=100), 'GBDT': GradientBoostingRegressor(n_estimators=100), 'DART': LGBMRegressor(n_jobs=1, n_estimators=100, boosting_type='dart'), 'XGBoost': XGBRegressor(n_jobs=1, n_estimators=100), 'LightGBM': LGBMRegressor(n_jobs=1, n_estimators=100), 'CatBoost': CatBoostRegressor(n_estimators=100, thread_count=1, verbose=False, allow_writing_files=False), }[regr] score = cross_val_score(regressor, X, y) print(regr, score, np.mean(score)) scores_base.append(np.mean(score)) score = cross_val_score(regressor, new_X, y) print(regr, score, np.mean(score)) scores.append(np.mean(score)) scores_base = np.array(scores_base) scores = np.array(scores) |
RF [0.40687788 0.48232282 0.44269645 0.35267621 0.44181526] 0.4252777225424557 RF [0.44235306 0.55521468 0.50988165 0.42374457 0.56986234] 0.5002112584552403 ET [0.36793279 0.5021531 0.4315543 0.40709356 0.45861526] 0.43346980380606287 ET [0.43861455 0.53037941 0.51019364 0.43328818 0.5468222 ] 0.4918595966702258 AdaBoost [0.37194555 0.45511817 0.41798425 0.41877328 0.42923058] 0.4186103652544749 AdaBoost [0.38871318 0.5757655 0.49970396 0.43931434 0.53020966] 0.48674132683971505 GBDT [0.3351751 0.51969606 0.42910228 0.33293505 0.42697212] 0.40877612058000456 GBDT [0.42000258 0.51439486 0.49169029 0.33489281 0.51611125] 0.45541835729862523 DART [0.35379204 0.4339262 0.40526565 0.29617651 0.40656592] 0.37914526592589093 DART [0.36980069 0.51490581 0.49291405 0.34173729 0.51993531] 0.4478586279582305 XGBoost [0.19069273 0.31696014 0.38186465 0.15942315 0.30005706] 0.2697995450013659 XGBoost [0.27950515 0.4812901 0.43031115 0.25950939 0.45438655] 0.381000467688719 LightGBM [0.35463506 0.44812537 0.36198867 0.27123459 0.4335444 ] 0.3739056175995374 LightGBM [0.36277216 0.53871535 0.51197452 0.32168571 0.49241755] 0.44551305914917166 CatBoost [0.31416538 0.46649883 0.37371607 0.35872775 0.44555604] 0.39173281321499964 CatBoost [0.40044902 0.52514347 0.50549961 0.35889613 0.54496716] 0.46699107690343505 |
Evolutionary Forest不仅是一种有效的回归模型构建方法,可以构建一个强大的回归模型,也作为一个自动特征生成方法,可以用于生成可解释的特征以及提高现有机器学习系统的性能。自动构建的特征引起的改善如下图所示。
需要注意的是,即使发现的特征提高了验证得分,也存在过拟合的风险。因此,在实际应用中,我们应该对获得的模型进行检验,以确保新构建模型的有效性
EF参数
n_gen=100生成100代(generational)
gene_num一次关注几代特征
max_height=8树高度
更多推荐
所有评论(0)