使用Python的随机森林特征重要性

时间:2019-12-10 13:57:13

标签: python random-forest nameerror

我正在尝试以下代码进行随机森林分类。即使我已定义但得到NameError。 请帮助

def RFC_model(randomState, X_train, X_test, y_train, y_test):


   rand_forest = RandomForestClassifier()
   rand_forest.fit(X_train, y_train)
   forest_test_predictions = rand_forest.predict(X_test)
   print(accuracy_score(y_test, forest_test_predictions))

X_train, X_test, y_train, y_test = train_test_split(df_encoded.drop(['success'],axis='columns').values,      
                                                df_encoded.success, 
                                                test_size=0.2)

RFC_model(42, X_train, X_test, y_train, y_test)

0.994045375744328

rand_forest.feature_importances_.round(3)

NameError                                 Traceback (most recent call last)
<ipython-input-40-974786899b7f> in <module>
  1 #importance of features rounded to nearest 3 decimals
----> 2 rand_forest.feature_importances_.round(3)

NameError: name 'rand_forest' is not defined

1 个答案:

答案 0 :(得分:1)

您正在rand_forest函数范围内在本地定义变量RFC_model。函数完成执行后,对象将被销毁,因此您无法访问它。您可以通过返回rand_forest对象来解决此问题:

def RFC_model(randomState, X_train, X_test, y_train, y_test):
    rand_forest = RandomForestClassifier()
    rand_forest.fit(X_train, y_train)
    forest_test_predictions = rand_forest.predict(X_test)
    print(accuracy_score(y_test, forest_test_predictions))
    return rand_forest

X_train, X_test, y_train, y_test = train_test_split(df_encoded.drop(['success'],axis='columns').values,      
                                            df_encoded.success, 
                                            test_size=0.2)

rand_forest = RFC_model(42, X_train, X_test, y_train, y_test)
rand_forest.feature_importances_.round(3)