Data Every Day: ウォルマートリテール分析

# tldr

Kggleの[Walmart Data-Retail Analysis](https://www.kaggle.com/vik2012kvs/walmart-dataretail-analysis)を[Walmart Holiday Sale Prediction - Data Every Day #082](https://www.youtube.com/watch?v=kS1SsTBRo4k&list=PLs_CQl4mUvI7Z9HnwoeOS5EUu9gyvekjK&index=82)に沿ってやっていきます。

実行環境はGoogle Colaboratorです。

# インポート


```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

import sklearn.preprocessing as sp
from sklearn.model_selection import train_test_split
import sklearn.linear_model as slm
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier

import tensorflow as tf
```

# データのダウンロード

Google Driveをマウントします。


```python
from google.colab import drive
drive.mount('/content/drive')
```

    Mounted at /content/drive


KaggleのAPIクライアントを初期化し、認証します。
認証情報はGoogle Drive内（`/content/drive/My Drive/Colab Notebooks/Kaggle`）に`kaggle.json`として置いてあります。


```python
import os
kaggle_path = "/content/drive/My Drive/Colab Notebooks/Kaggle"
os.environ['KAGGLE_CONFIG_DIR'] = kaggle_path

from kaggle.api.kaggle_api_extended import KaggleApi
api = KaggleApi()
api.authenticate() 
```

Kaggle APIを使ってデータをダウンロードします。


```python
dataset_id = 'vik2012kvs/walmart-dataretail-analysis'
dataset = api.dataset_list_files(dataset_id)
file_name = dataset.files[0].name
file_path = os.path.join(api.get_default_download_dir(), file_name)
file_path
```




    '/content/Walmart_Store_sales.csv'




```python
api.dataset_download_file(dataset_id, file_name, force=True, quiet=False)
```

    100%|██████████| 355k/355k [00:00<00:00, 40.9MB/s]

    Downloading Walmart_Store_sales.csv to /content
    


    





    True



# データの読み込み

Pedumagalhaes/quality-prediction-in-a-mining-processadasを使ってダウンロードしてきたCSVファイルを読み込みます。


```python
data = pd.read_csv(file_path)
```


```python
data
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Store</th>
      <th>Date</th>
      <th>Weekly_Sales</th>
      <th>Holiday_Flag</th>
      <th>Temperature</th>
      <th>Fuel_Price</th>
      <th>CPI</th>
      <th>Unemployment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>05-02-2010</td>
      <td>1643690.90</td>
      <td>0</td>
      <td>42.31</td>
      <td>2.572</td>
      <td>211.096358</td>
      <td>8.106</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1</td>
      <td>12-02-2010</td>
      <td>1641957.44</td>
      <td>1</td>
      <td>38.51</td>
      <td>2.548</td>
      <td>211.242170</td>
      <td>8.106</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1</td>
      <td>19-02-2010</td>
      <td>1611968.17</td>
      <td>0</td>
      <td>39.93</td>
      <td>2.514</td>
      <td>211.289143</td>
      <td>8.106</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1</td>
      <td>26-02-2010</td>
      <td>1409727.59</td>
      <td>0</td>
      <td>46.63</td>
      <td>2.561</td>
      <td>211.319643</td>
      <td>8.106</td>
    </tr>
    <tr>
      <th>4</th>
      <td>1</td>
      <td>05-03-2010</td>
      <td>1554806.68</td>
      <td>0</td>
      <td>46.50</td>
      <td>2.625</td>
      <td>211.350143</td>
      <td>8.106</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>6430</th>
      <td>45</td>
      <td>28-09-2012</td>
      <td>713173.95</td>
      <td>0</td>
      <td>64.88</td>
      <td>3.997</td>
      <td>192.013558</td>
      <td>8.684</td>
    </tr>
    <tr>
      <th>6431</th>
      <td>45</td>
      <td>05-10-2012</td>
      <td>733455.07</td>
      <td>0</td>
      <td>64.89</td>
      <td>3.985</td>
      <td>192.170412</td>
      <td>8.667</td>
    </tr>
    <tr>
      <th>6432</th>
      <td>45</td>
      <td>12-10-2012</td>
      <td>734464.36</td>
      <td>0</td>
      <td>54.47</td>
      <td>4.000</td>
      <td>192.327265</td>
      <td>8.667</td>
    </tr>
    <tr>
      <th>6433</th>
      <td>45</td>
      <td>19-10-2012</td>
      <td>718125.53</td>
      <td>0</td>
      <td>56.47</td>
      <td>3.969</td>
      <td>192.330854</td>
      <td>8.667</td>
    </tr>
    <tr>
      <th>6434</th>
      <td>45</td>
      <td>26-10-2012</td>
      <td>760281.43</td>
      <td>0</td>
      <td>58.85</td>
      <td>3.882</td>
      <td>192.308899</td>
      <td>8.667</td>
    </tr>
  </tbody>
</table>
<p>6435 rows × 8 columns</p>
</div>



# 下準備


```python
data.info()
```

    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 6435 entries, 0 to 6434
    Data columns (total 8 columns):
     #   Column        Non-Null Count  Dtype  
    ---  ------        --------------  -----  
     0   Store         6435 non-null   int64  
     1   Date          6435 non-null   object 
     2   Weekly_Sales  6435 non-null   float64
     3   Holiday_Flag  6435 non-null   int64  
     4   Temperature   6435 non-null   float64
     5   Fuel_Price    6435 non-null   float64
     6   CPI           6435 non-null   float64
     7   Unemployment  6435 non-null   float64
    dtypes: float64(5), int64(2), object(1)
    memory usage: 402.3+ KB



```python
data['Year'] = data['Date'].apply(lambda x: x[-4:])
data['Month'] = data['Date'].apply(lambda x: x[3:5])

data = data.drop('Date', axis=1)
```

# 可視化


```python
corr = data.corr()
plt.figure(figsize=(12, 10))
sns.heatmap(corr, annot=True, vmin=-1.0, vmax=1.0)
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7f429d8ca518>




![png](Walmart_Data_Retail_Analysis_files/Walmart_Data_Retail_Analysis_18_1.png)


# エンコード


```python
def onehot_encode(df, column, prefix):
    df = df.copy()
    dummies = pd.get_dummies(df[column], prefix)
    df = pd.concat([df, dummies], axis=1)
    df = df.drop(column, axis=1)
    return df
```


```python
data = onehot_encode(data, column='Store', prefix='store')
```

# データの分割とスケーリング


```python
y = data['Holiday_Flag']
X = data.drop(['Holiday_Flag'], axis=1)
```


```python
scaler = sp.StandardScaler()
X = pd.DataFrame(scaler.fit_transform(X), index=X.index, columns=X.columns)
```


```python
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7)
```

# トレーニング


```python
log_model = slm.LogisticRegression()
svm_model = SVC()
dec_model = DecisionTreeClassifier()
```


```python
log_model.fit(X_train, y_train)
svm_model.fit(X_train, y_train)
dec_model.fit(X_train, y_train)
```




    DecisionTreeClassifier(ccp_alpha=0.0, class_weight=None, criterion='gini',
                           max_depth=None, max_features=None, max_leaf_nodes=None,
                           min_impurity_decrease=0.0, min_impurity_split=None,
                           min_samples_leaf=1, min_samples_split=2,
                           min_weight_fraction_leaf=0.0, presort='deprecated',
                           random_state=None, splitter='best')



# 結果


```python
print("Logistic Regression Accuracy:", log_model.score(X_test, y_test))
print("                SVM Accuracy:", svm_model.score(X_test, y_test))
print("      Decision Tree Accuracy:", dec_model.score(X_test, y_test))
```

    Logistic Regression Accuracy: 0.9337131020196789
                    SVM Accuracy: 0.9337131020196789
          Decision Tree Accuracy: 0.9528741584671155