相关文章推荐
耍酷的猴子  ·  URL: ...·  2 周前    · 
开朗的咖啡  ·  android ...·  1 年前    · 
豪气的苦瓜  ·  with root cause ...·  1 年前    · 
高兴的丝瓜  ·  Java ...·  2 年前    · 

goal:把(2, 3, 2)的matrix打平成(2, 3*2)的matrix

import numpy as np
x =  np.arange(12).reshape((2, 3, 2))
Out[44]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],
       [[ 6,  7],
        [ 8,  9],
        [10, 11]]])
x = x.reshape(x.shape[0],-1)
#x = x.reshape(x.shape[0], 3*2)
Out[47]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])
                    goal:把(2, 3, 2)的matrix打平成(2, 3*2)的matriximport numpy as npx =  np.arange(12).reshape((2, 3, 2))Out[44]: array([[[ 0,  1],        [ 2,  3],        [ 4,  5]],       [[ 6,  7],        [ 8,  9],        [10, 11]]])        x = x.reshape(x.shape[0],-1
				
numpy库对多维数组有非常灵巧的处理方式,主要的处理方法有: .reshape(shape) : 不改变数组元素,返回一个shape形状的数组,原数组不变 .resize(shape) : 与.reshape()功能一致,但修改原数组 In [22]: a = np.arange(20) #原数组不变 In [23]: a.reshape([4,5]) Out[23]: array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]) In [24]: a
在 MATLAB 中获取任何 ND 矩阵,并使用 fDim.m 将其展平为 N x 大小(ND,dim)二维矩阵。 在多维矩阵上编写复杂操作时,这通常是必要的。 还希望在展平后,保留的维度具有正确的顺序。 这对于矢量处理尤其重要。 一旦展平,并对二维​​矩阵执行了操作,通常需要将二维矩阵转换回原始多维矩阵。 这可以使用 eDim.m 执行。 有关使用各种矩阵大小和维度的测试用例,请参阅 multiDimDemo.m。 例子: 给定一个多维向量: >> vector_1 = rand(3,3,3,4,5,3); 将其展平为二维矩阵,保留对应于正确 xyz 顺序的第 3 维: >> [vector_1_f, fSeq] = fDim(vector_1,3); 将扁平矩阵转换回它的原始多维形式,仍然保留正确的 xyz 顺序: >> vector_1_e = eDim(vector_
reshape(shape) : 不改变数组元素,返回一个shape形状的数组,原数组不变。是对每行元素进行处理 resize(shape) : 与.reshape()功能一致,但修改原数组 In [1]: a = np.arange(20) #原数组不变 In [2]: a.reshape([4,5]) Out[2]: array([[ 0, 1, 2, 3, 4],
python numpy.matrix.reshape函数详解 The criterion to satisfy for providing the new shape is that ‘The new shape should be compatible with the original shape’ numpy allows us to give one of new shape param...
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) 这个三维矩阵包含两个二维矩阵,每个二维矩阵包含两行两列的数据。你可以使用arr.shape来查看这个矩阵的形状,例如: print(arr.shape) 输出结果为(2, 2, 2),表示这个矩阵有两个二维矩阵,每个二维矩阵有两行两列的数据。