"""
# How to convert Categorical variables into Numerical Variables using ColumnTransformer?

When we talk about categorical variables, we are talking about non numerical values like Strings or Text. For example, a city name, a store name - for that matter anything that is not a number is a categorical variable. For us humans, it is very easy to interpret and understand text - but for machines it is not the case.

In Machine Learning, all the algorithms in the sklearn library cannot handle categorical variables so before we give the data to the algorithm for training and predicting - we have to convert it to numbers.

So this recipe is a short example on how to convert categorical variables into numerical variables. Let's get started.
"""
"""
Step 1 - Import the library - ColumnTransformer
"""
import numpy as np
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
"""
Step 2 - Setup the Data
"""
dataset = {'research': [15000, 90000, 250000, 175000, 88000, 210000],
           'marketing': [5000, 25000, 31000, 44000, 19700, 21111],
           'city': ['Texas', 'Delaware', 'Florida', 'Texas', 'Delaware','Florida'],
           'profit': [9999, 5555, 3333, 4444, 1111, 2222]}
                
df = pd.DataFrame(dataset)
"""
Step 3 - Create an object of ColumnTransformer class
"""
"""
> **Parameters in ColumnTransformer()**

* 1) transformer
* name: this is just a name that we pass to the transformer
* transformer: here we need to pass an estimator that supports fit and transform. Since we want to encode the data, we will be passing OneHotEncoder here.
* columns: the index of columns that contain the categorical values that we want to convert

* 2) remainder
* This parameter basically tells the transformer what to do with the remaining columns other than the ones we have mentioned above for converting. The values can be "drop" or "passthrough". The default value is drop which means only the transformed columns will be returned by the transformer and the remaining columns will be dropped. But if we want the transformer to pass them - we have to use the value "passthrough"
"""
"""
As you can see, we have named the transformer as "encoder"
For the estimator, we are passing an object of OneHotEncoder
And for columns, we can see from our dataset that the 3rd column is the city column that we want to transform. Since the index start at 0, we are passing value 2 here
Lastly for remainder we are passing "passthrough" as we want the transformer to return the remaining columns
"""
columnTransformer = ColumnTransformer([('encoder', OneHotEncoder(), [2])], remainder='passthrough')
"""
Step 4 - Convert the categorical data with one line of code
"""
df = np.array(columnTransformer.fit_transform(df), dtype = np.str)
"""
Step 5 - Lets look at our dataset now
"""
print(df)