<jupyter_start><jupyter_text>Big Market Sales Prediction  Importing all essentials<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
sns.set_style('whitegrid')
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Data Analysis and Collection<jupyter_code>df = pd.read_csv('bigmarket.csv')
df.head()
df.shape
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 8523 entries, 0 to 8522
Data columns (total 12 columns):
 #   Column                     Non-Null Count  Dtype  
---  ------                     --------------  -----  
 0   Item_Identifier            8523 non-null   object 
 1   Item_Weight                7060 non-null   float64
 2   Item_Fat_Content           8523 non-null   object 
 3   Item_Visibility            8523 non-null   float64
 4   Item_Type                  8523 non-null   object 
 5   Item_MRP                   8523 non-null   float64
 6   Outlet_Identifier          8523 non-null   object 
 7   Outlet_Establishment_Year  8523 non-null   int64  
 8   Outlet_Size                6113 non-null   object 
 9   Outlet_Location_Type       8523 non-null   object 
 10  Outlet_Type                8523 non-null   object 
 11  Item_Outlet_Sales          8523 non-null   float64
dtypes: float64(4), int64(1), object(7)
memory usage: 799.2+ KB<jupyter_text>Categorical Features:- Item_Identifier- Item_Fat_Content- Item_Type- Outlet_Indentifier- Outlet_Size- Outlet_Location-Type- Outlet_Type<jupyter_code># checking for missing values
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>Handling Missing Values- MEAN -> AVERAGE- MODE -> MORE REPEATED VALUE<jupyter_code>df['Item_Weight'].mean()
# filling missing values in "Item-Weight" with MEAN VALUE
df['Item_Weight'].fillna(df['Item_Weight'].mean(), inplace=True)
# Mode of "Outlet_Size" column
df['Outlet_Size'].mode()
# filling the missing values in "Outlet_Size" column with Mode
mode_of_Outlet_size = df.pivot_table(values='Outlet_Size', columns='Outlet_Type', aggfunc=(lambda x: x.mode()[0]))
print(mode_of_Outlet_size)
miss_values = df['Outlet_Size'].isnull()
print(miss_values)
df.loc[miss_values, 'Outlet_Size'] = df.loc[miss_values, 'Outlet_Type'].apply(lambda x:mode_of_Outlet_size[x])
#checking for missing values
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>Data Analysis<jupyter_code>df.describe()
# Item_Weight distribution
plt.figure(figsize=(6,6))
sns.distplot(df['Item_Weight'])
plt.show()
# Item Visibility distribution
plt.figure(figsize=(6,6))
sns.distplot(df['Item_Visibility'])
plt.show()
# Item MRP distribution
plt.figure(figsize=(6,6))
sns.distplot(df['Item_MRP'])
plt.show()
# Item_Outlet_Sales distribution
plt.figure(figsize=(6,6))
sns.distplot(df['Item_Outlet_Sales'])
plt.show()
# Outlet_Establishment_Year column
plt.figure(figsize=(6,6))
sns.countplot(x='Outlet_Establishment_Year', data=df)
plt.show()
# Item_Fat_Content column
plt.figure(figsize=(6,6))
sns.countplot(x='Item_Fat_Content', data=df)
plt.show()
# Item_Type column
plt.figure(figsize=(30,6))
sns.countplot(x='Item_Type', data=df)
plt.show()
# Outlet_Size column
plt.figure(figsize=(6,6))
sns.countplot(x='Outlet_Size', data=df)
plt.show()<jupyter_output><empty_output><jupyter_text>Data Pre=Processing<jupyter_code>df.head()
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()<jupyter_output><empty_output><jupyter_text>Label Encoding<jupyter_code>from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
df['Item_Identifier'] = encoder.fit_transform(df['Item_Identifier'])

df['Item_Fat_Content'] = encoder.fit_transform(df['Item_Fat_Content'])

df['Item_Type'] = encoder.fit_transform(df['Item_Type'])

df['Outlet_Identifier'] = encoder.fit_transform(df['Outlet_Identifier'])

df['Outlet_Size'] = encoder.fit_transform(df['Outlet_Size'])

df['Outlet_Location_Type'] = encoder.fit_transform(df['Outlet_Location_Type'])

df['Outlet_Type'] = encoder.fit_transform(df['Outlet_Type'])
df.head()<jupyter_output><empty_output><jupyter_text>Splitting features and Target<jupyter_code>X = df.iloc[:,:-1].values
y = df.iloc[:,-1].values
print(X)
print(y)<jupyter_output>[3735.138   443.4228 2097.27   ... 1193.1136 1845.5976  765.67  ]<jupyter_text>Splitting into Train Test Split<jupyter_code>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=0)
print(X.shape, X_train.shape, X_test.shape)<jupyter_output>(8523, 11) (6818, 11) (1705, 11)<jupyter_text>Training Algortihm<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model = Sequential()

model.add(Dense(20, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(5, activation='relu'))

model.add(Dense(1)) # output layer

model.compile(optimizer='rmsprop', loss='mse')
model.fit(x = X_train, y = y_train, epochs=50)
model.summary()
loss_df = pd.DataFrame(model.history.history)
loss_df
loss_df.plot()<jupyter_output><empty_output><jupyter_text>From above graph we can say that loss of our model decreasing significantly.  Model Evaluation<jupyter_code># Model evaluation on test data
test_eval = model.evaluate(X_test, y_test, verbose=0)
print(test_eval)
# model evaluation on train set
train_eval = model.evaluate(X_train, y_train, verbose=0)
print(train_eval)
# Checking difference between train_eval and test_eval
model_diff = train_eval - test_eval
print(model_diff)
# Train prediction on test data
train_prediction = model.predict(X_train)
# Test prediction on test data
test_prediction = model.predict(X_test)
print(train_prediction)
print(test_prediction)
# R Squared : R-squared measures the strength of the relationship between your model and the dependent variable on a convenient 0 – 100% scale.
from sklearn.metrics import r2_score
r2_train = r2_score(y_train, train_prediction)
print('R squared value of train data:', r2_train)
r2_test = r2_score(y_test, test_prediction)
print('R squared value of test data:',r2_test)<jupyter_output>R squared value of test data: 0.4578482545374828
StarcoderdataJupyterStructuredCleanDedup