Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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 从三维矩阵中删除元素并转换为列表列表(二维数组)_Python_Numpy_Reshape - Fatal编程技术网

Python 从三维矩阵中删除元素并转换为列表列表(二维数组)

Python 从三维矩阵中删除元素并转换为列表列表(二维数组),python,numpy,reshape,Python,Numpy,Reshape,首先,我将介绍我正在做的事情,然后介绍我想要实现的目标 import numpy as np from scipy.spatial.distance import cdist def diff_len(string1, string2): return abs(len(string1) - len(string2)) def diff_len_2(string1, string2): return abs(len(string1) - len(string2))*2 def

首先,我将介绍我正在做的事情,然后介绍我想要实现的目标

import numpy as np
from scipy.spatial.distance import cdist

def diff_len(string1, string2):
    return abs(len(string1) - len(string2))

def diff_len_2(string1, string2):
    return abs(len(string1) - len(string2))*2

def diff_len_minus(string1, string2):
    return abs(len(string1) - len(string2))-3

first = np.array(["hello", "hello", "hellllo"])
second = np.array(["hlo", "halo", "alle"])

d1 = cdist(first[:, np.newaxis], second[:, np.newaxis], lambda a, b: diff_len(a[0], b[0]))
d2 = cdist(first[:, np.newaxis], second[:, np.newaxis], lambda a, b: diff_len_2(a[0], b[0]))
d3 = cdist(first[:, np.newaxis], second[:, np.newaxis], lambda a, b: diff_len_minus(a[0], b[0]))

mat3D = np.stack((d1, d2, d3))

mat3D.reshape(mat3D.shape[0], -1).T


# distance 1

[[2. 1. 1.]
 [2. 1. 1.]
 [4. 3. 3.]]


# distance 2   

[[4. 2. 2.]
 [4. 2. 2.]
 [8. 6. 6.]]


# distance 3

[[-1. -2. -2.]
 [-1. -2. -2.]
 [ 1.  0.  0.]]
上面我们得到了不同矩阵中每对字符串的所有特征

例如,每个矩阵中索引[0,0]处的元素对应于特定对的每个特征。在我的示例中,它是索引[0,0]处的一对(“hello”,“hlo”),它的特征是[2,4,-1]

我将所有矩阵堆叠在一起,这样我就得到了一个3D矩阵,每个维度对应一个特征,每个索引(行、列)对应一个特定的对

然后每对字符串都有一个特征向量

最后,我重塑我的3D数组,得到一个2D数组,其中包含所有对的所有特征,每对对应一行

[[ 2.  4. -1.]
 [ 1.  2. -2.]
 [ 1.  2. -2.]
 [ 2.  4. -1.]
 [ 1.  2. -2.]
 [ 1.  2. -2.]
 [ 4.  8.  1.]
 [ 3.  6.  0.]
 [ 3.  6.  0.]]
这是我的问题 我有一个数组,其中包含对的所有索引:

np.array([[0 0],
 [0, 1],
 [0, 2],
 [1, 0],
 [1, 1],
 [1, 2],
 [2, 0],
 [2, 1],
 [2, 2]])
但我有我的3d数组的某些索引,我不想把它放在我的最终2D数组中。例如,我不想要索引(1,1)和(0,1),所以我删除了它们

np.array([[0 0],
 [0, 2],
 [1, 0],
 [1, 2],
 [2, 0],
 [2, 1],
 [2, 2]])
现在,我如何实现与我所做的相同的事情(获取我的2d数组),但不计算我删除的索引,在2d数组中获取相同的顺序,而只是删除特定的索引对

我可以这样做并在阵列上循环:

mat3D[:,0,0]
mat3D[:,0,1]
mat3D[:,0,2]
mat3D[:,1,0]
...
但是有没有办法用我的索引数组一次进行选择呢

提前谢谢你的帮助,我希望我的问题是清楚的