【问题标题】:Using a list to select the same position in multiple arrays in Python在Python中使用列表选择多个数组中的相同位置
【发布时间】:2019-02-09 08:17:12
【问题描述】:

我有 53 个相同大小的二维 numpy 数组。我的第一个数组的值为零和一(有一些不重要的 -1(NaN 值)。我正在尝试使用第一个数组的值来选择剩余的值以制作单个元组/一维数组/向量按顺序包含52个元素。我想随机制作100个这些向量,将数据集比率保持在第一个数组中。所以我必须能够将它们放入单独的数据集中,在选择之前可以打乱。

为了实现这一点,我认为在第一个数组中创建一个元素列表及其位置很重要。要遵循的代码。

np.unique(index, return_counts = True)

# -1, 25. 0, 1210816. 1 , 1210816
###

wpx = 1916546
nwpx = 1210816
tpx = wpx + nwpx
wpxp = wpx/tpx * 100 #61%
nwpxp = nwpx/tpx * 100 #39%
#tpx
#wpxp
#nwpxp

###
#create a list of all values and their locations
#write values into txt/csv for portability to other projects using same data
indexdict = {}
i=0
j=0
indexfile = open('/location1/location2/file1.txt','w')
for ivalue in index:
#    while j < 5:
    while j < len(ivalue):
        #indexdict[(i,j)]=ivalue[j]
        indexfile.write("%i,%i,%i\n"%(i,j,ivalue[j]))
        j+=1
    j=0
    i+=1
#print(indexdict)
indexfile.close()

这会生成一个文本 (CSV) 文件,其中包含如下所示的数据(X、Y、值)

643,1613,1
643,1614,1
643,1615,1
643,1616,1
643,1617,0
643,1618,0
643,1619,0

然后我将这些值分成可以洗牌的东西。

###
#Read text in and sort values into separate "buckets"
indexfile = open('/location1/location2/file1.txt','r')
zerointerest=[]
TargetA=[]
TargetB=[]
for line in indexfile:
    line=line.rstrip()
    iline=line.split(',')
    #print(iline[2])
    if iline[2] == "-1":
        zerointerest.append((iline[0],iline[1]))
    elif iline[2] == "0":
        TargetA.append((iline[0],iline[1]))
    elif iline[2] == "1":
        TargetB.append((iline[0],iline[1]))  
indexfile.close()
#print(zerointerest)
#print(TargetA)
#print(TargetB)

这就是我卡住的地方。我已经创建了一个值和位置列表,但我不知道如何继续。在将数组 53 的值添加到它的末尾之前,我无法弄清楚如何使用我创建的列表来选择数组 1-52 中相同位置的值,IE:

[25,26,27,28,29,33,35,37,40,45,50,55,60,75,80,90,100,110,105,100,95,90,85,80,100,120,140,150,150,150,150,150,150,150,150,150,150,145,140,135,130,125,120,115,110,100,100,100,100,100,100,1]

【问题讨论】:

  • 如果您可以简化提供给minimal reproducible example 的代码,我将对您和这里的人们有所帮助,例如,您可以生成带有随机数据的示例数组以重现问题,所以我们可以测试并给出合适的答案
  • 对不起,我还不太精通python。我无法理解如何使我的代码最小化(我的印象是它已经是)。我也不确定如何在不共享我的数据集的情况下生成示例数组。
  • 您也可以使用numpy.random.choice 来“从给定的一维数组生成随机样本”,numpy.random.shuffle 来“通过改组其内容来就地修改序列”。

标签: arrays python-3.x pandas numpy time-series


【解决方案1】:

这里有一些灵感代码:

import numpy as np

a_list_of_2D_arrays = [ np.arange(9).reshape(3, 3) for _ in range(5) ]

# Create a uniaue 3D array by concatenating the arrays:
a_3D_array = np.dstack(a_list_of_2D_arrays)

print(a_3D_array.shape)  # (3, 3, 5)

mask = np.array([[0, 0, 1],   # your first array?
                 [-1, 0, 1],
                 [1, 0, -1]])

mask.nonzero() # a tuple of indexes:
# gives (array([0, 1, 1, 2, 2]), array([2, 0, 2, 0, 2]))

(mask == 1).nonzero()  # consider only the 1 values
# gives (array([0, 1, 2]), array([2, 2, 0]))

a_3D_array[mask.nonzero()]  # extract the values along the third dim
                            # at the given indexes

最后一行给出:

array([[2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3],
       [5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6],
       [8, 8, 8, 8, 8]])

【讨论】:

  • 非常感谢,我构思了一个 3 维(堆栈)数组,这对于我决定使用更小的数据部分非常有用(非常感谢) .但是,我不能以主要方式使用它,因为我的数组绝对庞大,内存很快就会成为问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-02
  • 2014-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
相关资源
最近更新 更多