 # Untitled

Nov 26th, 2019
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #!/bin/python3
2. import math
3. import os
4. import random
5. import re
6. import sys
7.
8.
9. #
10. # Complete the 'predictMissingPrices' function below.
11. #
12. # The function is expected to return an INTEGER_ARRAY.
13. # The function accepts following parameters:
14. #  1. STRING startDate
15. #  2. STRING endDate
16. #  3. STRING_ARRAY knownTimestamps
17. #  4. INTEGER_ARRAY pricesAtKnownTimeStamps
18. #  5. STRING_ARRAY unknownTimestamps
19. #
20.
21. # y = mx + b
22. def makeLinearPrediction(value_map, i, X):
23.     # need two points
24.     X1 = None
25.     X2 = None
26.     if i == 0:
27.         j = i + 1
28.         while X1 == None or X2 == None:
29.             if X[j] in value_map:
30.                 if X1 == None:
31.                     X1 = (j, value_map[X[j]])
32.                 else:
33.                     X2 = (j, value_map[X[j]])
34.             j += 1
35.     elif i == len(X) - 1:
36.         j = i - 1
37.         while X1 == None or X2 == None:
38.             if X[j] in value_map:
39.                 if X1 == None:
40.                     X1 = (j, value_map[X[j]])
41.                 else:
42.                     X2 = (j, value_map[X[j]])
43.             j -= 1
44.     else:
45.         distance = 1
46.         while X1 == None or X2 == None:
47.             j = i - distance
48.             if 0 < j and X[j] in value_map:
49.                 if X1 == None:
50.                     X1 = (j, value_map[X[j]])
51.                 else:
52.                     X2 = (j, value_map[X[j]])
53.             j = i + distance
54.             if j < len(X) and X[j] in value_map:
55.                 if X1 == None:
56.                     X1 = (j, value_map[X[j]])
57.                 else:
58.                     X2 = (j, value_map[X[j]])
59.
60.             distance += 1
61.
62.     m = (X2-X1) / float((X2-X1))
63.     b = X1 - (m * X1)
64.
65.     x = i
66.     return round(m * x + b)
67.
68. def timestampToFeatures(ts):
69.     ts_split = ts.split(' ')
70.     date_split = ts_split.split('-')
71.     year = int(date_split)
72.     month = int(date_split)
73.     day = int(date_split)
74.     hour = int(ts_split.split(':'))
75.
76.     return (year, month, day, hour)
77.
78.
79. def predictMissingPrices(startDate, endDate, knownTimestamps, pricesAtKnownTimeStamps, unknownTimestamps):
80.     value_map = {}
81.     X_train = [timestampToFeatures(ts) for ts in knownTimestamps]
82.     for i in range(len(pricesAtKnownTimeStamps)):
83.         value_map[X_train[i]] = pricesAtKnownTimeStamps[i]
84.
85.     X_test = [timestampToFeatures(ts) for ts in unknownTimestamps]
86.     X = sorted(X_train + X_test)
87.
88.     predictions = []
89.     for i in range(len(X)):
90.         Xi = X[i]
91.         if Xi not in value_map:
92.             predictions.append(makeLinearPrediction(value_map, i, X))
93.
94.     return predictions
95.
96. """
97. from sklearn.svm import SVR
98.
99. def predictMissingPrices(startDate, endDate, knownTimestamps, pricesAtKnownTimeStamps, unknownTimestamps):
100.    X_train = [timestampToFeatures(ts) for ts in knownTimestamps]
101.    y_train = pricesAtKnownTimeStamps
102.    X_test = [timestampToFeatures(ts) for ts in unknownTimestamps]
103.
104.    model = SVR(kernel='rbf', C=10, gamma=0.1, epsilon=.1)
105.
106.    model.fit(X_train, y_train)
107.
108.    y_test = model.predict(X_test)
109.    return y_test
110. """
111.
112.
113. if __name__ == '__main__':
114.     # IO handling and parsing