ML之RF:随机森林RF算法简介、应用、经典案例之详细攻略(二)

简介: ML之RF:随机森林RF算法简介、应用、经典案例之详细攻略

(3)、详细文档

class RandomForestRegressor Found at: sklearn.ensemble.forest

class RandomForestRegressor(ForestRegressor):

 

   Examples

   --------

   >>> from sklearn.ensemble import RandomForestRegressor

   >>> from sklearn.datasets import make_regression

   >>>

   >>> X, y = make_regression(n_features=4, n_informative=2,

   ...                        random_state=0, shuffle=False)

   >>> regr = RandomForestRegressor(max_depth=2, random_state=0)

   >>> regr.fit(X, y)

   RandomForestRegressor(bootstrap=True, criterion='mse',

    max_depth=2,

   max_features='auto', max_leaf_nodes=None,

   min_impurity_decrease=0.0, min_impurity_split=None,

   min_samples_leaf=1, min_samples_split=2,

   min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,

   oob_score=False, random_state=0, verbose=0, warm_start=False)

   >>> print(regr.feature_importances_)

   [ 0.17339552  0.81594114  0.          0.01066333]

   >>> print(regr.predict([[0, 0, 0, 0]]))

   [-2.50699856]

 

   Notes

   -----

   The default values for the parameters controlling the size of the trees

   (e.g. ``max_depth``, ``min_samples_leaf``, etc.) lead to fully grown and

   unpruned trees which can potentially be very large on some data sets.

    To

   reduce memory consumption, the complexity and size of the trees

    should be

   controlled by setting those parameter values.

 

   The features are always randomly permuted at each split. Therefore,

   the best found split may vary, even with the same training data,

   ``max_features=n_features`` and ``bootstrap=False``, if the improvement

   of the criterion is identical for several splits enumerated during the

   search of the best split. To obtain a deterministic behaviour during

   fitting, ``random_state`` has to be fixed.

 

   References

   ----------

 

   .. [1] L. Breiman, "Random Forests", Machine Learning, 45(1), 5-32,

    2001.

 

   See also

   --------

   DecisionTreeRegressor, ExtraTreesRegressor

   """

   def __init__(self,

       n_estimators=10,

       criterion="mse",

       max_depth=None,

       min_samples_split=2,

       min_samples_leaf=1,

       min_weight_fraction_leaf=0.,

       max_features="auto",

       max_leaf_nodes=None,

       min_impurity_decrease=0.,

       min_impurity_split=None,

       bootstrap=True,

       oob_score=False,

       n_jobs=1,

       random_state=None,

       verbose=0,

       warm_start=False):

       super(RandomForestRegressor, self).__init__

        (base_estimator=DecisionTreeRegressor(), n_estimators=n_estimators,

        estimator_params=("criterion", "max_depth", "min_samples_split",

        "min_samples_leaf", "min_weight_fraction_leaf", "max_features",

        "max_leaf_nodes", "min_impurity_decrease", "min_impurity_split",

        "random_state"), bootstrap=bootstrap, oob_score=oob_score,

        n_jobs=n_jobs, random_state=random_state, verbose=verbose,

        warm_start=warm_start)

       self.criterion = criterion

       self.max_depth = max_depth

       self.min_samples_split = min_samples_split

       self.min_samples_leaf = min_samples_leaf

       self.min_weight_fraction_leaf = min_weight_fraction_leaf

       self.max_features = max_features

       self.max_leaf_nodes = max_leaf_nodes

       self.min_impurity_decrease = min_impurity_decrease

       self.min_impurity_split = min_impurity_split


2、RF用于分类


0、RF用于分类与用于回归的两点区别

(1)、第一个不同是用于判断分割点质量的标准。DT的训练过程,需要尝试所有可能的属性,针对每个属性尝试所有可能的分割点,然后从中找出最佳的属性及其分割点。对于回归决策树,分割点的质量是由平方误差和( sum squared error)决定的。但是平方误差和对于分类问题就不起作用了,需要类似误分类率的指标来描述。

(2)、


1、sklearn.ensemble.RandomForestClassifier类构造函数

函数API官方解释:https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn-ensemble-randomforestclassifier


(1)、重点参数解释


Criterion:字符串,可选(缺省值为“gini”)。可能的取值如下。

Gini :利用基尼不纯度(Gini impurity)。

Entropy :利用基于熵的信息增益。

         当训练数据终结于决策树的叶子节点时,叶子节点含有属于不同类别的数据,则根据叶子节点中不同类别的数据所占的百分比,分类决策树自然就可以得到数据属于某个类别的概率。依赖于具体的应用,可能想直接获得上述的概率,或者想直接将叶子节点中所占数据最多的类别作为预测值返回。如果在获得预测结果的同时想要调整阈值,则需要获得概率值。为了生成曲线下面积(area under the curve,AUC),可能想获得接收者操作特征曲线(receiver operating curve,ROC) 及其概率以保证精确度。如果想计算误分类率,则需要将概率转换为类别的预测。


(2)、重点方法解释


Fit( X, y, sample_weight=None):随机森林分类版本惟一的不同在于标签y 的特征。对于分类问题,标签的取值为0 到类别数减1 的整数。对于二分类问题,标签的取值是0 或1。对于有nClass 个不同类别的多类别分类问题,标签是从0 到nClass-1 的整数。

Predict(X):对于属性矩阵(二维的numpy 数组)X,此函数产生所属类别的预测。它生成一个单列的数组,行数等于X 的行数。每个元素是预测的所属类别,不管问题是二分类问题还是多类别分类问题,都是一样的。

Predict_proba(X):这个版本的预测函数产生一个二维数组。行数等于X 的行数。列数就是预测的类别数(对于二分类问题就是2 列)。每行的元素就是对应类别的概率。

Predict_log_proba(X):这个版本的预测函数产生一个与predict_proba 相似的二维数组。但是显示的不是所属类别的概率,而是概率的log 值。


(3)、详细文档

class RandomForestClassifier Found at: sklearn.ensemble.forest

class RandomForestClassifier(ForestClassifier):

 

   Examples

   --------

   >>> from sklearn.ensemble import RandomForestClassifier

   >>> from sklearn.datasets import make_classification

   >>>

   >>> X, y = make_classification(n_samples=1000, n_features=4,

   ...                            n_informative=2, n_redundant=0,

   ...                            random_state=0, shuffle=False)

   >>> clf = RandomForestClassifier(max_depth=2, random_state=0)

   >>> clf.fit(X, y)

   RandomForestClassifier(bootstrap=True, class_weight=None,

    criterion='gini',

   max_depth=2, max_features='auto', max_leaf_nodes=None,

   min_impurity_decrease=0.0, min_impurity_split=None,

   min_samples_leaf=1, min_samples_split=2,

   min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,

   oob_score=False, random_state=0, verbose=0, warm_start=False)

   >>> print(clf.feature_importances_)

   [ 0.17287856  0.80608704  0.01884792  0.00218648]

   >>> print(clf.predict([[0, 0, 0, 0]]))

   [1]

 

   Notes

   -----

   The default values for the parameters controlling the size of the trees

   (e.g. ``max_depth``, ``min_samples_leaf``, etc.) lead to fully grown and

   unpruned trees which can potentially be very large on some data

    sets. To

   reduce memory consumption, the complexity and size of the trees

    should be

   controlled by setting those parameter values.

 

   The features are always randomly permuted at each split. Therefore,

   the best found split may vary, even with the same training data,

   ``max_features=n_features`` and ``bootstrap=False``, if the

    improvement

   of the criterion is identical for several splits enumerated during the

   search of the best split. To obtain a deterministic behaviour during

   fitting, ``random_state`` has to be fixed.

 

   References

   ----------

 

   .. [1] L. Breiman, "Random Forests", Machine Learning, 45(1), 5-32,

    2001.

 

   See also

   --------

   DecisionTreeClassifier, ExtraTreesClassifier

   """

   def __init__(self,

       n_estimators=10,

       criterion="gini",

       max_depth=None,

       min_samples_split=2,

       min_samples_leaf=1,

       min_weight_fraction_leaf=0.,

       max_features="auto",

       max_leaf_nodes=None,

       min_impurity_decrease=0.,

       min_impurity_split=None,

       bootstrap=True,

       oob_score=False,

       n_jobs=1,

       random_state=None,

       verbose=0,

       warm_start=False,

       class_weight=None):

       super(RandomForestClassifier, self).__init__

        (base_estimator=DecisionTreeClassifier(), n_estimators=n_estimators,

        estimator_params=("criterion", "max_depth", "min_samples_split",

        "min_samples_leaf", "min_weight_fraction_leaf", "max_features",

        "max_leaf_nodes", "min_impurity_decrease", "min_impurity_split",

        "random_state"), bootstrap=bootstrap, oob_score=oob_score,

        n_jobs=n_jobs, random_state=random_state, verbose=verbose,

        warm_start=warm_start, class_weight=class_weight)

       self.criterion = criterion

       self.max_depth = max_depth

       self.min_samples_split = min_samples_split

       self.min_samples_leaf = min_samples_leaf

       self.min_weight_fraction_leaf = min_weight_fraction_leaf

       self.max_features = max_features

       self.max_leaf_nodes = max_leaf_nodes

       self.min_impurity_decrease = min_impurity_decrease

       self.min_impurity_split = min_impurity_split



