title: What is the best method to map a cosine function to a large set of data in Python?
body: <p>I have a large data set (~17,000 values) made up of an array of datetime objects and another array of associated float values. I'm trying to map a rough cosine function to all the data points in order to help analyze sleep patterns. I've been trying to figure out the best way to map a cosine curve to all this data. I've researched a few of the methods and found some similar situations, but one of the challenges i've encountered is figuring out how to do the calculations while using datetimes instead of some other arbitrary value.</p>
  
  <p>The code i've written thus far extracts the necessary data into 2 arrays. One with floats and the other with the associated datetime values.</p>
  
  <h2>Example Data:</h2>
  
  <p>Here's an example of the data i'm working with, copied from the debugger. The first array contains datetimes, and the 2nd array contains integer values. I've been finding a few ways to fit curves to data, but i'm having trouble figuring it out with the datetimes. I found this <a href="https://stackoverflow.com/questions/46296263/python-fit-data-to-given-cosine-function">method here</a> and tried converting the datetimes to floats by using the timestamp() function. </p>
  
  <pre><code>Array 1:
  00000 = {datetime} 2017-11-30 11:04:00
  00001 = {datetime} 2017-11-30 11:05:00
  00002 = {datetime} 2017-11-30 11:06:00
  00003 = {datetime} 2017-11-30 11:07:00
  00004 = {datetime} 2017-11-30 11:08:00
  00005 = {datetime} 2017-11-30 11:09:00
  00006 = {datetime} 2017-11-30 11:10:00
  00007 = {datetime} 2017-11-30 11:11:00
  00008 = {datetime} 2017-11-30 11:12:00
  00009 = {datetime} 2017-11-30 11:13:00
  00010 = {datetime} 2017-11-30 11:14:00
  
  Array 2:
  00000 = {int} 847
  00001 = {int} 684
  00002 = {int} 3602
  00003 = {int} 1029
  00004 = {int} 1114
  00005 = {int} 2701
  00006 = {int} 3469
  00007 = {int} 2445
  00008 = {int} 538
  00009 = {int} 425
  00010 = {int} 115
  </code></pre>
  
  <h2>Example Code</h2>
  
  <p>In this case, data<a href="https://stackoverflow.com/questions/46296263/python-fit-data-to-given-cosine-function">1</a> is the array of dateTimes (Array 1) and data[2] is the array of integer values (Array 2). I attempted to modify the code from the stackoverflow I linked to earlier, but this just yields a blank graph. I'm not sure if this is the best approach, this looked the most similar to what I was trying to do. There is probably some better method that I'm not aware of. </p>
  
  <pre><code>x = np.array(data[1])                   #Array of integers
  y = mdates.date2num(np.array(data[0]))  #Array with datetime Objects
  def form(theta, I_0, theta0, offset):
      return I_0 * np.cos(np.radians(theta - theta0)) ** 2 + offset
  
  yAverage= ((y[np.argmax(y)]- y[0]) / 2) + y[0] #Find average value
  param, covariance = optimize.curve_fit(form, x, y, [3000, y, 0])
  print
  'I_0: {0:e} / theta_0: {1} degrees / offset: {2:e}'.format(*param)
  print
  covariance
  
  plt.scatter(x, y, label='data')
  plt.ylim(0, 5000)
  plt.xlim(y[0], y[np.argmax(y)])
  plt.plot(x, form(x, *param), 'b-')
  plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
  plt.axes().xaxis.set_major_locator(mdates.AutoDateLocator())
  plt.show()
  </code></pre>