Python 如何在不使用重塑的情况下更改矩阵的维度
How to change a matrix's dimension without using reshape in Python
下面是(3*3)的矩阵
a_matrix=np.array([[2,3,5],[3,2,7],[1,4,2]])
我想将其更改为 (9*1),即
[[2],[3],[5],[3],[2],[7],[1],[4],[2]]
问题是我需要在不使用 numpy 中的 reshape 方法的情况下执行此操作。顺便说一句,下面是我所做的,这是错误的。有人可以帮我吗?顺便说一句,**我不能使用那些预定义的方法来做,我必须用我自己的方法来实现它。 **。感谢任何帮助!!!
import numpy as np
a_list=[]
a_matrix=np.array([[2,3,5],[3,2,7],[1,4,2]]) #3*3 matrix
for i in range(3):
a_list.extend(a_matrix[i,:])
a_list=np.asarray(a_list) #To convert the list to numpy array
print(a_list.T.shape) #To print the shape of transpose
--->(9,) # 我想要 (9,1) 而不是 (9,)
将其展平并使用列表理解
result = np.array([[x] for x in a_matrix.ravel()])
您可以使用 np.ravel
然后添加一个虚拟轴。
l_list = a_matrix.ravel()[:,None]
编辑:
如果你想要一个无 numpy 的方法:
l_list = []
for i in range(3):
for j in range(3):
# replace [i][j] with [i,j] if a_matrix
# is allowed to be a numpy array
l_list.append([a_matrix[i][j]])
如果你想在不使用 ravel
或 reshape
的情况下将结果作为 numpy 数组,你可以提前创建输出数组
l_list = np.empty((9,1))
for i in range(3):
for j in range(3):
# replace [i][j] with [i,j] if a_matrix
# is allowed to be a numpy array
l_list[i*3 + j] = a_matrix[i][j]
这是您自己编写解决方案的答案(根据您的作业要求)。
import numpy as np
a_matrix = np.array([[2,3,5],[3,2,7],[1,4,2]])
a_list = [[elem] for row in a_matrix for elem in row]
a_list = np.asarray(a_list)
print(a_list.T.shape)
输出应该符合预期。
纯列表操作:
In [122]: alist = [[2,3,5],[3,2,7],[1,4,2]]
In [123]: [[i] for x in alist for i in x]
Out[123]: [[2], [3], [5], [3], [2], [7], [1], [4], [2]]
忘记之前或之后的 np.array
东西。如果你不能使用 reshape
,那么谈论 numpy
就没有意义了。 shape
是 numpy 数组的组成部分,用 reshape
改变它是一个基本操作。
下面是(3*3)的矩阵
a_matrix=np.array([[2,3,5],[3,2,7],[1,4,2]])
我想将其更改为 (9*1),即
[[2],[3],[5],[3],[2],[7],[1],[4],[2]]
问题是我需要在不使用 numpy 中的 reshape 方法的情况下执行此操作。顺便说一句,下面是我所做的,这是错误的。有人可以帮我吗?顺便说一句,**我不能使用那些预定义的方法来做,我必须用我自己的方法来实现它。 **。感谢任何帮助!!!
import numpy as np
a_list=[]
a_matrix=np.array([[2,3,5],[3,2,7],[1,4,2]]) #3*3 matrix
for i in range(3):
a_list.extend(a_matrix[i,:])
a_list=np.asarray(a_list) #To convert the list to numpy array
print(a_list.T.shape) #To print the shape of transpose
--->(9,) # 我想要 (9,1) 而不是 (9,)
将其展平并使用列表理解
result = np.array([[x] for x in a_matrix.ravel()])
您可以使用 np.ravel
然后添加一个虚拟轴。
l_list = a_matrix.ravel()[:,None]
编辑: 如果你想要一个无 numpy 的方法:
l_list = []
for i in range(3):
for j in range(3):
# replace [i][j] with [i,j] if a_matrix
# is allowed to be a numpy array
l_list.append([a_matrix[i][j]])
如果你想在不使用 ravel
或 reshape
的情况下将结果作为 numpy 数组,你可以提前创建输出数组
l_list = np.empty((9,1))
for i in range(3):
for j in range(3):
# replace [i][j] with [i,j] if a_matrix
# is allowed to be a numpy array
l_list[i*3 + j] = a_matrix[i][j]
这是您自己编写解决方案的答案(根据您的作业要求)。
import numpy as np
a_matrix = np.array([[2,3,5],[3,2,7],[1,4,2]])
a_list = [[elem] for row in a_matrix for elem in row]
a_list = np.asarray(a_list)
print(a_list.T.shape)
输出应该符合预期。
纯列表操作:
In [122]: alist = [[2,3,5],[3,2,7],[1,4,2]]
In [123]: [[i] for x in alist for i in x]
Out[123]: [[2], [3], [5], [3], [2], [7], [1], [4], [2]]
忘记之前或之后的 np.array
东西。如果你不能使用 reshape
,那么谈论 numpy
就没有意义了。 shape
是 numpy 数组的组成部分,用 reshape
改变它是一个基本操作。