![f2381a938f3a3db300a9b2560c8685cf.png](https://img-blog.csdnimg.cn/img_convert/f2381a938f3a3db300a9b2560c8685cf.png)
背景资料
随着海拔高度的上升,温度越来越低,经过气象专家的研究,在一定的海拔高度范围内,高度和温度呈线性关系。现有一组实测资料,我们需要对这些数据进行处理拟合,获得此线性关系。
解决思路
采用sklearn库中的LinearRegression线性回归类进行拟合。
代码
# 导入所需的模块
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# 已有数据
height = [0.0, 500.0, 1000.0, 1500.0, 2000.0, 2500.0, 3000.0, 3500.0, 4000.0]
temperature = [12.834044009405147, 10.190648986884316, 5.50022874963469, 2.8546651452636795, -0.7064882183657739, -4.065322810462405, -7.1274795772446575, -10.058878545913904, -13.206465051538661]
# 数据处理
# sklearn 拟合输入输出一般都是二维数组,这里将一维转换为二维。
height = np.array(height).reshape(-1, 1)
temp = np.array(temperatu