Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在多行上执行函数_Python_Python 3.x_Pandas_Dataframe_Optimization - Fatal编程技术网

Python 在多行上执行函数

Python 在多行上执行函数,python,python-3.x,pandas,dataframe,optimization,Python,Python 3.x,Pandas,Dataframe,Optimization,我定义了一个函数。这被称为“二分法”,其代码在帖子下方 我还有一个名为“v”的数据框,它包含7列和2行: dph 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等

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

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

dph 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等于零。现在,'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列

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

  for i in range(0,2,1):
      v['k'] = bisection(f,a,b,N)
这给了我以下结果:

dph 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)
我得到:

dph 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
所以我想要的结果是:

dph 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:序列的真值不明确。使用a.empty,
a、 bool()、a.item()、a.any()或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
def二等分(f,a,b,N):
''区间[a,b]上f(x)=0的二分法近似解
方法。
参数
----------
f:功能
我们试图近似解f(x)=0的函数。
a、 b:数字
搜索解决方案的时间间隔。函数返回
如果f(a)*f(b)>=0,则无,因为不保证解决方案。
N:(正)整数
要实现的迭代次数。
退换商品
-------
x_N:号码
用二分法计算的第n个区间的中点。这个
初始间隔[a_0,b_0]由[a,b]给出。如果f(m_n)=0
中点m_n=(a_n+b_n)/2,则函数返回此解。
如果值f(a_n)、f(b_n)和f(m_n)的所有符号在任何时刻都相同
迭代时,二分法失败并返回None。
例子
--------
>>>f=λx:x**2-x-1
>>>二等分(f,1,2,25)
1.618033990263939
>>>f=λx:(2*x-1)*(x-3)
>>>二等分(f,0,1,10)
0.5
'''
如果f(a)*f(b)>=0:
打印(“二分法失败”)
一无所获
a_n=a
b_n=b
对于范围(1,n+1)内的n:
m_n=(a_n+b_n)/2
f_m_n=f(m_n)
如果f(a_n)*f_m_n<0:
a_n=a_n
b_n=m_n
elif(b_n)*f_m_n<0:
a_n=m_n
b_n=b_n
elif f_m_n==0:
打印(“找到精确的解决方案”)
返回m\n
其他:
打印(“二分法失败”)
一无所获
返回(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)

请分享完整的回溯错误。问题和代码非常混乱(至少对我来说)。简单化并不是“除了问题之外”,它会帮助其他人理解你的问题。打扫完后,你可能会自己找到答案(比如打扫房间,找到你要找的东西)。通过共享令人困惑的代码和令人困惑的问题,您降低了有人能够帮助您的可能性。如果人们坚持要告诉你这些,尽管这很烦人,也许你应该听听。谢谢@sandalphon!但是现在我得到了一个索引器:indexer:single positional indexer超出了范围…:sIndex序列类似于dataframe(我在建议的答案中添加了index=v.index)