This repository has been archived on 2025-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
Files_for_MM/relation-plot3.py
2025-01-26 01:12:57 +08:00

38 lines
957 B
Python

import json
import numpy as np
import matplotlib.pyplot as plt
colorList = json.load(open('color/config.json','r'))["color"]
import csv
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(int).T
xList = data[1]
yList = data[2]
plt.scatter(xList,yList,color=colorList[0])
from scipy.optimize import curve_fit
def quadratic(x,a,b,c):
return a*x*x+b*x+c
vala,valb,valc = curve_fit(quadratic,xList,yList)[0]
residuals = yList - quadratic(xList,vala,valb,valc)
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,80000,1000),quadratic(np.arange(0,80000,1000),vala,valb,valc),color=colorList[1])
plt.xlabel('Ferry Passenger Count (Number of Passengers)')
plt.ylabel('Hotel & Motel Gross Business Sales (USD)')
plt.legend()
plt.show()