title: Encoding Ordinal Values in Python
body: <p>I'm trying to encode Ordinal Categorical Values in the 3rd column of my dataset where "Tiny Mongra" has the lowest value and "1st Wand" has the highest values. It is synonymous to using small, medium and large sizes where the current dataset denotes the size of  a grain of rice.</p>
  
  <p>I keep getting the following error when I run this snippet:</p>
  
  <pre><code>Traceback (most recent call last):
  
    File "&lt;ipython-input-1-ae4501cc0ac1&gt;", line 19, in &lt;module&gt;
      X[:, 2] = ordinalencoder_X_3.fit_transform(X[:, 2])
  
    File "/Users/anhad/anaconda3/lib/python3.6/site-packages/sklearn/base.py", line 462, in fit_transform
      return self.fit(X, **fit_params).transform(X)
  
    File "/Users/anhad/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py", line 794, in fit
      self._fit(X)
  
    File "/Users/anhad/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py", line 61, in _fit
      X = self._check_X(X)
  
    File "/Users/anhad/anaconda3/lib/python3.6/site-packages/sklearn/preprocessing/_encoders.py", line 47, in _check_X
      X_temp = check_array(X, dtype=None)
  
    File "/Users/anhad/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py", line 552, in check_array
      "if it contains a single sample.".format(array))
  
  ValueError: Expected 2D array, got 1D array instead:
  array=['1st Wand' '1st Wand' '1st Wand' ... '1st Wand' '1st Wand' '1st Wand'].
  </code></pre>
  
  <p>On further inspection, I have found that the error was not warning me about the list of categorical data, but was referring to the column that I wanted encoded. For some reason it considers that column a 1D array of the form:</p>
  
  <pre><code>array=['1st Wand' '1st Wand' '1st Wand' '1st Wand' '1st Wand' 'Dubar' '2nd Wand'
   'Tibar' 'Mongra' '1st Wand' '1st Wand' '1st Wand' '1st Wand' '1st Wand'
   '1st Wand' '2nd Wand' 'Super Dubar' 'Super Tibar' ... '1st Wand' '1st Wand'].
  </code></pre>
  
  <p>This is weird since I am using LabelEncoder to fit_transform other categorical values in my dataset and they work fine.</p>
  
  <p>Here is a link to the Data. See "Data" sheet: </p>
  
  <p><a href="https://docs.google.com/spreadsheets/d/12nAU5QztVnVroRYDsRDsZGUyBpBTwAD5yMmbMaAxnHQ/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/12nAU5QztVnVroRYDsRDsZGUyBpBTwAD5yMmbMaAxnHQ/edit?usp=sharing</a></p>
  
  <p>Here is the full code. Refer to the last part:</p>
  
  <pre><code>import numpy as np
  import pandas as pd
  
  # Importing the dataset
  dataset = pd.read_csv('Ryze Price NN Data.csv')
  X = dataset.iloc[:, 1:7].values
  y = dataset.iloc[:, 7].values
  
  # Encoding categorical data
  from sklearn.preprocessing import LabelEncoder, OneHotEncoder, OrdinalEncoder
  
  labelencoder_X_1 = LabelEncoder()
  X[:, 0] = labelencoder_X_1.fit_transform(X[:, 0])
  
  labelencoder_X_2 = LabelEncoder()
  X[:, 1] = labelencoder_X_2.fit_transform(X[:, 1])
  
  # SEE THIS PART
  category_array = ["Tiny Mongra","Mini Mongra","Mongra","Super Mongra","Mini Dubar","Dubar","Super Dubar","Mini Tibar","Tibar","Super Tibar","2nd Wand","Super 2nd Wand","1st Wand"]
  ordinalencoder_X_3 = OrdinalEncoder(categories=category_array)
  X[:, 2] = ordinalencoder_X_3.fit_transform(np.array(X[:,2])
  </code></pre>
  
  <p>I expect that categorical data is encoded as follows:
  "Tiny Mongra" should be encoded as 0
  .
  .
  "1st Wand" should be encoded as 12</p>