title: Solving coefficients of data set using curve_fit from scipy.optimize
body: <p>I have an array <code>A</code> exported from excel, containing data values as shown. 1st column <code>x</code> and 2nd column <code>y</code> are dependent variables, while 3rd column <code>z</code> are independent variables (the output). </p>
  
  <pre><code>from xlrd import open_workbook
  
  Data = open_workbook("simple.xls")
  sheet = Data.sheet_by_name('Sheet1')
  
  A=[]
  
  # Read row by row
  for rownum in range(sheet.nrows):
      rowValues = sheet.row_values(rownum)
      A.append(rowValues)
  
  A = np.array(A)
  
  A=
  [[  0.00000000e+00   1.49761692e-05   0.00000000e+00]
   [  8.85000000e+02   1.49761692e-05   6.41362500e-02]
   [  1.48500000e+03   1.49761692e-05   1.19340000e-01]
   [  2.09000000e+03   1.49761692e-05   1.58760000e-01]
   [  3.36000000e+03   1.49761692e-05   2.08080000e-01]
   [  3.87000000e+03   1.49761692e-05   2.16933750e-01]
   [  6.48000000e+03   1.49761692e-05   2.46746250e-01]
   [  8.22000000e+03   1.49761692e-05   2.54700000e-01]
   [  1.05300000e+04   1.49761692e-05   2.59470000e-01]
   [  1.58250000e+04   1.49761692e-05   2.62035000e-01]
   [  2.37600000e+04   1.49761692e-05   2.68751250e-01]
   [  8.18400000e+04   1.49761692e-05   2.92848750e-01]
   [  0.00000000e+00   8.57250668e-06   0.00000000e+00]
   [  6.75000000e+02   8.57250668e-06   4.97436412e-02]
   [  1.27500000e+03   8.57250668e-06   1.27749375e-01]
   [  1.88000000e+03   8.57250668e-06   1.88617039e-01]
   [  3.15000000e+03   8.57250668e-06   2.65089780e-01]
   [  3.66000000e+03   8.57250668e-06   2.90344849e-01]
   [  6.27000000e+03   8.57250668e-06   3.36295316e-01]
   [  8.01000000e+03   8.57250668e-06   3.42702439e-01]
   [  1.03200000e+04   8.57250668e-06   3.65205982e-01]
   [  1.56150000e+04   8.57250668e-06   3.67269626e-01]
   [  2.35500000e+04   8.57250668e-06   3.87296798e-01]
   [  8.16300000e+04   8.57250668e-06   4.43486869e-01]
   [  0.00000000e+00   4.26671486e-06   0.00000000e+00]
   [  4.65000000e+02   4.26671486e-06   2.61407250e-02]
   [  1.06500000e+03   4.26671486e-06   1.22371762e-01]
   [  1.67000000e+03   4.26671486e-06   2.19629475e-01]
   [  2.94000000e+03   4.26671486e-06   3.26680087e-01]
   [  3.45000000e+03   4.26671486e-06   3.34340662e-01]
   [  6.06000000e+03   4.26671486e-06   4.18330575e-01]
   [  7.80000000e+03   4.26671486e-06   4.50631350e-01]
   [  1.01100000e+04   4.26671486e-06   4.55053950e-01]
   [  1.54050000e+04   4.26671486e-06   4.60937587e-01]
   [  2.33400000e+04   4.26671486e-06   5.10770813e-01]
   [  8.14200000e+04   4.26671486e-06   6.12569587e-01]
   [  0.00000000e+00   2.13335743e-06   0.00000000e+00]
   [  8.55000000e+02   2.13335743e-06   1.03773150e-01]
   [  1.46000000e+03   2.13335743e-06   2.21130000e-01]
   [  2.73000000e+03   2.13335743e-06   3.45515625e-01]
   [  3.24000000e+03   2.13335743e-06   3.85634925e-01]
   [  5.85000000e+03   2.13335743e-06   4.76061300e-01]
   [  7.59000000e+03   2.13335743e-06   4.79220300e-01]
   [  1.51950000e+04   2.13335743e-06   5.24709900e-01]
   [  2.31300000e+04   2.13335743e-06   5.64829200e-01]
   [  8.12100000e+04   2.13335743e-06   6.46568325e-01]
   [  0.00000000e+00   1.42359023e-06   0.00000000e+00]
   [  6.45000000e+02   1.42359023e-06   8.03596500e-02]
   [  1.25000000e+03   1.42359023e-06   2.36700000e-01]
   [  2.52000000e+03   1.42359023e-06   4.25941650e-01]
   [  3.03000000e+03   1.42359023e-06   4.61683350e-01]
   [  5.64000000e+03   1.42359023e-06   5.99561100e-01]
   [  7.38000000e+03   1.42359023e-06   6.05952000e-01]
   [  9.69000000e+03   1.42359023e-06   6.16958550e-01]
   [  1.49850000e+04   1.42359023e-06   6.57434250e-01]
   [  2.29200000e+04   1.42359023e-06   6.45954300e-01]
   [  8.10000000e+04   1.42359023e-06   7.79689800e-01]
   [  0.00000000e+00   9.36010573e-07   0.00000000e+00]
   [  4.35000000e+02   9.36010573e-07   3.40200000e-02]
   [  1.04000000e+03   9.36010573e-07   1.91160000e-01]
   [  2.31000000e+03   9.36010573e-07   3.77640000e-01]
   [  2.82000000e+03   9.36010573e-07   4.44240000e-01]
   [  5.43000000e+03   9.36010573e-07   5.50440000e-01]
   [  7.17000000e+03   9.36010573e-07   5.36580000e-01]
   [  9.48000000e+03   9.36010573e-07   5.83740000e-01]
   [  1.47750000e+04   9.36010573e-07   5.87340000e-01]
   [  2.27100000e+04   9.36010573e-07   6.33060000e-01]
   [  8.07900000e+04   9.36010573e-07   7.36200000e-01]]
  
  x= A[:,0]
  y= A[:,1]
  z= A[:,2]
  </code></pre>
  
  <p>I have a function that would fit into the data from array A in order to solve for coefficients <code>a</code> and <code>b</code>.</p>
  
  <pre><code>def func(data,a,b):
      return a/(data[:,1]*b)*np.log(1+(data[:,1]*b/a)*(1-np.exp(-a*data[:,0]))) 
  </code></pre>
  
  <p>The rest of the code shows the initial guess of the coefficients <code>a</code> and <code>b</code>, the <code>scipy.optimize.curve_fit()</code> function, and <code>matplotlib.pyplot</code> to plot the result.</p>
  
  <pre><code>guess = [3.0e-5, 128 ]  
  
  print guess, 'initial guessed parameters' 
  
  params, pcov = scipy.optimize.curve_fit(func, A[:,:2], A[:,2], guess)
  
  print params, 'fitted parameters' 
  
  import matplotlib.pyplot as plt 
  plt.plot(x,func(A,params[0],params[1]),'-r',x,z,'o') 
  plt.title('Plot') 
  plt.legend(['Fit', 'Data'], loc='lower right')
  plt.show()
  </code></pre>
  
  <p>The result of the plot is this</p>
  
  <p><img src="https://i.stack.imgur.com/vHExi.png" alt="enter image description here"></p>
  
  <p>And the resultant coefficient is: </p>
  
  <pre><code>[3e-05, 128] initial guessed parameters
  [  2.00773153e-04   1.22752179e+02] fitted parameters
  </code></pre>
  
  <p>Because all the data is inside <code>array</code>A, <code>scipy</code> thinks that the points in the array joins from one point to another, resulting in the end each curve to go back to the origin, which is also the start of subsequent curves.</p>
  
  <p>How should I code in <code>python</code> , such that <code>scipy.optimize.curve_fit</code> knows that the data in the array consists of multiple curves, instead of it being one single conjoined data? Any advice would be greatly appreciated.</p>