线性回归

import matplotlib.pyplot as plt
from pandas import DataFrame
from sklearn.linear_model import LinearRegression
import statsmodels.api as sm

# 创建数据集
examDict = {'深度': [20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0],
            '质心': [21.0, 26.1, 31.0, 36.1, 40.9, 46.2, 51.2, 55.9, 61.1, 66.2, 71.2, 76.2, 80.8, 86.4, 90.6]}

#转换为DataFrame的数据格式
examDf = DataFrame(examDict)

# -------------------------------------------------------绘制散点图
plt.scatter(examDf.深度, examDf.质心, color='b', label="strawberry data")

# 添加图的标签(x轴,y轴)
plt.xlabel("bbox_center_depth(cm)")
plt.ylabel("strawberry_distance(cm)")
# 显示图像
plt.show()
# -------------------------------------------------------

X = examDf.深度.values.reshape(-1,1)

y = examDf.质心.values.reshape(-1,1)

reg = LinearRegression()

reg.fit(X, y)

print("The linear model is: Y = {:.5} + {:.5}X".format(reg.intercept_[0], reg.coef_[0][0]))

# -------------------------------------------------------

predictions = reg.predict(X)

plt.figure(figsize=(16, 8))
plt.scatter(
    examDf.深度,
    examDf.质心,
    c='black'
)
plt.plot(
    examDf.深度,
    predictions,
    c='blue',
    linewidth=2
)
plt.xlabel("bbox_center_depth(cm)")
plt.ylabel("strawberry_distance(cm)")
plt.show()

# -------------------------------------------------------

X = examDf.深度
y = examDf.质心

X2 = sm.add_constant(X)
est = sm.OLS(y, X2)
est2 = est.fit()
print(est2.summary())