Numpy随机选择多个循环

时间:2015-07-09 14:02:52

标签: python numpy pandas

我试图多次执行多次模拟以获得所需的模拟分布。我有一个类似下面的数据集。

fruit_type, reading, prob
Apple, 12,.05
apple, 15, .5
orange 18, .99

我的代码示例如下。

def sim(seconds):
    output = pd.DataFrame()
    current = []
    #output = pd.DataFrame()
    for i in range(1, 100000000):
        if data2['fruit_type'].all() == 'Apple':
            hostrecord1 = np.random.choice(data2['reading'], size=23, replace=True, p=data2['prob'])
            current = hostrecord1.sum() + 150

        if data2['fruit_type'].all() == 'Orange':
            hostrecord2 = np.random.choice(data2['reading'], size=23, replace=True, p=data2['prob'])
            current = hostrecord2.sum() + 150

        if data2['fruit_type'].all() == 'Peach':
            hostrecord3 = np.random.choice(data2['reading'], size=20, replace=True, p=data2['prob'])
            current = hostrecord3.sum() + 150

    #put all records in one array
    #return all records 
    output = pd.concat(current)
    return output

我试图弄清楚如何使用fruit_type不同的条件执行多个模拟,但目前无法弄清楚逻辑。每个模拟都应选择与fruit_type相关的特定行,因此模拟由fruit_type指定。每个样本的大小因设计而异,因为每个fruit_type具有不同的条件。

我的预期输出是所有模拟值的数组。我还想将所有结果附加到一个pandas数据帧中。

1 个答案:

答案 0 :(得分:2)

你的解释还不清楚,但这是一个猜测:

# initialize data
In [1]: fruits = ['apple', 'peach', 'orange']
In [2]: data = np.vstack((np.random.choice(fruits, size=10), 
                          np.random.randint(0, 100, size=10), 
                          np.random.rand(10))).T
In [3]: df = pd.DataFrame(data, columns=['fruit_type', 'reading', 'prob'])

关键是索引df,以便df[df.fruit_type == fruit_of_interest]。这是一个示例函数:

def simulate(df, N_trials):
    # replace with actual sizes for ['apple', 'peach', 'orange'] respectively
    sample_sizes = [N1, N2, N3]
    fruits = ['apple', 'peach', 'orange']

    results = np.empty((N_trials, len(fruits))
    for i in xrange(N_trials): # switch to range if using python3
        for j, (fruit, size) in enumerate(zip(fruits, sample_sizes)):
            sim_data = df[df.fruit_type == fruit]
            record = np.random.choice(sim_data.reading, size=size, p=sim_data.prob)
            # do something with the record
            results[i, j] = record.sum()

请注意,如果您要进行1亿次试验,结果数组可能太大而无法存储在内存中。如果你交换for循环也可能会更快,因此fruit / size one是for循环的最外层。

值得注意的是,您可以使用for生成一个巨大的样本,而不是np.random.choice - 循环,然后重塑:

np.random.choice([0, 1], size=1000000).reshape(10000, 100)

将为您提供10000次试验,每次试验100个样本。如果您的1亿次试验耗时太长,这可能会有用 - 您可以将其分成100个循环,choice一次完成100万个样本。一个例子可能是

def simulate(df, N_trials, chunk_size=10000):
    # replace with actual sizes for ['apple', 'peach', 'orange'] respectively
    sample_sizes = [N1, N2, N3]
    fruits = ['apple', 'peach', 'orange']

    for i in xrange(N_trials/chunk_size): # switch to range if using python3
        chunk_results = np.empty((chunk_size, len(fruits))
        for j, (fruit, size) in enumerate(zip(fruits, sample_sizes)):
            sim_data = df[df.fruit_type == fruit]
            record = np.random.choice(sim_data.reading, size=(chunk_size, size), 
                                      p=sim_data.prob)
            chunk_results[:, j] = record.sum(axis=1)

        # do something intermediate with this chunk
相关问题