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

# ## Black Friday Dataset (EDA and Feature Engineering)

# # Problem Statement: 

# A retail company “ABC Private Limited” wants to understand the customer purchase behaviour (specifically, purchase amount) against various products of different categories. They have shared purchase summary of various customers for selected high volume products from last month. The data set also contains customer demographics (age, gender, marital status, city_type, stay_in_current_city), product details (product_id and product category) and Total purchase_amount from last month.
#
# Now, they want to build a model to predict the purchase amount of customer against various products which will help them to create personalized offer for customers against different products.

import pandas as pd
import seaborn as sns
import numpy as np 
import matplotlib.pyplot as plt 
# %matplotlib inline

df_train = pd.read_csv(r'C:\Users\Personal\OneDrive\Documents\Jupyter Notebook\train.csv\train.csv')
df_train.head()

df_test = pd.read_csv(r"C:\Users\Personal\OneDrive\Documents\Jupyter Notebook\test.csv\test.csv")
df_test.head()

df = df_train.append(df_test)
df.head(10)

df.info()

df.isnull().sum()

df.describe()

df.drop(['User_ID'], axis = 1 , inplace = True) #axis=1 implies we are dropping it column-wise

df.head()

# ## Handling the Categorical Feature -> 'Gender'

df['Gender'] = df["Gender"].map({'F':0,'M':1}) #Converting the Gender column in terms of 0s and 1s 

df.head()

# ## Handling the Categorical Feature -> 'Age'

df["Age"].unique()

df["Age"]=df["Age"].map({'0-17':1,'18-25':2,'26-35':3,'36-45':4,'46-50':5,'51-55':6,'55+':7})
df.head(10)

# ## Handling the Categorical Feature --> 'City_Category'

df_city=pd.get_dummies(df['City_Category'],drop_first=True) 
df_city
## Why drop_first? -> Two categories are sufficient to represent all the three categories

df = pd.concat([df,df_city],axis = 1) 
df.drop('City_Category',axis=1,inplace=True)
df.head()

# ## Handling Missing Values 

df.isnull().sum()

df['Product_Category_2'].unique()

df['Product_Category_2'].value_counts()

df['Product_Category_2'].mode()[0] # A series was forming with 0 and 8.0

# #### Why mode?
#
# It makes more sense to fill the null values as a category that was bought the most rather than filling it with the mean of all the categorical values.  
# Also, using mean could create a new category of products.

df['Product_Category_2'] = df['Product_Category_2'].fillna(df['Product_Category_2'].mode()[0])
df.isnull().sum()

df['Product_Category_3'].unique()

df['Product_Category_3'] = df['Product_Category_3'].fillna(df['Product_Category_3'].mode()[0])
df.isnull().sum()

df['Stay_In_Current_City_Years'].unique()

df['Stay_In_Current_City_Years'] = df['Stay_In_Current_City_Years'].str.replace('+',' ')

df.head()

df.info()

# ## Converting the object into integers

df['Stay_In_Current_City_Years'] = df['Stay_In_Current_City_Years'].astype(int)

df.info()

df['B']=df['B'].astype(int)
df['C']=df['C'].astype(int)

# ## Creating VIsualisations

sns.barplot(x='Age',y='Purchase',hue='Gender',data=df, palette='flare')

# 1. Purchase of men is higher than women
# 2. Among men purchase of age group 6 is the highest.
# 3. Among women purchase of age group 6 is the hghest.
#

df.head()

df['Marital_Status'].value_counts().values

colors_gender = sns.color_palette('crest')
explode_gender = (0.01,0)

import matplotlib
matplotlib.rcParams['figure.figsize'] = (12,6)
plt.pie(df['Marital_Status'].value_counts().values,labels=['Single','Married'],colors=colors_gender,explode=explode_gender,autopct = '%1.1f%%')

# 1. Single people are more likely to buy than married people

fig, axes = plt.subplots(1, 3, figsize=(30, 6), sharey=True)
fig.suptitle('Purchase vs Product Categories')
sns.barplot(ax=axes[0],data=df, x='Product_Category_1', y='Purchase')
axes[0].set_title('Product_Category_1')
sns.barplot(ax=axes[1],data=df, x='Product_Category_2', y='Purchase')
axes[1].set_title('Product_Category_2')
sns.barplot(ax=axes[2],data=df, x='Product_Category_3', y='Purchase')
axes[2].set_title('Product_Category_3')


# ## The Most sold category is 1

# # Feature Scaling

df_test = df[df['Purchase'].isnull()]

df_train = df[~df['Purchase'].isnull()]

X=df_train.drop('Purchase',axis=1)
X.head()

y=df_train['Purchase']
y.head()

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
     X, y, test_size=0.33, random_state=42)

X_train.drop('Product_ID',axis=1,inplace=True)
X_test.drop('Product_ID',axis=1,inplace=True)

from sklearn.preprocessing import StandardScaler
sc=StandardScaler()
X_train=sc.fit_transform(X_train)
X_test=sc.transform(X_test)

# +
from sklearn.linear_model import LinearRegression

lm = LinearRegression()
lm.fit(X_train, y_train)
# -

predictions = lm.predict(X_test)
print("Predicted purchases (in dollars) for new costumers:", predictions)

# Plot a scatter between predicted and actual
sns.regplot(x=y_test,y=predictions)
plt.title('Actual vs Predictions in Linear Regression')
plt.xlabel('Actual')
plt.ylabel('Predictions')

# +
from sklearn import metrics

print('MAE:', metrics.mean_absolute_error(y_test, predictions))
print('MSE:', metrics.mean_squared_error(y_test, predictions))
# -



StarcoderdataJupyterScriptsDedupFiltered