title: How to convert a continuous variable to a categorical variable?
body: <p>Please point me in the right direction with this one.  How can I convert a column that contains a continuous variable into a discrete variable?  I have prices of financial instruments that I'm trying to convert into some kind of categorical value.  I thought I could do the following.</p>
  
  <pre><code>labels = df['PRICE'].astype('category').cat.categories.tolist()
  replace_map_comp = {'PRICE' : {k: v for k,v in zip(labels,list(range(1,len(labels)+1)))}}
  print(replace_map_comp)
  </code></pre>
  
  <p>However, when I try to run a RandomForestClassifier over a subset of data, I'm getting an error.</p>
  
  <pre><code>from sklearn.ensemble import RandomForestClassifier
  features = np.array(['INTEREST',
  'SPREAD',
  'BID',
  'ASK',
  'DAYS'])
  clf = RandomForestClassifier()
  clf.fit(df[features], df1['PRICE'])
  </code></pre>
  
  <p>Error message reads: <code>ValueError: Unknown label type: 'continuous'</code></p>
  
  <p>I'm pretty sure this is close, but something is definitely off here.</p>
  
  <p>CODE UPDATE BELOW:</p>
  
  <pre><code># copy only numerics to new DF
  df1 = df.select_dtypes(include=[np.number])
  
  from sklearn import linear_model
  features = np.array(['INTEREST',
  'SPREAD',
  'BID',
  'ASK',
  'DAYS'])
  reg = linear_model.LinearRegression()
  reg.fit(df1[features], df1['PRICE'])
  
  # problems start here...
  importances = clf.feature_importances_
  sorted_idx = np.argsort(importances)
  
  padding = np.arange(len(features)) + 0.5
  plt.barh(padding, importances[sorted_idx], align='center')
  plt.yticks(padding, features[sorted_idx])
  plt.xlabel("Relative Importance")
  plt.title("Variable Importance")
  plt.show()
  </code></pre>
  
  <p>Error: AttributeError: 'LinearRegression' object has no attribute 'feature_importances_'</p>
  
  <p>Following concept from here:</p>
  
  <p><a href="http://blog.yhat.com/tutorials/5-Feature-Engineering.html" rel="noreferrer">http://blog.yhat.com/tutorials/5-Feature-Engineering.html</a></p>
  
  <p>FYI, I tried the one-hot encoding and the code transformation grew the columns too large and I got an error.  Maybe the way to handle this is to take a small subset of the data.  With 250k rows, I'm guessing maybe 100k rows should be fairly representative of the entire data set.  Maybe that's the way to go.  Just thinking out loud here.</p>