Home > Enterprise >  Apply a function to each element of an array in Python
Apply a function to each element of an array in Python

Time:12-19

I am trying to do two things in Python:

  1. Simulate 100 random draws from a Poisson distribution. I have done this by:

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

  2. Take the above sample, and apply an UMP test I've generated to each individual observation (e.g., test the hypothesis against each individual observation). The test should accept the null hypothesis if the observation has a value < 8; reject with probability ~50% if observation has value = 8; reject if observation has value > 8

I cannot figure out how to do the second part of this. The function code I've made is:

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. Second, even if I remove that part, I can't actually apply the function to sample100 since it is an array.

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. Perhaps I should change how I'm generating my sample data so I can apply a function to it? Maybe there's a way to apply a function to each element of an array? How do I make the test logic work when the output = k (which I will set to 8 in this case)?

CodePudding user response:

syntax error is

in Python if condition then..else becomes

if condition:
       pass
else:
       pass

CodePudding user response:

For applying function on your list's elements you can convert your list to a pandas dataframe. Then use apply function. For example if your list name is " data " and your function is "func" do this:

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

result = df.apply(func)

CodePudding user response:

This is invalid python syntax

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?

You can use a for-loop or map a function over a list:

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. Is your list a list of tuples?

  • Related