-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
72 lines (58 loc) · 2.65 KB
/
helpers.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
import numpy as np
import csv
def calculate_rmse_score(x1, x2):
"""
Calculates rmse score between given two numpy vectors of same size
"""
# return rmse between x1, x2
return np.sqrt(np.mean(np.square(x1-x2)))
def filter_models(test_rmses, preds_train_trains, preds_train_tests, preds_tests, threshold=1.2):
"""
Takes predictions on train, test and dataset_testing; as well as test_rmses and threshold
returns predictions with test rmse smaller than given threshold
"""
# find index of models with smaller test rmse on threshold
index_lst = list(np.where(test_rmses < threshold)[0])
# initialize predictions
preds_train_trains_best = []
preds_train_tests_best = []
preds_tests_best = []
# traverse each prediction
for i in range(len(preds_tests)):
# check if test rmse smaller than given threshold
if i in index_lst:
# append predictions
preds_train_trains_best.append(preds_train_trains[i])
preds_train_tests_best.append(preds_train_tests[i])
preds_tests_best.append(preds_tests[i])
# return filtered predictions
return preds_train_trains_best, preds_train_tests_best, preds_tests_best
def stack_predictions(preds_train_trains, preds_train_tests, preds_tests):
"""
Given predictions as a list of numpy arrays, stack them together.
return 2d numpy array for predictions on train, test and dataset_testing
where number of columns are number of different predictions
"""
# stack predictions
all_preds_train_trains = np.vstack(preds_train_trains).T
all_preds_train_tests = np.vstack(preds_train_tests).T
all_preds_tests = np.vstack(preds_tests).T
# return stacked predictions
return all_preds_train_trains, all_preds_train_tests, all_preds_tests
def create_csv_submission(user_ids, movie_ids, y_pred, name):
"""
Creates an output file in csv format for submission to kaggle
Arguments: ids (event ids associated with each prediction)
y_pred (predicted class labels)
name (string name of .csv output file to be created)
"""
# open file as csvfile
with open(name, 'w') as csvfile:
# set fieldnames
fieldnames = ['Id', 'Prediction']
# write
writer = csv.DictWriter(csvfile, delimiter=",", fieldnames=fieldnames)
writer.writeheader()
# write predictions
for u, m, pred in zip(user_ids, movie_ids, y_pred):
writer.writerow({'Id':"r{}_c{}".format(u,m),'Prediction':float(pred)})