Related
I'm using the following bit of code to plot two arrays of the same length -
import matplotlib.pyplot as plt
from scipy import stats
from scipy.stats import linregress
G_mag_values = [11.436, 11.341, 11.822, 11.646, 11.924, 12.057, 11.884, 11.805, 12.347, 12.662, 12.362, 12.555, 12.794, 12.819, 12.945, 12.733, 12.789, 12.878, 12.963, 13.094, 13.031, 12.962, 13.018, 12.906, 13.016, 13.088, 13.04, 13.035, 13.094, 13.032, 13.216, 13.062, 13.083, 13.126, 13.101, 13.089, 13.073, 13.182, 13.116, 13.145, 13.235, 13.161, 13.154, 13.383, 13.315, 13.429, 13.461, 13.287, 13.494, 13.459, 13.478, 13.534, 13.536, 13.536, 13.483, 13.544, 13.564, 13.544, 13.608, 13.655, 13.665, 13.668, 13.697, 13.649, 13.742, 13.756, 13.671, 13.701, 13.788, 13.723, 13.697, 13.713, 13.708, 13.765, 13.847, 13.992, 13.706, 13.79, 13.783, 13.844, 13.945, 13.928, 13.936, 13.956, 13.898, 14.059, 13.945, 14.039, 13.999, 14.087, 14.05, 14.083, 14.136, 14.124, 14.189, 14.149, 14.182, 14.281, 14.177, 14.297, 14.268, 14.454, 14.295]
G_cal_values = [-8.553610547563503, -8.085853602272588, -7.98491731861732, -7.852060056526794, -7.550944423333883, -7.569289687795749, -7.547088847268468, -7.544445036682168, -7.480698829329534, -7.184407435874912, -7.382606680295108, -7.2231275160942054, -7.093385973539046, -7.0473097125206685, -6.775012624594927, -6.814667514017907, -6.719898703328146, -6.741699011193633, -6.483121454948265, -6.320533066162438, -6.216044707275117, -6.037365656714626, -6.058593802250578, -6.0203190345840865, -6.036176430437363, -5.817887798572345, -5.838439347527171, -5.864922270102037, -5.755152671040021, -5.7709095683554725, -5.729226240967218, -5.606533007538604, -5.5817719334376905, -5.578993138005095, -5.62616747769538, -5.648413591916503, -5.611676700504294, -5.557722166623976, -5.5584623064502825, -5.425878164810264, -5.582204334985258, -5.529395790688368, -5.560750195967957, -5.433224654816512, -5.4751198268734385, -5.4592032005417215, -5.514591770369543, -5.580278698184566, -5.520695348050357, -5.501615700174841, -5.578645415877418, -5.692203332547151, -5.569497861450115, -5.335209902666812, -5.470963349023013, -5.44265375533589, -5.538541653702721, -5.355732832969871, -5.318164588926453, -5.376154615199398, -5.372133774215322, -5.361689907587619, -5.37608154921679, -5.412657572197508, -5.454613589602333, -5.339384591430104, -5.367511403407703, -5.258069473329993, -5.347580031901464, -4.9905279263992, -5.445096880253789, -5.192885553786512, -5.2983352094538505, -5.3930571447307365, -5.057910469318639, -5.32585105504838, -5.238649399637653, -5.122431894813153, -5.084559296025157, -5.139042420486851, -4.9919273140342915, -5.103619454431522, -5.017946144298159, -4.98136832081144, -5.084355565584671, -5.048634391386494, -4.887073481359435, -5.074683293771264, -5.050703776716202, -5.104772289705188, -4.9597601680524415, -4.971489935988787, -4.895283369236485, -4.9859511256778974, -4.840717539517584, -4.815665714699117, -4.937635861879118, -4.887219819695687, -4.813729758415283, -4.82667464608015, -4.865176481168528, -4.885105289124561, -4.887072278243732]
fig, ax = plt.subplots()
plt.scatter(G_mag_values,G_cal_values)
ax.minorticks_on()
ax.grid(which='major', linestyle='-', linewidth='0.5')
ax.grid(which='minor', linestyle='-', linewidth='0.5')
fig.set_size_inches(10,7)
best_fit_Y_G = []
slope_G, intercept_G, r_value_G, p_value_G, std_err_G = stats.linregress(G_mag_values,G_cal_values)
for value_G in G_mag_values:
best_fit_Y_G.append(intercept_G + slope_G*value_G)
plt.plot(G_mag_values, best_fit_Y_G, 'r', label = 'Best fit')
plt.title('M67 Calibration graph for G filter')
plt.xlabel('Real magnitude')
plt.ylabel('Measured magnitude')
plt.show()
curve_G = np.polyfit(G_mag_values,G_cal_values,1)
print('G filter polyfit line: slope {}; intercept = {}'.format(curve_G[0],curve_G[1]))
print('G filter linregress: slope {}; intercept = {}'.format(slope_G,intercept_G))
When I run this, it prints the values for slope and intercept from the best_fit_Y_G and curve_G, but it doesnt display the plot at all. Where am I going wrong?
I copy/pasted and run your code.
curve_G = np.polyfit(G_mag_values,G_cal_values,1)
That line gives me error. Then I imported numpy as np and problem solved.
output figure
I am using m.connection to estimate variables initial conditions but I am getting 12 warning messages like:
Moreover, the APM file shows:
I am not sure how to solve these messages.
I am following this explanation "If pos1 or pos2 is not None, the associated var must be a GEKKO variable and the position is the (0-indexed) time-discretized index of the variable" to write m.Connection(var1,var2,pos1=None,pos2=None,node1='end',node2='end').
https://gekko.readthedocs.io/en/latest/quick_start.html#connections
Thanks in advance.
from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
import math as math
import pandas as pd
tm1 = [0, 0.0667,0.5,1,4, 22.61667]
mca1 = [5.68, 3.48, 3.24, 3.36, 2.96, 1.96]
tm2 = [0, 0.08333,0.5,1,4.25 , 22.8167]
mca2 = [5.68, 4.20, 4.04, 4.00, 3.76, 2.88]
tm3 = [0,0.08333,0.5,1,4.33 , 22.9500]
mca3 = [5.68, 4.64, 4.52, 4.56, 4.24, 3.72]
tm4 = [0,0.08333,0.5,1,4.0833 , 23.0833]
mca4 =[18.90,15.4,14.3,15.10,13.50, 10.90]
tm5 = [0,0.08333,0.5,1,4.5, 23.2167]
mca5 =[18.90, 15.5, 16.30, 16, 14.70, 13.00]
tm6 = [0,0.08333,0.5,1,4.6667, 23.3333 ]
mca6 = [18.90, 15.8, 11.70,15.5,12, 9.5 ]
df1=pd.DataFrame({'time':tm1,'x1':mca1})
df2=pd.DataFrame({'time':tm2,'x2':mca2})
df3=pd.DataFrame({'time':tm3,'x3':mca3})
df4=pd.DataFrame({'time':tm4,'x4':mca4})
df5=pd.DataFrame({'time':tm5,'x5':mca5})
df6=pd.DataFrame({'time':tm6,'x6':mca6})
df1.set_index('time',inplace=True)
df2.set_index('time',inplace=True)
df3.set_index('time',inplace=True)
df4.set_index('time',inplace=True)
df5.set_index('time',inplace=True)
df6.set_index('time',inplace=True)
#simulation time points
dfx = pd.DataFrame({'time':np.linspace(0,25,101)})
dfx.set_index('time',inplace=True)
#merge dataframes
dfxx=dfx.join(df1,how='outer')
dfxxx=dfxx.join(df2,how='outer')
dfxxxx=dfxxx.join(df3,how='outer')
dfxxxxx=dfxxxx.join(df4,how='outer')
dfxxxxxx=dfxxxxx.join(df5,how='outer')
df=dfxxxxxx.join(df6,how='outer')
# get True (1) or False (0) for measurement
df['meas1']=(df['x1'].values==df['x1'].values).astype(int)
df['meas2']=(df['x2'].values==df['x2'].values).astype(int)
df['meas3']=(df['x3'].values==df['x3'].values).astype(int)
df['meas4']=(df['x4'].values==df['x4'].values).astype(int)
df['meas5']=(df['x5'].values==df['x5'].values).astype(int)
df['meas6']=(df['x6'].values==df['x6'].values).astype(int)
#replace NaN with zeros
df0=df.fillna(value=0)
m = GEKKO()
m.time = df0.index.values
meas1 = m.Param(df0['meas1'].values)
meas2 = m.Param(df0['meas2'].values)
meas3 = m.Param(df0['meas3'].values)
meas4 = m.Param(df0['meas4'].values)
meas5 = m.Param(df0['meas5'].values)
meas6 = m.Param(df0['meas6'].values)
#adjustable Parameters
kf=m.FV(1.3,lb=0.01,ub=10)
ks=m.FV(1.3,lb=0.01,ub=10)
cnf01=m.FV(1.3,lb=0.01,ub=10)
cns01=m.FV(1.3,lb=0.01,ub=10)
#constrains
cnf02=m.FV(value=cnf01*0.5,lb=cnf01*0.5, ub=cnf01*0.5)
cns02=m.FV(value=cns01*0.5,lb=cns01*0.5, ub=cns01*0.5)
cnf03=m.FV(value=cnf01*0.25,lb=cnf01*0.25, ub=cnf01*0.25)
cns03=m.FV(value=cns01*0.25,lb=cns01*0.25, ub=cns01*0.25)
cnf04=m.FV(value=cnf01,lb=cnf01, ub=cnf01)
cns04=m.FV(value=cns01,lb=cns01, ub=cns01)
cnf05=m.FV(value=cnf01*0.5,lb=cnf01*0.5, ub=cnf01*0.5)
cns05=m.FV(value=cns01*0.5,lb=cns01*0.5, ub=cns01*0.5)
cnf06=m.FV(value=cnf01*0.25,lb=cnf01*0.25, ub=cnf01*0.25)
cns06=m.FV(value=cns01*0.25,lb=cns01*0.25, ub=cns01*0.25)
#Variables
c1 = m.Var(value=mca1[0])
c2 = m.Var(value=mca2[0])
c3 = m.Var(value=mca3[0])
c4 = m.Var(value=mca4[0])
c5 = m.Var(value=mca5[0])
c6 = m.Var(value=mca6[0])
cm1 = m.Param(df0['x1'].values)
cm2 = m.Param(df0['x2'].values)
cm3 = m.Param(df0['x3'].values)
cm4 = m.Param(df0['x4'].values)
cm5 = m.Param(df0['x5'].values)
cm6 = m.Param(df0['x6'].values)
m.Minimize((meas1*(c1-cm1)**2)+(meas2*(c2-cm2)**2)\
+(meas3*(c3-cm3)**2)+(meas4*(c4-cm4)**2)\
+(meas5*(c5-cm5)**2)+(meas6*(c6-cm6)**2))
cnf1=m.Var(value=cnf01,fixed_initial=False)
cns1=m.Var(value=cns01,fixed_initial=False)
cnf2=m.Var(value=cnf02,fixed_initial=False)
cns2=m.Var(value=cns02,fixed_initial=False)
cnf3=m.Var(value=cnf03,fixed_initial=False)
cns3=m.Var(value=cns03,fixed_initial=False)
cnf4=m.Var(value=cnf04,fixed_initial=False)
cns4=m.Var(value=cns04,fixed_initial=False)
cnf5=m.Var(value=cnf05,fixed_initial=False)
cns5=m.Var(value=cns05,fixed_initial=False)
cnf6=m.Var(value=cnf06,fixed_initial=False)
cns6=m.Var(value=cns06,fixed_initial=False)
#Equations
t = m.Param(value=m.time)
m.Connection(cnf1,cnf01,pos1=0,pos2=0,node1=1,node2=1)
m.Connection(cnf2,cnf02,pos1=0,pos2=0,node1=1,node2=1)
m.Connection(cnf3,cnf03,pos1=0,pos2=0,node1=1,node2=1)
m.Connection(cnf4,cnf04,pos1=0,pos2=0,node1=1,node2=1)
m.Connection(cnf5,cnf05,pos1=0,pos2=0,node1=1,node2=1)
m.Connection(cnf6,cnf06,pos1=0,pos2=0,node1=1,node2=1)
m.Connection(cns1,cns01,pos1=0,pos2=0,node1=1,node2=1)
m.Connection(cns2,cns02,pos1=0,pos2=0,node1=1,node2=1)
m.Connection(cns3,cns03,pos1=0,pos2=0,node1=1,node2=1)
m.Connection(cns4,cns04,pos1=0,pos2=0,node1=1,node2=1)
m.Connection(cns5,cns05,pos1=0,pos2=0,node1=1,node2=1)
m.Connection(cns6,cns06,pos1=0,pos2=0,node1=1,node2=1)
m.Equation(cnf1.dt()==-kf*c1*cnf1)
m.Equation(cns1.dt()==-ks*c1*cns1)
m.Equation(c1.dt()==cnf1.dt()+cns1.dt())
m.Equation(cnf2.dt()==-kf*c2*cnf2)
m.Equation(cns2.dt()==-ks*c2*cns2)
m.Equation(c2.dt()==cnf2.dt()+cns2.dt())
m.Equation(cnf3.dt()==-kf*c3*cnf3)
m.Equation(cns3.dt()==-ks*c3*cns3)
m.Equation(c3.dt()==cnf3.dt()+cns3.dt())
m.Equation(cnf4.dt()==-kf*c4*cnf4)
m.Equation(cns4.dt()==-ks*c4*cns4)
m.Equation(c4.dt()==cnf4.dt()+cns4.dt())
m.Equation(cnf5.dt()==-kf*c5*cnf5)
m.Equation(cns5.dt()==-ks*c5*cns5)
m.Equation(c5.dt()==cnf5.dt()+cns5.dt())
m.Equation(cnf6.dt()==-kf*c6*cnf6)
m.Equation(cns6.dt()==-ks*c6*cns6)
m.Equation(c6.dt()==cnf6.dt()+cns6.dt())
if True:
kf.STATUS=1
ks.STATUS=1
cnf01.STATUS=1
cns01.STATUS=1
cnf02.STATUS=1
cns02.STATUS=1
cnf03.STATUS=1
cns03.STATUS=1
cnf04.STATUS=1
cns04.STATUS=1
cnf05.STATUS=1
cns05.STATUS=1
cnf06.STATUS=1
cns06.STATUS=1
#Options
m.options.SOLVER = 1 #IPOPT solver
m.options.IMODE = 5 #Dynamic Simultaneous - estimation = MHE
m.options.EV_TYPE = 2 #absolute error
m.options.NODES = 3 #collocation nodes (2,5)
m.solve(disp=True)
m.open_folder()
print('Final SSE Objective: ' + str(m.options.objfcnval))
print('Solution')
print('cnf01 = ' + str(cnf01.value[0]))
print('cns01 = ' + str(cns01.value[0]))
print('kf = ' + str(kf.value[0]))
print('ks = ' + str(ks.value[0]))
print('cns02 = '+ str(cns02.value[0]))
print('cnf02 = '+ str(cnf02.value[0]))
print('cns03 = '+ str(cns03.value[0]))
print('cnf03 = '+ str(cnf03.value[0]))
print('cns04 = '+ str(cns04.value[0]))
print('cnf04 = '+ str(cnf04.value[0]))
print('cns05 = '+ str(cns05.value[0]))
print('cnf05 = '+ str(cnf05.value[0]))
print('cns06 = '+ str(cns06.value[0]))
print('cnf06 = '+ str(cnf06.value[0]))
plt.figure(1,figsize=(8,5))
plt.plot(m.time,c1.value,'r',label='Predicted c1')
plt.plot(m.time,c2.value,'y',label='Predicted c2')
plt.plot(m.time,c3.value,'c',label='Predicted c3')
plt.plot(m.time,c4.value,'g',label='Predicted c4')
plt.plot(m.time,c5.value,'b',label='Predicted c5')
plt.plot(m.time,c6.value,'m',label='Predicted c6')
plt.plot(tm1,mca1,'rx',label='Meas c1')
plt.plot(tm2,mca2,'yx',label='Meas c2')
plt.plot(tm3,mca3,'cx',label='Meas c3')
plt.plot(tm4,mca4,'go',label='Meas c4')
plt.plot(tm5,mca5,'bo',label='Meas c5')
plt.plot(tm6,mca6,'mo',label='Meas c6')
plt.xlabel('time (h)')
plt.ylabel('Concentration (mgCl2/L)')
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), ncol=2)
The underlying node structure has a 1-index instead of a 0-index that is common in Python. Using pos1=1 and pos2=1 resolves the warnings.
m.Connection(cnf1,cnf01,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cnf2,cnf02,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cnf3,cnf03,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cnf4,cnf04,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cnf5,cnf05,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cnf6,cnf06,pos1=1,pos2=1,node1=1,node2=1)
Another issue is that Gekko variables shouldn't generally be used to initialize other values. I recommend setting x0=1.3 and using that float to initialize the variables. Change m.Var() to m.SV() to avoid reclassification of m.Var() as an m.FV() during the connection. The m.SV() is a promoted type of variable that is at the same level of precedence as the m.FV(). Here is a complete script although the results don't look optimal.
from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
import math as math
import pandas as pd
tm1 = [0, 0.0667,0.5,1,4, 22.61667]
mca1 = [5.68, 3.48, 3.24, 3.36, 2.96, 1.96]
tm2 = [0, 0.08333,0.5,1,4.25 , 22.8167]
mca2 = [5.68, 4.20, 4.04, 4.00, 3.76, 2.88]
tm3 = [0,0.08333,0.5,1,4.33 , 22.9500]
mca3 = [5.68, 4.64, 4.52, 4.56, 4.24, 3.72]
tm4 = [0,0.08333,0.5,1,4.0833 , 23.0833]
mca4 =[18.90,15.4,14.3,15.10,13.50, 10.90]
tm5 = [0,0.08333,0.5,1,4.5, 23.2167]
mca5 =[18.90, 15.5, 16.30, 16, 14.70, 13.00]
tm6 = [0,0.08333,0.5,1,4.6667, 23.3333 ]
mca6 = [18.90, 15.8, 11.70,15.5,12, 9.5 ]
df1=pd.DataFrame({'time':tm1,'x1':mca1})
df2=pd.DataFrame({'time':tm2,'x2':mca2})
df3=pd.DataFrame({'time':tm3,'x3':mca3})
df4=pd.DataFrame({'time':tm4,'x4':mca4})
df5=pd.DataFrame({'time':tm5,'x5':mca5})
df6=pd.DataFrame({'time':tm6,'x6':mca6})
df1.set_index('time',inplace=True)
df2.set_index('time',inplace=True)
df3.set_index('time',inplace=True)
df4.set_index('time',inplace=True)
df5.set_index('time',inplace=True)
df6.set_index('time',inplace=True)
#simulation time points
dfx = pd.DataFrame({'time':np.linspace(0,25,101)})
dfx.set_index('time',inplace=True)
#merge dataframes
dfxx=dfx.join(df1,how='outer')
dfxxx=dfxx.join(df2,how='outer')
dfxxxx=dfxxx.join(df3,how='outer')
dfxxxxx=dfxxxx.join(df4,how='outer')
dfxxxxxx=dfxxxxx.join(df5,how='outer')
df=dfxxxxxx.join(df6,how='outer')
# get True (1) or False (0) for measurement
df['meas1']=(df['x1'].values==df['x1'].values).astype(int)
df['meas2']=(df['x2'].values==df['x2'].values).astype(int)
df['meas3']=(df['x3'].values==df['x3'].values).astype(int)
df['meas4']=(df['x4'].values==df['x4'].values).astype(int)
df['meas5']=(df['x5'].values==df['x5'].values).astype(int)
df['meas6']=(df['x6'].values==df['x6'].values).astype(int)
#replace NaN with zeros
df0=df.fillna(value=0)
m = GEKKO()
m.time = df0.index.values
meas1 = m.Param(df0['meas1'].values)
meas2 = m.Param(df0['meas2'].values)
meas3 = m.Param(df0['meas3'].values)
meas4 = m.Param(df0['meas4'].values)
meas5 = m.Param(df0['meas5'].values)
meas6 = m.Param(df0['meas6'].values)
#adjustable Parameters
kf=m.FV(1.3,lb=0.01,ub=10)
ks=m.FV(1.3,lb=0.01,ub=10)
x0 = 1.3
cnf01=m.FV(x0,lb=0.01,ub=10)
cns01=m.FV(x0,lb=0.01,ub=10)
#constrains
cnf02=m.FV(value=x0*0.5,lb=x0*0.5, ub=x0*0.5)
cns02=m.FV(value=x0*0.5,lb=x0*0.5, ub=x0*0.5)
cnf03=m.FV(value=x0*0.25,lb=x0*0.25, ub=x0*0.25)
cns03=m.FV(value=x0*0.25,lb=x0*0.25, ub=x0*0.25)
cnf04=m.FV(value=x0,lb=x0, ub=x0)
cns04=m.FV(value=x0,lb=x0, ub=x0)
cnf05=m.FV(value=x0*0.5,lb=x0*0.5, ub=x0*0.5)
cns05=m.FV(value=x0*0.5,lb=x0*0.5, ub=x0*0.5)
cnf06=m.FV(value=x0*0.25,lb=x0*0.25, ub=x0*0.25)
cns06=m.FV(value=x0*0.25,lb=x0*0.25, ub=x0*0.25)
#Variables
c1 = m.SV(value=mca1[0])
c2 = m.SV(value=mca2[0])
c3 = m.SV(value=mca3[0])
c4 = m.SV(value=mca4[0])
c5 = m.SV(value=mca5[0])
c6 = m.SV(value=mca6[0])
cm1 = m.Param(df0['x1'].values)
cm2 = m.Param(df0['x2'].values)
cm3 = m.Param(df0['x3'].values)
cm4 = m.Param(df0['x4'].values)
cm5 = m.Param(df0['x5'].values)
cm6 = m.Param(df0['x6'].values)
m.Minimize((meas1*(c1-cm1)**2)+(meas2*(c2-cm2)**2)\
+(meas3*(c3-cm3)**2)+(meas4*(c4-cm4)**2)\
+(meas5*(c5-cm5)**2)+(meas6*(c6-cm6)**2))
cnf1=m.SV(value=x0,fixed_initial=False)
cns1=m.SV(value=x0,fixed_initial=False)
cnf2=m.SV(value=x0,fixed_initial=False)
cns2=m.SV(value=x0,fixed_initial=False)
cnf3=m.SV(value=x0,fixed_initial=False)
cns3=m.SV(value=x0,fixed_initial=False)
cnf4=m.SV(value=x0,fixed_initial=False)
cns4=m.SV(value=x0,fixed_initial=False)
cnf5=m.SV(value=x0,fixed_initial=False)
cns5=m.SV(value=x0,fixed_initial=False)
cnf6=m.SV(value=x0,fixed_initial=False)
cns6=m.SV(value=x0,fixed_initial=False)
#Equations
t = m.Param(value=m.time)
m.Connection(cnf1,cnf01,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cnf2,cnf02,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cnf3,cnf03,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cnf4,cnf04,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cnf5,cnf05,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cnf6,cnf06,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cns1,cns01,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cns2,cns02,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cns3,cns03,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cns4,cns04,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cns5,cns05,pos1=1,pos2=1,node1=1,node2=1)
m.Connection(cns6,cns06,pos1=1,pos2=1,node1=1,node2=1)
m.Equation(cnf1.dt()==-kf*c1*cnf1)
m.Equation(cns1.dt()==-ks*c1*cns1)
m.Equation(c1.dt()==cnf1.dt()+cns1.dt())
m.Equation(cnf2.dt()==-kf*c2*cnf2)
m.Equation(cns2.dt()==-ks*c2*cns2)
m.Equation(c2.dt()==cnf2.dt()+cns2.dt())
m.Equation(cnf3.dt()==-kf*c3*cnf3)
m.Equation(cns3.dt()==-ks*c3*cns3)
m.Equation(c3.dt()==cnf3.dt()+cns3.dt())
m.Equation(cnf4.dt()==-kf*c4*cnf4)
m.Equation(cns4.dt()==-ks*c4*cns4)
m.Equation(c4.dt()==cnf4.dt()+cns4.dt())
m.Equation(cnf5.dt()==-kf*c5*cnf5)
m.Equation(cns5.dt()==-ks*c5*cns5)
m.Equation(c5.dt()==cnf5.dt()+cns5.dt())
m.Equation(cnf6.dt()==-kf*c6*cnf6)
m.Equation(cns6.dt()==-ks*c6*cns6)
m.Equation(c6.dt()==cnf6.dt()+cns6.dt())
#Options
m.options.SOLVER = 1 # APOPT solver
m.options.IMODE = 5 # Dynamic Simultaneous - estimation = MHE
m.options.EV_TYPE = 2 # Squared error
m.options.NODES = 3 # Collocation nodes (2,5)
if True:
kf.STATUS=1
ks.STATUS=1
cnf01.STATUS=1
cns01.STATUS=1
cnf02.STATUS=1
cns02.STATUS=1
cnf03.STATUS=1
cns03.STATUS=1
cnf04.STATUS=1
cns04.STATUS=1
cnf05.STATUS=1
cns05.STATUS=1
cnf06.STATUS=1
cns06.STATUS=1
m.options.TIME_SHIFT = 0
try:
m.solve(disp=True)
except:
print("don't stop when not finding cnf01...cnf06")
#m.open_folder()
print('Final SSE Objective: ' + str(m.options.objfcnval))
print('Solution')
print('cnf01 = ' + str(cnf1.value[0]))
print('cns01 = ' + str(cns1.value[0]))
print('kf = ' + str(kf.value[0]))
print('ks = ' + str(ks.value[0]))
print('cns02 = '+ str(cns2.value[0]))
print('cnf02 = '+ str(cnf2.value[0]))
print('cns03 = '+ str(cns3.value[0]))
print('cnf03 = '+ str(cnf3.value[0]))
print('cns04 = '+ str(cns4.value[0]))
print('cnf04 = '+ str(cnf4.value[0]))
print('cns05 = '+ str(cns5.value[0]))
print('cnf05 = '+ str(cnf5.value[0]))
print('cns06 = '+ str(cns6.value[0]))
print('cnf06 = '+ str(cnf6.value[0]))
plt.figure(1,figsize=(8,5))
plt.plot(m.time,c1.value,'r',label='Predicted c1')
plt.plot(m.time,c2.value,'y',label='Predicted c2')
plt.plot(m.time,c3.value,'c',label='Predicted c3')
plt.plot(m.time,c4.value,'g',label='Predicted c4')
plt.plot(m.time,c5.value,'b',label='Predicted c5')
plt.plot(m.time,c6.value,'m',label='Predicted c6')
plt.plot(tm1,mca1,'rx',label='Meas c1')
plt.plot(tm2,mca2,'yx',label='Meas c2')
plt.plot(tm3,mca3,'cx',label='Meas c3')
plt.plot(tm4,mca4,'go',label='Meas c4')
plt.plot(tm5,mca5,'bo',label='Meas c5')
plt.plot(tm6,mca6,'mo',label='Meas c6')
plt.xlabel('time (h)')
plt.ylabel('Concentration (mgCl2/L)')
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), ncol=2)
plt.show()
"""
I'm trying to fit an exponential deacy function to a large gene expression dataset. I've spent lots of hours on stackoverflow and I found something that I'd like to try
Curve fit an exponential decay function in Python using given data points
But I'm getting multiple regression lines and I'm not sure why and how to fix that. Also I really would appreciate if you could suggest a better option or function.
Thank you in advance for helping me out.
"""
smallRNA = [1.4847814317391115,
1.233300788320302,
1.3482469618574953,
1.370572550987182,
1.2942438021815492,
1.8362649665550308,
1.9478952510177636,
1.7449833728367992,
1.133732460554052,
1.3172194208200945,
2.2366329568831316,
1.2651420206147634,
2.0615652798644457,
1.5188246709945887,
1.1466238867527543,
1.3408780599606556,
1.4771209497656745,
1.651149617228606,
1.6860351720116376,
1.2506453147790253,
1.4870430365838434,
1.524647241933158,
1.4547220043517506,
1.7128458331422909,
1.2111590790051767,
1.4833400674312536,
1.5011399997244916,
1.3913217014556971,
1.4161650806831194,
1.6373102731988107,
2.3951290051934726,
1.6848115128561434,
2.9188929470100784,
1.9035779757468991,
1.7306658643903157,
1.698255623968752,
1.2847578416257817,
2.2638689793456934,
1.5930676648572581,
1.679254296180541,
1.2719964106792359,
1.2037024877731124,
1.7079879931344195,
1.6544933181202117,
1.0485948565930532,
1.9978932787991883,
2.1584455179465056,
1.5855391880033107,
1.4131624973162846,
1.0491493504262577,
1.5440379870152185,
0.9634572526320548,
1.334006585966365,
1.7079879931344195,
1.3190420464570736,
1.7181179328838307,
1.6561191123457741,
1.6147186951451824,
1.6720869623098138,
1.6979813431306043,
1.3513988445861689,
1.09654186866835,
2.315102280947673,
1.651724518310352,
1.557770505403525,
1.6749162715346937,
2.830459562006103,
1.6607070287389956,
1.8426125062701317,
1.9938047370848269,
1.512871040315023,
1.4719534246949146,
1.9462952066313157,
1.3476999540804735,
1.552935620670088,
1.6963623896975115,
1.6051862355080382,
2.1400574327564597,
1.6455025236112542,
1.6099862265092482,
1.260871347310878,
1.4181056734587187,
1.602878345784571,
1.2470179140349023,
1.6386413699560323,
1.6153749530357946,
0.5972531564093516,
1.3845760323803993,
1.8707082507565052,
1.9446057842893594,
2.5911426074059265,
1.6216778595798695,
0.8267211148215126,
1.280072164005035,
2.1448277971531833,
1.5136542988762927,
1.3597890830112078,
1.1988223702026333,
1.485048887858052,
1.5852724352684031,
1.9208129532406653,
1.5292685023520693,
2.2402533306161008,
1.5809553088645034,
1.0324008985174453,
1.8372713254640423,
1.4196121857885755,
0.8993753827747732,
1.5813286847624104,
0.5972531564093516,
1.4514089682216447,
1.9534275512344357,
1.1590345756261624,
1.804573660514313,
0.9634572526320548,
1.188872137425364,
0.8796857765384195,
1.0875640778916444,
1.034092960428115,
1.6288585084680207,
2.3897534428254215,
1.75047276418574,
1.9774667460114315,
1.8513017159377254,
1.7340966607967765,
1.645240704520543,
1.5108664977177522,
1.4648163848908131,
2.1410823211778403,
1.7687196791762685,
1.6052523491007507,
2.675390064982142,
1.5070320893546723,
2.2705554362094054,
1.5778785887497413,
1.2719078004338622,
1.8033673563678885,
1.6084038766685629,
1.5577609032739919,
2.626402622679313,
1.368055381284621,
1.5096502028846615,
1.4691415136668815,
1.8571664694047927,
1.4357218027785223,
1.9861933433596186,
1.3559989421473164,
1.6960705653999977,
1.5797554562676404,
2.115884188384194,
1.6867706454133078,
1.526968779225678,
1.4058990171935448,
1.4992705571074072,
1.8738790555045006,
2.234660547275298,
1.849839119767255,
1.6956736498899025,
1.3843844598715376,
1.5522559968891674,
1.5989945747221277,
1.5668369355838003,
1.5051787003315549,
2.1660347974066982,
1.5701051075918644,
1.4417027546447603,
1.678262947477572,
1.8586497010487721,
1.7331317397634975,
2.184363648350726,
1.302107256488202,
1.7992548616068385,
1.7233369992526086,
2.013788196548857,
1.1118655144253595,
1.704118347478623,
1.098330571815151,
1.847087848386142,
1.1941744714226987,
2.223319853488265,
1.4970096850509844,
1.4482165159623221,
2.1289427671857952,
2.0193939201711064,
1.3704162307853769,
1.7787672437633182,
2.8434461654396217,
1.542418719275772,
1.3168345086654032,
1.6394693078981621,
1.7693003851568454,
1.8268796411139967,
1.7639346193000356,
1.3493655386282142,
1.7773817319879697,
0.9922918299081754,
1.2368583317807773,
1.636321610695747,
1.1519565606344542,
1.3065764144587242,
1.7440698169796038,
2.061929554443146,
1.3491116903528981,
1.6063853580404708,
2.10098612376305,
1.198169879301653,
1.4518215531821759,
0.7675283643313485,
0.9611096795514968,
2.124215900612652,
2.559775672893104,
1.7229719764638614,
1.6462651881380896,
1.843684956282479,
1.6311941249724367,
1.8157031826262484,
1.6158347871076075,
1.7408216031833423,
1.9934887015956644,
1.4499191247016987,
1.4534251582590787,
1.8941771885645704,
1.8228838742044629,
1.65265052648444,
1.5987070126638212,
1.7492374674300186,
1.5240334282452728,
1.451787927798003,
2.1244081460368367,
1.3664968183514556,
1.2587077236907418,
0.9634572526320548,
1.2931813736665314,
1.3748856243767726,
1.5491163587347603,
1.4115648465514437,
1.3978420086866388,
1.2332977966159,
1.344852878031716,
1.162517954982421,
1.1517456660514334,
1.716341874802147,
1.1958484823358815,
1.2544892396556067,
1.3063709067507911,
1.3413680728167385,
1.972490647444594,
2.2086387144887407,
1.109882319947273,
1.6147924084093643,
1.6682388199482967,
1.6916983477048313,
1.749454060523717,
1.5310321270003897,
1.5317066167115303,
1.8090237778630935,
1.7305465679927772,
1.3671638088116929,
1.1722510807146163,
1.326669744758649,
1.8474648005518322,
1.495649602488043,
1.7548840396870868,
1.7917594692280552,
1.5090723851424706,
1.3211543639244325,
1.4517146391746436,
1.639557958338597,
1.7943435003173827,
2.4502027948363927,
2.4079079798034706,
1.6700158510497638,
1.8695920379297737,
2.9903512612966665,
1.610484318675355,
1.4769844744372003,
1.2625974419262989,
1.4318932648446774,
2.045787584289522,
1.3535950298155,
1.584629122645107,
1.3492740003805157,
2.187673458149201,
1.4480468601975305,
1.3304008082847716,
2.3867159840087835,
1.4811867967797938,
1.6704033645129963,
1.377201264878756,
1.2299643405653156,
1.717234054622453,
1.2811699579251061,
2.300995121671421,
2.4856644882243546,
1.7744060018328025,
1.2932915858480336,
1.5513201167191744,
1.5011091737147157,
1.8078913880852043,
1.3006275650643129]
geneExpr = [1.3769050211772507,
3.7562462827448257,
2.6719763224779958,
1.1088030594011895,
3.146026602007864,
5.1314962447314505,
2.6605132571854435,
2.286485479964256,
1.9650113466570955,
1.2787138204750903,
2.7636641799765105,
4.5285096091180135,
2.055471332190772,
1.9978097053593409,
2.087677493805377,
0.6863704250688618,
2.0974613358210905,
2.2132254700071208,
2.48323841200138,
1.5039881301377562,
2.065007107942714,
2.049230428240303,
3.1503271044572427,
2.3342037488522647,
1.2879035090495468,
2.4580229092932604,
1.326194618426605,
1.467041043454956,
2.069229739358799,
0.9202774621803934,
1.913175975101608,
1.5289070141065204,
3.9252170136010194,
0.4793125314239097,
0.9274139930893066,
3.0955995956056253,
0.9350589706109779,
1.3667921114846069,
1.2438646855071578,
1.4746053869947549,
1.3828684829828457,
1.1102630744716409,
1.2884669631143617,
1.2019684569402267,
1.768584417233937,
3.3488851200048906,
1.3085841085041314,
1.949837840980232,
1.5458642815467911,
2.8734656865803703,
1.0097062308572082,
2.26417092115662,
0.5494502953108996,
2.852342629706449,
1.1610537731711952,
1.8628414163276428,
2.055877681980133,
2.031529704965473,
1.706727039705061,
5.562953440306615,
4.516805355870563,
1.7852720556314563,
2.1005987032703493,
2.0298194651259567,
4.067765793497416,
1.7752345258038185,
1.9350866537015463,
1.0961439126058516,
1.602554255421435,
2.291438448294199,
1.340880292313372,
2.408586242306745,
2.100697732173863,
0.77438404464467,
2.8981547413635966,
3.1998902982962987,
2.6960014653459727,
1.92074196528459,
1.8060268874721686,
0.7341812462712731,
4.040338021942181,
1.0206096517096142,
1.3877909962108417,
1.6341703208718996,
3.0749360871652254,
1.9089116792175367,
1.9474345491984089,
1.268331974681008,
0.8795809981676224,
1.9144190091281208,
0.1270802397390998,
4.24833595711353,
4.3669910031275565,
1.0416935096825009,
1.1940615718143244,
0.8558026237665811,
1.812046098646976,
1.311673507873615,
3.0093397849673607,
2.3649421461780626,
3.1504408119984464,
1.3703907267761202,
2.035010457899985,
1.97571648922404,
1.508716929296002,
0.9259976145453455,
5.087583944061218,
2.163327839574992,
5.164145580225468,
1.882381183129369,
2.1537323048551884,
1.4641679356399222,
1.2401260403911023,
1.8947336676344437,
1.87430000038156,
1.9324192128382656,
2.3637097605868256,
1.8153183897580145,
2.7034472776356075,
1.2548972291582945,
1.5302245388784623,
1.1170554826855297,
1.5474674616317405,
1.4778135582987868,
1.5177027250537067,
1.3100328840517919,
1.2022761604083556,
2.061201470946425,
1.4032400860384922,
1.6048491382684908,
1.213512354892811,
1.84219352998238,
0.7936446864683406,
2.2700301363565782,
3.003996024096597,
3.660411491521197,
2.195575305005744,
1.1541602033596847,
0.7881284979664975,
2.6849886985602893,
0.7206015440349863,
2.7252722206622386,
1.6010500579439904,
1.3923463784019967,
1.8243640168449449,
2.7880079433128633,
1.0659521525480418,
0.8876191860288731,
1.2036185828499717,
1.5163663935683656,
1.8751375205086147,
1.6593808016539568,
1.3450629536711138,
1.743046254509446,
2.383365679891565,
1.7202874287730594,
1.2505241744841202,
1.0648829013065944,
1.676612144448407,
2.5646208023284705,
2.9383803354532816,
0.6636096277744107,
1.980524826088373,
1.88716841838094,
1.4573506929257631,
2.8157783254949216,
2.3132066108617453,
3.5849957091245392,
1.3104222176649503,
5.005869186797619,
1.656583830703468,
1.4758563116669652,
3.1590106444683967,
1.5082266816326058,
1.3998383532758802,
1.84885674137052,
2.09998977154838,
3.78907586873952,
0.4662883394312476,
2.5388739765778756,
0.7611096254627837,
0.9830374176395882,
2.7322374977290544,
0.9792087840489858,
3.675105498156061,
0.8825732342313036,
3.1960685882274653,
5.259124190596204,
1.204476090855313,
1.3351614013975992,
1.3109417110191364,
2.100469817786245,
2.0151382362984953,
1.361158569885664,
1.0179610833986736,
0.8346346836719615,
1.9797759350043793,
3.2861656450393952,
2.334863099051601,
1.7086361720456942,
3.3297249235714377,
2.7703363951836004,
1.0897853295849105,
1.6964377929291818,
2.6302524565571352,
2.3217444361354165,
0.9778636256720471,
1.1641320302129619,
3.669759881539616,
1.8989776903183184,
1.8451729468670635,
2.284817884485841,
1.4322665246452273,
1.8028063251468571,
6.424313801576621,
1.3658464843728737,
3.6036740034305152,
2.4287401275948666,
1.9307836296113483,
2.3056947098759233,
1.7764027636098791,
1.1657155836935535,
3.0758095175369498,
2.1186188694505184,
1.0635003471332336,
0.6507112000963948,
1.7708163474637342,
2.2882337233824046,
1.3158185947256855,
1.8132723096072454,
1.6137762247932652,
1.108713242644792,
2.563727794452838,
2.6229335504139892,
1.63571914984831,
1.8453736442669983,
2.989346077449386,
0.6276060667250072,
1.0609944423522246,
2.15349991293981,
2.3320143470152193,
1.5118060325992106,
2.835346822718986,
1.5022536697497582,
0.8256556265785171,
2.7321833634584944,
2.526064328111412,
1.466756407403721,
1.7215191254561708,
1.0388635252424339,
1.715325294157353,
1.2076875587847855,
1.2362371642474348,
0.33394812674227087,
4.675284054378683,
2.389760294726976,
4.792628954075339,
2.1024724402368418,
0.8976139347165087,
0.6952074669183057,
2.709776326779585,
1.1764754694078634,
1.0073671761913532,
1.1187112550934273,
1.2764159114560854,
2.608374090493331,
2.27112424293213,
1.4332125797844384,
3.5283190803536235,
2.334990667936763,
2.285800451959592,
1.1186112799702084,
2.0399703968427336,
1.4570913440289304,
0.7014837257279548,
1.7852613553987886,
1.959023435165816,
1.5829877972968962,
1.4426312620833972,
1.496764100430386,
4.933175199545022,
4.060964038202633,
3.0709086261971392,
2.490130727772556,
3.964732991745056,
1.950304860608104,
3.60356269649804,
3.0678005050984707,
1.5710675762000597,
1.7144908152637672,
0.687838463709859,
4.599006382272776,
2.2675849365369922,
1.3590435271624708,
1.148793683412492,
2.2624825972759024,
0.9497618693732038,
1.6619770001458867,
1.53102207591445,
5.821619725812528]
df_sample = pd.DataFrame(list(zip(smallRNA,geneExpr)),columns=['smallRNA','geneExpr'])
This is the function
def func(t, a, tau, c):
return a * np.exp(-t / tau) + c
Applying the function to my data
t = np.array(df_sample['smallRNA'])
y = np.array(df_sample['geneExpr'])
t_norm = (t - t[0])/(t[-1] - t[0]) # normalized
c_0 = y[-1]
tau_0 = 1
a_0 = (y[0] - y[-1])
popt, pcov = curve_fit(func, t_norm, y, p0=(a_0, tau_0, c_0))
a, tau, c = popt
y_fit = func(t_norm, a, tau, c)
plt.plot(t, y, 'b.')
plt.plot(t, y_fit, 'r-')
plt.show()
You just need to sort your data:
t = np.array(df_sample['smallRNA'])
y = np.array(df_sample['geneExpr'])
t_norm = (t - t[0])/(t[-1] - t[0]) # normalized
c_0 = y[-1]
tau_0 = 1
a_0 = (y[0] - y[-1])
popt, pcov = curve_fit(func, t_norm, y, p0=(a_0, tau_0, c_0))
a, tau, c = popt
y_fit = func(t_norm, a, tau, c)
plt.plot(t_norm, y, 'b.')
idx = np.argsort(t_norm) # sort the data
plt.plot(t_norm[idx], y_fit[idx], 'r-') # plot sorted fit
plt.show()
Although it does not seem like there is any structure there...
How can I plot two or more overlapping Gaussian surfaces in the same graph, as below?
This is the code I have written, But the first surface is being covered by the second one. They are overlapping , But i want them to be displayed transparently
result obtained: https://i.stack.imgur.com/5LSsW.png
code :https://pastebin.com/embed_iframe/ms8cngXm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def loaddata(filename,label):
file = open(filename, 'r')
text = file.read()
text=text.split('\n')
file.close()
dataset = list()
for line in text:
if len(line)>0:
value = line.split()
dataset.append([float(value[0]), float(value[1]), label])
return dataset
def multivariate_gaussian(pos, mu, Sigma):
n = mu.shape[0]
Sigma_det = np.linalg.det(Sigma)
Sigma_inv = np.linalg.inv(Sigma)
N = np.sqrt((2*np.pi)**n * Sigma_det)
fac = np.einsum('...k,kl,...l->...', pos-mu, Sigma_inv, pos-mu)
return np.exp(-fac / 2) / N
#this is just for maptype ignore this
_viridis_data = [[0.267004, 0.004874, 0.329415],
[0.268510, 0.009605, 0.335427],
[0.269944, 0.014625, 0.341379],
[0.271305, 0.019942, 0.347269],
[0.272594, 0.025563, 0.353093],
[0.273809, 0.031497, 0.358853],
[0.274952, 0.037752, 0.364543],
[0.276022, 0.044167, 0.370164],
[0.277018, 0.050344, 0.375715],
[0.277941, 0.056324, 0.381191],
[0.278791, 0.062145, 0.386592],
[0.279566, 0.067836, 0.391917],
[0.280267, 0.073417, 0.397163],
[0.280894, 0.078907, 0.402329],
[0.281446, 0.084320, 0.407414],
[0.281924, 0.089666, 0.412415],
[0.282327, 0.094955, 0.417331],
[0.282656, 0.100196, 0.422160],
[0.282910, 0.105393, 0.426902],
[0.283091, 0.110553, 0.431554],
[0.283197, 0.115680, 0.436115],
[0.283229, 0.120777, 0.440584],
[0.283187, 0.125848, 0.444960],
[0.283072, 0.130895, 0.449241],
[0.282884, 0.135920, 0.453427],
[0.282623, 0.140926, 0.457517],
[0.282290, 0.145912, 0.461510],
[0.281887, 0.150881, 0.465405],
[0.281412, 0.155834, 0.469201],
[0.280868, 0.160771, 0.472899],
[0.280255, 0.165693, 0.476498],
[0.279574, 0.170599, 0.479997],
[0.278826, 0.175490, 0.483397],
[0.278012, 0.180367, 0.486697],
[0.277134, 0.185228, 0.489898],
[0.276194, 0.190074, 0.493001],
[0.275191, 0.194905, 0.496005],
[0.274128, 0.199721, 0.498911],
[0.273006, 0.204520, 0.501721],
[0.271828, 0.209303, 0.504434],
[0.270595, 0.214069, 0.507052],
[0.269308, 0.218818, 0.509577],
[0.267968, 0.223549, 0.512008],
[0.266580, 0.228262, 0.514349],
[0.265145, 0.232956, 0.516599],
[0.263663, 0.237631, 0.518762],
[0.262138, 0.242286, 0.520837],
[0.260571, 0.246922, 0.522828],
[0.258965, 0.251537, 0.524736],
[0.257322, 0.256130, 0.526563],
[0.255645, 0.260703, 0.528312],
[0.253935, 0.265254, 0.529983],
[0.252194, 0.269783, 0.531579],
[0.250425, 0.274290, 0.533103],
[0.248629, 0.278775, 0.534556],
[0.246811, 0.283237, 0.535941],
[0.244972, 0.287675, 0.537260],
[0.243113, 0.292092, 0.538516],
[0.241237, 0.296485, 0.539709],
[0.239346, 0.300855, 0.540844],
[0.237441, 0.305202, 0.541921],
[0.235526, 0.309527, 0.542944],
[0.233603, 0.313828, 0.543914],
[0.231674, 0.318106, 0.544834],
[0.229739, 0.322361, 0.545706],
[0.227802, 0.326594, 0.546532],
[0.225863, 0.330805, 0.547314],
[0.223925, 0.334994, 0.548053],
[0.221989, 0.339161, 0.548752],
[0.220057, 0.343307, 0.549413],
[0.218130, 0.347432, 0.550038],
[0.216210, 0.351535, 0.550627],
[0.214298, 0.355619, 0.551184],
[0.212395, 0.359683, 0.551710],
[0.210503, 0.363727, 0.552206],
[0.208623, 0.367752, 0.552675],
[0.206756, 0.371758, 0.553117],
[0.204903, 0.375746, 0.553533],
[0.203063, 0.379716, 0.553925],
[0.201239, 0.383670, 0.554294],
[0.199430, 0.387607, 0.554642],
[0.197636, 0.391528, 0.554969],
[0.195860, 0.395433, 0.555276],
[0.194100, 0.399323, 0.555565],
[0.192357, 0.403199, 0.555836],
[0.190631, 0.407061, 0.556089],
[0.188923, 0.410910, 0.556326],
[0.187231, 0.414746, 0.556547],
[0.185556, 0.418570, 0.556753],
[0.183898, 0.422383, 0.556944],
[0.182256, 0.426184, 0.557120],
[0.180629, 0.429975, 0.557282],
[0.179019, 0.433756, 0.557430],
[0.177423, 0.437527, 0.557565],
[0.175841, 0.441290, 0.557685],
[0.174274, 0.445044, 0.557792],
[0.172719, 0.448791, 0.557885],
[0.171176, 0.452530, 0.557965],
[0.169646, 0.456262, 0.558030],
[0.168126, 0.459988, 0.558082],
[0.166617, 0.463708, 0.558119],
[0.165117, 0.467423, 0.558141],
[0.163625, 0.471133, 0.558148],
[0.162142, 0.474838, 0.558140],
[0.160665, 0.478540, 0.558115],
[0.159194, 0.482237, 0.558073],
[0.157729, 0.485932, 0.558013],
[0.156270, 0.489624, 0.557936],
[0.154815, 0.493313, 0.557840],
[0.153364, 0.497000, 0.557724],
[0.151918, 0.500685, 0.557587],
[0.150476, 0.504369, 0.557430],
[0.149039, 0.508051, 0.557250],
[0.147607, 0.511733, 0.557049],
[0.146180, 0.515413, 0.556823],
[0.144759, 0.519093, 0.556572],
[0.143343, 0.522773, 0.556295],
[0.141935, 0.526453, 0.555991],
[0.140536, 0.530132, 0.555659],
[0.139147, 0.533812, 0.555298],
[0.137770, 0.537492, 0.554906],
[0.136408, 0.541173, 0.554483],
[0.135066, 0.544853, 0.554029],
[0.133743, 0.548535, 0.553541],
[0.132444, 0.552216, 0.553018],
[0.131172, 0.555899, 0.552459],
[0.129933, 0.559582, 0.551864],
[0.128729, 0.563265, 0.551229],
[0.127568, 0.566949, 0.550556],
[0.126453, 0.570633, 0.549841],
[0.125394, 0.574318, 0.549086],
[0.124395, 0.578002, 0.548287],
[0.123463, 0.581687, 0.547445],
[0.122606, 0.585371, 0.546557],
[0.121831, 0.589055, 0.545623],
[0.121148, 0.592739, 0.544641],
[0.120565, 0.596422, 0.543611],
[0.120092, 0.600104, 0.542530],
[0.119738, 0.603785, 0.541400],
[0.119512, 0.607464, 0.540218],
[0.119423, 0.611141, 0.538982],
[0.119483, 0.614817, 0.537692],
[0.119699, 0.618490, 0.536347],
[0.120081, 0.622161, 0.534946],
[0.120638, 0.625828, 0.533488],
[0.121380, 0.629492, 0.531973],
[0.122312, 0.633153, 0.530398],
[0.123444, 0.636809, 0.528763],
[0.124780, 0.640461, 0.527068],
[0.126326, 0.644107, 0.525311],
[0.128087, 0.647749, 0.523491],
[0.130067, 0.651384, 0.521608],
[0.132268, 0.655014, 0.519661],
[0.134692, 0.658636, 0.517649],
[0.137339, 0.662252, 0.515571],
[0.140210, 0.665859, 0.513427],
[0.143303, 0.669459, 0.511215],
[0.146616, 0.673050, 0.508936],
[0.150148, 0.676631, 0.506589],
[0.153894, 0.680203, 0.504172],
[0.157851, 0.683765, 0.501686],
[0.162016, 0.687316, 0.499129],
[0.166383, 0.690856, 0.496502],
[0.170948, 0.694384, 0.493803],
[0.175707, 0.697900, 0.491033],
[0.180653, 0.701402, 0.488189],
[0.185783, 0.704891, 0.485273],
[0.191090, 0.708366, 0.482284],
[0.196571, 0.711827, 0.479221],
[0.202219, 0.715272, 0.476084],
[0.208030, 0.718701, 0.472873],
[0.214000, 0.722114, 0.469588],
[0.220124, 0.725509, 0.466226],
[0.226397, 0.728888, 0.462789],
[0.232815, 0.732247, 0.459277],
[0.239374, 0.735588, 0.455688],
[0.246070, 0.738910, 0.452024],
[0.252899, 0.742211, 0.448284],
[0.259857, 0.745492, 0.444467],
[0.266941, 0.748751, 0.440573],
[0.274149, 0.751988, 0.436601],
[0.281477, 0.755203, 0.432552],
[0.288921, 0.758394, 0.428426],
[0.296479, 0.761561, 0.424223],
[0.304148, 0.764704, 0.419943],
[0.311925, 0.767822, 0.415586],
[0.319809, 0.770914, 0.411152],
[0.327796, 0.773980, 0.406640],
[0.335885, 0.777018, 0.402049],
[0.344074, 0.780029, 0.397381],
[0.352360, 0.783011, 0.392636],
[0.360741, 0.785964, 0.387814],
[0.369214, 0.788888, 0.382914],
[0.377779, 0.791781, 0.377939],
[0.386433, 0.794644, 0.372886],
[0.395174, 0.797475, 0.367757],
[0.404001, 0.800275, 0.362552],
[0.412913, 0.803041, 0.357269],
[0.421908, 0.805774, 0.351910],
[0.430983, 0.808473, 0.346476],
[0.440137, 0.811138, 0.340967],
[0.449368, 0.813768, 0.335384],
[0.458674, 0.816363, 0.329727],
[0.468053, 0.818921, 0.323998],
[0.477504, 0.821444, 0.318195],
[0.487026, 0.823929, 0.312321],
[0.496615, 0.826376, 0.306377],
[0.506271, 0.828786, 0.300362],
[0.515992, 0.831158, 0.294279],
[0.525776, 0.833491, 0.288127],
[0.535621, 0.835785, 0.281908],
[0.545524, 0.838039, 0.275626],
[0.555484, 0.840254, 0.269281],
[0.565498, 0.842430, 0.262877],
[0.575563, 0.844566, 0.256415],
[0.585678, 0.846661, 0.249897],
[0.595839, 0.848717, 0.243329],
[0.606045, 0.850733, 0.236712],
[0.616293, 0.852709, 0.230052],
[0.626579, 0.854645, 0.223353],
[0.636902, 0.856542, 0.216620],
[0.647257, 0.858400, 0.209861],
[0.657642, 0.860219, 0.203082],
[0.668054, 0.861999, 0.196293],
[0.678489, 0.863742, 0.189503],
[0.688944, 0.865448, 0.182725],
[0.699415, 0.867117, 0.175971],
[0.709898, 0.868751, 0.169257],
[0.720391, 0.870350, 0.162603],
[0.730889, 0.871916, 0.156029],
[0.741388, 0.873449, 0.149561],
[0.751884, 0.874951, 0.143228],
[0.762373, 0.876424, 0.137064],
[0.772852, 0.877868, 0.131109],
[0.783315, 0.879285, 0.125405],
[0.793760, 0.880678, 0.120005],
[0.804182, 0.882046, 0.114965],
[0.814576, 0.883393, 0.110347],
[0.824940, 0.884720, 0.106217],
[0.835270, 0.886029, 0.102646],
[0.845561, 0.887322, 0.099702],
[0.855810, 0.888601, 0.097452],
[0.866013, 0.889868, 0.095953],
[0.876168, 0.891125, 0.095250],
[0.886271, 0.892374, 0.095374],
[0.896320, 0.893616, 0.096335],
[0.906311, 0.894855, 0.098125],
[0.916242, 0.896091, 0.100717],
[0.926106, 0.897330, 0.104071],
[0.935904, 0.898570, 0.108131],
[0.945636, 0.899815, 0.112838],
[0.955300, 0.901065, 0.118128],
[0.964894, 0.902323, 0.123941],
[0.974417, 0.903590, 0.130215],
[0.983868, 0.904867, 0.136897],
[0.993248, 0.906157, 0.143936]]
from matplotlib.colors import ListedColormap
viridis = ListedColormap(_viridis_data, name='viridis')
plt.register_cmap(name='viridis', cmap=viridis)
plt.set_cmap(viridis)
filename=r"C:/Users/santhoskumar/Desktop/random/pattern/class1_rw.txt"
label=0
dataset1= loaddata(filename,label)
print('Loaded data file {0} with {1} rows'.format(filename, len(dataset1)))
filename = r"C:/Users/santhoskumar/Desktop/random/pattern/class2_rw.txt"
label=1
dataset2 = loaddata(filename,label)
print('Loaded data file {0} with {1} rows'.format(filename, len(dataset2)))
filename = r'C:/Users/santhoskumar/Desktop/random/pattern/class3_rw.txt'
label=2
dataset3 = loaddata(filename,label)
print('Loaded data file {0} with {1} rows'.format(filename, len(dataset3)))
N = 600
X = np.linspace(200, 800, N)
Y = np.linspace(300, 1200, N)
X, Y = np.meshgrid(X, Y)
dataset=np.array(dataset1)
x,y,label=dataset.T
dat=x,y
dat=np.array(dat)
cov=np.cov(dat)
mu=np.mean(dat,axis=1)
print(mu)
# Pack X and Y into a single 3-dimensional array
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y
# The distribution on the variables X, Y packed into pos.
Z = multivariate_gaussian(pos, mu, cov)
minn=1e-15
for i in range(len(Z)):
for j in range(len(Z[i])):
Z[i][j]*=1e4
fig = plt.figure()
ax = fig.gca(projection='3d')
ax1=fig.gca(projection='3d')
ax.plot_surface(X, Y, Z,rstride=30,cstride=30, linewidth=1,antialiased=True,cmap=viridis)
cset = ax.contourf(X, Y, Z,zdir='z',offset=-0.4,cmap=viridis)
ax.set_zlim(-0.4,0.40)
ax.set_zticks(np.linspace(0,0.40,5))
ax.view_init(27, -21)
dataset=np.array(dataset2)
x,y,label=dataset.T
dat=x,y
dat=np.array(dat)
cov=np.cov(dat)
mu=np.mean(dat,axis=1)
print(mu)
# Pack X and Y into a single 3-dimensional array
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y
# The distribution on the variables X, Y packed into pos.
Z = multivariate_gaussian(pos, mu, cov)
minn=1e-15
for i in range(len(Z)):
for j in range(len(Z[i])):
Z[i][j]*=1e4
ax.plot_surface(X, Y, Z,rstride=20,cstride=20, linewidth=1,antialiased=True,color='red',cmap=viridis)
cset1 = ax.contourf(X, Y, Z,zdir='z',offset=-0.4,cmap=viridis)
ax.set_zlim(-0.4,0.40)
ax.set_zticks(np.linspace(0,0.40,5))
ax.view_init(27, -21)
plt.subplots_adjust(hspace=0.5)
plt.show()
If I understand your question correctly, you just have to call the plotting method multiple times such as:
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(x1, y1, z1,cmap='viridis',linewidth=0)
ax.plot_surface(x2, y2, z2,cmap='viridis',linewidth=0)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.show()
I am having a problem with the guess function of lmfit. I am trying to fit some experimental data and I want to use different built in models of lmfit, but I cannot run the built in modules, only if I define the function directly.
The following code does not work, but if I comment the guess function it works.
P.S. It would be more interesting for me that the index is the first column because I will put this in a loop that will use all the same first column of the data and therefore i could put each new second column of the data as a new column in the DataFrame.
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from lmfit import Model
from lmfit.models import GaussianModel
minvalue = 3.25
maxvalue = 3.45
rawdata = pd.read_csv('datafile.txt', delim_whitespace = True, names=['XX','YY'])
#Section the data
data = rawdata[(rawdata['XX']>minvalue) & (rawdata['XX'] < maxvalue)]
#Create a DataFrame with the data
dataDataframe = pd.DataFrame()
dataDataframe[0] = data['YY']
dataDataframe = dataDataframe.set_index(data['XX'])
# Gaussian curve
def gaussian(x, amp, cen, wid):
"1-d gaussian: gaussian(x, amp, cen, wid)"
return (amp/(np.sqrt(2*np.pi)*wid)) * np.exp(-(x-cen)**2 /(2*wid**2))
result_gaussian = Model(gaussian).fit(dataDataframe[0], x=dataDataframe.index.values, amp=5, cen=5, wid=1)
mod = GaussianModel()
pars = mod.guess(dataDataframe[0], x = np.float32(dataDataframe.index.values))
out = mod.fit(dataDataframe[0], pars , x = np.float32(dataDataframe.index.values))
plt.plot(dataDataframe.index.values, dataDataframe[0],'bo')
plt.plot(dataDataframe.index.values, result_gaussian.best_fit, 'r-', label = 'Gaussian')
plt.plot(dataDataframe.index.values, out.best_fit, 'b-', label = 'Gaussian2')
plt.legend()
plt.show()
Error message I am having if I uncomment the built in modules:
File "/Users/johndoe/anaconda2/lib/python2.7/site-packages/lmfit/models.py", line 52, in guess_from_peak
cen = x[imaxy]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
I have tried to run the guess_from_peak from models.py and i did not have a problem it resulted in an integer.
Raw data:
1.1661899e+000 7.3414581e+002
1.1730889e+000 7.4060590e+002
1.1799880e+000 7.3778076e+002
1.1868871e+000 7.2950366e+002
1.1937861e+000 7.0154932e+002
1.2006853e+000 7.0399518e+002
1.2075844e+000 7.3814081e+002
1.2144834e+000 7.5750049e+002
1.2213825e+000 7.6613043e+002
1.2282816e+000 7.4348322e+002
1.2351807e+000 7.2836584e+002
1.2420797e+000 7.0964618e+002
1.2489789e+000 7.1938611e+002
1.2558780e+000 7.0620062e+002
1.2627770e+000 7.2354883e+002
1.2696761e+000 7.1347961e+002
1.2765752e+000 7.1027679e+002
1.2834742e+000 7.4422925e+002
1.2903733e+000 7.5596112e+002
1.2972724e+000 7.2770599e+002
1.3041714e+000 7.2000342e+002
1.3110706e+000 7.4451556e+002
1.3179697e+000 7.4411346e+002
1.3248687e+000 6.9408307e+002
1.3317678e+000 6.8662170e+002
1.3386669e+000 7.0951758e+002
1.3455659e+000 6.7616663e+002
1.3524650e+000 6.7230786e+002
1.3593642e+000 7.1053870e+002
1.3662632e+000 7.2593860e+002
1.3731623e+000 7.1484381e+002
1.3800614e+000 7.3073920e+002
1.3869605e+000 7.2766406e+002
1.3938595e+000 7.1958862e+002
1.4007586e+000 7.0147577e+002
1.4076577e+000 6.9747528e+002
1.4145567e+000 6.9634515e+002
1.4214559e+000 6.6082648e+002
1.4283550e+000 6.4877466e+002
1.4352540e+000 6.6942896e+002
1.4421531e+000 6.8172211e+002
1.4490522e+000 6.5540350e+002
1.4559512e+000 6.4846545e+002
1.4628503e+000 6.6383038e+002
1.4697495e+000 6.4449670e+002
1.4766484e+000 6.3950043e+002
1.4835476e+000 6.4479529e+002
1.4904467e+000 6.4849249e+002
1.4973457e+000 6.4100800e+002
1.5042448e+000 6.6731049e+002
1.5111439e+000 6.8118671e+002
1.5180429e+000 6.5618878e+002
1.5249420e+000 6.3446680e+002
1.5318412e+000 6.3301892e+002
1.5387402e+000 6.5466571e+002
1.5456393e+000 6.5982983e+002
1.5525384e+000 6.3588879e+002
1.5594375e+000 6.1257922e+002
1.5663365e+000 6.2805811e+002
1.5732356e+000 6.1877094e+002
1.5801347e+000 6.0427368e+002
1.5870337e+000 6.3391718e+002
1.5939329e+000 6.4173145e+002
1.6008320e+000 6.2423242e+002
1.6077310e+000 6.0993829e+002
1.6146301e+000 6.0605164e+002
1.6215292e+000 6.2812646e+002
1.6284282e+000 6.4028595e+002
1.6353273e+000 6.2281421e+002
1.6422265e+000 6.0742285e+002
1.6491255e+000 5.9783905e+002
1.6560246e+000 5.8637256e+002
1.6629237e+000 6.0021320e+002
1.6698227e+000 6.1169287e+002
1.6767218e+000 6.1003906e+002
1.6836209e+000 5.9548285e+002
1.6905199e+000 5.8961163e+002
1.6974190e+000 5.9599597e+002
1.7043182e+000 5.9016595e+002
1.7112173e+000 5.7669794e+002
1.7181163e+000 5.6394800e+002
1.7250154e+000 5.5043781e+002
1.7319145e+000 5.6813892e+002
1.7388135e+000 5.8987500e+002
1.7457126e+000 5.9018683e+002
1.7526118e+000 5.8595575e+002
1.7595108e+000 5.8304041e+002
1.7664099e+000 5.9360785e+002
1.7733090e+000 5.9706018e+002
1.7802080e+000 5.7838733e+002
1.7871071e+000 5.7011194e+002
1.7940062e+000 5.8080725e+002
1.8009052e+000 5.7853046e+002
1.8078043e+000 5.7998969e+002
1.8147035e+000 5.4928967e+002
1.8216025e+000 5.2888440e+002
1.8285016e+000 5.4854303e+002
1.8354007e+000 5.5585767e+002
1.8422997e+000 5.5588806e+002
1.8491988e+000 5.5359229e+002
1.8560979e+000 5.5033203e+002
1.8629971e+000 5.2563916e+002
1.8698961e+000 5.3607788e+002
1.8767952e+000 5.7113812e+002
1.8836943e+000 5.5775525e+002
1.8905933e+000 5.2081384e+002
1.8974924e+000 5.1039877e+002
1.9043915e+000 5.3863855e+002
1.9112905e+000 5.6284332e+002
1.9181896e+000 5.5691626e+002
1.9250888e+000 5.3292615e+002
1.9319878e+000 5.4550836e+002
1.9388869e+000 5.6732916e+002
1.9457860e+000 5.4372571e+002
1.9526850e+000 5.1244263e+002
1.9595841e+000 5.1212933e+002
1.9664832e+000 5.1553162e+002
1.9733822e+000 5.2064484e+002
1.9802814e+000 5.3102246e+002
1.9871805e+000 5.2069739e+002
1.9940795e+000 5.0833780e+002
2.0009787e+000 5.1853204e+002
2.0078776e+000 5.2843738e+002
2.0147767e+000 5.2046942e+002
2.0216758e+000 5.4993433e+002
2.0285749e+000 5.4103894e+002
2.0354741e+000 5.0149301e+002
2.0423732e+000 5.0521149e+002
2.0492721e+000 5.2875800e+002
2.0561712e+000 5.1962280e+002
2.0630703e+000 4.9481357e+002
2.0699694e+000 4.9459094e+002
2.0768685e+000 4.9837778e+002
2.0837677e+000 4.9990302e+002
2.0906668e+000 4.9616635e+002
2.0975657e+000 4.9398682e+002
2.1044648e+000 4.9411301e+002
2.1113639e+000 5.0085464e+002
2.1182630e+000 5.1741498e+002
2.1251621e+000 5.1049081e+002
2.1320612e+000 4.9854333e+002
2.1389601e+000 4.9250342e+002
2.1458592e+000 4.8195938e+002
2.1527584e+000 4.9623288e+002
2.1596575e+000 5.0226831e+002
2.1665566e+000 5.1108215e+002
2.1734557e+000 5.0001602e+002
2.1803546e+000 4.8078720e+002
2.1872537e+000 4.9371985e+002
2.1941528e+000 4.9578796e+002
2.2010520e+000 5.0061276e+002
2.2079511e+000 4.9850949e+002
2.2148502e+000 4.9680969e+002
2.2217491e+000 5.0683179e+002
2.2286482e+000 5.0175012e+002
2.2355473e+000 4.8996030e+002
2.2424464e+000 4.8759747e+002
2.2493455e+000 4.7695905e+002
2.2562447e+000 4.7682187e+002
2.2631438e+000 4.8609653e+002
2.2700427e+000 4.8575693e+002
2.2769418e+000 4.9476901e+002
2.2838409e+000 4.8241449e+002
2.2907400e+000 4.7581494e+002
2.2976391e+000 5.0079959e+002
2.3045382e+000 5.0975296e+002
2.3114371e+000 4.9256650e+002
2.3183362e+000 4.8954599e+002
2.3252354e+000 4.9478619e+002
2.3321345e+000 5.1234747e+002
2.3390336e+000 5.4276178e+002
2.3459327e+000 5.4188184e+002
2.3528316e+000 5.4555566e+002
2.3597307e+000 5.4856274e+002
2.3666298e+000 5.2246918e+002
2.3735290e+000 4.9281882e+002
2.3804281e+000 4.8422125e+002
2.3873272e+000 5.0562274e+002
2.3942261e+000 5.0024243e+002
2.4011252e+000 4.8827591e+002
2.4080243e+000 4.8137762e+002
2.4149234e+000 4.7244000e+002
2.4218225e+000 4.7699164e+002
2.4287217e+000 4.7515668e+002
2.4356208e+000 4.6413528e+002
2.4425197e+000 4.6328885e+002
2.4494188e+000 4.6013199e+002
2.4563179e+000 4.6177853e+002
2.4632170e+000 4.5766202e+002
2.4701161e+000 4.4741263e+002
2.4770153e+000 4.4859024e+002
2.4839141e+000 4.6913116e+002
2.4908133e+000 5.0019971e+002
2.4977124e+000 4.8486560e+002
2.5046115e+000 4.6070554e+002
2.5115106e+000 4.3163672e+002
2.5184097e+000 4.4147137e+002
2.5253086e+000 4.3510056e+002
2.5322077e+000 4.4211298e+002
2.5391068e+000 4.6599957e+002
2.5460060e+000 4.5878577e+002
2.5529051e+000 4.4981293e+002
2.5598042e+000 4.6061084e+002
2.5667033e+000 4.6963638e+002
2.5736022e+000 4.7663760e+002
2.5805013e+000 4.6380307e+002
2.5874004e+000 4.5866577e+002
2.5942996e+000 4.5507098e+002
2.6011987e+000 4.4790939e+002
2.6080978e+000 4.6447559e+002
2.6149967e+000 4.5061194e+002
2.6218958e+000 4.2355850e+002
2.6287949e+000 4.2002722e+002
2.6356940e+000 4.2429697e+002
2.6425931e+000 4.2280334e+002
2.6494923e+000 4.3304733e+002
2.6563911e+000 4.5999661e+002
2.6632903e+000 4.7144125e+002
2.6701894e+000 4.6819211e+002
2.6770885e+000 4.6265125e+002
2.6839876e+000 4.6332251e+002
2.6908867e+000 4.5123907e+002
2.6977856e+000 4.6259286e+002
2.7046847e+000 4.6975299e+002
2.7115839e+000 4.4647833e+002
2.7184830e+000 4.4722562e+002
2.7253821e+000 4.6617062e+002
2.7322812e+000 4.6656949e+002
2.7391803e+000 4.4081876e+002
2.7460792e+000 4.5200452e+002
2.7529783e+000 4.5094382e+002
2.7598774e+000 4.4421115e+002
2.7667766e+000 4.5470145e+002
2.7736757e+000 4.5202261e+002
2.7805748e+000 4.4788058e+002
2.7874737e+000 4.3493640e+002
2.7943728e+000 4.4102286e+002
2.8012719e+000 4.3156961e+002
2.8081710e+000 4.2983533e+002
2.8150702e+000 4.4627554e+002
2.8219693e+000 4.4581104e+002
2.8288682e+000 4.2150226e+002
2.8357673e+000 4.1737479e+002
2.8426664e+000 4.5602731e+002
2.8495655e+000 4.6227423e+002
2.8564646e+000 4.5953806e+002
2.8633637e+000 4.5829834e+002
2.8702629e+000 4.5450616e+002
2.8771617e+000 4.5531360e+002
2.8840609e+000 4.4464761e+002
2.8909600e+000 4.6128970e+002
2.8978591e+000 4.4664514e+002
2.9047582e+000 4.4719708e+002
2.9116573e+000 4.4492749e+002
2.9185562e+000 4.4260013e+002
2.9254553e+000 4.5593594e+002
2.9323545e+000 4.6237164e+002
2.9392536e+000 4.7034845e+002
2.9461527e+000 4.7368185e+002
2.9530518e+000 4.7302234e+002
2.9599507e+000 4.7327332e+002
2.9668498e+000 4.4960791e+002
2.9737489e+000 4.4319986e+002
2.9806480e+000 4.5416092e+002
2.9875472e+000 4.6674429e+002
2.9944463e+000 4.6089871e+002
3.0013452e+000 4.6334650e+002
3.0082443e+000 4.6833719e+002
3.0151434e+000 4.8842966e+002
3.0220425e+000 4.8455182e+002
3.0289416e+000 4.6504678e+002
3.0358407e+000 4.6673508e+002
3.0427399e+000 4.6887064e+002
3.0496387e+000 4.6799823e+002
3.0565379e+000 4.5299500e+002
3.0634370e+000 4.5381485e+002
3.0703361e+000 4.5956931e+002
3.0772352e+000 4.6477676e+002
3.0841343e+000 4.6114374e+002
3.0910332e+000 4.6816293e+002
3.0979323e+000 4.6245181e+002
3.1048315e+000 4.6533044e+002
3.1117306e+000 4.7819165e+002
3.1186297e+000 4.9699246e+002
3.1255288e+000 4.8907956e+002
3.1324277e+000 4.9116394e+002
3.1393268e+000 5.0308936e+002
3.1462259e+000 5.0668982e+002
3.1531250e+000 5.0537222e+002
3.1600242e+000 4.9574966e+002
3.1669233e+000 4.9894128e+002
3.1738222e+000 4.9885315e+002
3.1807213e+000 5.1417163e+002
3.1876204e+000 5.2202740e+002
3.1945195e+000 5.2219598e+002
3.2014186e+000 5.4433679e+002
3.2083178e+000 5.6957477e+002
3.2152169e+000 5.9891089e+002
3.2221158e+000 6.0682019e+002
3.2290149e+000 6.0779541e+002
3.2359140e+000 6.1212280e+002
3.2428131e+000 6.5589185e+002
3.2497122e+000 7.1807507e+002
3.2566113e+000 7.5950916e+002
3.2635102e+000 8.1842242e+002
3.2704093e+000 9.1277783e+002
3.2773085e+000 1.0486207e+003
3.2842076e+000 1.3214080e+003
3.2911067e+000 1.7085295e+003
3.2980058e+000 2.4946370e+003
3.3049047e+000 4.1229609e+003
3.3118038e+000 7.1944038e+003
3.3187029e+000 1.1714122e+004
3.3256021e+000 1.5338923e+004
3.3325012e+000 1.5092694e+004
3.3394003e+000 1.1227008e+004
3.3462994e+000 6.9070176e+003
3.3531983e+000 4.0318586e+003
3.3600974e+000 2.5069387e+003
3.3669965e+000 1.7313556e+003
3.3738956e+000 1.3203175e+003
3.3807948e+000 1.0810967e+003
3.3876939e+000 9.2702356e+002
3.3945928e+000 8.2453217e+002
3.4014919e+000 7.5468195e+002
3.4083910e+000 7.1011224e+002
3.4152901e+000 6.7312701e+002
3.4221892e+000 6.2927734e+002
3.4290884e+000 6.0679126e+002
3.4359872e+000 5.8445929e+002
3.4428864e+000 5.5084033e+002
3.4497855e+000 5.2990625e+002
3.4566846e+000 5.3244171e+002
3.4635837e+000 5.3299860e+002
3.4704828e+000 5.2270801e+002
3.4773817e+000 5.0838147e+002
3.4842808e+000 4.9768036e+002
3.4911799e+000 4.9974271e+002
3.4980791e+000 5.1852539e+002
3.5049782e+000 5.2486890e+002
3.5118773e+000 5.3554919e+002
3.5187764e+000 5.4363098e+002
3.5256753e+000 5.2134320e+002
3.5325744e+000 4.9386557e+002
3.5394735e+000 4.7175720e+002
3.5463727e+000 4.6334061e+002
3.5532718e+000 4.4633063e+002
3.5601709e+000 4.4021204e+002
3.5670698e+000 4.4216010e+002
3.5739689e+000 4.3208749e+002
3.5808680e+000 4.3210999e+002
3.5877671e+000 4.3717999e+002
3.5946662e+000 4.3084845e+002
3.6015654e+000 4.1379028e+002
3.6084642e+000 4.1567856e+002
3.6153634e+000 4.2414615e+002
3.6222625e+000 4.2964746e+002
3.6291616e+000 4.1986203e+002
3.6360607e+000 4.0300714e+002
3.6429598e+000 4.1156561e+002
3.6498590e+000 4.1897156e+002
3.6567578e+000 4.1506668e+002
3.6636569e+000 4.2337305e+002
3.6705561e+000 4.2956845e+002
3.6774552e+000 4.1608209e+002
3.6843543e+000 4.1159943e+002
3.6912534e+000 4.0408707e+002
3.6981523e+000 3.8742813e+002
3.7050514e+000 3.8193686e+002
3.7119505e+000 3.8675006e+002
3.7188497e+000 3.8995547e+002
3.7257488e+000 3.9189124e+002
3.7326479e+000 3.9534134e+002
3.7395468e+000 4.0249893e+002
3.7464459e+000 4.0382443e+002
3.7533450e+000 3.9881796e+002
3.7602441e+000 4.0283856e+002
3.7671432e+000 4.0544543e+002
3.7740424e+000 3.9527063e+002
3.7809412e+000 3.9659631e+002
3.7878404e+000 4.0054132e+002
3.7947395e+000 3.9123737e+002
3.8016386e+000 3.8058502e+002
3.8085377e+000 3.7388980e+002
3.8154368e+000 3.7337103e+002
3.8223360e+000 3.6008588e+002
3.8292348e+000 3.5135416e+002
3.8361340e+000 3.5958188e+002
3.8430331e+000 3.5756583e+002
3.8499322e+000 3.5956232e+002
3.8568313e+000 3.7803802e+002
3.8637304e+000 3.9012396e+002
3.8706293e+000 3.8674255e+002
3.8775284e+000 3.7771600e+002
3.8844275e+000 3.7648160e+002
3.8913267e+000 3.7692780e+002
3.8982258e+000 3.6927103e+002
3.9051249e+000 3.7007745e+002
3.9120238e+000 3.7482629e+002
3.9189229e+000 3.7230219e+002
3.9258220e+000 3.6110025e+002
3.9327211e+000 3.6490872e+002
3.9396203e+000 3.7283734e+002
3.9465194e+000 3.7933209e+002
3.9534183e+000 3.6968182e+002
3.9603174e+000 3.5532330e+002
3.9672165e+000 3.5889478e+002
3.9741156e+000 3.6407483e+002
3.9810147e+000 3.6295535e+002
3.9879138e+000 3.6387720e+002
3.9948130e+000 3.6416183e+002
4.0017118e+000 3.6089911e+002
4.0086112e+000 3.6826599e+002
4.0155101e+000 3.7570581e+002
4.0224090e+000 3.6361679e+002
4.0293083e+000 3.6003177e+002
4.0362072e+000 3.7528265e+002
4.0431066e+000 3.7368362e+002
4.0500054e+000 3.8174683e+002
4.0569048e+000 4.0386084e+002
4.0638037e+000 4.2738324e+002
4.0707026e+000 4.4587668e+002
4.0776019e+000 4.5433987e+002
4.0845008e+000 4.4404083e+002
4.0914001e+000 4.2589066e+002
4.0982990e+000 3.9662262e+002
4.1051979e+000 3.7311325e+002
4.1120973e+000 3.5790594e+002
4.1189961e+000 3.4554794e+002
4.1258955e+000 3.5435367e+002
4.1327944e+000 3.7766489e+002
4.1396937e+000 3.7425708e+002
4.1465926e+000 3.5805182e+002
4.1534915e+000 3.5078519e+002
4.1603909e+000 3.5888739e+002
4.1672897e+000 3.7242688e+002
4.1741891e+000 3.7792575e+002
4.1810880e+000 3.7338031e+002
4.1879873e+000 3.6538324e+002
4.1948862e+000 3.5872525e+002
4.2017851e+000 3.4688391e+002
4.2086844e+000 3.4881918e+002
4.2155833e+000 3.4818274e+002
4.2224827e+000 3.4055273e+002
4.2293816e+000 3.3977536e+002
4.2362804e+000 3.3322891e+002
4.2431798e+000 3.3594962e+002
4.2500787e+000 3.4658536e+002
4.2569780e+000 3.4479083e+002
4.2638769e+000 3.4267456e+002
4.2707763e+000 3.4828876e+002
4.2776752e+000 3.4845041e+002
4.2845740e+000 3.3986469e+002
4.2914734e+000 3.3093433e+002
4.2983723e+000 3.3255331e+002
4.3052716e+000 3.4089511e+002
4.3121705e+000 3.4742932e+002
4.3190699e+000 3.3570422e+002
4.3259687e+000 3.2636673e+002
4.3328676e+000 3.3228806e+002
4.3397670e+000 3.5141977e+002
4.3466659e+000 3.5683167e+002
4.3535652e+000 3.4719943e+002
4.3604641e+000 3.4054718e+002
4.3673630e+000 3.2842471e+002
4.3742623e+000 3.2503146e+002
4.3811612e+000 3.3431540e+002
4.3880606e+000 3.3462808e+002
4.3949594e+000 3.3529224e+002
4.4018588e+000 3.3313510e+002
4.4087577e+000 3.4015598e+002
4.4156566e+000 3.3703552e+002
4.4225559e+000 3.3024448e+002
4.4294548e+000 3.2974786e+002
As I suggested in the comment above, coercing the pandas Series into an ndarray will fix the problem:
mod = GaussianModel()
ydata = np.array(dataDataframe[0])
xdata = np.array(dataDataframe.index.values)
pars = mod.guess(ydata, x=xdata)
out = mod.fit(ydata, pars, x=xdata)
This example works for me:
#!/usr/bin/env python
from lmfit.models import LorentzianModel
import matplotlib.pyplot as plt
import pandas as pd
dframe = pd.read_csv('peak.csv')
model = LorentzianModel()
params = model.guess(dframe['y'], x=dframe['x'])
result = model.fit(dframe['y'], params, x=dframe['x'])
print(result.fit_report())
result.plot_fit()
plt.show()
with peaks.csv of
x,y
0.000000, 0.021654
0.200000, 0.385367
0.400000, 0.193304
0.600000, 0.103481
0.800000, 0.404041
1.000000, 0.212585
1.200000, 0.253212
1.400000, -0.037306
1.600000, 0.271415
1.800000, 0.025614
2.000000, 0.066419
2.200000, -0.034347
2.400000, 0.153702
2.600000, 0.161341
2.800000, -0.097676
3.000000, -0.061880
3.200000, 0.085341
3.400000, 0.083674
3.600000, 0.190944
3.800000, 0.222168
4.000000, 0.214417
4.200000, 0.341221
4.400000, 0.634501
4.600000, 0.302566
4.800000, 0.101096
5.000000, -0.106441
5.200000, 0.567396
5.400000, 0.531899
5.600000, 0.459800
5.800000, 0.646655
6.000000, 0.662228
6.200000, 0.820844
6.400000, 0.947696
6.600000, 1.541353
6.800000, 1.763981
7.000000, 1.846081
7.200000, 2.986333
7.400000, 3.182907
7.600000, 3.786487
7.800000, 4.822287
8.000000, 5.739122
8.200000, 6.744448
8.400000, 7.295213
8.600000, 8.737766
8.800000, 9.693782
9.000000, 9.894218
9.200000, 10.193956
9.400000, 10.091519
9.600000, 9.652392
9.800000, 8.670938
10.000000, 8.004205
10.200000, 6.773599
10.400000, 6.076502
10.600000, 5.127315
10.800000, 4.303762
11.000000, 3.426006
11.200000, 2.416431
11.400000, 2.311363
11.600000, 1.748020
11.800000, 1.135594
12.000000, 0.888514
12.200000, 1.030794
12.400000, 0.543024
12.600000, 0.767751
12.800000, 0.657551
13.000000, 0.495730
13.200000, 0.447520
13.400000, 0.173839
13.600000, 0.256758
13.800000, 0.596106
14.000000, 0.065328
14.200000, 0.197267
14.400000, 0.260038
14.600000, 0.460880
14.800000, 0.335248
15.000000, 0.295977
15.200000, -0.010228
15.400000, 0.138670
15.600000, 0.192113
15.800000, 0.304371
16.000000, 0.442517
16.200000, 0.164944
16.400000, 0.001907
16.600000, 0.207504
16.800000, 0.012640
17.000000, 0.090878
17.200000, -0.222967
17.400000, 0.391717
17.600000, 0.180295
17.800000, 0.206875
18.000000, 0.240595
18.200000, -0.037437
18.400000, 0.139918
18.600000, 0.012560
18.800000, -0.053009
19.000000, 0.226069
19.200000, 0.076879
19.400000, 0.078599
19.600000, 0.016125
19.800000, -0.071217
20.000000, -0.091474