将索引附加到numpy数组

时间:2018-03-05 17:58:03

标签: python arrays numpy

我通过迭代行和列并关联值来创建变量corr_matrix

import numpy as np
import random

enc_dict = {k: int(random.uniform(1,24)) for k in range(24)}
ret_dict = {k: int(random.uniform(1,24)) for k in range(24)}

corr_matrix=np.zeros((24,24))
ind_matrix = np.zeros((24,24))

data = np.random.rand(24,24)
for enc_row in range(0,24):
            for ret_col in range(0,24):
                corr_matrix[enc_row, ret_col] = np.corrcoef(data[enc_row,:], data[ret_col,:])[0,1]
                if enc_dict[enc_row] == ret_dict[ret_col]:
                    ind_matrix = np.append(ind_matrix, [[enc_row, ret_col]])

我想将索引存储在矩阵中,其中enc_dict[enc_row] == ret_dict[ret_col]作为变量用于索引corr_matrix。我可以打印这些值,但我无法弄清楚如何将它们存储在一个变量中,以便我以后可以使用它们进行索引。

我想:

  1. 创建一个变量ind_matrix,它是上述陈述为真的索引。

  2. 我想在我的相关矩阵中使用ind_matrix进行索引。我希望能够索引整行以及上述陈述为真的确切值(enc_dict[enc_row] == ret_dict[ret_col]

  3. 我尝试ind_matrix = np.append(ind_matrix, [[enc_row, ret_col]])给了我正确的值,但由于某些原因它在#s之前有很多0。此外,它不允许我将每对点一起调用以用于索引。我希望能够做corr_matrix[ind_matrix[1]]

    之类的事情

1 个答案:

答案 0 :(得分:1)

以下是您的代码的修改版本,其中包含一些建议和评论:

import numpy as np

# when indices are 0, 1, 2, ... don't use dictionary
# also for integer values use randint
enc_ = np.random.randint(1, 24, (24,))
ret_ = np.random.randint(1, 24, (24,))

data = np.random.rand(24,24)
# np.corrcoef is vectorized, no need to loop:
corr_matrix = np.corrcoef(data)
# the following is the clearest, but maybe not the fastest way of generating
# your index array:
ind_matrix = np.argwhere(np.equal.outer(enc_, ret_))

# this can't be used for indexing directly, you'll have to choose
# one of the following idioms

# EITHER spread to two index arrays
I, J = ind_matrix.T
# or directly I, J = np.where(np.equal.outer(enc_, ret_))
# single index
print(corr_matrix[I[1], J[1]])
# multiple indices
print(corr_matrix[I[[1,2,0]], J[[1,2,0]]])
# whole row
print(corr_matrix[I[1]])

# OR use tuple conversion
ind_matrix = np.array(ind_matrix)
# single index
print(corr_matrix[(*ind_matrix[1],)])
# multiple indices
print(corr_matrix[(*zip(*ind_matrix[[1,2,0]],),)])
# whole row
print(corr_matrix[ind_matrix[1, 0]])

# OR if you do not plan to use multiple indices
as_tuple = list(map(tuple, ind_matrix))
# single index
print(corr_matrix[as_tuple[1]])
# whole row
print(corr_matrix[as_tuple[1][0]])