# ---
# 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="YLChqRF_EwLH"
# # **IN Vehickle Coupon Recommendation**

# + [markdown] id="u1OI4JA0x_hm"
# # **Importing the Libraries**
# This data was collected via a survey on Amazon Mechanical Turk. The survey describes different driving scenarios including the destination, current time, weather, passenger, etc., and then ask the person whether he will accept the coupon if he is the driver.

# + id="qBKo-D32x4vP"
# Importing pandas as pd
import pandas as pd
# Importing matplotlib as mt
import matplotlib as mt
# Importing LabelEncoder from sklearn
from sklearn.preprocessing import LabelEncoder
# Importing StandardScaler from sklearn
from sklearn.preprocessing import StandardScaler
# Importing OneHotEncoder from sklearn
from sklearn.preprocessing import OneHotEncoder
# Importing make_imbalance from imblearn
from imblearn.datasets import make_imbalance
# Importing matplotlib.pyplot as plt
import matplotlib.pyplot as plt
# Importing seaborn as sns
import seaborn as sns
# Importing counter from collection
from collections import Counter
# Importing matplotlib
import matplotlib

# + [markdown] id="sCKS64loyE9-"
# # **Loading the dataset**

# + colab={"base_uri": "https://localhost:8080/", "height": 835} id="s8sv3kJlyHQM" outputId="aef0fd99-b5ad-4b87-dbc5-64c66f69de20"
# loading the dataset in csv from to data variable
data = pd.read_csv('vehicle-coupon.csv')
# to print the loaded dataset
data

# + [markdown] id="uJ_BcJpuyUVt"
# # **Pre-Processing and Cleaning the data**

# + id="TdESDNqpB38m"
# Here we are dropping the two column by using drop function
data.drop(['car','toCoupon_GEQ5min'], axis=1, inplace=True)

# + colab={"base_uri": "https://localhost:8080/", "height": 443} id="alcKFoH-B7SY" outputId="263bf90f-ff46-4c31-a52c-665cc7d982a6"
# to print first 5 rows of dataset we use head command
data.head()

# + colab={"base_uri": "https://localhost:8080/"} id="d2Ozvnz6FYyj" outputId="5dd3e90d-8e33-4f7d-cf30-245a28a8a245"
# the command to check the types of data is
data.dtypes

# + colab={"base_uri": "https://localhost:8080/", "height": 443} id="FtMItrJuyHZt" outputId="a0b05e70-2540-4877-ab15-99183f73e4ee"
# cleanup is used here to convert the datapytes
cleanup = {"gender": {"Female":0, "Male":1},
           "destination": {"No Urgent Place":0, "Home":1, "Work":2},
           "passanger": {"Alone":0, "Friend(s)":1, "Kid(s)":2, "Partner":3},
           "weather": {"Sunny":0, "Rainy":1, "Snowy":2},
           "time": {"2PM":0, "10AM":1, "10PM":2, "6PM":3, "7AM":4},
           "expiration": {"1d":0, "2h":1},
           "maritalStatus": {"Unmarried partner":0, "Single":1, "Divorced":2, "Widowed":3, "Married partner":4},
           "coupon": {"Restaurant(<20)":0, "Coffee House":1, "Carry out & Take away":2, "Restaurant(20-50)":3, "Bar":4},
           #"education": {"Some college - no degree":0, "Bachelors degree":1, "Associates degree":2, "High School Graduate":3, "Graduate degree (Masters or Doctorate)":4},
           "income": {"$37500 - $49999":0, "$62500 - $74999":1, "$12500 - $24999":2, "$75000 - $87499":3, "$50000 - $62499":4, "$25000 - $37499":5, "$100000 or More":6, "$87500 - $99999":7, "Less than $12500":8},
            "Bar": {"never": 0, "less1":1, "1~3":2, "gt8":3, "4~8":4},
           "CoffeeHouse": {"never":0, "less1":1, "4~8":2, "1~3":3, "gt8":4, "nan":5},
           "age": {"21":0, "46":1, "26":2, "31":3, "41":4, "50plus":5, "36":6, "below21":7},
           "CarryAway": {"4~8":0, "1~3":1, "gt8":2, "less1":3, "never":4},
           "RestaurantLessThan20": {"4~8":0, "1~3":1, "less1":2, "gt8":3, "never":4},
           "Restaurant20To50": {"4~8":0, "1~3":1, "less1":2, "gt8":3, "never":4, "nan":5}

          }
