随机森林

##随机森林-分类

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification

#1.导入数据
#data = pd.read_csv(’’)

#2.数据预处理
#略,最终生成x_train,y_train,x_test
#导入sklearn的数据集
x_train, y_train = make_classification(n_samples=1000, n_features=4, n_informative=2, n_redundant=0, random_state=0, shuffle=False)

#3.模型训练
clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0)
clf.fit(x_train, y_train)

#4.模型预测

#特征重要性
print(‘feature_importances:’, clf.feature_importances_)
#构造数据
x_test = [[0, 0, 0, 0], [2, 6, 4, 4]]
y_predict = clf.predict(x_test)

print(y_predict)

#参数列表与调参方法
RandomForestClassifier(n_estimators=’warn’, criterion=’gini’, 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, class_weight=None)

#n_estimators : integer, optional (default=10)
森林里的树木数量。
在版本0.20中更改:默认值n_estimators将从版本0.20中的10更改为版本0.22中的100。

#criterion : string, optional (default=”gini”)
衡量分裂质量的功能。支持的标准是基尼系数的“gini”和信息增益的“entropy”(熵)。

#max_depth : integer or None, optional (default=None)
树的最大深度。如果为None,则扩展节点直到所有叶子都是纯的或直到所有叶子包含少于min_samples_split个样本。

#min_samples_split : int, float, optional (default=2)
拆分内部节点所需的最小样本数:
如果是int,则考虑min_samples_split为最小数量。
如果是浮点数,那么它min_samples_split是一个分数, 是每个分割的最小样本数。ceil(min_samples_split * n_samples)

#min_samples_leaf : int, float, optional (default=1)
叶节点所需的最小样本数。只有在每个左右分支中留下至少min_samples_leaf个训练样本时,才会考虑任何深度的分裂点。这可能具有平滑模型的效果,尤其是在回归中。
如果是int,则考虑min_samples_leaf为最小数量。
如果是float,那么它min_samples_leaf是一个分数, 是每个节点的最小样本数。ceil(min_samples_leaf * n_samples)

#min_weight_fraction_leaf : float, optional (default=0.)
需要在叶节点处的权重总和(所有输入样本的总和)的最小加权分数。当未提供sample_weight时,样本具有相同的权重。

#max_features : int, float, string or None, optional (default=”auto”)
寻找最佳分割时要考虑的特征数量:
如果是int,则每次拆分时考虑max_features特征。
如果是浮点数,那么max_features是一个分数,并且 每次拆分时都会考虑int(max_features * n_features)特征。
如果是“自动”,那么max_features=sqrt(n_features)。
如果是“sqrt”,那么max_features=sqrt(n_features)。
如果是“log2”,那么max_features=log2(n_features)。
如果没有,那么max_features=n_features。
注意:在找到节点样本的至少一个有效分区之前,搜索分割不会停止,即使它需要有效地检查多个max_features功能。

#max_leaf_nodes : int or None, optional (default=None)
以best-first的方式种max_leaf_nodes节点的树。最佳节点定义为杂质的相对减少。如果为None则无限数量的叶节点。

#min_impurity_decrease : float, optional (default=0.)
如果该分裂导致杂质的减少大于或等于该值,则将分裂节点。

#min_impurity_split : float, (default=1e-7)
弃用

#bootstrap : boolean, optional (default=True)
是否在构建树时使用bootstrap样本。如果为False,则使用整个数据集构建每个树。

#oob_score : bool (default=False)
是否使用袋外样本来估计泛化精度。

#n_jobs : int or None, optional (default=None)
要运行的并行作业数。 None除非在joblib.parallel_backend上下文中,否则表示1 。 -1表示使用所有处理器。

#random_state : int, RandomState instance or None, optional (default=None)
如果是int,则random_state是随机数生成器使用的种子; 如果是RandomState实例,则random_state是随机数生成器;
如果为None,则随机数生成器是由np.random使用的RandomState实例。

#verbose : int, optional (default=0)
在拟合和预测时控制详细程度。

#warm_start : bool, optional (default=False)
设置True为时,重用上一个调用的解决方案以适应并向整体添加更多估算器,否则,重新训练整个新森林。

#class_weight : dict, list of dicts, “balanced”, “balanced_subsample” or None, optional (default=None)
与表单中的类相关联的权重。如果没有给出,所有类都应该有一个重量。对于多输出问题,可以按与y列相同的顺序提供dicts列表。{class_label: weight}
请注意,对于多输出(包括多标记),应为其自己的dict中的每个列的每个类定义权重。
例如,对于四类多标签分类权重应为[{0:1,1:1},{0:1,1:5},{0:1,1:1},{0:1,1: 1}]而不是[{1:1},{2:5},{3:1},{4:1}]。
“平衡”模式使用y的值自动调整与输入数据中的类频率成反比的权重 n_samples / (n_classes * np.bincount(y))
“balanced_subsample”模式与“balanced”相同,只是基于每个生长的树的bootstrap样本计算权重。
对于多输出,y的每列的权重将相乘。
请注意,如果指定了sample_weight,这些权重将与sample_weight(通过fit方法传递)相乘。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值