假设x是小时风速,我试图使用牛顿-拉弗森方法拟合,如下所示。K是一个无量纲参数,我打算用x值求解。def f(k)是实际方程,def d_f(k)是方程的导数,def f2(c)求解第二个参数c。默认情况下,Scipy优化库需要以下输入,才能使用牛顿-拉弗森法或割线法求零。
scipy.optimize.newton(func, x0, fprime=None, args=(), tol=1.48e-08, maxiter=50, fprime2=None)
import warnings
import numpy as np
import scipy
import scipy.stats
import sympy
from scipy import stats as st
x = np.random.randint(1, 23, size=3000)
x0 = np.random.choice(x, 100, replace=True)
def f(k):
return np.sum(x**k*np.log (x))/np.sum(x**k)-1/k- \
1/len(x)*np.sum(np.log(x))
def d_f(k):
return len(x)*np.sum(np.log (x)*np.log(np.log-[1-f(k)])- \
np.sum(np.log(x)*np.sum(np.log(np.log-[1-f(k)]))))
def f2(c):
return np.exp(k*np.sum(np.log (x))-np.sum(np.log(x)* \
np.sum(np.log(np.log-[1-f(k)]))))
#Initial Guess
k = 1.2
scipy.optimize.newton(f(k), x0, fprime=d_f(k))
Generated Output
AttributeError: 'numpy.float64' object has no attribute 'concatenate'