
原始数据
| x | y |
|---|---|
| 15 | 330 |
| 20 | 345 |
| 25 | 365 |
| 30 | 405 |
| 35 | 445 |
| 40 | 450 |
| 45 | 455 |
# -*- coding: utf-8 -*-"""Created on Fri Sep 24 10:39:51 2021@author: Machi"""import pandas as pdimport matplotlib.pyplot as pltimport statsmodels.formula.api as smf#read filedf = pd.read_csv('2.csv')print(df)#Draw a scatter diagramplt.figure(figsize=(8, 6))plt.scatter(df['x'], df['y'],label='data')plt.legend(loc='best')#The univariate linear regression model is establishedresult = smf.ols('y~x',data=df).fit()print(result.params)print(result.summary())#Draw a fitting diagram.y_fitted = result.fittedvaluesa = plt.subplots(figsize=(10,10))a.plot(df['x'], y_fitted, 'r-',label='OLS')a.plot(df['x'], df['y'], 'o', label='data')a.legend(loc='best')plt.show() 效果图
运行结果