from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures from sklearn.metrics import r2_score import matplotlib.pyplot as plt degree = 3 #多項式の次数 polynomial_features = PolynomialFeatures(degree = degree) #8個のデータの読み込み x=[[1.5],[2.0],[2.5],[3.0],[4.0],[5.0],[5.5],[6.0]] y=[[1.5],[2.5],[3.1],[4.0],[4.0],[4.5],[4.9],[5.0]] #回帰分析 x_poly = polynomial_features.fit_transform(x) model = LinearRegression() model.fit(x_poly, y) y_poly_pred = model.predict(x_poly) #相関式の切片と回帰係数の出力 print("model.intercept_",model.intercept_) print("model.coef_",model.coef_) print("R2値=", r2_score(y, y_poly_pred) ) #データのプロット plt.scatter(x, y, color='r', s=50) #回帰解析による予測のプロット plt.plot(x, y_poly_pred, 'k') #print(y_poly_pred) plt.xlabel("x") plt.ylabel("y") plt.show()