【发布时间】:2016-06-24 15:36:56
【问题描述】:
我正在尝试创建一个函数,但它涉及两个不同长度的变量。我的设置如下:
import pandas as pd
import numpy as np
u = np.random.normal(0,1,50)
t = 25
x = t*u/(1-u)
x = np.sort(x, axis=0)
theta = list(range(1, 1001, 1)
theta = np.divide(theta, 10) # theta is now 1000 numbers, going from 0.1 to 100
fx = np.zeros(1000)*np.nan
fx = np.reshape(fx, (1000,1))
我希望我的功能如下:
def function(theta):
fx = 50/theta - 2 * np.sum(1/(theta + x))
return fx
但它不起作用,因为 theta 的长度为 1000,而 x 的长度为 50。我希望它为每个 theta 迭代工作,以及最后的部分:
np.sum(1/(theta + x)
我希望它将单个 theta 添加到 x 中的 50 个数字中的每一个。如果我要这样做一次,它看起来像:
fx[0] = 50/theta[0] - 2 * np.sum(1/(theta[0] + x))
我可以让它与“for”循环一起工作,但我最终需要将它输入到一个最大似然函数中,因此使用它是行不通的。有什么想法吗?
【问题讨论】:
-
尝试map对每个x执行最后一次操作
标签: python function numpy pandas iteration