# ---
# 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
# ---

# # Big Mart Sales Prediction

# ### Importing libraries

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
# %matplotlib inline
sns.set_style('whitegrid')

# ### Importing dataset

df = pd.read_csv('Train.csv')

df.head()

df.shape

df.info()

# ##### Listing the categorial features
# - Item_Identifier
# - Item_Fat_Content
# - Item_Type 
# - Outlet_Identifier
# - Outlet_Size
# - Outlet_Location_Type
# - Outlet_Type

df.isnull().sum()

# ### Handling the missing values

# ##### Filling the NaN value of Item_weight with mean of that column

df['Item_Weight'].mean()

df['Item_Weight'].fillna(df['Item_Weight'].mean(), inplace=True)

df.isnull().sum()

# ##### Filling the NaN value of Outlet_Size with mode of that column

mode_of_outlet_size = df.pivot_table(values='Outlet_Size', columns='Outlet_Type', aggfunc=(lambda x: x.mode()[0]))
mode_of_outlet_size

missing_values = df['Outlet_Size'].isnull()
missing_values

df.loc[missing_values, 'Outlet_Size'] = df.loc[missing_values, 'Outlet_Type'].apply(lambda x: mode_of_outlet_size[x])

df

df.describe()

# ### Visualizing different columns

sns.set()

### Item weight distribution
plt.figure(figsize=(8, 8))
sns.displot(df.Item_Weight, kde=True)
plt.show()

### Item visibility distribution
plt.figure(figsize=(8, 8))
sns.displot(df.Item_Visibility, kde=True)
plt.show()

### Item MRP distribution
plt.figure(figsize=(8, 8))
sns.displot(df.Item_MRP, kde=True)
plt.show()

### Item Outlet Sales distribution
plt.figure(figsize=(8, 8))
sns.displot(df.Item_Outlet_Sales, kde=True)
plt.show()

### Item Outlet Establishment Year distribution
plt.figure(figsize=(8, 8))
sns.countplot(df.Outlet_Establishment_Year)
plt.show()

### Item Fat Content distribution
plt.figure(figsize=(8, 8))
sns.countplot(df.Item_Fat_Content)
plt.show()

### Item type distribution
plt.figure(figsize=(15, 8))
sns.countplot(df.Item_Type)
plt.xticks(rotation=45)
plt.show()

### Outlet Size distribution
plt.figure(figsize=(10, 8))
sns.countplot(df.Outlet_Size)
plt.xticks(rotation=45)
plt.show()

# ### Data Preprocessing

# ##### Since in item fat content column, same feature is of different name so we make them same

df['Item_Fat_Content'].value_counts()

df.replace({'Item_Fat_Content': {'low fat': 'Low Fat', 'LF': 'Low Fat', 'reg': 'Regular'}}, inplace=True)

df['Item_Fat_Content'].value_counts()

# #### Changing the categorial value to the numerical value

# +
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()

categorial_features = ['Item_Identifier', 'Item_Fat_Content', 'Item_Type', 'Outlet_Identifier', 'Outlet_Size', 'Outlet_Location_Type', 'Outlet_Type']
for features in categorial_features:
    df[features] = le.fit_transform(df[features])
# -

df.head()

# ### Splitting the data into features and target

X = df.drop('Item_Outlet_Sales', axis=1)
y = df.Item_Outlet_Sales

# ### Splitting the data into training and test data

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=2)

# ### Fitting the model with train dataset

from xgboost import XGBRegressor
xgb = XGBRegressor()
xgb.fit(X_train, y_train)

y_preds_train = xgb.predict(X_train)
y_preds_train

y_preds = xgb.predict(X_test)
y_preds

xgb.score(X_train, y_train)

xgb.score(X_test, y_test)

from sklearn.metrics import r2_score
print('The r2 score of training data: ', r2_score(y_train, y_preds_train))
print('The r2 score of test data: ', r2_score(y_test, y_preds))

# ### Now let's fit the other model (like linear regressor, random forest regressor, decision tree regressor, support vector regressor) into the training data 

from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.svm import SVR


# +
## Let's define a function fit_model

def fit_model(model, X_train, X_test, y_train, y_test):
    model.fit(X_train, y_train)
    print('The predicted values:\n ', model.predict(X_test))
    print('\nThe accuracy score: ', model.score(X_test, y_test))


# -

# #### 1. Linear Regression

fit_model(LinearRegression(), X_train, X_test, y_train, y_test)

# #### 2. Decision Tree Regressor

fit_model(DecisionTreeRegressor(), X_train, X_test, y_train, y_test)

# #### 3. Random Forest Regressor

fit_model(RandomForestRegressor(), X_train, X_test, y_train, y_test)

# #### 4. Support vector regressor

fit_model(SVR(kernel='linear'), X_train, X_test, y_train, y_test)



StarcoderdataJupyterScriptsDedupFiltered