forked from cuducos/whiskyton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_add_correlations.py
More file actions
executable file
·78 lines (62 loc) · 1.9 KB
/
db_add_correlations.py
File metadata and controls
executable file
·78 lines (62 loc) · 1.9 KB
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
#!venv/bin/python
from app import db, models
# our data
tastes = (
'body',
'sweetness',
'smoky',
'medicinal',
'tobacco',
'honey',
'spicy',
'winey',
'nutty',
'malty',
'fruity',
'floral')
whiskies = models.Whisky.query.all()
# helper functions
def pearsonr(x, y):
n = len(x)
sum_x = float(sum(x))
sum_y = float(sum(y))
sum_x_sq = sum(i**2 for i in x)
sum_y_sq = sum(i**2 for i in y)
psum = sum(i * j for i, j in zip(x, y))
num = psum - ((sum_x * sum_y)/n)
multiplier_1 = sum_x_sq - ((sum_x ** 2) / n)
multiplier_2 = sum_y_sq - ((sum_y ** 2) / n)
den = (multiplier_1 * multiplier_2) ** 0.5
try:
return num / den
except ZeroDivisionError:
return 0
def get_tastes(whisky):
return [getattr(whisky, taste) for taste in tastes]
def corr_exists(id1, id2):
attempt1 = models.Correlation.query.filter_by(reference=id1, whisky=id2).first()
if attempt1 is not None:
return True
else:
attempt2 = models.Correlation.query.filter_by(reference=id2, whisky=id1).first()
if attempt2 is None:
return False
else:
return True
# calc and add to db
correlations_list = []
for reference in whiskies:
for whisky in whiskies:
item = str(whisky.id) + 'x' + str(reference.id)
item_alt = str(reference.id) + 'x' + str(whisky.id)
cond1 = item in correlations_list
cond2 = item_alt in correlations_list
cond3 = reference.id == whisky.id
if not cond1 and not cond2 and not cond3:
corr = pearsonr(get_tastes(reference), get_tastes(whisky))
row = models.Correlation(reference=reference.id,
whisky=whisky.id,
r=corr)
db.session.add(row)
correlations_list.append(item)
db.session.commit()