python - 我如何解决 future 的警告 -> % (min_groups, self.n_splits)), Warning) in python?

标签 python scikit-learn linearmodels future-warning

当我在程序中运行 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)
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)
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)
C:\Users\L31307\PycharmProjects\FYP\venv\lib\site-packages\sklearn\linear_model\logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
C:\Users\L31307\PycharmProjects\FYP\venv\lib\site-packages\sklearn\linear_model\logistic.py:459: FutureWarning: Default multi_class will be changed to 'auto' in 0.22. Specify the multi_class option to silence this warning.
  "this warning.", FutureWarning)
C:\Users\L31307\PycharmProjects\FYP\venv\lib\site-packages\sklearn\linear_model\logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
C:\Users\L31307\PycharmProjects\FYP\venv\lib\site-packages\sklearn\linear_model\logistic.py:459: FutureWarning: Default multi_class will be changed to 'auto' in 0.22. Specify the multi_class option to silence this warning.
  "this warning.", FutureWarning)
C:\Users\L31307\PycharmProjects\FYP\venv\lib\site-packages\sklearn\linear_model\logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
C:\Users\L31307\PycharmProjects\FYP\venv\lib\site-packages\sklearn\linear_model\logistic.py:459: FutureWarning: Default multi_class will be changed to 'auto' in 0.22. Specify the multi_class option to silence this warning.
  "this warning.", FutureWarning)
C:\Users\L31307\PycharmProjects\FYP\venv\lib\site-packages\sklearn\linear_model\logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
C:\Users\L31307\PycharmProjects\FYP\venv\lib\site-packages\sklearn\linear_model\logistic.py:459: FutureWarning: Default multi_class will be changed to 'auto' in 0.22. Specify the multi_class option to silence this warning.
  "this warning.", FutureWarning)

最佳答案

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

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

否则指定求解器:

LogisticRegression(solver='lbfgs')

Source :

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.

关于python - 我如何解决 future 的警告 -> % (min_groups, self.n_splits)), Warning) in python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52640386/

相关文章:

python - 使用 Python 中的 matplotlib 从线性模型绘制 3d 曲面图

python - from_formula() 缺少 2 个必需的位置参数 : 'formula' and 'data'

python - Python 面板数据中的多重共线性

python - 2 个 Bitnami Django 项目同时运行 Apache WSGI

python - 将自身对象传递给需要此类型的函数

python - sklearn.svm.svc 的函数 predict_proba() 如何在内部工作?

python - SciKit-Learn GradientBoostingClassifier 中的零重要性特征删除无需重新调整

python - 使用自定义编码将 Pandas Dataframe 转换为 JSON

python - 如何不返回调用函数?

numpy - 从NumPy数组中提取 block 或补丁