-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrendlijn.py
76 lines (62 loc) · 1.77 KB
/
trendlijn.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
73
74
import csv
import matplotlib.pyplot as plt
filename = 'utrecht.csv'
jaren = ['2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018']
columns = {}
rows = []
data = {}
def is_number(s):
try:
int(s)
return True
except ValueError:
return False
with open(filename) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
line_count = 0
for row in csv_reader:
if line_count == 0:
line_count += 1
i = 0
for c in row:
columns[c] = i
data[i] = []
i += 1
else:
line_count += 1
if 'JJ00' in row[columns['Perioden']]:
rows.append(row)
i = 0
for c in row:
if is_number(c):
data[i].append(int(c))
else:
data[i].append(c)
i = i+1
aantalGeboren = data[4]
def sum_lijst(variabele):
sum = 0
for x in variabele:
sum += x
return sum
def count(variabele):
item_count = 0
for x in variabele[:]:
item_count += 1
return item_count
y = aantalGeboren
N = count(y)
x = range(N)
A = (sum_lijst(x[i] * y[i] for i in range(N)) - 1./N*sum_lijst(x)*sum_lijst(y)) / (sum_lijst(x[i]**2 for i in range(N)) - 1./N*sum_lijst(x)**2)
B = sum_lijst(y)/N - A * sum_lijst(x)/N
# print("( %f * x ) + %f " % (A,B))
predict=[A*index + B for index in range(10)]
# print(predict)
plt.plot(jaren,aantalGeboren,'ro',label='geboren')
plt.plot([A*index + B for index in range(17)],'g',label='trendlijn')
plt.xticks(rotation=90)
plt.xlabel('jaren')
plt.ylabel('aantal')
plt.legend()
plt.title('Kinderen levend geboren')
plt.show()