相关文章
|
2月前
|
数据采集 机器学习/深度学习 算法
|
2月前
|
搜索推荐 前端开发 数据可视化
【优秀python web毕设案例】基于协同过滤算法的酒店推荐系统,django框架+bootstrap前端+echarts可视化,有后台有爬虫
本文介绍了一个基于Django框架、协同过滤算法、ECharts数据可视化以及Bootstrap前端技术的酒店推荐系统,该系统通过用户行为分析和推荐算法优化,提供个性化的酒店推荐和直观的数据展示,以提升用户体验。
|
2月前
|
机器学习/深度学习 人工智能 算法
【人工智能】传统语音识别算法概述,应用场景,项目实践及案例分析,附带代码示例
传统语音识别算法是将语音信号转化为文本形式的技术,它主要基于模式识别理论和数学统计学方法。以下是传统语音识别算法的基本概述
56 2
|
2月前
|
机器学习/深度学习 算法 数据可视化
决策树算法介绍:原理与案例实现
决策树算法介绍:原理与案例实现
|
1天前
|
传感器 算法 C语言
基于无线传感器网络的节点分簇算法matlab仿真
该程序对传感器网络进行分簇,考虑节点能量状态、拓扑位置及孤立节点等因素。相较于LEACH算法,本程序评估网络持续时间、节点死亡趋势及能量消耗。使用MATLAB 2022a版本运行,展示了节点能量管理优化及网络生命周期延长的效果。通过簇头管理和数据融合,实现了能量高效和网络可扩展性。
|
28天前
|
算法 BI Serverless
基于鱼群算法的散热片形状优化matlab仿真
本研究利用浴盆曲线模拟空隙外形,并通过鱼群算法(FSA)优化浴盆曲线参数,以获得最佳孔隙度值及对应的R值。FSA通过模拟鱼群的聚群、避障和觅食行为,实现高效全局搜索。具体步骤包括初始化鱼群、计算适应度值、更新位置及判断终止条件。最终确定散热片的最佳形状参数。仿真结果显示该方法能显著提高优化效率。相关代码使用MATLAB 2022a实现。
|
28天前
|
算法 数据可视化
基于SSA奇异谱分析算法的时间序列趋势线提取matlab仿真
奇异谱分析(SSA)是一种基于奇异值分解(SVD)和轨迹矩阵的非线性、非参数时间序列分析方法,适用于提取趋势、周期性和噪声成分。本项目使用MATLAB 2022a版本实现从强干扰序列中提取趋势线,并通过可视化展示了原时间序列与提取的趋势分量。代码实现了滑动窗口下的奇异值分解和分组重构,适用于非线性和非平稳时间序列分析。此方法在气候变化、金融市场和生物医学信号处理等领域有广泛应用。
|
29天前
|
资源调度 算法
基于迭代扩展卡尔曼滤波算法的倒立摆控制系统matlab仿真
本课题研究基于迭代扩展卡尔曼滤波算法的倒立摆控制系统,并对比UKF、EKF、迭代UKF和迭代EKF的控制效果。倒立摆作为典型的非线性系统,适用于评估不同滤波方法的性能。UKF采用无迹变换逼近非线性函数,避免了EKF中的截断误差;EKF则通过泰勒级数展开近似非线性函数;迭代EKF和迭代UKF通过多次迭代提高状态估计精度。系统使用MATLAB 2022a进行仿真和分析,结果显示UKF和迭代UKF在非线性强的系统中表现更佳,但计算复杂度较高;EKF和迭代EKF则更适合维数较高或计算受限的场景。
|
1月前
|
算法
基于SIR模型的疫情发展趋势预测算法matlab仿真
该程序基于SIR模型预测疫情发展趋势,通过MATLAB 2022a版实现病例增长拟合分析,比较疫情防控力度。使用SIR微分方程模型拟合疫情发展过程,优化参数并求解微分方程组以预测易感者(S)、感染者(I)和移除者(R)的数量变化。![]该模型将总人群分为S、I、R三部分,通过解析或数值求解微分方程组预测疫情趋势。
|
1月前
|
算法 数据可视化 数据安全/隐私保护
基于LK光流提取算法的图像序列晃动程度计算matlab仿真
该算法基于Lucas-Kanade光流方法,用于计算图像序列的晃动程度。通过计算相邻帧间的光流场并定义晃动程度指标(如RMS),可量化图像晃动。此版本适用于Matlab 2022a,提供详细中文注释与操作视频。完整代码无水印。
下一篇
无影云桌面