• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Dec 14th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import linear
2. import numpy
3. import math
4. import lpputils
5. import datetime
6.
7. def MAE(data, prediction):
8.     return numpy.mean([abs(a - b) for a,b in zip(data, prediction)])
9.
10. class Naloga3:
11.     def __init__(self):
12.         self.departureData = []
13.         self.arrivalData = []
14.
15.         self.departureData2 = []
16.
17.     def ucniPodatki(self):
18.         with open("../ucni_podatki/train_pred.csv") as f:
20.
21.             for line in f:
22.                 row = line.strip().split("\t")
23.                 departureTime = lpputils.parsedate(row[6])
24.                 arrivalTime = lpputils.parsedate(row[8])
25.
26.                 # znacilka za vikend
27.                 vikend = 1
28.                 ura = 1
29.                 if departureTime.weekday() < 5: vikend = 1000000
30.                 if departureTime.hour < 17 and departureTime.hour > 8: ura = departureTime.hour*100000000000
31.                 #if departureTime.weekday() == 6: vikend = 0
32.
33.                 #ura = departureTime.hour * 3600 + departureTime.minute * 60 + departureTime.second
34.
35.                 self.departureData.append([departureTime.timestamp(), vikend, ura])
36.                 self.arrivalData.append(arrivalTime.timestamp())
37.
38.
39.     def izracunPrihodov(self):
40.         with open("../ucni_podatki/test_pred.csv") as f:
42.             for line in f:
43.                 row = line.strip().split("\t")
44.                 departureTime = lpputils.parsedate(row[6])
45.
46.                 # znacilka za vikend
47.                 vikend = 1
48.                 ura = 1
49.                 if departureTime.weekday() < 5: vikend = 1000000
50.                 elif departureTime.weekday() == 6: vikend = 500000000
51.
52.
53.                 #print(departureTime, departureTime.weekday(), vikend)
54.                 #if departureTime.weekday() == 6: vikend = 0
55.                 if departureTime.hour < 17 and departureTime.hour > 8: ura = departureTime.hour*100000000000
56.                 #ura = departureTime.hour*3600 + departureTime.minute*60 + departureTime.second
57.                 #print(ura)
58.                 self.departureData2.append([departureTime.timestamp(), vikend, ura])
59.
60.
61.         x = numpy.array(self.departureData)
62.         y = numpy.array(self.arrivalData)
63.         #print(self.departureData)
64.         #print(x)
65.         #print(y)
66.         lr = linear.LinearLearner(lambda_=1.)
67.         napovednik = lr(x, y)
68.
69.         #print("Koeficienti", napovednik.th)  # prvi je konstanten faktor
70.         napovedi = numpy.array(self.departureData2)
71.         #print(napovedi)
72.         #napovedanDatum = napovednik(napovedi[1])
73.         #napovedanDatum = datetime.datetime.fromtimestamp(napovedanDatum)
74.
75.         f = open('napovedi_pred.txt', 'w')
76.         nap = []
77.         for napoved in napovedi:
78.             print(napoved)
79.             napoved = napovednik(napoved)
80.             napoved = datetime.datetime.fromtimestamp(napoved)
81.             #print(napoved)
82.             f.write(str(napoved) + "\n")
83.
84.         for napoved in self.departureData:
85.             napoved = napovednik(napoved)
86.             nap.append(float(str(napoved)))
87.
88.         #print(nap)
89.         print("MAE ", MAE(self.arrivalData, nap))
90.     def run(self):
91.         self.ucniPodatki()
92.         self.izracunPrihodov()
93.
94.
95. naloga3 = Naloga3()
96. naloga3.run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top