1.正向激励
说实话不是很理解
有点决策树与线性分类结合的意思
它的想法是:为样本矩阵中的样本随机分配初始权重,由此构建一棵带有权重的决策树,然后与实际相比更改权重。我的理解是这样的,比如在 https://blog.youkuaiyun.com/m0_46861439/article/details/119742127 的手写版中,样本的特征有x1与x2,分类依据如下,那么按正向激励的操作,可能是这般
方法:
import sklearn.tree as st
import sklearn.ensemble as se
# model: 决策树模型(一颗)
model = st.DecisionTreeRegressor(max_depth=4)
# 自适应增强决策树回归模型
# n_estimators:构建400棵不同权重的决策树,训练模型
model = se.AdaBoostRegressor(model, n_estimators=400, random_state=7)
# 训练模型
model.fit(train_x, train_y)
# 测试模型
pred_test_y = model.predict(test_x)
import sklearn.ensemble as se
import sklearn.datasets as sd
import sklearn.utils as su
import sklearn.tree as st
import sklearn.metrics as sm
#下载数据集
boston=sd.load_boston()
#boston=boston.reshape(-1,1)
#打乱原数据集的排序
#random_state:随机种子(可认为是标签),相同的随机种子对应的排序一致
x,y=su.shuffle(boston.data,boston.target,random_state=7)
#分割,80%的用于训练
train_size=int(len(x)*0.8)
train_x,test_x,train_y,test_y=x[:train_size],x[train_size:],y[:train_size],y[train_size:]
#模型
num=len(boston.feature_names)
model=st.DecisionTreeRegressor(max_depth=4)
model.fit(train_x,train_y)
#训练
pred_y=model.predict(test_x)
r=sm.r2_score(test_y,pred_y)
print('normal:',r)
model2=st.DecisionTreeRegressor(max_depth=4)
model3=se.AdaBoostRegressor(model2,n_estimators=400,random_state=7)
model3.fit(train_x,train_y)
pred_test_y_2=model3.predict(test_x)
r=sm.r2_score(test_y,pred_test_y_2)
print('r2_AdaBoostRegressor:',r)
2.特征重要性属性
根据每个特征划分子表前后的信息熵减少量就标志了该特征的重要程度,此即为该特征重要性指标。训练得到的模型对象提供了属性:feature_importances_来存储每个特征的重要性.
应该还是根据这个(entropy loss 熵减少量)的大小判顶重要性
方法:
model.fit(train_x, train_y)
fi = model.feature_importances_
结果是产生类似于这样的图
但是这里的难点在于呈现所用的代码
import matplotlib.pyplot as plt
import sklearn.tree as st
import sklearn.ensemble as se
import sklearn.datasets as sd
import sklearn.utils as su
import numpy as np
boston=sd.load_boston()
x,y=su.shuffle(boston.data,boston.target,random_state=7)
num=int(len(x)*0.7)
train_x,train_y,test_x,test_y=x[:num],y[:num],x[num:],y[num:]
feature_names=boston.feature_names
#train for the decision tree
model=st.DecisionTreeRegressor()
model.fit(train_x,train_y)
dt_fi=model.feature_importances_
#train for Adaboost
model2=se.AdaBoostRegressor(model,n_estimators=400,random_state=7)
model2.fit(train_x,train_y)
ab_fi=model2.feature_importances_
plt.figure('Feature Importance', facecolor='lightgray')
plt.subplot(211)
plt.title('decision')
sort_index=dt_fi.argsort()[::-1]
dt_fi=dt_fi[sort_index]
pos=np.arange(sort_index.size)
plt.bar(pos,dt_fi,facecolor='steelblue',edgecolor='deepskyblue')
plt.xticks(pos,feature_names[sort_index], rotation=30)
plt.subplot(212)
plt.title('ab')
sort_index=ab_fi.argsort()[::-1]
ab_fi=ab_fi[sort_index]
pos=np.arange(sort_index.size)
plt.bar(pos,ab_fi,facecolor='lightcoral',edgecolor='indianred')
plt.xticks(pos,feature_names[sort_index], rotation=30)
plt.tight_layout()
plt.legend()
plt.show()
3.随机森林
随机选择部分特征(就不是一次计算中并不是所有的特征都参与),这样的集合算法,不仅规避了强势样本对预测结果的影响,而且也削弱了强势特征的影响,使模型的预测能力更加泛化。
可以认为是决策树的升级版
import sklearn.ensemble as se
# 随机森林回归模型 (属于集合算法的一种)
# max_depth:决策树最大深度10
# n_estimators:构建 num 棵决策树,训练模型
# min_samples_split: 子表中最小样本数 若小于这个数字,则不再继续向下拆分
model = se.RandomForestRegressor(max_depth=10, n_estimators=num, min_samples_split=2)
'''
import sklearn.ensemble as se
# 随机森林回归模型 (属于集合算法的一种)
# max_depth:决策树最大深度10
# n_estimators:构建 num 棵决策树,训练模型
# min_samples_split: 子表中最小样本数 若小于这个数字,则不再继续向下拆分
model = se.RandomForestRegressor(max_depth=10, n_estimators=num, min_samples_split=2)
'''
import matplotlib.pyplot as plt
import sklearn.tree as st
import sklearn.ensemble as se
import sklearn.datasets as sd
import sklearn.utils as su
import numpy as np
data=np.loadtxt(r'D:\BaiduNetdiskDownload\数据集 21.8\乔总结数据集\bike_day.csv', unpack=False, dtype='U20', delimiter=',')
day_headers = data[0, 2:13]
x = np.array(data[1:, 2:13], dtype=float)
y = np.array(data[1:, -1], dtype=float)
x,y=su.shuffle(x,y,random_state=7)
num=int(len(x))
train_x,train_y,test_x,test_y=x[:num],y[:num],x[num:],y[num:]
model=se.RandomForestRegressor()
model.fit(train_x,train_y)
day_fi=model.feature_importances_
plt.subplot(211)
plt.figure('feature importance')
plt.title('day')
sort_indices=day_fi.argsort()[::-1]
pos=np.arange(sort_indices.size)
plt.xticks(pos,day_headers[sort_indices],weight=0.2)
plt.bar(pos,day_fi[sort_indices],facecolor='steelblue',edgecolor='deepskyblue')
print(sort_indices)
plt.show()