title: How to fix this NameError: name 'clf' is not defined?
body: <pre><code>#Feature Selection --&gt; 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)
  </code></pre>
  
  <p>I believe that the 'clf' is declared in the above code. But I don't understand why I'm still getting this error: </p>
  
  <pre><code>NameError                                 Traceback (most recent call last)
  &lt;ipython-input-30-bcd9b039b6a5&gt; in &lt;module&gt;
       26     predict_labels(model, X_test, y_test)
       27 
  ---&gt; 28     if clf == clf_RF:
       29         print("\n")
       30         feature_importance(clf_RF)
  
  NameError: name 'clf' is not defined
  </code></pre>