Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用numpy获取多维数组的所有对角线(包括小对角线)_Python_Arrays_Numpy - Fatal编程技术网

Python 使用numpy获取多维数组的所有对角线(包括小对角线)

Python 使用numpy获取多维数组的所有对角线(包括小对角线),python,arrays,numpy,Python,Arrays,Numpy,我试图得到多维对象的对角线(和反对角线)元素 这些形状类似于(2,2),(3,3,3),(4,4,4,4),(5,5,5,5)等等。不过,我认为这并不太相关 我用ndarray的.diagonal方法找到了获得对角线元素的方法,但是我找不到任何可以得到反对角线的东西 那么我必须用手来做吗 [编辑] 所以 所以我想要“水平”对角线,比如: [54, 45, 49], [ 4. 20, 75], etc. 但从某种意义上说,它们也是水平的 [ 6, 45, 31], [39, 20, 18] 然

我试图得到多维对象的对角线(和反对角线)元素

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

我用
ndarray
.diagonal
方法找到了获得对角线元素的方法,但是我找不到任何可以得到反对角线的东西

那么我必须用手来做吗

[编辑] 所以

所以我想要“水平”对角线,比如:

[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]
然后这些也是“更长”的对角线,就像前一条一样(如果你把矩阵看作一个三维几何结构,在几何意义上更长,数字放在立方体的顶点上,在它们之间的线的中间)

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

[31, 45, 6],
[31, 83, 38]  # this is the first classical anti-diagonal in the first matrix
当然,我没有把所有的对角线都放在这里,但这是我的要求。我不需要从任何主/反对角线偏移的对角线


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

如果您想要从
corner1
corner2
的对角线,并以

(0,0,0,…,0),(0,0,0,…,1),(1,1,1,…,1)

其中0表示“0处的此维度”,1表示“1处的此维度/结束”

然后这将返回从
corner1
corner2
得到的值,假设数组在每个维度上都具有相同的大小

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))])

这应该可以通过使用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.

列出多维数组的示例输入和预期输出,如
(3,3,3)
?三维或四维数组的(主对角线和反对角线)是什么?因为有很多them@Divakar是 啊我想要所有的。我想要所有可能的对角线。我正在尝试建立一个多维版本的tic-tac-toeyah。。。我们想要一个例子:)你说的小的是什么意思?@Divakar所以如果一个大的只从上到下,从左到右,一个小的会切换其中一个。我会想出一个关于我需要什么的一般数学规则。但我想我得到了我的答案:这在numpy中可能是不可能的,一旦我有了我的数学公式,用python实现它将是微不足道的。
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))])
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.