title: Preparing Ordinal and Nominal Features for classification using OneHotEncoder in scikit-learn
body: <p>I want to prepare a dataset that contains continuous, nominal and ordinal features for classification. I have some workaround below, but I am wondering if there is a better way using <code>scikit-learn</code>'s encoders?</p>
  
  <p>Let's consider the following example dataset:</p>
  
  <pre><code>import pandas as pd
  df = pd.DataFrame([['green', 'M', 10.1, 'class1'], ['red', 'L', 13.5, 'class2'], ['blue', 'XL', 15.3, 'class1']])
  df.columns = ['color', 'size', 'prize', 'class label']
  df
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Z7ZpP.png" alt="enter image description here"></p>
  
  <p>Now, the class labels can be simply converted by a label encoder (the classifier ignores order in the class labels).</p>
  
  <pre><code>from sklearn.preprocessing import LabelEncoder
  class_le = LabelEncoder()
  df['class label'] = class_le.fit_transform(df['class label'].values)
  </code></pre>
  
  <p>And I would convert the ordinal feature column <code>size</code> like so:</p>
  
  <pre><code>size_mapping = {
             'XL': 3,
             'L': 2,
             'M': 1}
  
  df['size'] = df['size'].apply(lambda x: size_mapping[x])
  df
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/lMmKJ.png" alt="enter image description here"></p>
  
  <p>And finally the ordinal <code>color</code> feature:</p>
  
  <pre><code>color_mapping = {
             'green': [0,0,1],
             'red': [0,1,0],
             'blue': [1,0,0]}
  
  df['color'] = df['color'].apply(lambda x: color_mapping[x])
  df
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/rWm57.png" alt="enter image description here"></p>
  
  <pre><code>y = df['class label'].values
  X = df.iloc[:, :-1].values
  X = np.apply_along_axis(func1d= lambda x: np.array(x[0] + list(x[1:])), axis=1, arr=X)
  X
  
  array([[  0. ,   0. ,   1. ,   1. ,  10.1],
         [  0. ,   1. ,   0. ,   2. ,  13.5],
         [  1. ,   0. ,   0. ,   3. ,  15.3]])
  </code></pre>