Jump to content

scipy.optimize.curve_fit Error

Hi, I am pretty new to python (and programming in general), I am trying to create a curve fit to my data,

 

I am loading two arrays 'Tnn_month' and 'amon_month' from .mat files, I want to create a scatter plot of these two parameters and then assign a curve fit to show the correlation between both,

 

I tried to apply the curve_fit examples done on simple plots, but I still get this error: error: Result from function call is not a proper array of floats.

 

Tnn_month (x) and amon_month (y) are both  arrays of length 5433656

find attached the scatter plot201005_test_day.thumb.png.98e4f008966a469e3e0e754155654870.png

 

-----------------------------------------------------

input:

 

import numpy as np
import matplotlib.pyplot as plt
import scipy.io as io
import pandas as pd
import seaborn as sns
from matplotlib.pyplot import figure

 

MainFolder= r"/home/abeed/Documents/thesis/python/scatter/"
yyyymm_str= '201005'
d_or_n = 'day'

colors = (0,0,0)
area = np.pi*3

 

#load data
mat = io.loadmat('Tnn_amon0.01_201005.mat')

Tnn_month = mat['Tnn_index']
amon_month = mat['amon_index']

colors=np.array([0.1, 0.5, 0.5])

 

# scatter plot


figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')

plt.scatter(Tnn_month, amon_month, s=area, c=colors, alpha=0.5)
plt.title('May 2010', fontsize=14)
plt.ylabel('Ammonia Total Concentration (molec/cm$^{2}$)',fontsize=14)
plt.xlabel('Skin Temperature (Kelvin)', fontsize=14)

plt.savefig(MainFolder + yyyymm_str + '_test_' + d_or_n +'.png', format='png', dpi=700)
#plt.show()

 

# curve_fit
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np


Tnn_month[np.isnan(Tnn_month)]=0 #something for nans
amon_month[np.isnan(amon_month)]=0


def func(X, a, b, c):
    return a * np.exp(-b * X) + c

 

Y = func(Tnn_month, 2.5, 1.3, 0.5)

np.random.seed(len(Tnn_month))

 

print(Tnn_month)

 

popt, pcov = curve_fit(func, Tnn_month, amon_month)

 

plt.plot(Tnn_month, func(Tnn_month, *popt), 'g--',label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))


plt.show()

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

output:




[[272.64697509]
 [273.43998094]
 [272.89872854]
 ...
 [265.03563904]
 [264.87200613]
 [273.74241245]]
 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
ValueError: object too deep for desired array

 
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-2-2ed29e6832ed> in <module>
     71 print(Tnn_month)
     72 
---> 73 popt, pcov = curve_fit(func, Tnn_month, amon_month)
     74 
     75 plt.plot(Tnn_month, func(Tnn_month, *popt), 'g--',label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))

~/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
    750         # Remove full_output from kwargs, otherwise we're passing it in twice.
    751         return_full = kwargs.pop('full_output', False)
--> 752         res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
    753         popt, pcov, infodict, errmsg, ier = res
    754         cost = np.sum(infodict['fvec'] ** 2)

~/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
    394             maxfev = 200*(n + 1)
    395         retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,
--> 396                                  gtol, maxfev, epsfcn, factor, diag)
    397     else:
    398         if col_deriv:

error: Result from function call is not a proper array of floats.

Link to comment
Share on other sites

Link to post
Share on other sites

Hi @Abeed.R please include code tags in your post. 

Thank-you.

 

 

Quote

When asking for help with programming issues, please use the code tags to enclose your code so that it is formatted correctly.

It makes things much more easily readable for the people trying to help you, thus improving your chances of actually getting help.
 

To add code tags, click the <> button on the editor toolbar, then enter your code in the code editor that appears.

 

COMMUNITY STANDARDS   |   TECH NEWS POSTING GUIDELINES   |   FORUM STAFF

LTT Folding Users Tips, Tricks and FAQ   |   F@H & BOINC Badge Request   |   F@H Contribution    My Rig   |   Project Steamroller

I am a Moderator, but I am fallible. Discuss or debate with me as you will but please do not argue with me as that will get us nowhere.

 

Spoiler

  

 

Character is like a Tree and Reputation like its Shadow. The Shadow is what we think of it; The Tree is the Real thing.  ~ Abraham Lincoln

Reputation is a Lifetime to create but seconds to destroy.

You have enemies? Good. That means you've stood up for something, sometime in your life.  ~ Winston Churchill

Docendo discimus - "to teach is to learn"

 

 CHRISTIAN MEMBER 

 

 
 
 
 
 
 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×