获取具有numpy

时间:2016-03-25 16:07:04

标签: python arrays numpy

我试图获得多维物体的对角线(和反对角线)元素。

形状类似于(2,2)(3,3,3)(4,4,4,4)(5,5,5,5,5)等等。我不认为这太相关了。

我找到了使用.diagonal的{​​{1}}方法获取对角线元素的方法,但我找不到任何可以让我获得反对角线的方法。

我必须手工完成吗?

[编辑]

ndarray

所以我想要“横向”对角线,如:

array([[[54, 81, 31],
        [ 4, 83, 18],
        [38, 32, 52]],

       [[ 2, 45, 87],
        [33, 20,  3],
        [85, 31, 35]],

       [[ 6, 11, 49],
        [39, 76, 75],
        [28, 52, 63]]])

但在某种意义上这些也是水平的

[54, 45, 49],
[ 4. 20, 75],
etc.

然后是“垂直”之类的:

[ 6, 45, 31],
[39, 20, 18]

但是这些也是垂直的:

[54, 33, 28],
[81, 20, 52],
etc.

然后是这个,不过你称之为

[6, 33, 38],
[11, 20, 32]

然后这些也是“更长”的对角线,就像前一个(在几何意义上更长,如果你认为矩阵是一个三维几何结构,数字放在一个立方体的顶点上,并且在他们之间的界线中间)

[54, 20, 63]

然后,在这个矩阵中,一个小的对角线将是从右到左或从下到上(但不是同时)的对角线,如:

[38, 20, 49],
[6, 20, 52]

当然,我没有把所有的对角线放在这里,但这是我的要求。我不需要偏离任何主/反对角线的对角线。

如果你也知道这是不可能的,请告诉我,因为我会手工完成。

2 个答案:

答案 0 :(得分:1)

如果您需要从corner1corner2的对角线,并以

的形式定义角落

(0,0,0,...,0) , (0,0,0,....,1),...,(1,1,1,...,1)

其中0表示“此维度为0 ”,1表示“此维度为-1 /结束

然后这会返回从corner1corner2的值,假设数组在每个维度都有相同的大小。

import numpy
def diagonal(arr,corner1,corner2):
    arr=numpy.array(arr)
    #Change values to fit array
    corner1Copy=(len(arr)-1)*numpy.array(corner1)
    corner2Copy=(len(arr)-1)*numpy.array(corner2)

    #create return array by running from corner1 to corner2 and returning the values
    return [arr[tuple((i*corner2Copy+(len(arr)-i-1)*corner1Copy)/(len(arr)-1))] for i in range(len(arr))]

以下是两个小测试案例,但我建议再创建一些,以防我错过了一些内容:

arr=[[[i+j+k for i in range(5)]for j in range(5)] for k in range(5)]
corner1=[0,0,0]
corner2=[1,1,1]

#returns arr[0,0,0],arr[1,1,1],....,arr[-1,-1,-1]
print(diagonal(arr,corner1,corner2))
print([arr[i][i][i] for i in range(len(arr))])

arr2=[[i+j for i in range(5)]for j in range(5)]

corner12=[0,1]
corner22=[1,1]
#return arr[0,-1],arr[1,-1],....,arr[-1,-1]
print(diagonal(arr2,corner12,corner22))
print([arr2[i][-1] for i in range(len(arr2))])

答案 1 :(得分:0)

这应该使用numpy数组完成。它为生成器提供了所有对角线,它们的维数与数组相同。 还提供原始数组的视图(而不是副本)。 解释在代码下方。

import numpy as np
def get_diagonals_np(arr):
    if arr.ndim == 1:
        yield arr
    else:
        yield from get_diagonals_np(arr.diagonal())
        yield from get_diagonals_np(np.flip(arr, 0).diagonal())

# The function is recursive. How it works is best shown by example.
# 1d: arr = [0, 1] then the diagonal is also [0, 1].

# 2d: arr = [[0, 1],
#            [2, 3]]
# The numpy diagonal method gives the main diagonal = [0, 3], a 1d array
# which is recursively passed to the function.
# To get the opposite diagonal we first use the numpy flip function to
# reverse the order of the elements along the given dimension, 0 in this case.
# This gives [[2, 3],
#              0, 1]]
# The numpy diagonal method gives the main diagonal = [2, 1], a 2d array
# which is recursively passed to the function.

# 3d: arr = [[[0, 1],
#             [2, 3]],
#            [[4, 5],
#             [6, 7]]]
# The numpy diagonal method gives the main diagonals in the 3rd dimension
# as rows.
#            [[0, 6],
#             [1, 7]]
# Note that the diagonals of this array are [0, 7] and [6, 1] which are
# retrieved by a recurive call to the function.
# We now have 2 of the 4 3-agonals of the orginal 3d arr.
# To get the opposite 3-agonals we first use the numpy flip function which
# gives
#           [[[4, 5],
#             [6, 7]],
#            [[0, 1],
#             [2, 3]]]
# and a call to the numpy diagonal method gives
#            [[4, 2],
#             [5, 3]]
# The diagonals of this array are [4, 3] and [2, 5]
# We now have all four 3-agonals of the original 3d arr.