-
Notifications
You must be signed in to change notification settings - Fork 5
/
manager.py
189 lines (163 loc) · 5.48 KB
/
manager.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import date, timedelta
city_to_country = {
'zagreb': 'croatia',
'berlin': 'germany',
'poznan': 'poland',
'warszaw': 'poland',
'gdynia': 'poland',
'gdansk': 'poland',
'sopot': 'poland',
'krakow': 'poland',
'wroclaw': 'poland',
'malmo': 'sweden',
'gothenburg': 'sweden',
'vasteras': 'sweden',
'stockholm': 'sweden',
'copenhagen': 'denmark',
'prague': 'czechia',
'bergamo': 'italy',
'milano': 'italy',
}
country_to_currency = {
'croatia': 'HRK',
'poland': 'PLN',
'italy': 'EUR',
'germany': 'EUR',
'sweden': 'SEK',
'denmark': 'DKK',
'czechia': 'CZK',
}
rates = {
('PLN', 'HRK'): 1.73,
('EUR', 'HRK'): 7.43,
('CZK', 'HRK'): 0.29,
('HRK', 'EUR'): 0.13,
}
def get_rate(fromc, toc, date):
if fromc==toc:
return 1
return rates[fromc, toc]
def transform_row(r):
if len(r.date) == 6:
r.date += '2018.'
d = r.date[:-1].split('.')
r.date = date(*map(int, d[::-1]))
r.country = city_to_country[r.city]
r.currency = country_to_currency[r.country]
if np.isnan(r.hrk):
r.hrk = r.lcy * get_rate(r.currency, 'HRK', r.date)
r.eur = r.hrk * get_rate('HRK', 'EUR', r.date)
return r
df = pd.read_csv('./expenses.csv')
df = df.apply(transform_row, axis=1)
'''
category_sum = []
for category, rows in df.groupby(['category'])['eur']:
category_sum.append((sum(rows.values), category))
sums, labels = zip(*sorted(category_sum, reverse=True)[:11])
explode = [0.1]*len(sums)
fig1, ax1 = plt.subplots()
ax1.pie(sums, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=0)
ax1.axis('equal')
plt.title('percentage of money spend on each category')
plt.show()
'''
'''
preferred_transport = []
for desc, rows in df.groupby(['description']):
if all(i in ['travel', 'transport'] for i in rows['category']):
preferred_transport.append((sum(rows['eur'].values), desc))
sums, labels = zip(*sorted(preferred_transport, reverse=True))
explode = [0.1]*len(sums)
fig1, ax1 = plt.subplots()
ax1.pie(sums, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=0)
ax1.axis('equal')
plt.title('preferred transport')
plt.show()
'''
'''
all_categories = tuple(set(df['category']) - set('travel'))
cities_daily = []
for city, rows in df.groupby(['city']):
days = set(rows['date'].values)
days = (max(days) - min(days)).days + 1
descs = {desc: sum(rs['eur'].values)/days for desc, rs in rows[rows['category'] != 'travel'].groupby(['category'])}
cities_daily.append((city, tuple(descs[i] if i in descs else 0 for i in all_categories)))
cities, sums = zip(*sorted(cities_daily, reverse=True, key=lambda t: sum(t[1])))
sums = list(zip(*sums))
ind = np.arange(len(cities))
width = 0.35
colors = ['maroon','c','orange','k','b','darkmagenta','g','m','yellow','r','peru','navy','cyan','plum','grey','teal','lime']
bars = [plt.bar(ind, sums[0], width, color=colors[0])]
for i in range(1, len(all_categories)):
bars.append(plt.bar(ind, sums[i], width, bottom=list(map(sum, zip(*sums[:i]))), color=colors[i]))
plt.title('amount of money spent daily per city')
plt.xticks(ind, cities)
plt.yticks(np.arange(0, 26, 1))
plt.legend(list(zip(*bars))[0], all_categories)
plt.show()
'''
daily_expenses = []
all_dates = list(pd.date_range(min(df['date']), max(df['date']), freq='D'))
cities = []
for d in list(all_dates):
value = sum(df[df['date'] == d.date()]['eur'])
if value:
cities.append(df[df['date'] == d.date()]['city'].values[-1])
daily_expenses.append((d.date(), value))
else:
all_dates.remove(d)
dates, sums = zip(*daily_expenses)
'''
ind = np.arange(len(all_dates))
plt.bar(ind, sums, color='red', width=0.35)
plt.xticks(ind, list(range(len(all_dates))))
plt.title('daily amount of money spend')
plt.xlabel('day number')
plt.ylabel('amount of money in eur')
plt.show()
'''
# encoding strings
x = np.array([*zip(range(len(dates)), cities)])
y = sums
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer, make_column_transformer
preprocess = make_column_transformer((OneHotEncoder(), [-1])).fit_transform(x)
x = np.array([*zip(preprocess, x[:, 0])])
# avoiding the dummy variable trap
x = x[:, 1:]
# splitting into test set and training set
from sklearn.model_selection import train_test_split as tts
xtrain, xtest, ytrain, ytest = tts(x, y, test_size = 0.2)
# fitting the regressor to our training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(xtrain, ytrain)
# applying the regressor to our test set
ypred = regressor.predict(xtest)
# backward elimination
import statsmodels.formula.api as sm
xopt = np.hstack([np.ones((x.shape[0], 1)), x])
for i in range(xopt.shape[1]):
pvalues = sm.OLS(y, xopt.astype(np.float64)).fit().pvalues
mi = np.argmax(pvalues)
mp = pvalues[mi]
if mp > 0.05:
xopt = np.delete(xopt, [mi], 1)
else:
break
xtrain, xtest, ytrain, ytest = tts(xopt, y, test_size = 0.2, random_state = 0)
regressor = LinearRegression()
regressor.fit(xtrain, ytrain)
ypredopt = regressor.predict(xtest)
plt.plot(ytest, color = 'green')
plt.plot(ypred, color = 'navy')
plt.plot(ypredopt, color = 'red')
plt.ylabel('predicted value in eur')
plt.xlabel('days in the test set')
plt.show()