python - 温度传感器线性组合曲线拟合

标签 python pandas curve-fitting

我正在尝试对不同的温度传感器进行线性组合,并使用应变传感器对其进行曲线化。

我所做的是可以安装一个温度传感器和一个应变传感器。

但我不知道如何在一个应变传感器上对不同温度传感器进行线性组合。

这是我的尝试:

def process_data_curve_fitting(temperature, strain):

   #mean_T = (temperature[[i for i in temperature.columns.tolist() if str(i)[:2] == 'TW']].mean(axis=1))
   print("process data")

   T1 = temperature['T1'].tolist()
   T2 = temperature['T2'].tolist()
   T3 = temperature['T3'].tolist()
   T4 = temperature['T4'].tolist()
   T5 = temperature['T5'].tolist()
   T6 = temperature['T6'].tolist()
   T7 = temperature['T7'].tolist()
   T8 = temperature['T8'].tolist()
   T9 = temperature['T9'].tolist()
   T10 = temperature['T10'].tolist()

   df = pd.DataFrame(list(zip(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)))
   mean_T = df.mean(axis = 1)

   print(mean_T)
   Sensor_Names = [ 'W_A1', 'W_A2', 'W_F1', 'W_F2', 'W_F4', 'W_S1', 'W_S2', 'W_S3', 'W_S4', 'W_KF1', 'W_KF2', 'W_KF3', 'W_KF4', 'W_DB1', 'W_DB2']
   ys = []
   for i in range(len(strain)):
       cof = np.polyfit(mean_T, strain[i], 2)
       poly = np.polyval(cof, mean_T)
       ys.append(poly)
       print (cof)
       print (poly)

   for i in range(len(strain)):
       fig = plt.figure()
       plt.scatter(mean_T, strain[i],s=0.1)
      # fig.savefig(r'c:\\ahmed\\'+Sensor_Names[i]+'.png')
       plt.plot(mean_T, ys[i], color='r')
       fig.savefig(r'c:\\ahmed\\'+"Curve_fitting__" + Sensor_Names[i]+'.png',dpi=300)

       plt.ylabel('strain' + Sensor_Names[i])
       plt.xlabel('temperature')

请看一下等式 enter image description here

最佳答案

作为两个温度传感器的“概念验证” (这里既不添加噪音也不考虑实际参数):

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import leastsq

def strain( t, a, b, c, d ):
    return a * t**3 + b * t**2 + c * t + d

def residuals( params, x1Data, x2Data, yData ):
    s1, s2, a, b, c, d = params
    cxData = [ (s1**2 * x1 + s2**2 * x2) /( s1**2 + s2**2 ) for x1, x2 in zip( x1Data, x2Data) ]
    diff = [ strain( x, a, b, c, d ) -y for x, y in zip( cxData, yData ) ]
    return diff

timeList = np.linspace( 0, 25, 55 )
t1List = np.fromiter( (  5 + 25. * (1 - np.exp( -t / 9. ) )for t in timeList ), np.float )
t2List = np.fromiter( (30. * (1 - np.exp( -t / 7. ) ) * ( 1 - np.exp( -t / 3. ) ) for t in timeList ), np.float )

combinedList = np.fromiter( ( (.7 * a + .2 * b)/.9 for a, b in zip( t1List, t2List ) ), np.float )
strainList = np.fromiter( ( strain( t, .01, -.1, .88, .2 ) for t in combinedList  ), np.float )

fit, ier = leastsq( residuals, [.71,.22, 0,0, .1, .1 ], args=( t1List, t2List, strainList ), maxfev=5000 )
print fit 
fittedT = [ (fit[0]**2 * x1 + fit[1]**2 *x2 ) /( fit[0]**2 + fit[1]**2 ) for x1, x2 in zip( t1List, t2List) ]
fittedS = [ strain( t, *(fit[2:]) ) for t in fittedT ]

fig = plt.figure()
ax = fig.add_subplot( 3, 1, 1 )
bx = fig.add_subplot( 3, 1, 2 )
cx = fig.add_subplot( 3, 1, 3 )
ax.plot( timeList, t1List )
ax.plot( timeList, t2List )
ax.plot( timeList, combinedList )
bx.plot( combinedList, strainList, linestyle='', marker='x' )
bx.plot( fittedT, fittedS )
cx.plot( timeList, fittedT ,'--')
cx.plot( timeList, combinedList,':' )
plt.show()

给予

[ 4.21350842e+03  2.25221499e+03  1.00000000e-02 -1.00000000e-01 8.80000000e-01  2.00000000e-01]

并显示:

fit

顶部:温度 1(蓝色)和 2(橙色)以及线性组合(绿色) 中心:“模拟数据”(蓝色)和拟合(橙色) 底部:拟合温度(蓝色)、真实温度(橙色)

根据实际数据,可能需要一些调整。

关于python - 温度传感器线性组合曲线拟合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52364406/

相关文章:

python - 如何使用套接字从服务器向客户端发送消息

python - Pandas - 添加平均值、最大值、最小值作为数据框中的列

python-3.x - pandas df : add column if doesn't exist, 将值添加到字典中的新列

matlab - 在matlab中拟合二维曲线

python - unicodedata.normalize(form, unistr) 是如何工作的?

python - 在移动 pygame 时移动 Sprite

python - 如何获取平衡括号组的内容

python-3.x - 将 string.capwords 与 Pandas 列一起使用

python - Curve.fit优化错误: 'Covariance of the parameters could not be estimated

matlab - 将曲线拟合到 Matlab 中具有特定颜色的区域