前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >快速入门Python机器学习(21)

快速入门Python机器学习(21)

作者头像
顾翔
发布2022-09-23 20:04:20
2470
发布2022-09-23 20:04:20
举报
文章被收录于专栏:啄木鸟软件测试

10.1.3 随机森林回归法

类参数、属性和方法

代码语言:javascript
复制
class sklearn.ensemble.RandomForestRegressor(n_estimators=100, *, criterion='mse', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, bootstrap=True, oob_score=False, n_jobs=None, random_state=None, verbose=0, warm_start=False, ccp_alpha=0.0, max_samples=None)

参数

参数

类型

解释

n_estimators

int, default=100

森林中树木的数量。

random_state

RandomState instance or None, default=None

控制生成树时使用的样本引导的随机性(如果bootstrap=True)和在每个节点上查找最佳分割时要考虑的特征的采样(如果max_features < n_features)。

属性

属性

解释

base_estimator_

DecisionTreeClassifier用于创建拟合子估计器集合的子估计器模板。

estimators_

list of DecisionTreeClassifier拟合子估计量的集合。

n_features_

int执行拟合时的特征数。

n_outputs_

int执行拟合时的输出数。

feature_importances_

ndarray of shape (n_features,)基于杂质的特征非常重要。

oob_score_

float使用现成的估计值获得的训练数据集的得分。只有当oob_score为True时,此属性才存在。

oob_decision_function

ndarray of shape (n_samples, n_classes)利用训练集上的包外估计计算决策函数。如果nèu估计量很小,则可能在引导过程中从未遗漏数据点。在这种情况下,oob_decision_function_可能包含NaN。只有当oob_score为True时,此属性才存在。

方法

apply(X)

将森林中的树应用到X,返回叶指数。

decision_path(X)

返回林中的决策路径。

fit(X, y[, sample_weight])

从训练集(X,y)建立一个树的森林。

get_params([deep])

获取此估计器的参数。

predict(X)

预测X的回归目标。

score(X, y[, sample_weight])

返回预测的确定系数R2。

set_params(**params)

设置此估计器的参数。

随机森林回归分析make_regression数据
代码语言:javascript
复制
#加入噪音
def DecisionTreeRegressor_for_make_regression_add_noise():
       myutil = util()
       X,y = make_regression(n_samples=100,n_features=1,n_informative=2,noise=50,random_state=8)
       X_train,X_test,y_train,y_test = train_test_split(X, y, random_state=8,test_size=0.3)
       clf = DecisionTreeRegressor().fit(X,y)
       title = "make_regression DecisionTreeRegressor()回归线(有噪音)"
       myutil.print_scores(clf,X_train,y_train,X_test,y_test,title)
       myutil.draw_line(X[:,0],y,clf,title)
       myutil.plot_learning_curve(DecisionTreeRegressor(),X,y,title)
       myutil.show_pic(title)

输出

代码语言:javascript
复制
make_regression DecisionTreeRegressor()回归线(有噪音):
100.00%
make_regression DecisionTreeRegressor()回归线(有噪音):
100.00%
随机森林回归分析波士顿房价数据
代码语言:javascript
复制
#分析波士顿房价数据
def DecisionTreeRegressor_for_boston():
       myutil = util()
       boston = datasets.load_boston()
       X,y = boston.data,boston.target
       X_train,X_test,y_train,y_test = train_test_split(X, y, random_state =8)
       for max_depth in [1,3,5,7]:
              clf = DecisionTreeRegressor(max_depth=max_depth)
              clf.fit(X_train,y_train)
              title=u"波士顿据测试集(max_depth="+str(max_depth)+")"
              myutil.print_scores(clf,X_train,y_train,X_test,y_test,title)
              myutil.plot_learning_curve(DecisionTreeRegressor(max_depth=max_depth),X,y,title)
              myutil.show_pic(title)

输出

代码语言:javascript
复制
波士顿据训练集(max_depth=1):
45.95%
波士顿据测试集(max_depth=1):
35.44%
波士顿据训练集(max_depth=3):
83.84%
波士顿据测试集(max_depth=3):
62.87%
波士顿据训练集(max_depth=5):
93.82%
波士顿据测试集(max_depth=5):
70.37%
波士顿据训练集(max_depth=7):
97.31%
波士顿据测试集(max_depth=7):
77.55%

仍旧存在过拟合现象

随机森林回归分析糖尿病数据
代码语言:javascript
复制
#分析糖尿病数据
def DecisionTreeRegressor_for_diabetes():
       myutil = util()
       diabetes = datasets.load_diabetes()
       X,y = diabetes.data,diabetes.target
       X_train,X_test,y_train,y_test = train_test_split(X, y, random_state =8)
       for max_depth in [1,3,5,7]:
              clf = DecisionTreeRegressor(max_depth=max_depth)
              clf.fit(X_train,y_train)
              title=u"糖尿病据测试集(max_depth="+str(max_depth)+")"
              myutil.print_scores(clf,X_train,y_train,X_test,y_test,title)
              myutil.plot_learning_curve(DecisionTreeRegressor(max_depth=max_depth),X,y,title)
              myutil.show_pic(title)

输出

代码语言:javascript
复制
糖尿病据训练集(max_depth=1):
30.44%
糖尿病据测试集(max_depth=1):
15.21%
糖尿病据训练集(max_depth=3):
55.64%
糖尿病据测试集(max_depth=3):
28.37%
糖尿病据训练集(max_depth=5):
71.81%
糖尿病据测试集(max_depth=5):
19.97%
糖尿病据训练集(max_depth=7):
84.30%
糖尿病据测试集(max_depth=7):
-1.30%

仍旧存在严重过拟合现象

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-06-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试培训 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 10.1.3 随机森林回归法
    • 类参数、属性和方法
      • 随机森林回归分析make_regression数据
        • 随机森林回归分析波士顿房价数据
          • 随机森林回归分析糖尿病数据
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档