访问函数内部的变量进行计算

问题描述 投票:0回答:1

我正在尝试从函数内部访问变量,它给了我一个错误,提示:

NameError: name 'perm_sample_1' is not defined

代码在这里:

def permutation_sample(data1,data2):
    data = np.concatenate((data1,data2))
    permuted_data = np.random.permutation(data)
    perm_sample_1 = permuted_data[:len(data1)]
    perm_sample_2 = permuted_data[len(data1):]
    return perm_sample_1 , perm_sample_2
    #return perm_sample_1.shape ,perm_sample_2.shape

def draw_perm_reps_func_diff(data1,data2,func,size=1):
    perm_replicates = np.empty(size)
    for i in range(size):
        perm_sample_1 , perm_sample_2 = permutation_sample(data1,data2)
        perm_replicates[i] = func(perm_sample_1) - func(perm_sample_2)
    return perm_replicates

stats.ttest_ind_from_stats(mean1=np.mean(perm_sample_1), std1=np.std(perm_sample_1), nobs1=len(perm_sample_1),mean2=np.mean(perm_sample_2), std2=np.std(perm_sample_2), nobs2=len(perm_sample_2))
python function permutation replicate
1个回答
0
投票
假设您正在谈论第二个函数中的错误。您尝试访问的变量超出了您在第一个函数中定义的范围。

作为参数提供。

© www.soinside.com 2019 - 2024. All rights reserved.