#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
df=pd.read_csv(r'/Users/thomasmolner/Desktop/A2/Walmart_Store_sales.csv')
df.head()
df.columns.values
#checking column data types
df.dtypes
# checking null.
df.isnull().sum()
#Get Correlation of "Holiday" with other variables:
plt.figure(figsize=(7,3))
df.corr()['Holiday_Flag'].sort_values(ascending = False).plot(kind='bar')
#Get Correlation of "Store" with other variables:
plt.figure(figsize=(7,3))
df.corr()['Store'].sort_values(ascending = False).plot(kind='bar')
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)
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')
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')
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')
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')
df.describe()
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')
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')
# Logistic Regression
# 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
# 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)
# Running logistic regression model
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
result = model.fit(X_train, y_train)
from sklearn import metrics
prediction_test = model.predict(X_test)
# Print the prediction accuracy
print (metrics.accuracy_score(y_test, prediction_test))
# Support Vector Machine
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=99)
from sklearn.svm import SVC
model.svm = SVC(kernel='linear')
model.svm.fit(X_train,y_train)
preds = model.svm.predict(X_test)
metrics.accuracy_score(y_test, preds)
# Create the Confusion matrix
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test,preds))
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())