data.replace(cleanup, inplace=True)
# to print first 5 rows of dataset we use head command
data.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 322} id="BjYXZ2PEyHfE" outputId="1a196b66-5b17-437c-a733-ee5aaa955bce"
# get_dummies is used as one-hot encode for categorical data
data=pd.get_dummies(data)
# to print first 5 rows of dataset we use head command
data.head()

# + colab={"base_uri": "https://localhost:8080/"} id="kaOGYZkBTO3C" outputId="13a587c8-9052-4cd8-858d-1b3a695a3747"
#filling the missinf values with mod
mod = data.mode()['CarryAway']
print(mod[0])
data['CarryAway'].fillna(mod[0], inplace=True)

# + colab={"base_uri": "https://localhost:8080/"} id="0FVzwmkzHFkK" outputId="e1837a5d-1748-40af-ebe0-30fd7d7401bb"
#filling the missinf values with mod
mod = data.mode()['Bar']
print(mod[0])
data['Bar'].fillna(mod[0], inplace=True)

# + colab={"base_uri": "https://localhost:8080/"} id="VLmG1sX3HFoS" outputId="de07087b-939a-4c88-f57c-b2d7b354d3e2"
#filling the missinf values with mod
mod = data.mode()['CoffeeHouse']
print(mod[0])
data['CoffeeHouse'].fillna(mod[0], inplace=True)

# + colab={"base_uri": "https://localhost:8080/"} id="V9qFQpmuHFsU" outputId="ebf4d38f-53f7-462b-add7-ca591dae37c9"
#filling the missinf values with mod
mod = data.mode()['Restaurant20To50']
print(mod[0])
data['Restaurant20To50'].fillna(mod[0], inplace=True)

# + colab={"base_uri": "https://localhost:8080/"} id="583Q-VIiHFww" outputId="cc44f267-70e1-4987-a6ad-ff5d4d7c7e2a"
#filling the missinf values with mod
mod = data.mode()['RestaurantLessThan20']
print(mod[0])
data['RestaurantLessThan20'].fillna(mod[0], inplace=True)

# + colab={"base_uri": "https://localhost:8080/"} id="AiBYUMHc7969" outputId="c3d048b7-fe92-4f69-9b19-da1da69d9b1c"
# here we used isnull for checking any null value is present or not
data.isnull().sum(axis = 0)

# + colab={"base_uri": "https://localhost:8080/"} id="UzKLrdFzTQ3p" outputId="6e7ca13f-4e84-464e-9e41-25149f4078f4"
# the command to check the types of data is
data.dtypes

# + colab={"base_uri": "https://localhost:8080/", "height": 322} id="ZiYCzr8wDhq9" outputId="336a6ef1-c59f-4a4a-d1f9-14bad65113a4"
# to print first 5 rows of dataset we use head command
data.head()

# + id="JdBnmZu5yHik"


# + id="dWYzZnqFyHn0"


# + colab={"base_uri": "https://localhost:8080/", "height": 827} id="YwiVCNN279xV" outputId="b80ac6d5-a1aa-403c-db92-1eb2f3b6db2a"
# this is the code for plotting the heat map
color = sns.color_palette()
sns.set_style('darkgrid')
corrmat = data.corr()
plt.subplots(figsize=(12,9))
sns.heatmap(corrmat, vmax=0.9, square=True)

# + colab={"base_uri": "https://localhost:8080/"} id="VtVNYAeB790l" outputId="04c8489c-61d9-4144-ff1d-054ff2b6a9b4"
# to count the number of each class in attribute we use value_counts
data.Y.value_counts()

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="JztlojxuEQMC" outputId="85ec4501-43c5-4ad4-937f-74dc79481a73"
# Plotting histograms for all the attributes of dataset
data.hist(figsize = (20,20))
# To show the plotted histogram
plt.show()

# + colab={"base_uri": "https://localhost:8080/"} id="ceuJH03jFJrZ" outputId="5192b699-57c2-4e18-c8ed-058645d4da61"
# copying the data of heart disease to y variable
y = data['Y'].copy()
# dropping the heart disease attribute from dataset and storing it in x variable
x = data.drop(['Y'], axis = 1)
# printing the number of class present in y with their time of occurance
print(f'Distribution Before Imblancing : {Counter(y)}')

# + colab={"base_uri": "https://localhost:8080/"} id="DYRumuMwFhbS" outputId="0eb7ca06-ea13-42b3-9062-467d748c2691"
#Calculating hte imbalance percentage in the dataset
imbalance = len(data[data['Y'] == 0])/len(data)*100
imbalance

# + colab={"base_uri": "https://localhost:8080/", "height": 312} id="xPKeF7g-794E" outputId="b93f4c20-1cb1-4551-c001-3df7975ebb6d"
# plotting the histogram for low imbalancing
hist = data['Y'].hist()
# tile of histogram
plt.title('Balance Data Set')
# label for x-axis
plt.xlabel('Number of Classes')
# label for y-axis
plt.ylabel('Total Number of Sample')
# to display histogram
plt.show

# + id="mb8ZEmsFB1Mp"
# saving the figure in pdf format of hist
fig= hist.get_figure()
fig.savefig('Hist_Main.jpg')

# + [markdown] id="jrr_Q6ViP0Kx"
# # **Low imblance**
# **The dataset over here is 65% Imbalanced**

# + colab={"base_uri": "https://localhost:8080/"} id="dJtZhYer79-F" outputId="e586cdfe-16c7-436a-b4e0-cb139aa65eb6"
# making the data imbalance by 65% and storing it in XA and YA
XA, YA = make_imbalance(x, y, 
                          sampling_strategy={1: 7210, 0: 3882},
                        random_state = 42)
# printing the count after low imbalancing
print(f'Distribution After Imblancing 65%: {Counter(YA)}')

# + colab={"base_uri": "https://localhost:8080/", "height": 312} id="CxmOdYG67-BE" outputId="2b6964a0-8b56-44a8-d974-e15d7e7bc6cc"
# plotting the histogram for low imbalancing
hist1 = YA.hist()
# tile of histogram
plt.title('65% Imblance Dataset')
# label for x-axis
plt.xlabel('Number of Classes')
# label for y-axis
plt.ylabel('Total Number of Sample')
# to display histogram
plt.show

# + id="Q_vu2uTe7-EN"
# saving the figure in pdf format of hist1
fig= hist1.get_figure()
fig.savefig('Hist_65%.pdf')

# + [markdown] id="xs5SPun3P_Ww"
# # **Medium Imblance**
# **The dataset over here is 75% Imbalanced**

# + colab={"base_uri": "https://localhost:8080/"} id="5TxPcA5f7-G_" outputId="46e3ddfc-5acd-4bd8-d6c5-a80408635953"
# making the data imbalance by 75% and storing it in XB and YB
XB, YB = make_imbalance(x, y, 
                          sampling_strategy={1: 7210, 0: 2403},
                        random_state = 42)
# printing the count after medium imbalancing
print(f'Distribution After Imblancing 75% : {Counter(YB)}')

# + colab={"base_uri": "https://localhost:8080/", "height": 312} id="ez0JMUuO7-Jp" outputId="958a5b86-9b2f-448f-b3c9-fef5def60d26"
# plotting the histogram for low imbalancing
hist2 = YB.hist()
# tile of histogram
plt.title('75% Imblance Dataset')
# label for x-axis
plt.xlabel('Number of Classes')
# label for y-axis
plt.ylabel('Total Number of Sample')
# to display histogram
plt.show

# + id="vHeTJHny7-MF"
# saving the figure in pdf format of hist1
fig= hist2.get_figure()
fig.savefig('Hist_75%.pdf')

# + [markdown] id="vDB3MnPFQIvA"
# # **High Imbalance**
# **The dataset over here is 90% Imbalanced**

# + colab={"base_uri": "https://localhost:8080/"} id="QdkELe_e7-SN" outputId="b0339e26-af9b-41b0-96c2-0b3c5ad02dbc"
# making the data imbalance by 90% and storing it in XC and YC
XC, YC = make_imbalance(x, y, 
                          sampling_strategy={1: 7210, 0: 801},
                        random_state = 42)
# printing the count after High imbalancing
print(f'Distribution After Imblancing 90% : {Counter(YC)}')

# + colab={"base_uri": "https://localhost:8080/", "height": 312} id="qrOpQbPOQMgR" outputId="727807d1-2f25-476f-9ee0-ceb2f06ce68f"
# plotting the histogram for low imbalancing
hist3 = YC.hist()
# tile of histogram
plt.title('90% Imblance Dataset')
# label for x-axis
plt.xlabel('Number of Classes')
# label for y-axis
plt.ylabel('Total Number of Sample')
# to display histogram
plt.show

# + id="W2dNtvy8QMjQ"
# saving the figure in pdf format of hist1
fig= hist3.get_figure()
fig.savefig('Hist_90%.jpg')

StarcoderdataJupyterScriptsDedupFiltered