首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >机器学习随机林

机器学习随机林
EN

Stack Overflow用户
提问于 2018-10-31 20:56:10
回答 2查看 385关注 0票数 1

我试图在一个不平衡的数据集中安装一个随机森林分类器,使用的是scikit-learn库。

我的目标是获得多少相同的值,以保证召回和精度,为此,我使用了class_weight函数的RandomForestClassifier参数。

当用class_weight = {0:1,1:1}来拟合随机森林时(换句话说,假设数据集不平衡),我获得:

准确性: 0.79精度: 0.63召回: 0.32 AUC: 0.74

当我将class_weight更改为{0:1,1:10}时,我获得:

准确性: 0.79精度: 0.65召回: 0.29 AUC: 0.74

因此,召回和精确值几乎没有变化(即使我从10增加到100,变化也是最小的)。

由于X_train和X_test都不平衡在相同的比例(数据集有超过100万行),当使用class_weight = {0:1,1:10}时,难道不应该获得非常不同的召回和精确值吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-31 21:03:12

如果您想要提高您的模型的召回,有一个更快的方法这样做。

您可以使用sklearn计算精确召回曲线

这条曲线将为你的模型提供精确性和召回性之间的权衡。

这意味着,如果您想要增加对模型的回忆,您可以要求随机林检索每个类的概率,将0.1添加到第1类,再减去0级的概率。这将有效地提高你的召回率。

如果绘制精确召回曲线,您将能够找到相同精度和召回的最佳阈值。

在这里,您有来自sklearn的例子。

代码语言:javascript
复制
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
import numpy as np

iris = datasets.load_iris()
X = iris.data
y = iris.target

# Add noisy features
random_state = np.random.RandomState(0)
n_samples, n_features = X.shape
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]

# Limit to the two first classes, and split into training and test
X_train, X_test, y_train, y_test = train_test_split(X[y < 2], y[y < 2],
                                                    test_size=.5,
                                                    random_state=random_state)

# Create a simple classifier
classifier = svm.LinearSVC(random_state=random_state)
classifier.fit(X_train, y_train)
y_score = classifier.decision_function(X_test)

from sklearn.metrics import precision_recall_curve
import matplotlib.pyplot as plt
from sklearn.utils.fixes import signature

precision, recall, _ = precision_recall_curve(y_test, y_score)

# In matplotlib < 1.5, plt.fill_between does not have a 'step' argument
step_kwargs = ({'step': 'post'}
               if 'step' in signature(plt.fill_between).parameters
               else {})
plt.step(recall, precision, color='b', alpha=0.2,
         where='post')
plt.fill_between(recall, precision, alpha=0.2, color='b', **step_kwargs)

plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])

它应该会给你一些类似的东西

票数 2
EN

Stack Overflow用户

发布于 2019-03-13 10:50:02

作为补充,您还可以尝试向一个或多个度量标准优化您的模型。您可以使用RandomizedSearchCV为您寻找超参数的良好组合。例如,如果您训练一个随机森林分类器“:

代码语言:javascript
复制
#model
MOD = RandomForestClassifier() 
#Implemente RandomSearchCV
m_params = { 
            "RF": {
                    "n_estimators" : np.linspace(2, 500, 500, dtype = "int"),  
                    "max_depth": [5, 20, 30, None], 
                    "min_samples_split": np.linspace(2, 50, 50, dtype = "int"),  
                    "max_features": ["sqrt", "log2",10, 20, None],
                    "oob_score": [True],
                    "bootstrap": [True]
                    },
            }
    scoreFunction = {"recall": "recall", "precision": "precision"}
    random_search = RandomizedSearchCV(MOD,
                                       param_distributions = m_params[model], 
                                       n_iter = 20,
                                       scoring = scoreFunction,               
                                       refit = "recall",
                                       return_train_score = True,
                                       random_state = 42,
                                       cv = 5,
                                        verbose = 1 + int(log)) 

    #trains and optimizes the model
    random_search.fit(x_train, y_train)

    #recover the best model
    MOD = random_search.best_estimator_

请注意,参数评分和修改将告诉RandomizedSerachCV您最感兴趣的指标最大化。此方法还将节省手动调优时间(并且可能会使您的模型在测试数据上过度匹配)。

祝好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53091838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档