简体   繁体   English

将 function 应用于 Python 中数组的每个元素

[英]Apply a function to each element of an array in Python

I am trying to do two things in Python:我试图在 Python 中做两件事:

  1. Simulate 100 random draws from a Poisson distribution.从泊松分布中模拟 100 次随机抽取。 I have done this by:我通过以下方式做到了这一点:

    sample100 = poisson.rvs(mu=5,size=100) sample100 = poisson.rvs(mu=5,size=100)

  2. Take the above sample, and apply an UMP test I've generated to each individual observation (eg, test the hypothesis against each individual observation).取上述样本,并将我生成的 UMP 测试应用于每个单独的观察(例如,针对每个单独的观察测试假设)。 The test should accept the null hypothesis if the observation has a value < 8;如果观察值 < 8,则测试应接受 null 假设; reject with probability ~50% if observation has value = 8;如果观察值 = 8,则以约 50% 的概率拒绝; reject if observation has value > 8如果观察值 > 8 则拒绝

I cannot figure out how to do the second part of this.我无法弄清楚如何做第二部分。 The function code I've made is:我制作的 function 代码是:

def optionaltest(y,k,g):

    if (y > k):
        return 1
    if (y == k):
        if rand(uniform(0,1)) >= 0.4885 then 1
        else 0
    if (y < k):
        return 0

But there are two issues - apparently if (y==k) is invalid syntax.但是有两个问题 - 显然 if (y==k) 是无效的语法。 Second, even if I remove that part, I can't actually apply the function to sample100 since it is an array.其次,即使我删除了该部分,我实际上也无法将 function 应用于 sample100,因为它是一个数组。

How can I modify this to make it work?我该如何修改它以使其工作? Clearly, I'm very new to Python but I have been scouring the internet for hours.显然,我对 Python 很陌生,但我已经在互联网上搜索了几个小时。 Perhaps I should change how I'm generating my sample data so I can apply a function to it?也许我应该更改生成示例数据的方式,以便可以对其应用 function? Maybe there's a way to apply a function to each element of an array?也许有一种方法可以将 function 应用于数组的每个元素? How do I make the test logic work when the output = k (which I will set to 8 in this case)?当 output = k(在这种情况下我将设置为 8)时,如何使测试逻辑工作?

syntax error is语法错误是

in Python if condition then..else becomes在 Python 如果条件 then..else 变为

if condition:
       pass
else:
       pass

This is invalid python syntax这是无效的 python 语法

if rand(uniform(0,1)) >= 0.4885 then 1
    else 0

Instead, you could do this:相反,您可以这样做:

return 1 if rand(uniform(0,1)) >= 0.4885 else 0

You could also do something more verbose but potentially more straightforward (this is often a matter of taste), like this:你也可以做一些更冗长但可能更直接的事情(这通常是一个品味问题),像这样:

def optionaltest(y,k,g):

    if (y > k):
        return 1
    if (y == k):
        if rand(uniform(0,1)) >= 0.4885:
            return 1
        else:
            return 0
    if (y < k):
        return 0

Or even like this:甚至像这样:

def optionaltest(y,k,g):

    if (y > k):
        return 1
    if (y == k) and rand(uniform(0,1)) >= 0.4885:
        return 1
    else:
        return 0

For this question:对于这个问题:

Maybe there's a way to apply a function to each element of an array?也许有一种方法可以将 function 应用于数组的每个元素?

You can use a for-loop or map a function over a list:您可以在列表上使用 for 循环或map和 function:

results = []
for elem in somelist:
     results.append(my_function(elem))

Alternately:交替:

results = list(map(my_function, somelist))

Your function takes three arguments, though, and it's not clear to me where those are coming from.不过,您的 function 需要三个 arguments,我不清楚这些来自哪里。 Is your list a list of tuples?您的列表是元组列表吗?

For applying function on your list's elements you can convert your list to a pandas dataframe.要在列表元素上应用 function,您可以将列表转换为 pandas dataframe。 Then use apply function.然后使用申请 function。 For example if your list name is " data " and your function is "func" do this:例如,如果您的列表名称是“数据”并且您的 function 是“func”,请执行以下操作:

import pandas as pd
data = ['item_1', 'item_2', 'item_3']
df = pd.DataFrame (data, columns = ['column_name'])

result = df.apply(func)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM