<jupyter_start><jupyter_text>Creation of Target<jupyter_code>import pandas as pd
pd.options.display.max_columns = 200
pd.options.display.max_rows = 1000
import matplotlib.pyplot as plt
import seaborn as sns
from __future__ import print_function
import os
import subprocess
import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier, export_graphviz
%matplotlib inline
sales_pdf = pd.read_csv('../UseCase_3_Datasets/sales_granular.csv')
sales_pdf.shape
sales_pdf.columns
sales_pdf_daily = sales_pdf.fillna(0)
sales_pdf_daily.head()<jupyter_output><empty_output><jupyter_text>Delete columns for which a lot of records are empty.Visual inspection reveals that a lot data is missing in the initial days, either data was not available or the shop did not exist. Removing data up until 1st Oct 2016 . Total sales values are created for the data from 1st Oct 2016 till 25th June 2017 .<jupyter_code>from datetime import datetime as dt

col_list = sales_pdf_daily.columns
for col in col_list[1:]:
    if dt.strptime(col.split(" ")[0],"%m/%d/%y") < dt.strptime("10/1/16", "%m/%d/%y"):
        #print ("Cols to delete", col)
        sales_pdf_daily.drop(col, axis = 1, inplace = True)
# sales_pdf_daily[sales_pdf_daily.store_code == 10055].hist()
# plt.show()
sales_pdf_daily['total_sales'] = sales_pdf_daily.loc[:, sales_pdf_daily.columns != 'store_code'].sum(axis=1)
sales_pdf_daily['sales_per_hr'] = sales_pdf_daily['total_sales']/sales_pdf_daily.shape[1]
sales_pdf_daily['sales_per_hr'].describe()
sales_pdf_daily.head()
import math
sales_pdf_daily['sales_per_hr']= sales_pdf_daily['sales_per_hr'].apply(lambda x : math.log(x+1))
sales_pdf_daily['sales_per_hr'].hist(bins=20)
plt.show()
sales_pdf_daily.to_csv("../UseCase_3_Datasets/sales_per_hr.csv",index_label=False)<jupyter_output><empty_output><jupyter_text>Depending on the locality a store is present , the sales pattern can be different . Creating fields that identify when did the shop sell most of it items .<jupyter_code>def getPhar(hour):
    if hour in ['5:00','6:00','7:00','8:00','9:00','10:00','11:00','12:00']:
        return "Morning"
    if hour in ['13:00','14:00','15:00','16:00']:
        return "Afternoon"
    if hour in ['17:00','18:00','19:00','20:00','21:00']:
        return "Evening"
    else :
        return "Night"

def merge_hr(df):
    df = df.fillna(0)
    temp_df = pd.DataFrame()
    for col in df.columns[:]:
        #print (col.split(" "))
        if len(col.split(" "))>=2:
            phar = getPhar(col.split(" ")[1])
            if phar in temp_df.columns:
                temp_df[phar] = temp_df[phar]+df[col]
            else :
                temp_df[phar] = df[col]
        else :
            temp_df[col] = df[col]
    #temp_df['store_code']=df['store_code']
    return temp_df


sales_pdf_hr =  merge_hr(sales_pdf_daily)
sales_pdf_hr.head()
sales_pdf_hr['Morning'] = sales_pdf_hr['Morning']/(sales_pdf_hr['total_sales']+1)
sales_pdf_hr['Afternoon'] = sales_pdf_hr['Afternoon']/(sales_pdf_hr['total_sales']+1)
sales_pdf_hr['Evening'] = sales_pdf_hr['Evening']/(sales_pdf_hr['total_sales']+1)
sales_pdf_hr['Night'] = sales_pdf_hr['Night']/(sales_pdf_hr['total_sales']+1)
sales_pdf_hr.describe()
# sales = {'account': ['Jones LLC', 'Alpha Co', 'Blue Inc'],
#          'Jan': [150, 200, 50],
#          'Feb': [200, 210, 90],
#          'Mar': [140, 215, 95]}
# df = pd.DataFrame.from_dict(sales)
# df.head()
# df[:].sum(axis=1)
sales_pdf_hr.loc[sales_pdf_hr['Afternoon'] == 30.0]
#Clean the data set a bit
sales_pdf_hr = sales_pdf_hr.query('Afternoon >= 0.0' and 'Afternoon <= 1.0')
sales_pdf_hr = sales_pdf_hr.query('Evening >= 0.0' and 'Evening <= 1.0')
sales_pdf_hr.head()
sales_pdf_hr.to_csv("../UseCase_3_Datasets/sales_per_day_period.csv",index_label=False)
sales_pdf_hr.head()
def getMaxSalesPeriod(m,a,e):
    max_val = max(m,a,e)
    if max_val == m:
        return 'Morning'
    if max_val == a :
        return 'Afternoon'
    else :
        return 'Evening'

print(getMaxSalesPeriod(2,7,8))
sales_pdf_hr['sales_pattern'] = sales_pdf_hr.apply(lambda row: getMaxSalesPeriod(row['Morning'],row['Afternoon'],row['Evening']), axis=1)
sales_pdf_hr['sales_pattern'].value_counts()
sales_pdf_hr.head(10)
sales_pdf_hr.to_csv("../UseCase_3_Datasets/sales_per_day_period.csv",index_label=False)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup