鱼C论坛

 找回密码
 立即注册
查看: 2065|回复: 3

[已解决]本人零基础,想问下这个什么意思

[复制链接]
发表于 2020-4-7 20:43:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码如下
import pandas as pd #导入数据集
url=r"C:\Users\zmj佳佳佳\Desktop\第六步离散化测试.csv"
df = pd.read_csv(url, header = None,low_memory=False)#将数据集分为训练集和测试集
df.columns=["sub_grade","dti","delinq_2yrs","earliest_cr_line","fico_range_low","inq_last_6mths",
            "mths_since_last_delinq","pub_rec","revol_bal","revol_util","mths_since_last_major_derog",
            "tot_cur_bal","open_acc_6m","open_il_12m","open_il_24m","mths_since_rcnt_il","open_rv_12m",
            "open_rv_24m","max_bal_bc","all_util","inq_last_12m","acc_open_past_24mths","avg_cur_bal",
            "bc_open_to_buy","mo_sin_old_il_acct","mo_sin_old_rev_tl_op","mo_sin_rcnt_rev_tl_op","mo_sin_rcnt_tl",
           "mort_acc","mths_since_recent_bc_dlq","mths_since_recent_inq","mths_since_recent_revol_delinq",
            "num_accts_ever_120_pd","num_actv_bc_tl","num_actv_rev_tl","num_bc_sats","num_bc_tl",
            "num_rev_accts","num_rev_tl_bal_gt_0","num_tl_90g_dpd_24m","num_tl_op_past_12m","pct_tl_nvr_dlq",
            "pub_rec_bankruptcies"]
#将数据集分为训练集和测试集
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
x, y = df.iloc[:, 1:].values, df.iloc[:, 0].values
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 0)
feat_labels = df.columns[1:]
forest = RandomForestClassifier()
param={"n_estimators":[10,20,30,50,80,],"max_depth":[5,8,10,15,18]}
#网格搜索与交叉验证
gc=GridSearchCV(forest,param_grid=param,cv=2)
gc.fit(x_train,y_train)
print("准确率:",gc.score(x_test,y_test))
print("查看选择的参数模型:",gc.best_params_)
#特征重要性评估
import numpy as np
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
for f in range(x_train.shape[1]):
    print("%2d) %-*s %f" % (f + 1, 30, feat_labels[indices[f]], importances[indices[f]]))

报错什么意思要怎么修改
Warning (from warnings module):
  File "C:\Users\zmj佳佳佳\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\model_selection\_split.py", line 665
    warnings.warn(("The least populated class in y has only %d"
UserWarning: The least populated class in y has only 1 members, which is less than n_splits=2.
准确率: 0.12197540043994175
查看选择的参数模型: {'max_depth': 8, 'n_estimators': 50}
Traceback (most recent call last):
  File "C:\Users\zmj佳佳佳\Desktop\特征选择.py", line 31, in <module>
    importances = forest.feature_importances_
  File "C:\Users\zmj佳佳佳\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\ensemble\_forest.py", line 427, in feature_importances_
    check_is_fitted(self)
  File "C:\Users\zmj佳佳佳\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\utils\validation.py", line 967, in check_is_fitted
    raise NotFittedError(msg % {'name': type(estimator).__name__})
sklearn.exceptions.NotFittedError: This RandomForestClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
最佳答案
2020-4-7 20:55:44
上面不是写的很清楚了吗?
在使用模型推断前要进行fit,不拟合模型怎么学习?没学习过的模型是不能进行推断的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-7 20:55:44 | 显示全部楼层    本楼为最佳答案   
上面不是写的很清楚了吗?
在使用模型推断前要进行fit,不拟合模型怎么学习?没学习过的模型是不能进行推断的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-7 21:02:26 | 显示全部楼层
BngThea 发表于 2020-4-7 20:55
上面不是写的很清楚了吗?
在使用模型推断前要进行fit,不拟合模型怎么学习?没学习过的模型是不能进行推 ...

嗯嗯,可以了,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-7 21:59:47 | 显示全部楼层
嘴角向上 发表于 2020-4-7 21:02
嗯嗯,可以了,谢谢

想问下那个userwarning什么意思,怎么修改
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-26 04:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表