为了账号安全,请及时绑定邮箱和手机立即绑定

如何解决 Python 中的未来警告

如何解决 Python 中的未来警告

ABOUTYOU 2021-06-30 18:58:25
当我在我的程序中运行 mean_acc() 方法时,有 % (min_groups, self.n_splits)), Warning) 错误...def mean_acc():    models = [        RandomForestClassifier(n_estimators=200, max_depth=3, random_state=0),        LinearSVC(),        MultinomialNB(),        LogisticRegression(random_state=0)]    CV = 6    cv_df = pd.DataFrame(index=range(CV * len(models)))    entries = []    for model in models:        model_name = model.__class__.__name__        accuracies = cross_val_score(model, features, labels, scoring='accuracy', cv=CV)        for fold_idx, accuracy in enumerate(accuracies):            entries.append((model_name, fold_idx, accuracy))    cv_df = pd.DataFrame(entries, columns=['model_name', 'fold_idx', 'accuracy'])    print(cv_df.groupby('model_name').accuracy.mean())这些是我使用 mean_acc() 方法运行程序时显示的错误。我可以知道如何解决下面的这些错误吗?请帮我看看上面导致这些错误的代码,谢谢!!! % (min_groups, self.n_splits)), Warning)C:\Users\L31307\PycharmProjects\FYP\venv\lib\site-packages\sklearn\model_selection\_split.py:626: Warning: The least populated class in y has only 1 members, which is too few. The minimum number of members in any class cannot be less than n_splits=5.  % (min_groups, self.n_splits)), Warning)
查看完整描述

2 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

如果您想忽略它,请将以下内容添加到顶部的代码中:


import warnings

warnings.filterwarnings("ignore", category=FutureWarning)

否则指定求解器如下:


LogisticRegression(solver='lbfgs')


solver : str, {‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’, ‘saga’}, default: ‘liblinear’.

Algorithm to use in the optimization problem.


For small datasets, ‘liblinear’ is a good choice, whereas ‘sag’ and ‘saga’ are faster for large ones.

For multiclass problems, only ‘newton-cg’, ‘sag’, ‘saga’ and ‘lbfgs’ handle multinomial loss; ‘liblinear’ is limited to one-versus-rest schemes.

‘newton-cg’, ‘lbfgs’ and ‘sag’ only handle L2 penalty, whereas ‘liblinear’ and ‘saga’ handle L1 penalty.



查看完整回答
反对 回复 2021-07-06
?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

如果您使用的逻辑回归模型将惩罚='l1' 作为超参数,您可以使用 solver='liblinear'

我的代码示例::

logistic_regression_model=LogisticRegression(penalty='l1',dual=False,max_iter=110, solver='liblinear')


查看完整回答
反对 回复 2021-07-06
  • 2 回答
  • 0 关注
  • 269 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信