'How to call the amount an element in a list fits a criterium?

In Python, I am trying to print the amount that p-value is smaller than 0.05 (for 100 random p-values). The p-value itself is the slope element of a linear regression which I also called.

I currently have the following code: As a result I get an error message. I know why this occurs but just don't know any other way to print the amount of p values which fit my criterium.

import numpy as np
from scipy import stats
from scipy.stats import linregress

def func():
    a = 0
    x = np.random.rand(100)
    y = np.random.rand(100)
    slope, intercept, r_value, p_value, std_err = linregress(x, y)
    return  slope, intercept, p_value, x, y

a = 0
for i in range(100):
    myfunc= func()
    p = myfunc[3] #[3] assigning p to the p_values
    if p[i] < 0.05:
        a += 1
    print(len(a))


Solution 1:[1]

Report the error you are getting

Note that the p value is the third element returned by func. You want index 2 not 3. You want p, not p[i] in the comparison. 'a' is an integer that does not have a length. . In any case you want to print a, not len(a) and you want to do so outside the loop.

Note the p value is not the slope, though the two are related.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 William