← 返回题库
中级

提取随机森林特征重要性前20名

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    from pyodide.http import open_url
    from io import StringIO
    loans_featured_csv = open_url(____).read()
    import pandas as pd
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.model_selection import train_test_split
    from io import StringIO
    df = pd.read_csv(StringIO(____))
    num_cols=[c for c in df.select_dtypes(____).columns if c not in ['____','____'] and not c.endswith(____)]
    X=df[num_cols].fillna(____); y=df['____']
    X_tr,X_te,y_tr,y_te=train_test_split(____)
    rf=RandomForestClassifier(____)
    rf.fit(____)
    imp=pd.DataFrame({'____':num_cols,'____':rf.feature_importances_.round(____)})
    print(imp.nlargest(____,'____').to_string(____))

示例

输入
solve()
期望输出
feature  importance
         interestRate      0.0491
       income_to_loan      0.0455
     payment_pressure      0.0435
             acc_util      0.0430
           risk_index      0.0425
            revolUtil      0.0424
credit_history_months      0.0424
                  dti      0.0397
         log_revolBal      0.0392
          installment      0.0387
             revolBal      0.0383
           log_income      0.0343
         annualIncome      0.0341
             totalAcc      0.0341
             subGrade      0.0340
           regionCode      0.0333
             loanAmnt      0.0305
              openAcc      0.0283
          issue_month      0.0275
           issue_year      0.0219
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码