当前位置:首页 >> 中药方剂 >> 决策树和随机森林的理论、实现和超参数缩减

决策树和随机森林的理论、实现和超参数缩减

发布时间:2023-04-18

om_state=42)5. 应用于大树和随机热带雨林启发式from sklearn.metrics import accuracy_score, confusion_matrix, classification_reportdef print_score(clf, X_train, y_train, X_test, y_test, train=True): if train: pred = clf.predict(X_train) print("Train Result:================================================") print(f"Accuracy Score: {accuracy_score(y_train, pred) * 100:.2f}%") print("_______________________________________________") print(f"Confusion Matrix: {confusion_matrix(y_train, pred)}") elif train==False: pred = clf.predict(X_test) print("Test Result:================================================") print(f"Accuracy Score: {accuracy_score(y_test, pred) * 100:.2f}%") print("_______________________________________________") print(f"Confusion Matrix: {confusion_matrix(y_test, pred)}")

5.1 确定性确定性

确定性匹配:

criterion: 量度重组准确性。支持的标准是佐拉溶解的“佐拉”和电子邮件增益的“不确定性”。splitter:用于在每个终端处并不无需要重组的战略。支持的战略是“best”并不无需要最佳重组和“random”并不无需要随机重组。max_depth:大树的最大浅层。如果为None,则揭开终端,直到所有叶为终端,或者直到所有叶为相关联的样品小于min_samples_split。min_samples_split:重组内部终端所需要的最大者样品数。min_samples_leaf:叶为终端上所需要的最大者样品数。min_weight_fraction_leaf:叶为终端上所需要的总也就是说的最大者量化高分。当没有透过sample_weight时,样品带有相等的权绝对值。max_features:寻找最佳重组时要回避的基本特征为数。max_leaf_nodesmax_leaf_nodes:以最佳应将的方式将用到max_leaf_nodes形成大树。最佳终端定义为溶解的相对减缓。如果为None,则有无限为数的叶为终端。min_impurity_decrease:如果该重组导致溶解减缓若有该绝对值,则该终端将被重组。min_impurity_split: 原定暂缓的阈绝对值。如果一个终端的溶解略高于阈绝对值,则该终端将重组,否则,它是一个叶为子。from sklearn.tree import DecisionTreeClassifiertree_clf = DecisionTreeClassifier(random_state=42)tree_clf.fit(X_train, y_train)print_score(tree_clf, X_train, y_train, X_test, y_test, train=True)print_score(tree_clf, X_train, y_train, X_test, y_test, train=False)

5.2确定性确定性超强匹配调优

超强匹配max_depth遏制确定性的基本上不确定性。这个超强匹配无需要在了事近似和过近似确定性二者之间顺利完成权衡。让我们为分类学和回归构筑一棵浅大树,然后再次构筑一棵深的大树,以了解匹配的直接影响。

超强匹配min_samples_leaf、min_samples_split、max_leaf_nodes或min_implitity_reduce无需要在叶为级或终端级应用于约束。超强匹配min_samples_leaf是叶为子无需要有最少样品数,否则将不能侦查进一步的重组。这些超强匹配可以作为max_depth超强匹配的多余建议书。

from sklearn.tree import DecisionTreeClassifierfrom sklearn.model_selection import GridSearchCVparams = { "criterion":("gini", "entropy"), "splitter":("best", "random"), "max_depth":(list(range(1, 20))), "min_samples_split":[2, 3, 4], "min_samples_leaf":list(range(1, 20)), }tree_clf = DecisionTreeClassifier(random_state=42)tree_cv = GridSearchCV(tree_clf, params, scoring="accuracy", n_jobs=-1, verbose=1, cv=3)tree_cv.fit(X_train, y_train)best_params = tree_cv.best_params_print(f"Best paramters: {best_params})")tree_clf = DecisionTreeClassifier(**best_params)tree_clf.fit(X_train, y_train)print_score(tree_clf, X_train, y_train, X_test, y_test, train=True)print_score(tree_clf, X_train, y_train, X_test, y_test, train=False)

5.3大树的仿真

from IPython.display import Imagefrom six import StringIOfrom sklearn.tree import export_graphvizimport pydotfeatures = list(df.columns)features.remove("Attrition")dot_data = StringIO()export_graphviz(tree_clf, out_file=dot_data, feature_names=features, filled=True)graph = pydot.graph_from_dot_data(dot_data.getvalue())Image(graph[0].create_png())

