# Data Preprocessing Tools

# Importing the libraries
import numpy as np # Needed for arrays
import matplotlib.pyplot as plt # Needed for plots
import pandas as pd # Needed for matrix and dependence variables

# Importing the dataset (iloc stands for locate indexes. 
# : means all)
dataset = pd.read_csv('Data.csv')
X = dataset.iloc[:, :-1].values # Selects all (:) columns except the last
y = dataset.iloc[:, -1].values # Selects only last column
print(X)
print(y)

# Taking care of missing data
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
imputer.fit(X[:, 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])
print(X)

# Encoding categorical data
# Encoding the Independent Variable
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder # Creates dummies
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [0])], 
                             remainder='passthrough')
X = np.array(ct.fit_transform(X))
print(X)

# Encoding the Dependent Variable
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = le.fit_transform(y)
print(y)

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
                                                    random_state = 1)
print(X_train)
print(X_test)
print(y_train)
print(y_test)

# Feature Scaling or variable standardization
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train[:, 3:] = sc.fit_transform(X_train[:, 3:])
X_test[:, 3:] = sc.transform(X_test[:, 3:])
print(X_train)
print(X_test)