Учитывая, что двумерный массив Numpy представляет двумерное распределение, как выбрать данные из этого распределения с помощью функций Numpy или Scipy? - PullRequest
3 голосов
/ 07 мая 2019

Для двумерного массива dist с формой (200,200), где каждая запись массива представляет общую вероятность (x1, x2) для всех x1, x2 ∈ {0, 1,. , , , 199}. Как мне выбрать двумерные данные x = (x1, x2) из ​​этого распределения вероятности с помощью Numpy или Scipy API?

1 Ответ

2 голосов
/ 07 мая 2019

Вот способ, но я уверен, что есть гораздо более элегантное решение, использующее Scipy.numpy.random не имеет отношения к 2d pmfs, поэтому вам нужно немного изменить форму гимнастики, чтобы пойти по этому пути.

import numpy as np

# construct a toy joint pmf
dist=np.random.random(size=(200,200)) # here's your joint pmf 
dist/=dist.sum() # it has to be normalized 

# generate the set of all x,y pairs represented by the pmf
pairs=np.indices(dimensions=(200,200)).T # here are all of the x,y pairs 

# make n random selections from the flattened pmf without replacement
# whether you want replacement depends on your application
n=50 
inds=np.random.choice(np.arange(200**2),p=dist.reshape(-1),size=n,replace=False)

# inds is the set of n randomly chosen indicies into the flattened dist array...
# therefore the random x,y selections
# come from selecting the associated elements
# from the flattened pairs array
selections = pairs.reshape(-1,2)[inds]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...