Skip to content

Instantly share code, notes, and snippets.

@dolphin2410
Created May 8, 2024 12:42
Show Gist options
  • Save dolphin2410/c9707c58425a9bbad97c2aa726b8ec5d to your computer and use it in GitHub Desktop.
Save dolphin2410/c9707c58425a9bbad97c2aa726b8ec5d to your computer and use it in GitHub Desktop.
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import numpy as np
data_time = [9.13E-02, 1.08E-01, 1.25E-01, 1.41E-01, 1.58E-01, 1.75E-01, 1.91E-01, 2.08E-01]
data_y = [1.50E-03, -4.40E-02, -9.14E-02, -1.44E-01, -2.00E-01, -2.63E-01, -3.24E-01, -3.92E-01]
regression = LinearRegression()
regression.fit(np.array(list(map(lambda x: [x**2, x], data_time))).reshape(-1, 2), np.array(data_y).reshape(-1, 1))
a = regression.coef_[0][0]
b = regression.coef_[0][1]
c = regression.intercept_[0]
print([a,b,c])
plt.title("y")
t_list = np.linspace(data_time[0], data_time[7], 10)
plt.plot(t_list, sum([item[0] * t_list**item[1] for item in [[c, 0], [b, 1], [a, 2]]]))
plt.scatter(data_time, data_y, marker="^", c="#000000")
plt.xlabel("t")
plt.ylabel("y")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment