# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/AstitvaSharma/ML_Algorithms/blob/main/data_preprocessing_tools.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="37puETfgRzzg"
# # Data Preprocessing Tools

# + [markdown] id="EoRP98MpR-qj"
# ## Importing the libraries

# + id="nhLNqFwu8gGa"
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# + [markdown] id="RopL7tUZSQkT"
# ## Importing the dataset

# + id="iINyGQMA8rs2"
dataset = pd.read_csv("Data.csv")
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

# + colab={"base_uri": "https://localhost:8080/"} id="8wk1Cxkx_iQ5" outputId="37fac68b-412a-41fd-e08f-f9546f5eef2f"
print(x)

# + colab={"base_uri": "https://localhost:8080/"} id="GjTkcdA9_ycR" outputId="7cb1820b-0abb-4f64-f864-2b9ed0f18edf"
print(y)

# + [markdown] id="nhfKXNxlSabC"
# ## Taking care of missing data

# + id="YaYfCV98Dqx5"
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])

# + colab={"base_uri": "https://localhost:8080/"} id="TuJvhWr-Eg19" outputId="595454b2-f88c-4d8d-9785-604ecae8650f"
print(x)

# + [markdown] id="CriG6VzVSjcK"
# ## Encoding categorical data

# + [markdown] id="AhSpdQWeSsFh"
# ### Encoding the Independent Variable

# + id="GwnincOVUvIn"
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers =[('encoders', OneHotEncoder(),[0])] , remainder='passthrough')
x = np.array(ct.fit_transform(x))

# + colab={"base_uri": "https://localhost:8080/"} id="9AtCHQcmB-bp" outputId="bf4c41e5-d5ed-4904-ae38-c0bd412118d3"
print(x)

# + [markdown] id="DXh8oVSITIc6"
# ### Encoding the Dependent Variable

# + id="843yhw27DuWX"
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = le.fit_transform(y)

# + id="DW7dbY_LECYT" outputId="1d860f38-8b7e-42ff-dc45-36dc288bc52d" colab={"base_uri": "https://localhost:8080/"}
print(y)

# + [markdown] id="qb_vcgm3qZKW"
# ## Splitting the dataset into the Training set and Test set

# + id="L_ek1vBvTfc7"
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)


# + colab={"base_uri": "https://localhost:8080/"} id="gXHoa4LDUCCX" outputId="ce57e6f2-54a4-4f98-a0f5-87b3bf5621a9"
print(x_train)

# + colab={"base_uri": "https://localhost:8080/"} id="MNSPrZKTUHUG" outputId="9348ecfc-2ba6-4a8a-bf77-2b3a49368133"
print(x_test)

# + colab={"base_uri": "https://localhost:8080/"} id="gFNGxz8WUJ5y" outputId="4a088b26-2d93-4160-a3c9-b7e9348896a8"
print(y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="VYurkWsUUMlc" outputId="acdffaba-530f-4a0a-8e66-751d6c2be8e9"
print(y_test)

# + [markdown] id="TpGqbS4TqkIR"
# ## Feature Scaling

# + id="RJCfj_XmaJOm"
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train[:, 3:] = sc.fit_transform(x_train[:, 3:] )
x_test[:, 3:] = sc.transform(x_test[:, 3:])

# + colab={"base_uri": "https://localhost:8080/"} id="RW6ffqe_a4Da" outputId="8a1764d5-1a7f-41bc-a212-5da39380d12b"
print(x_train)

# + colab={"base_uri": "https://localhost:8080/"} id="Aq-SyxMba6mu" outputId="ee342c9b-a5a0-4be6-bf25-f2ecbd9c0829"
print(x_test)

StarcoderdataJupyterScriptsDedupFiltered