# グラフにプロットしてみる
# http://ailaby.com/least_square/

import csv
from pprint import pprint
import numpy as np
from matplotlib import pyplot as plt

infile = 'data.csv'

i = 0
x = []
y = []
with open(infile, "r") as f:
    reader = csv.reader(f)

    for row in reader:
        if i == 0:
            header = row
        else:
            x.append(float(row[0]))
            y.append(float(row[1]))
        i += 1

print("header:", header)

print("x:", x)
print("y:", y)


print("polynomial fit start:")
ai = np.polyfit(x, y, 2)
print("  lsq result: ai=", ai)
print("  y = {} + {} * x + {} * x^2".format(ai[0], ai[1], ai[2]))

plt.scatter(x , y)
plt.show()
