如何修复此NameError:未定义名称'clf'?

时间:2019-03-25 22:15:09

标签: python python-3.x

#Feature Selection --> Random Forest
def feature_importance(clf):
    # Relative Importance (Features)
    clf.fit(X_train,y_train)
    # Get Feature Importance from the classifier
    feature_importance = clf.feature_importances_
    # Normalize The Features
    feature_importance = 100.0 * (feature_importance / feature_importance.max())
    # Sort Features and Creat Horizontal Bar Plot
    sorted_idx = np.argsort(feature_importance)
    pos = np.arange(sorted_idx.shape[0]) + .5
    pl.figure(figsize=(16, 12))
    pl.barh(pos, feature_importance[sorted_idx], align='center', color='#0033CC')
    pl.yticks(pos, np.asanyarray(df.columns.tolist())[sorted_idx])
    pl.xlabel("Relative Importance")
    pl.title("Variable Importance - Random Forest")
    pl.show()


clf_NB = GaussianNB()
clf_SVC = SVC()
clf_RF = RandomForestClassifier(n_estimators = 100)

algorithms = [clf_NB,clf_SVC,clf_RF]

for model in algorithms:
    print("\n")
    print("==============================")
    print("Model: {}".format(model.__class__.__name__))
    print("==============================")
    print("\n")
    print("**********************************************************")
    print("**Training**")
    print("Data Size:",len(X_train))
    # Fit model to training data
    train_classifier(model, X_train, y_train)

    # Predict on training set and compute F1 score
    predict_labels(model, X_train, y_train)

    #Predict on Testing Data
    print("**********************************************************")
    print("**Testing**")
    print("Data Size:",len(X_test))
    predict_labels(model, X_test, y_test)

    if clf == clf_RF:
        print("\n")
        feature_importance(clf_RF)

我相信上面的代码中声明了'clf'。但我不明白为什么我仍然会收到此错误:

NameError                                 Traceback (most recent call last)
<ipython-input-30-bcd9b039b6a5> in <module>
     26     predict_labels(model, X_test, y_test)
     27 
---> 28     if clf == clf_RF:
     29         print("\n")
     30         feature_importance(clf_RF)

NameError: name 'clf' is not defined

2 个答案:

答案 0 :(得分:1)

clf仅在feature_importance方法的范围内定义。 clf的值不会存储在此方法之外的任何位置,因此一旦离开该方法,就好像clf永远不存在。

似乎好像要检查您当前正在迭代的model的值是否为clf_RF,这要遍历循环。如果您更改if语句以检查model == clf_RF,则代码应按预期工作。

答案 1 :(得分:1)

好像clf在功能feature_importance的参数中声明,但使用它的地方不在范围内。