-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrossman_ab.py
144 lines (100 loc) · 4.78 KB
/
rossman_ab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
from pandas import *
from numpy import *
from sklearn.linear_model import *
from sklearn.preprocessing import OneHotEncoder
import time
DATA_DIR = 'C:/Users/Guerzhoy/Desktop/utds/rossmann/'
os.chdir(DATA_DIR)
random.seed(0)
def convert_date(date_str):
pattern = '%Y-%m-%d'
epoch = int(time.mktime(time.strptime(date_str, pattern)))
return epoch//86400
def get_data_categ(data):
data.loc[:, "StateHoliday"] = data.loc[:,"StateHoliday"].astype(str)
data.loc[data.loc[:,"StateHoliday"] != '0', "StateHoliday"] = 'a'
data.loc[:, "Promo"] = data.loc[:, "Promo"].astype(int)
data.loc[:, "Open"] = data.loc[:, "Promo"].astype(int)
data_categ = DataFrame()
#data_categ = concat([data_categ, get_dummies(data.loc[:,"Store"], prefix = ["STORE"])], axis = 1)
data_categ = concat([data_categ, get_dummies(data.loc[:,"DayOfWeek"], prefix = ["DAY"])], axis = 1)
data_categ = concat([data_categ, get_dummies(data.loc[:,"StateHoliday"], prefix = ["StH"])], axis = 1)
data_categ = concat([data_categ, get_dummies(data.loc[:,"SchoolHoliday"], prefix = ["SchH"])], axis = 1)
data_categ = concat([data_categ, get_dummies(data.loc[:,"Promo"], prefix = ["PROMO"])], axis = 1)
data_categ = concat([data_categ, get_dummies(data.loc[:,"Open"], prefix = ["OPEN"])], axis = 1)
#data_categ = concat([data_categ, get_dummies(data.loc[:,"month"], prefix = ["MONTH"])], axis = 1)
data_categ = data_categ.fillna(0)
return data_categ
data = read_csv("data/train.csv", low_memory=False)
data = data.loc[data["Sales"]!=0,:]
store_means = DataFrame(data.groupby("Store").median()["Sales"])
store_means["Store"] = store_means.index
store_means.columns = ["SalesMean", "Store"]
data = data.merge(store_means, on = ["Store"])
#data["epoch"] = data["Date"].apply(convert_date)
#data["month"] = data["epoch"]//12
#read in the store data
store_data = read_csv("data/store.csv")
#Not doing anything with store_data for now...
data_categ = get_data_categ(data)
data_categ = concat([data_categ, data["SalesMean"]], axis = 1)
data_categ.index = range(len(data_categ))
data_cols = list(data_categ.columns)
data_test = read_csv("data/test.csv")
data_test = data_test.merge(store_means, on = ["Store"])
#data_test["epoch"] = data_test["Date"].apply(convert_date)
#data_test["month"] = data_test["epoch"]//12
data_test_categ = get_data_categ(data_test)
data_test_categ = concat([data_test_categ, data_test["SalesMean"]], axis = 1)
data_test_x = data_test_categ.loc[:, data_cols]
data_x = data_categ.loc[:, data_cols]
data_y = data.loc[:, ["Sales"]]
random.seed(0)
idx = random.permutation(range(len(data_x)))
idx_train = idx[:int(.9*len(idx))]
##############################################################################
#idx_train = idx_train[where(data.loc[idx_train, "Store"] == 50)]
##############################################################################
idx_valid = idx[int(.9*len(idx)):]
###############################################################################
#idx_valid = idx_valid[where(data.loc[idx_valid, "Store"] == 50)]
###############################################################################
train_x = data_x.loc[idx_train,:]
train_x.index = range(len(train_x))
train_y = data_y.loc[idx_train,:]
train_y.index = range(len(train_y))
valid_x = data_x.loc[idx_valid, :]
valid_x.index = range(len(valid_x))
valid_y = data_y.loc[idx_valid, :]
valid_y.index = range(len(valid_y))
train_x["SalesMean"] = log(1+train_x["SalesMean"])
valid_x["SalesMean"] = log(1+valid_x["SalesMean"])
data_test_x["SalesMean"] = log(1+data_test_x["SalesMean"])
lr = RidgeCV(alphas= [.75, 1, 1.25, 1.5, 2, 10, 20, 100])
lr.fit(train_x.as_matrix(), log(1+train_y.as_matrix().reshape((len(train_y),))) )
df = DataFrame()
df["var"] = valid_x.columns
df["coef"] = lr.coef_
pred_valid = exp(lr.predict(valid_x.as_matrix()))-1
actual_valid = valid_y.as_matrix().reshape((len(valid_y),))
pred_valid = pred_valid[actual_valid>0]
actual_valid = actual_valid[actual_valid>0]
#validation RMSPE .23966
#store 50: .22
sqrt(mean( ((pred_valid-actual_valid)/actual_valid)**2 ))
#sqrt(mean((lr.predict(train_x.as_matrix()).flatten()-train_y.as_matrix().flatten())**2))
#sqrt(mean((lr.predict(valid_x.as_matrix()).flatten()-valid_y.as_matrix().flatten())**2))
submit = False
if submit:
submission = DataFrame()
submission["Id"] = data_test["Id"]
pred = exp(lr.predict(data_test_x.as_matrix()))-1
pred = around(pred.astype(double)).astype(int32)
pred = maximum(0, pred)
submission["Sales"] = pred
submission.to_csv("submission.csv", index=False)