通過Python的Scikit-Learn庫可以輕松搭建一進制線性回歸模型。
#1.繪制散點圖:
import matplotlib.pyplot as plt
x=[[1],[2],[4],[5]]
y=[2,4,6,8]
plt.scatter(x,y)
plt.show()

#2.引入Scikit-Learn庫搭模組化型
from sklearn.linear_model import LinearRegression
regr=LinearRegression()
regr.fit(x,y)
#3.模型預測
#模型預測,假設自變量為1.5
y_pre=regr.predict([[1.5]])
y_pre
Out[69]: array([2.9])
#同時預測多個變量
y_pre2=regr.predict([[1.5],[2.5],[4.5]])
y_pre2
Out[72]: array([2.9, 4.3, 7.1])
#4.模型可視化
還可以将搭建好的模型以可視化的形式展示出來,代碼如下。
plt.scatter(x,y)
plt.plot(x,regr.predict(x))
plt.show()
繪制效果如下圖所示,此時的一進制線性回歸模型就是