-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlez9.py
More file actions
116 lines (83 loc) · 2.71 KB
/
Copy pathlez9.py
File metadata and controls
116 lines (83 loc) · 2.71 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#from matplotlib import pyplot
#definisco classe CSVFile
class CSVFile:
#definisco costruttore con nome del file per inizializzare l'oggetto
def __init__(self,name):
self.name = name
#definisco il metodo getData per estrarre i dati numerici dal file in una lista
def getData(self, start=None, end=None):
if(isinstance(self.name, str)==False):
raise Exception("Il valore {} non è una stinga".format(self.name))
return None
csvValues = []
try:
csvFile = open(self.name, "r")
except Exception as e:
print('Il file "{}" non esiste: \n"{}"'.format(self.name, e))
#ritorno null dalla funzione
return None
for line in csvFile:
elements = line.split(',')
if elements[0] != 'Date':
dateVal = elements[0]
value = elements[1]
try:
value=float(value)
except Exception as e:
print('Il carattere è di tipo stringa: \n{}\n'.format(e))
value=0
#salto al prossimo giro del ciclo
continue
csvValues.append((value))
csvFile.close()
return csvValues
class Utilities():
def computeAvgIncrement(self, data):
data_length = len(data)
increments_sum = 0
for i, val in enumerate(data):
if (i>0):
increment = val - data[i-1]
increments_sum += increment
increment_average = increments_sum/(data_length-1)
return increment_average
class Model(object):
def fit(self, data):
pass
def predict(self):
pass
class IncrementModel(Model):
def fit(self, data):
utilities = Utilities()
self.global_increment_average = utilities.computeAvgIncrement(data)
#print(self.global_increment_average)
#return increment_average
def predict(self, prev_months):
utilities = Utilities()
increment_average = utilities.computeAvgIncrement(prev_months)
predicted_increment = (self.global_increment_average+ increment_average)/2
predicted_value = prev_months[-1] + predicted_increment
return predicted_value
#sales_value = [8,19,31,41,50,52,60]
csvFile = CSVFile('shampoo_sales_err.csv')
sales_value = csvFile.getData()
training_set = sales_value[0:20]
model = IncrementModel()
model.fit(sales_value[20:23])
prediction = model.predict(training_set)
print(prediction)
'''
prev_months= sales_value[4:7]
model = IncrementModel()
model.fit(sales_value[0:4])
prediction = model.predict(prev_months)
print (prediction)
pyplot.plot(sales_value + [prediction], color='tab:red')
pyplot.plot(sales_value, color='tab:blue')
pyplot.show()
'''
#csvFile = CSVFile('shampoo_sales_err.csv')
#sales_value = csvFile.getData()
#prev_months = sales_value[24:36]
#prediction = model.predict(prev_months)
#print(prediction)