使用python进行一元线性函数拟合(包含程序及运行结果图) |
您所在的位置:网站首页 › 如何用excel拟合一次函数 › 使用python进行一元线性函数拟合(包含程序及运行结果图) |
使用python进行一元线性函数拟合
一元线性回归主要是使用最小二乘法计算得到斜率与截距的 计算后的公式: y = a x + b y = ax+b y=ax+b 其中: a = ∑ i = 1 n ( x i − x ‾ ) ( y i − y ‾ ) ∑ i = 1 n ( x i − x ‾ ) 2 a=\frac {\sum_{i=1}^n(x^i-\overline{x})(y^i-\overline{y})}{\sum_{i=1}^n(x^i-\overline{x})^2} a=∑i=1n(xi−x)2∑i=1n(xi−x)(yi−y) b = y ‾ − a x ‾ b= \overline{y} - a\overline{x} b=y−ax 示例程序: import matplotlib.pyplot as plt import numpy as np x = np.array([1, 2, 4, 6, 8]) y = np.array([2, 5, 7, 8, 9]) x_mean = np.mean(x) # x的均值 y_mean = np.mean(y) # y的均值 denominator = 0.0 # 分母 numerator = 0.0 # 分子 for x_i, y_i in zip(x, y): numerator += (x_i - x_mean) * (y_i - y_mean) denominator += (x_i - x_mean) ** 2 a = numerator / denominator # 求得a,斜率 b = y_mean - a * x_mean # 求得b,截距 x_test = np.linspace(0, 10, 100) y_predict = a * x_test + b plt.scatter(x, y, color='b') plt.plot(x_test, y_predict, color='r') plt.xlabel('管子的长度', fontproperties='simHei', fontsize=15) plt.ylabel('收费', fontproperties='simHei', fontsize=15) plt.show()运行结果 当输入一个新的测试数据的时候,我们就能通过 y _ p r e d i c t = a × x _ t e s t + b y\_predict = a\times x\_test +b y_predict=a×x_test+b得到预测值 x_test = 7 y_predict_value = a * x_test + b print(y_predict_value)输出为:8.74390243902439 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |