in Education by
I need to iterate through the elements in a numpy array so I can treat any zero elements separately. The code below works for straighforward evaluations, but not when used with scipy.optimize.curve_fit(). Is there a way to make this work with the curve_fit fn? import numpy as np from matplotlib.pyplot import * from scipy.optimize import curve_fit def my_fn(x_array, b, a): y = [] for x in np.nditer(x_array): #This doesn't work with curve_fit() if x == 0: y.append(0) else: y.append(b*(1/np.tanh(x/a) - a/x)) return np.array(y) x_meas = [0, 5, 20, 50, 100, 200, 600] y_meas = [0, 0.275, 1.22, 1.64, 1.77, 1.84, 1.9] xfit = np.linspace(0,600,601) yfit2 = my_fn(xfit, 1.95, 8.2) #manual fit #Not working #popt, pcov = curve_fit(my_fn, x_meas, y_meas, p0=[1.95, 8.2]) #yfit1 = my_fn(xfit, *popt) #auto fit figure(1) plot(x_meas, y_meas, 'o', xfit, yfit2) show() JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
To make the larsmans' answer actually work, you will also need to convert your data samples to NumPy arrays: x_meas = numpy.array([0, 5, 20, 50, 100, 200, 600], float) y_meas = numpy.array([0, 0.275, 1.22, 1.64, 1.77, 1.84, 1.9], float) (Converting y_meas is not strictly necessary.) Here is larsmans' code with my suggestions incorporated: def my_fn(x, b, a): y = np.zeros_like(x) nonzero = x != 0 x = x[nonzero] y[nonzero] = b*(1/np.tanh(x/a) - a/x) return y

Related questions

0 votes
    I need to iterate through the elements in a numpy array so I can treat any zero elements separately. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have three 3D mesh matrices (X, Y, Z) corresponding to the xyz coordinate space. I also have a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    Do Numpy And Scipy Support Python 3.x?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    I store all of the Google Maps marker objects in a single array. Right now I am trying to set up ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    Does Numpy/scipy Work With Ironpython (.net)?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    Does Numpy/scipy Work With Jython?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    Why Both Numpy.linalg And Scipy.linalg? What’s The Difference?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    How Do I Make 3d Plots/visualizations Using Numpy/scipy?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    How Do I Make Plots Using Numpy/scipy?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    What Is The Difference Between Numpy And Scipy?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    Which is better in terms of performance for iterating an array? (a) for(int i=0; i=0; i-) (c) ... Autoboxing & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which is better in terms of performance for iterating an array? (a) for(int i=0; i=0; i-) ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    Point out the wrong statement. (a) Each universal function takes array inputs and produces array outputs (b) Broadcasting ... input arguments are ndarrays (d) All of the mentioned...
asked Oct 7, 2021 in Technology by JackTerrance
0 votes
    Shown below is the input NumPy array. Delete column two and replace it with the new column given below....
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    I'm making a checklist for people to do tasks on our company website. The feature I'm working on ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
...