5.4随机热带雨林

随机热带雨林是一种元推估器,它将多个确定性确定性对统计数据集的完全相同子样品顺利完成近似,并用到均绝对值来大幅提高得出精度和遏制过近似。

随机热带雨林启发式匹配:

n_estimators: 大树的为数。criterion: 量度重组准确性的参数。支持的标准是gini和电子邮件增益的“不确定性”。max_depth:大树的最大浅层。如果为None,则揭开终端,直到所有叶为子都是纯的,或者直到所有叶为子相关联的样品少于min_samples_split。min_samples_split:重组内部终端所需要的最大者样品数。min_samples_leaf:叶为终端所需要的最大者样品数。min_samples_leaf只有在左右共同点当中的每个共同点当中大概留下锻炼样品时,才会回避任何浅层的分割点。这不太可能带有圆滑模同型的效果,尤其是在回归当中。min_weight_fraction_leaf:无需要在叶为终端处的总也就是说(所有读写样品的)的最大者量化高分。当未透过 sample_weight 时,样品带有大致相同的也就是说。max_features:寻找最佳分割时要回避的基本特征为数。max_leaf_nodesmax_leaf_nodes:以最佳应将方式将种植一棵大树。最佳终端定义为溶解的相对减缓。如果 None 则无限为数的叶为终端。min_impurity_decrease:如果该内部矛盾导致溶解减缓若有该绝对值,则该终端将被内部矛盾。min_impurity_split: 大树原定暂缓的阈绝对值。如果一个终端的溶解略高于阈绝对值,则该终端将内部矛盾,否则,它是一个叶为子。bootstrap:构筑大树时究竟用到bootstrap样品。如果为 False,则用到整个统计数据集来构筑每棵大树。oob_score:究竟用到out-of-bag样品来推估一般化精度。from sklearn.ensemble import RandomForestClassifierrf_clf = RandomForestClassifier(n_estimators=100)rf_clf.fit(X_train, y_train)print_score(rf_clf, X_train, y_train, X_test, y_test, train=True)print_score(rf_clf, X_train, y_train, X_test, y_test, train=False)

5.5随机热带雨林超强匹配调优

调优随机热带雨林的主要匹配是n_estimators匹配。;也,热带雨林当中的大树越多,一般化性能越好,但它会减缓近似和得出的时间。

我们还可以调优遏制热带雨林当中每棵大树浅层的匹配。有两个匹配非常关键性:max_depth和max_leaf_nodes。实际上,max_depth将强制带有更菱形的大树,而max_leaf_nodes会约束最大叶为终端为数。

n_estimators = [100, 500, 1000, 1500]max_features = ['auto', 'sqrt']max_depth = [2, 3, 5]max_depth.append(None)min_samples_split = [2, 5, 10]min_samples_leaf = [1, 2, 4, 10]bootstrap = [True, False]params_grid = {'n_estimators': n_estimators, 'max_features': max_features, 'max_depth': max_depth, 'min_samples_split': min_samples_split, 'min_samples_leaf': min_samples_leaf, 'bootstrap': bootstrap}rf_clf = RandomForestClassifier(random_state=42)rf_cv = GridSearchCV(rf_clf, params_grid, scoring="f1", cv=3, verbose=2, n_jobs=-1)rf_cv.fit(X_train, y_train)best_params = rf_cv.best_params_print(f"Best parameters: {best_params}")rf_clf = RandomForestClassifier(**best_params)rf_clf.fit(X_train, y_train)print_score(rf_clf, X_train, y_train, X_test, y_test, train=True)print_score(rf_clf, X_train, y_train, X_test, y_test, train=False)最后

本文主要解说了以下内容:

确定性和随机热带雨林启发式以及每种启发式的匹配。如何相应确定性和随机热带雨林的超强匹配。在锻炼在此之后无需要最大限度你的统计数据集。从每个类当中抽取大致相同为数的样品。通过将每个类的样品也就是说(sample_weight)的和二阶为大致相同的绝对值。。

婴儿长期拉肚子怎么办
补充维生素哪个牌子好
成都妇科
沈阳妇科医院预约挂号
贵阳妇科专科医院哪好
标签:
友情链接: