1
def linearize(p, x):
    return p[0] * x**p[1]
def error(p, x, y):    
    return (np.log10(y) - np.log10(linearize(p, x)))


from scipy import optimize

args = freq_log[1:9063], np.abs(spec_log[1:9063])
qout, success = optimize.leastsq(error,
                             [1e5,-0.8],
                             args=args,
                             maxfev=3000)

fig,ax1=plt.subplots(figsize=(5,5))

spec = ax1.plot(freq_log,np.abs(spec_log), 'o', alpha=0.3)
approx = ax1.plot(freq_log[1:], linearize(qout, freq_log[1:]), linewidth=3)

I am trying to replicate a code, and I am trying to understand what linearize does in this case, what arguments it takes, and what the return does in order to remedy the following errors

Currently it returns runtime errors and a ValueError

RuntimeWarning: divide by zero encountered in power return p[0] * x**p[1]
RuntimeWarning: invalid value encountered in power return p[0] * x**p[1]
ValueError: object too deep for desired array

My primary concern is the first four lines, can anyone help me understand what is causing this error and how to remedy it?

Full code is @ https://github.com/seg/tutorials-2017/blob/master/1710_Colored_inversion/Colored_inversion_notebook.ipynb

user3736201
  • 99
  • 2
  • 6

1 Answers1

0

A detailed description would be too long, nonetheless, it is worth knowing.

I found this good and short example online.

Leaving a link instead of copying and pasting here.

http://www.scipy-lectures.org/intro/summary-exercises/optimize-fit.html

The linearize here is trying to return the overall noise in passed data which would then be used to evaluate your error, and then the leastsq as it sounds, minimizes the sum of squares of the initial waveform.

Sayan Sil
  • 5,799
  • 3
  • 17
  • 32
  • Thank you. Could you explain briefly what the p[0] and p[1] for linearize would be? I don't follow what is causing the divide by 0 error in this case. – user3736201 Dec 02 '17 at 01:28
  • It must be the x values. I suggest you make sure that the values in x are not zero. Maybe replace them with a very very small number and see if the error persists. Also, I sniffed around a bit and found that the error might as well be due to older versions of scipy and numpy. So I suggest you update the same too. – Sayan Sil Dec 02 '17 at 01:34
  • Ah. You are correct, I tried running the section independently from the rest of the code and it works fine. However, I do not know what x is defined as? I didn't set anything in particular to be x. This may be a circular question but what is x defined as in this case? I think that info will help fully resolve issue. – user3736201 Dec 02 '17 at 01:39
  • As it seems, the freq_log[1:9063] is the set of x. – Sayan Sil Dec 02 '17 at 01:49
  • 1
    Please provide an answer which would be useful even if the link breaks, i.e. the main message of what is shown in the link should be part the answer. – ImportanceOfBeingErnest Dec 02 '17 at 11:57