最小二乘法python代码
时间: 2023-10-12 11:18:48 浏览: 56
python最小二乘法代码
Sure, here is an example of Python code for implementing the least squares method:
```python
import numpy as np
# Generate some test data
x = np.arange(0, 10, 0.1)
y = 2 * x + 1 + np.random.randn(len(x))
# Calculate the coefficients (slope and intercept) using the least squares method
X = np.vstack([x, np.ones(len(x))]).T
m, c = np.linalg.lstsq(X, y, rcond=None)[0]
# Print the coefficients
print("Slope: %.2f, Intercept: %.2f" % (m, c))
```
Hope this helps!
阅读全文