In [1]:
#loading libraries
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.ticker as mtick
import matplotlib.pyplot as plt
sns.set(style = 'white')
import os
In [2]:
df=pd.read_csv(r'/Users/thomasmolner/Desktop/A2/Walmart_Store_sales.csv')
In [3]:
df.head()
Out[3]:
Store Date Weekly_Sales Holiday_Flag Temperature Fuel_Price CPI Unemployment
0 1 05-02-2010 1643690.90 0 42.31 2.572 211.096358 8.106
1 1 12-02-2010 1641957.44 1 38.51 2.548 211.242170 8.106
2 1 19-02-2010 1611968.17 0 39.93 2.514 211.289143 8.106
3 1 26-02-2010 1409727.59 0 46.63 2.561 211.319643 8.106
4 1 05-03-2010 1554806.68 0 46.50 2.625 211.350143 8.106
In [4]:
df.columns.values
Out[4]:
array(['Store', 'Date', 'Weekly_Sales', 'Holiday_Flag', 'Temperature',
       'Fuel_Price', 'CPI', 'Unemployment'], dtype=object)
In [5]:
#checking column data types
df.dtypes
Out[5]:
Store             int64
Date             object
Weekly_Sales    float64
Holiday_Flag      int64
Temperature     float64
Fuel_Price      float64
CPI             float64
Unemployment    float64
dtype: object
In [6]:
# checking null.
df.isnull().sum()
Out[6]:
Store           0
Date            0
Weekly_Sales    0
Holiday_Flag    0
Temperature     0
Fuel_Price      0
CPI             0
Unemployment    0
dtype: int64
In [7]:
#Get Correlation of "Holiday" with other variables:
plt.figure(figsize=(7,3))
df.corr()['Holiday_Flag'].sort_values(ascending = False).plot(kind='bar')
Out[7]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f86de4961c0>
In [8]:
#Get Correlation of "Store" with other variables:
plt.figure(figsize=(7,3))
df.corr()['Store'].sort_values(ascending = False).plot(kind='bar')
Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f86df313c70>
In [9]:
ax = (df['Holiday_Flag'].value_counts()*100.0 /len(df))\
.plot.pie(autopct='%.1f%%', labels = ['0', '1'],figsize =(5,5), fontsize = 12 )                                                                           
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
ax.set_ylabel('Holiday_Flag',fontsize = 12)
ax.set_title('% of Holiday_Flag', fontsize = 12)
Out[9]:
Text(0.5, 1.0, '% of Holiday_Flag')
In [10]:
ax = sns.distplot(df['Weekly_Sales'], hist=True, kde=False, 
             bins=int(180/5), color = 'darkblue', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
ax.set_ylabel('# of Sales')
ax.set_xlabel('Amount (sales)')
ax.set_title('# Sales by amount of weekly sales')
Out[10]:
Text(0.5, 1.0, '# Sales by amount of weekly sales')
In [11]:
ax = sns.distplot(df['Temperature'], hist=True, kde=False, 
             bins=int(180/5), color = 'darkblue', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
ax.set_ylabel('# of Temperature')
ax.set_xlabel('Amount (Temp)')
ax.set_title('# Temp number by case sales')
Out[11]:
Text(0.5, 1.0, '# Temp number by case sales')
In [12]:
ax = sns.distplot(df['Unemployment'], hist=True, kde=False, 
             bins=int(180/5), color = 'darkblue', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
ax.set_ylabel('# of Unemployment')
ax.set_xlabel('Amount (Unemployment)')
ax.set_title('# Unemployment Numbers')
Out[12]:
Text(0.5, 1.0, '# Unemployment Numbers')
In [13]:
ax = sns.distplot(df['CPI'], hist=True, kde=False, 
             bins=int(180/5), color = 'darkblue', 
             hist_kws={'edgecolor':'black'},
             kde_kws={'linewidth': 4})
ax.set_ylabel('# of CPI')
ax.set_xlabel('Amount (CPI)')
ax.set_title('# CPI Numbers')
Out[13]:
Text(0.5, 1.0, '# CPI Numbers')
In [14]:
df.describe()
Out[14]:
Store Weekly_Sales Holiday_Flag Temperature Fuel_Price CPI Unemployment
count 6435.000000 6.435000e+03 6435.000000 6435.000000 6435.000000 6435.000000 6435.000000
mean 23.000000 1.046965e+06 0.069930 60.663782 3.358607 171.578394 7.999151
std 12.988182 5.643666e+05 0.255049 18.444933 0.459020 39.356712 1.875885
min 1.000000 2.099862e+05 0.000000 -2.060000 2.472000 126.064000 3.879000
25% 12.000000 5.533501e+05 0.000000 47.460000 2.933000 131.735000 6.891000
50% 23.000000 9.607460e+05 0.000000 62.670000 3.445000 182.616521 7.874000
75% 34.000000 1.420159e+06 0.000000 74.940000 3.735000 212.743293 8.622000
max 45.000000 3.818686e+06 1.000000 100.140000 4.468000 227.232807 14.313000
In [15]:
ax = sns.kdeplot(df.Unemployment[(df["Holiday_Flag"] == 0) ],
                color="Red", shade = True)
ax = sns.kdeplot(df.Unemployment[(df["Holiday_Flag"] == 1) ],
                ax =ax, color="Blue", shade= True)
ax.legend(["0","1"],loc='upper right')
ax.set_ylabel('Density')
ax.set_xlabel('Unemployment')
ax.set_title('Distribution of Unemployment by Holiday_Flag')
Out[15]:
Text(0.5, 1.0, 'Distribution of Unemployment by Holiday_Flag')
In [16]:
ax = sns.kdeplot(df.Weekly_Sales[(df["Holiday_Flag"] == 0) ],
                color="Red", shade = True)
ax = sns.kdeplot(df.Weekly_Sales[(df["Holiday_Flag"] == 1) ],
                ax =ax, color="Blue", shade= True)
ax.legend(["0","1"],loc='upper right')
ax.set_ylabel('Density')
ax.set_xlabel('Weekly_Sales')
ax.set_title('Distribution of Weekly_Sales by Holiday_Flag')
Out[16]:
Text(0.5, 1.0, 'Distribution of Weekly_Sales by Holiday_Flag')
In [17]:
# Logistic Regression
In [18]:
# We will use the data frame where we had created dummy variables
y = df['Holiday_Flag'].values
X = df.drop(columns = ['Holiday_Flag', 'Date'])

# Scaling all the variables to a range of 0 to 1
from sklearn.preprocessing import MinMaxScaler
features = X.columns.values
scaler = MinMaxScaler(feature_range = (0,1))
X.columns = features
In [19]:
# Create Train & 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.3, random_state=101)
In [20]:
# Running logistic regression model
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
result = model.fit(X_train, y_train)
In [21]:
from sklearn import metrics
prediction_test = model.predict(X_test)
# Print the prediction accuracy
print (metrics.accuracy_score(y_test, prediction_test))
0.923873640600725
In [22]:
# Support Vector Machine
In [23]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=99)
In [26]:
from sklearn.svm import SVC
In [27]:
model.svm = SVC(kernel='linear') 
model.svm.fit(X_train,y_train)
preds = model.svm.predict(X_test)
metrics.accuracy_score(y_test, preds)
Out[27]:
0.9176379176379177
In [28]:
# Create the Confusion matrix
from sklearn.metrics import classification_report, confusion_matrix  
print(confusion_matrix(y_test,preds)) 
[[1181    0]
 [ 106    0]]
In [29]:
ax1 = sns.catplot(x="Store", kind="count", hue="Holiday_Flag", data=df,
                  estimator=lambda x: sum(x==0)*100.0/len(x))
#ax1.yaxis.set_major_formatter(mtick.PercentFormatter())
In [ ]: