计算按前两列的索引分组的numpy数组条目的第N列之和? [英] calculate sum of Nth column of numpy array entry grouped by the indices in first two columns?

查看:53
本文介绍了计算按前两列的索引分组的numpy数组条目的第N列之和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以以下方式循环遍历 check_matrix :代码可以识别第一个和第二个元素是 1 1 还是 1 2 等?然后,对于每个对的单独类别,即 1,1 1,2 2,2 ,该代码应存储在新矩阵中,最后一个元素的总和(在本例中为8)乘以 exp(-i * q(check_matrix [k] [2:5] -check_matrix [k] [5:8])),其中 i 是iota(复数), k 是check_matrix上的运行索引,而 q 是定义如下的向量.因此,共有20个 q 向量.

I would like to loop over following check_matrix in such a way that code recognize whether the first and second element is 1 and 1 or 1 and 2 etc? Then for each separate class of pair i.e. 1,1 or 1,2 or 2,2, the code should store in the new matrices, the sum of last element (which in this case has index 8) times exp(-i*q(check_matrix[k][2:5]-check_matrix[k][5:8])), where i is iota (complex number), k is the running index on check_matrix and q is a vector defined as given below. So there are 20 q vectors.

import numpy as np

q= []
for i in np.linspace(0, 10, 20):
    q.append(np.array((0, 0, i)))
q = np.array(q)

check_matrix = np.array([[1, 1, 0, 0, 0, 0,    0,       -0.7977, -0.243293],
                         [1, 1, 0, 0, 0, 0,    0,       1.5954,  0.004567],
                         [1, 2, 0, 0, 0, -1,   0,       0,       1.126557],
                         [2, 1, 0, 0, 0, 0.5,  0.86603, 1.5954,  0.038934],
                         [2, 1, 0, 0, 0, 2,    0,       -0.7977, -0.015192],
                         [2, 2, 0, 0, 0, -0.5, 0.86603, 1.5954,  0.21394]])

这意味着原则上我将必须具有20个形状为2x2的矩阵,分别与每个 q 向量相对应.

This means in principles I will have to have 20 matrices of shape 2x2, corresponding to each q vector.

目前,即使我在 Matrices 中追加了代码,我的代码也只给出了一个矩阵,这似乎是最后一个矩阵.我的代码如下所示,

For the moment my code is giving only one matrix, which appears to be the last one, even though I am appending in the Matrices. My code looks like below,

for i in range(2):
    i = i+1
    for j in range(2):
        j= j +1
        j_list = []
        Matrices = []
        for k in range(len(check_matrix)):
            if check_matrix[k][0] == i and check_matrix[k][1] == j:
                j_list.append(check_matrix[k][8]*np.exp(-1J*np.dot(q,(np.subtract(check_matrix[k][2:5],check_matrix[k][5:8])))))
                j_11 = np.sum(j_list)
                I_matrix[i-1][j-1] = j_11
                Matrices.append(I_matrix)

I_matrix定义如下:

I_matrix is defined as below:

I_matrix= np.zeros((2,2),dtype=np.complex_)

此刻我得到以下输出.

Matrices = [array([[-0.66071446-0.77603624j, -0.29038112+2.34855023j],         [-0.31387562-0.08116629j,  4.2788    +0.j        ]])]

但是,我希望获得一个与每个 q 值相对应的矩阵,这意味着在这种情况下,总共应该有20个矩阵,其中每个2x2矩阵元素都将包含和,以使元素属于1,1和1,2和2,2对按以下方式

But, I desire to get a matrix corresponding to each q value meaning that in total there should be 20 matrices in this case, where each 2x2 matrix element would be containing sums such that elements belong to 1,1 and 1,2 and 2,2 pairs in following manner

 array([[11., 12.],
       [21., 22.]])

非常感谢您提出的更正建议.提前致谢!

I shall highly appreciate your suggestion to correct it. Thanks in advance!

推荐答案

我很确定您可以以更简单的方式解决此问题,但我不确定100%的理解是正确的,但是下面的代码可以我想你想要的.如果您可以检查结果是否有效,建议您这样做.

I am pretty sure you can solve this problem in an easier way and I am not 100% sure that I understood you correctly, but here is some code that does what I think you want. If you have a possibility to check if the results are valid, I would suggest you do so.

import numpy as np

n = 20
q = np.zeros((20, 3))
q[:, -1] = np.linspace(0, 10, n)

check_matrix = np.array([[1, 1, 0, 0, 0, 0,    0,       -0.7977, -0.243293],
                         [1, 1, 0, 0, 0, 0,    0,       1.5954,  0.004567],
                         [1, 2, 0, 0, 0, -1,   0,       0,       1.126557],
                         [2, 1, 0, 0, 0, 0.5,  0.86603, 1.5954,  0.038934],
                         [2, 1, 0, 0, 0, 2,    0,       -0.7977, -0.015192],
                         [2, 2, 0, 0, 0, -0.5, 0.86603, 1.5954,  0.21394]])
check_matrix[:, :2] -= 1  # python indexing is zero based

matrices = np.zeros((n, 2, 2), dtype=np.complex_)

for i in range(2):
    for j in range(2):
        k_list = []
        for k in range(len(check_matrix)):
            if check_matrix[k][0] == i and check_matrix[k][1] == j:
                k_list.append(check_matrix[k][8] *
                              np.exp(-1J * np.dot(q, check_matrix[k][2:5] 
                                                     - check_matrix[k][5:8])))

        matrices[:, i, j] = np.sum(k_list, axis=0)

注意:我将您的索引更改为一致从零开始的索引.

NOTE: I changed your indices to have consistent zero-based indexing.

这是另一种方法,其中我将k循环替换为矢量版本:

Here is another approach where I replaced the k-loop with a vectored version:

for i in range(2):
    for j in range(2):
        k = np.logical_and(check_matrix[:, 0] == i, check_matrix[:, 1] == j)
        temp = np.dot(check_matrix[k, 2:5] - check_matrix[k, 5:8], q[:, :, np.newaxis])[..., 0]
        temp = check_matrix[k, 8:] * np.exp(-1J * temp)
        matrices[:, i, j] = np.sum(temp, axis=0)

这篇关于计算按前两列的索引分组的numpy数组条目的第N列之和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