我正在尝试使用列表列表中的值
形成一个名为new_matrix的新矩阵similair = [1,2,3]
indices=[[0 2]
[1 2]
[2 1]]
new_matrix = np.array([])
for list in indices:
newrow=[]
for number in list:
newrow.append(similair[number])
new_matrix.vstack((new_matrix, np.array(newrow)) )
print new_matrix
渴望出局
values
[1,3]
[2,3]
[3,2]
然而我收到以下错误
new_matrix.vstack((new_matrix,np.array(newrow)))
AttributeError:'numpy.ndarray'对象没有属性'vstack'
答案 0 :(得分:4)
import numpy as np
similair = np.array([1,2,3])
indices=np.array([[0, 2],
[1, 2],
[2, 1]])
new_matrix = similair[indices]
print(new_matrix)
产量
[[1 3]
[2 3]
[3 2]]
答案 1 :(得分:0)
您可以按如下方式构建矩阵
similair = [1,2,3]
indices=[[0 2]
[1 2]
[2 1]]
new_matrix = [[similar[i] for i in index] for index in indices]
如果你想让new_matrix成为一个numpy数组,你可以使用array
命令
import numpy as np
new_matrix = np.array(new_matrix)