gpt4 book ai didi

python - 对多行执行函数

转载 作者:行者123 更新时间:2023-12-01 00:59:50 25 4
gpt4 key购买 nike

我定义了一个函数。这称为“二分法”,其代码位于帖子下方。

我还有一个名为“v”的数据框,其中包含 7 列和 2 行:

D   P   h     O   Q   SD      LT
80 27 0.37 50 2 3 1.51
50 27 0.25 50 2 3 0.03

您在二分函数中看到我需要四个参数。 f、a、b 和 N。它们的定义如下:

b = 5
a = 0.05
N = 1000

现在,“f”是一个在“x”中具有未知变量的函数。它是一个匿名函数(lambda x)。二分函数查找“x”的值,其中 f 等于 0。现在,“f”是一个非常令人讨厌的导数,是的,我会清理它,但请不要关注它,因为函数本身是正确的。所以,'f' 是:

f = lambda x: norm.ppf(1-(v.iloc[i,4]*v.iloc[i,1]*v.iloc[i,2])/(2*v.iloc[i,0]*x))*v.iloc[i,5]*np.sqrt(v.iloc[i,6])*v.iloc[i,1]*v.iloc[i,2]+np.sqrt(2*v.iloc[i,0]*v.iloc[i,3]*v.iloc[i,1]*v.iloc[i,2])-v.iloc[i,0]*x*(((-(norm.ppf(1-(v.iloc[i,4]*v.iloc[i,2]*v.iloc[i,1])/(2*x*v.iloc[i,0]))))*(1-norm.cdf((norm.ppf(1-(v.iloc[i,4]*v.iloc[i,2]*v.iloc[i,1])/(2*x*v.iloc[i,0]))),loc=0,scale=1))+(norm.pdf((norm.ppf(1-(v.iloc[i,4]*v.iloc[i,2]*v.iloc[i,1])/(2*x*v.iloc[i,0]))),loc=0,scale=1)))*v.iloc[i,5]*np.sqrt(v.iloc[i,6])-v.iloc[i,4])/v.iloc[i,4]*-1

目标是将“二分函数”应用于数据帧中的每一行:从而添加一个新列,为每一行提供二分函数的结果,其中该函数使用所有 7 列。

现在,当我想应用函数“bisection(f, a, b, N)”时,我尝试了以下代码:

  for i in range(0,2,1):
v['k'] = bisection(f,a,b,N)

这给了我以下结果:

D   P   h     O       Q SD  LT    k
80 27 0.37 50 2 3 1.51 3.814891
50 27 0.25 50 2 3 0.03 3.814891

如您所见,它找到了“x”的正确值,但仅限于第二行。第一行的结果是 4.50.. 当我将代码更改为:

  for i in range(0,1,1):
v['k'] = bisection(f,a,b,N)

我得到:

D   P   h     O      Q  SD  LT    k
80 27 0.37 50 2 3 1.51 4.503648
50 27 0.25 50 2 3 0.03 4.503648

所以我想要的结果是:

D   P   h     O      Q  SD  LT    k
80 27 0.37 50 2 3 1.51 4.503648
50 27 0.25 50 2 3 0.03 3.814891

如何实现这一目标?

我还尝试将“f”更改为:

f = lambda x: norm.ppf(1-(v.Q*v.P*v.h)/(2*v.D*x))*v.SD*np.sqrt(v.LT)*v.P*v.h+np.sqrt(2*v.D*v.O*v.P*v.h)-v.D*x*(((-(norm.ppf(1-(v.Q*v.P*v.h)/(2*x*v.D))))*(1-norm.cdf((norm.ppf(1-(v.Q*v.P*v.h)/(2*x*v.D))),loc=0,scale=1))+(norm.pdf((norm.ppf(1-(v.Q*v.P*v.h)/(2*x*v.D))),loc=0,scale=1)))*v.SD*np.sqrt(v.LT)-v.Q)/v.Q*-1

并尝试使用此代码进行迭代:

 for index, row in df.iterrows():
v.append(bisection(f,a,b,N))

但随后我收到错误:

  ValueError: The truth value of a Series is ambiguous. Use a.empty, 
a.bool(), a.item(), a.any() or a.all().

有人可以帮助我吗?

二分函数的代码:

def bisection(f,a,b,N):
'''Approximate solution of f(x)=0 on interval [a,b] by the bisection
method.

Parameters
----------
f : function
The function for which we are trying to approximate a solution f(x)=0.
a,b : numbers
The interval in which to search for a solution. The function returns
None if f(a)*f(b) >= 0 since a solution is not guaranteed.
N : (positive) integer
The number of iterations to implement.

Returns
-------
x_N : number
The midpoint of the Nth interval computed by the bisection method. The
initial interval [a_0,b_0] is given by [a,b]. If f(m_n) == 0 for some
midpoint m_n = (a_n + b_n)/2, then the function returns this solution.
If all signs of values f(a_n), f(b_n) and f(m_n) are the same at any
iteration, the bisection method fails and return None.

Examples
--------
>>> f = lambda x: x**2 - x - 1
>>> bisection(f,1,2,25)
1.618033990263939
>>> f = lambda x: (2*x - 1)*(x - 3)
>>> bisection(f,0,1,10)
0.5
'''
if f(a)*f(b) >= 0:
print("Bisection method fails.")
return None
a_n = a
b_n = b
for n in range(1,N+1):
m_n = (a_n + b_n)/2
f_m_n = f(m_n)
if f(a_n)*f_m_n < 0:
a_n = a_n
b_n = m_n
elif f(b_n)*f_m_n < 0:
a_n = m_n
b_n = b_n
elif f_m_n == 0:
print("Found exact solution.")
return m_n
else:
print("Bisection method fails.")
return None
return (a_n + b_n)/2

最佳答案

你的代码:

    for i in range(0,2,1):
v['k'] = bisection(f,a,b,N)

只需将新列“k”的所有值设置为每次迭代中二分函数计算出的任何值

创建一个系列并将其分配给新行“k”,可能像这样:

    v['k'] = pd.Series([bisection(f,a,b,N) for i in range(2)], index = v.index)

关于python - 对多行执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55893578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com