从数组中删除随机选择的元素

时间:2015-09-22 20:17:15

标签: python sorting numpy random

如何在Python中执行以下操作?

  • 以线性数组x开始,以及相关的概率密度函数(pdf)p(x)
  • N
  • 指定的方式随机删除p(x)个元素
  • 返回x,删除所选元素(x将更短)
  • 返回N个元素的新数组,按顺序选择

警告:数组x可能有重复的值xi,但它们有不同的概率(p(x)实际上取决于我没有显示的参数),所以我想确定删除所选的“正确”的,而不仅仅是具有相同值xi的元素。

我的代码因此:

import numpy as np

N = 5;
x = np.linspace(0,5,11)
pdf = np.exp(-x**2);
pdf = pdf/np.sum(pdf);

x_rand_sorted = np.random.choice(x,N,replace=False,p = pdf)

print 'x:',x
print 'first N random elements:', x_rand_sorted
print 'x without random elements = ??'

2 个答案:

答案 0 :(得分:1)

您可以使用布尔掩码:

import numpy as np

N = 5;
x = np.linspace(0,5,11)
pdf = np.exp(-x**2);
pdf = pdf/np.sum(pdf);

index = np.full(x.shape, False, bool)
index[np.random.choice(np.arange(index.shape[0]), N, replace = False,p = pdf)] = True

x_rand_sorted = x[index]
x_rand_remaining = x[~index]

print 'x:',x
print 'first N random elements:', x_rand_sorted
print 'x without random elements: ', x_rand_remaining

答案 1 :(得分:1)

输出:

  • 剩余价值:x_remaining
  • N随机元素:x_rand_vals
  • 随机元素索引:randices

示例:

import numpy as np
N = 9;
n = 4;
x = np.linspace(0,2,N);
pdf = np.exp(-x**2);
pdf = pdf/np.sum(pdf);

#create mask, choose random indices from x according to pdf, set chosen indices to True:
indices = np.full(x.shape, False, bool)
randices = np.random.choice(np.arange(indices.shape[0]), n, replace = False,p = pdf)
indices[randices] = True

x_rand_vals = x[randices]
x_remaining = x[~indices]

np.set_printoptions(precision=3)
print 'original x:\t\t', x
print 'pdf array:\t\t', pdf
print 'random indices:\t\t',randices
print 'random values:\t\t',x_rand_vals
print 'remaining array:\t',x_remaining

(示例输出):

    original x:         [ 0.   0.2  0.4  0.6  0.8  1.   1.2  1.4  1.6  1.8  2. ]
    random indices:     [4 2 5]
    random values:      [ 0.8  0.4  1. ]
    remaining array:    [ 0.   0.2  0.6  1.2  1.4  1.6  1.8  2. ]
相关问题