So I am trying to fit a 2-dimensional function with polynomials.
I have some data and I have written a function to test it.
But something unexpected happens like:
ValueError: object too deep for desired array
error: Result from function call is not a proper array of floats.
Then I go to the net but found nothing like my situation. I have no idea how to flatten my data.What should I do to fix this problem?
(this is my code:
def poly(order, cov, x):
z = 0
for r in range(order + 1):
for k in range(order + 1):
z += cov[r * order + k] * x[0] ** r * x[1] ** k
return z
def polynomial_func(x , *cov):
order = int(np.sqrt(len(cov))) - 1
return poly(order, cov, x)
k = int(input(‘Please type the order:’))
U = sorted(list(u))
V = sorted(list(v))[300:501]
U = U*201
V0 = []
for item in V:
V0 = V0 + [item]*151
V = V0
arr_x = np.array(U[::3])
arr_y = np.array(V[::3])
arr_z = g(arr_x,arr_y)
popt, pcov = curve_fit(polynomial_func, (arr_x,arr_y), arr_z , p0=[0.0] * (k+1)**2)
print(np.round(popt, 2))
where u and v are some random x y and g is the fuction generated by interpolation)
I have gone through the Web.
This problem seems commonly due to 2-D array, but I have no idea how to deal with them.
Is this happens because of arr_z?
Sinnaruil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.