简体   繁体   English

在 python 中反转二维矩阵

[英]reversing 2d matrix in python

I got this matrix:我得到了这个矩阵:

[[9 9 9]
 [1 0 5]
 [2 0 1]]

and im trying to reverse the order of the sets to be like this:我试图将集合的顺序颠倒成这样:

[[2 0 1]
 [1 0 5]
 [9 9 9]]

This is what i tried but none of them is giving me the answer I desire:这是我尝试过的,但他们都没有给我想要的答案:

sorted_ids=np.flipud(sorted_ids)

this gives me these values which are wrong:这给了我这些错误的值:

 [[ 9  9  9] 
  [ 6 -9  5] 
  [-2  5  3]]

and

sorted_ids=sorted_ids[::-1]

this does not change the output at all这根本不会改变 output

the output of sorted_ids.tolist(): sorted_ids.tolist() 的 output:

[1, 3, 4, 0, 2] [1, 3, 4, 0, 2]

this is the full code to get some context这是获取上下文的完整代码

def nearest_neighbor(v, candidates, k=1, cosine_similarity=cosine_similarity):


similarity_l = []

# for each candidate vector...
for row in candidates:
    # get the cosine similarity
    cos_similarity = cosine_similarity(v, row)

    # append the similarity to the list
    similarity_l.append(cos_similarity)

# sort the similarity list and get the indices of the sorted list    
sorted_ids =  np.argsort(similarity_l)  

# Reverse the order of the sorted_ids array

sorted_ids=sorted_ids[::-1]
# get the indices of the k most similar candidate vectors
k_idx =  sorted_ids[-k:]
### END CODE HERE ###
return k_idx
x = [[9, 9, 9], [1, 0, 5], [2, 0, 1]]
x = x[::-1]
print(x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM