-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoy_script.py
executable file
·71 lines (50 loc) · 2.11 KB
/
toy_script.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
def load_data(data_path):
FEATURES = range(2, 33)
N_TIME_SERIES = 3500
# Create the training and testing samples
LS_path = os.path.join(data_path, 'LS')
TS_path = os.path.join(data_path, 'TS')
X_train, X_test = [np.zeros((N_TIME_SERIES, (len(FEATURES) * 512))) for i in range(2)]
for f in FEATURES:
data = np.loadtxt(os.path.join(LS_path, 'LS_sensor_{}.txt'.format(f)))
X_train[:, (f-2)*512:(f-2+1)*512] = data
data = np.loadtxt(os.path.join(TS_path, 'TS_sensor_{}.txt'.format(f)))
X_test[:, (f-2)*512:(f-2+1)*512] = data
y_train = np.loadtxt(os.path.join(LS_path, 'activity_Id.txt'))
print('X_train size: {}.'.format(X_train.shape))
print('y_train size: {}.'.format(y_train.shape))
print('X_test size: {}.'.format(X_test.shape))
return X_train, y_train, X_test
def write_submission(y, submission_path='example_submission.csv'):
parent_dir = os.path.dirname(submission_path)
if parent_dir:
os.makedirs(parent_dir, exist_ok=True)
if os.path.exists(submission_path):
os.remove(submission_path)
y = y.astype(int)
outputs = np.unique(y)
# Verify conditions on the predictions
if np.max(outputs) > 14:
raise ValueError('Class {} does not exist.'.format(np.max(outputs)))
if np.min(outputs) < 1:
raise ValueError('Class {} does not exist.'.format(np.min(outputs)))
# Write submission file
with open(submission_path, 'a') as file:
n_samples = len(y)
if n_samples != 3500:
raise ValueError('Check the number of predicted values.')
file.write('Id,Prediction\n')
for n, i in enumerate(y):
file.write('{},{}\n'.format(n+1, int(i)))
print(f'Submission saved to {submission_path}.')
if __name__ == '__main__':
X_train, y_train, X_test = load_data(data_path='./')
clf = KNeighborsClassifier(n_neighbors=1)
clf.fit(X_train, y_train)
y_test = clf.predict(X_test)
write_submission(y_test)