48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
import json
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
colorList = json.load(open('color/config.json','r'))["color"]
|
|
|
|
import csv
|
|
|
|
data_pasg = {}
|
|
data_temp = {}
|
|
|
|
with open('data/passenger.csv', 'r') as f:
|
|
reader = csv.reader(f)
|
|
header = next(reader)
|
|
data = [row for row in reader]
|
|
|
|
data = np.array(data).astype(float).T
|
|
data[0]=data[0].astype(int)
|
|
|
|
xList=data[1]+data[2]+data[3]
|
|
yList=np.full(np.shape(data[6])[0],0)
|
|
for i in range(1,np.shape(data[6])[0]):
|
|
yList[i]=data[6][i]-data[6][i-1]
|
|
|
|
|
|
print(yList)
|
|
|
|
plt.scatter(xList[1:],yList[1:],color=colorList[0])
|
|
|
|
from scipy.optimize import curve_fit
|
|
def linear(x,k,b):
|
|
return k*x+b
|
|
valk,valb = curve_fit(linear,xList,yList)[0]
|
|
residuals = yList - linear(xList,valk,valb)
|
|
ss_res = np.sum(residuals**2)
|
|
ss_tot = np.sum((yList-np.mean(yList))**2)
|
|
r_squared = 1 - (ss_res / ss_tot)
|
|
print("R-squared:", r_squared)
|
|
|
|
plt.plot(np.arange(0,2.5e6,1e5),linear(np.arange(0,2.5e6,1e5),valk,valb),color=colorList[1])
|
|
|
|
plt.xlabel('Passenger')
|
|
plt.ylabel('Local Population Decline')
|
|
plt.title('Relation between Passenger and Local Population Decline')
|
|
plt.text( 60000, 110, "k = %f\nb = %f\nR^2 = %f"%(valk,valb,r_squared),color=colorList[1])
|
|
plt.savefig('result/passenger-and-locals-relation.png')
|
|
plt.show()
|