【问题标题】:How to create a 2D array with N lots of random numbers?如何创建具有 N 大量随机数的二维数组?
【发布时间】:2020-03-17 11:40:41
【问题描述】:

我正在尝试获取通过将 2x150 数组处理为离散相关函数而获得的值的方差。为了做到这一点,我需要随机抽样 80% 的原始数据 N 次,这将允许我计算这些值的方差。 到目前为止,已经能够使用以下方法创建一组随机抽样的数据:

rand_indices = []
running_var = (len(find_length)*0.8)
x=0
while x<running_var:
    rand_inx = randint(0, (len(find_length)-1))
    rand_indices.append(rand_inx)
    x=x+1

它创建了一个长度为我的原始长度的 80% 的数组,其中包含要挑选和处理的随机选择的索引。 我的问题是我不确定如何迭代它以获得 N 组这些随机数,我认为理想情况下是在 Nx120 大小的数组中。到目前为止,我的整个代码是:

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from random import randint

useless, just_to, find_length = np.loadtxt("w2_mjy_final.dat").T
w2_dat = np.loadtxt("w2_mjy_final.dat")
w2_rel = np.delete(w2_dat, 2, axis = 1)
w2_array = np.asarray(w2_rel)
w1_dat = np.loadtxt("w1_mjy_final.dat")
w1_rel = np.delete(w1_dat, 2, axis=1)
w1_array = np.asarray(w1_rel)

peaks = []
y=1
N = 0
x = 0
z = 0
rand_indices = []
rand_indices2d = []
running_var = (len(find_length)*0.8)


while z<N:
    while x<running_var:
        rand_inx = randint(0, (len(find_length)-1))
        rand_indices.append(rand_inx)
        x=x+1
    rand_indices2d.append(rand_indices)    
    z=z+1

while y<N:

    w1_sampled = w1_array[rand_indices, :]
    w2_sampled = w2_array[rand_indices, :]

    w1s_t, w1s_dat = zip(*w1_sampled)
    w2s_t, w2s_dat = zip(*w2_sampled)

    w2s_mean = np.mean(w2s_dat)
    w2s_stdev = np.std(w2s_dat)

    w1s_mean = np.mean(w1s_dat)
    w1s_stdev = np.std(w1s_dat)

    taus = []
    dcfs = []
    bins = 40

    for i in w2s_t:
        for j in w1s_t:
            tau_datpoint = i-j
            taus.append(tau_datpoint)


    for k in w2s_dat:
        for l in w1s_dat:
            dcf_datpoint = ((k - w2s_mean)*(l - w1s_mean))/((w2s_stdev*w1s_stdev))
            dcfs.append(dcf_datpoint)     

    plotdat = np.vstack((taus, dcfs)).T
    sort_plotdat = sorted(plotdat, key=lambda x:x[0])  
    np.savetxt("w1sw2sarray.txt", sort_plotdat) 
    taus_sort, dcfs_sort = np.loadtxt("w1w2array.txt").T 
    dcfs_means, taubins_edges, taubins_number = stats.binned_statistic(taus_sort, dcfs_sort, statistic='mean', bins=bins)                               
    taubin_edge = np.delete(taubins_edges, 0)

    import operator
    indexs, values = max(enumerate(dcfs_means), key=operator.itemgetter(1))
    percents = values*0.8
    dcf_lists = dcfs_means.tolist()
    centarr_negs, centarr_poss = np.split(dcfs_means, [indexs])
    centind_negs = np.argmin(np.abs(centarr_negs - percents))
    centind_poss = np.argmin(np.abs(centarr_poss - percents))
    lagcent_negs = taubins_edges[centind_negs]
    lagcent_poss = taubins_edges[int((bins/2)+centind_poss)]
    sampled_peak = (np.abs(lagcent_poss - lagcent_negs)/2)+lagcent_negs

    peaks.append(sampled_peak)
    y=y+1 

print peaks

【问题讨论】:

  • 你知道不是 3 行:1) z = 0 2) while z &lt; N: 3) z = z + 1 你可以做for z in range(N):

标签: python arrays numpy random statistics


【解决方案1】:

既然你已经在使用 numpy,为什么不使用 np.random.randint

在你的情况下:

np.random.randint(len(find_length)-1, size=(N, running_var))

会给你一个 N*running_var 大小的矩阵,其中包含从 0len(find_length)-2 的随机整数条目。

示例用法:

>>> N=4
>>> running_var=6
>>> find_length = [1,2,3]
>>> np.random.randint(len(find_length)-1, size=(N, running_var))
array([[1, 0, 1, 0, 0, 1],
   [1, 0, 1, 1, 0, 0],
   [1, 1, 0, 0, 1, 0],
   [1, 1, 0, 1, 0, 1]])

【讨论】:

  • 嘿,为此欢呼,我明白你的意思,这似乎是一种更好的方法,但这里给出的代码导致一个空数组,输出只是 []。
  • @PatrickScott 我添加了一个示例 - 希望可以帮助您解决问题?
猜你喜欢
  • 2018-08-22
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多