python实现拟合数据
线性拟合
import numpy as np
# 假设的数据点
adc_values = np.array([2520, 2477, 2410, 2362, 2308, 2248, 2197, 2136, 2080, 2027, 1973])
current_values = np.array([10.0, 8.0, 6.0, 4.0, 2.0, 0.0, -2.0, -4.0, -6.0, -8.0, -10.0])
# 使用numpy.polyfit进行线性拟合,其中1表示拟合的是一次多项式,即线性拟合
coefficients = np.polyfit(adc_values, current_values, 1)
# 打印拟合得到的斜率和截距
print(f'斜率(k): {coefficients[0]}')
print(f'截距(b): {coefficients[1]}')