My task is that when the input is *X, every input is an array of one column with n rows, but they may have different rows(eg X[0] is an array with 1 column and 10 rows, and X[2] is with 1 column and 9 rows), I want the code counts the p-value of the every two arrays and get the lowest p-value and the order of X[n](eg. X[1] means the first array and so on). The code goes wrong, 'local variable ans_1 referred before assignment'. I don't know how to do with this.
def mass_independent_ttest(*X):
min_pvalue = 10
for i in range(0, len(X)):
for j in range(i+1, len(X)):
df_1 = pd.DataFrame(X[i])
df_2 = pd.DataFrame(X[j])
df_first = df_1.loc[:,0]
df_second = df_2.loc[:,0]
temp = scipy.stats.ttest_ind(df_first, df_second)
temp_pvalue = temp.pvalue
if temp_pvalue < min_pvalue:
min_pvalue = temp_pvalue
ans_1 = i
ans_2 = j
ans_tuple = (ans_1, ans_2, min_pvalue)
return ans_tuple
`
At the last iteration of i
, range(i+1, len(X))
will be an empty list, so that code won't execute, and ans_1
and ans_2
don't exist when you call ans_tuple = (ans_1, ans_2, min_pvalue)
. So you should evaluate your outer and inner loops to see if you are getting the expected number of iterations.
This example shows conceptually what is happening.
for i in range(0, len(X)):
print(list(range(i+1, len(X))))
=== Output: ===
[1, 2, 3, 4]
[2, 3, 4]
[3, 4]
[4]
[]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.