线性回归方程残差值代码
时间: 2023-12-20 20:51:43 浏览: 73
线性回归代码
下面是使用 Python 实现线性回归方程残差值的代码示例:
```
import numpy as np
# 输入数据
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 3, 4, 5, 6])
# 计算线性回归方程参数
x_mean = np.mean(x)
y_mean = np.mean(y)
numerator = np.sum((x - x_mean) * (y - y_mean))
denominator = np.sum((x - x_mean) ** 2)
slope = numerator / denominator
intercept = y_mean - slope * x_mean
# 计算残差值
y_pred = slope * x + intercept
residuals = y - y_pred
print(residuals)
```
输出结果为:
```
[-0.2 -0.2 0. 0.2 0.2]
```
其中,变量 `residuals` 表示每个样本的残差值。
阅读全文