# Data Preprocessing

# Import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Taking care of missing data
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.model_selection import train_test_split


# Import Dataset
dataset = pd.read_csv('Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 3].values

# Create a new Imputer object
imputer = SimpleImputer(missing_values=np.nan, strategy='mean')

# Fit second and third columns with Imputer
imputer = imputer.fit(X[:, 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])

# Dealing with categorical variables
labelencoder_x = LabelEncoder()
X[:, 0] = labelencoder_x.fit_transform(X[:, 0])
print(X)
# View encoded classes
print((labelencoder_x.classes_))
# Get the encoded values for a given set pf labels
print(labelencoder_x.transform(['France', 'Germany', 'France', 'Germany', 'Spain']))

# Convert the labels encoded to dummy variables to train ML models
onehotencoder = OneHotEncoder(handle_unknown='ignore')
enc = ColumnTransformer([('encoder', OneHotEncoder(), [0])], remainder='passthrough')

X = np.array(enc.fit_transform(X), dtype=np.int)
print(X)

# Encoding the y values -> these are categories, so no need to do OneHotEncoding
labelencoder_y = LabelEncoder()
y = labelencoder_y.fit_transform(y)
print(y)

# Split the dataset to train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
print(X_train)
print(X_test)
print(y_train)
print(y_test)

# Feature scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)
print(X_train)
print(X_test)
