Alternative to scipy.optimize.curve_fit

I am trying to build some visualizations using matplotlib, and in one of my functions I check if the waves are logarithmic. This is my current working version:

import numpy as np def is_logarithmic(waves): def expfunc(x, a, b, c): return a*np.exp(b*x) + c wcopy = list(waves) wcopy.sort() # If the ratio of x-max : x-min < 10, don't use a logarithmic scale # (at least in matplotlib) if (wcopy[-1] / wcopy[0]) < 10: return False # Take a guess at whether it is logarithmic by seeing how well the x-scale # fits an exponential curve diffs = [] for ii in range(len(wcopy) - 1): diffs.append(wcopy[ii + 1] - wcopy[ii]) # Fit the diffs to an exponential curve x = np.arange(len(wcopy)-1) try: popt, pcov = curve_fit(expfunc, x, diffs) except Exception as e: print e popt = [0.0, 0.0, 0.0] pcov = np.inf # If a > 0.5 and covsum < 1000.0 # use a logarithmic scale. if type(pcov) == float: # It probably np.inf covsum = pcov else: covsum = pcov.diagonal().sum() res = (covsum < 1000.0) & (popt[0] > 0.5) return res 

I am trying to find an alternative to scipy curve_fit() because I do not want to install such a large library to use this single function. Is there anything else I can use, or a combination of other functions from using is ideally just numpy and matplotlib to get a similar result?

+5
source share
1 answer

Numpy can do linear ( numpy.linalg.lstsq ) and polynomial binding ( numpy.polyfit ). In general, you need scipy to install on functions that you define yourself (scipy uses fortran minpack, and numpy only with C).

However, for your example, you can use a similar approach to this question to match exp. Basically, take the logarithm of both sides of the equation and use numpy.polyfit .

+3
source

All Articles