栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytorch+numpy实现,按行从大到小排序 ,找出值大于0.5所对应的行列值

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytorch+numpy实现,按行从大到小排序 ,找出值大于0.5所对应的行列值

问题

pytorch+numpy实现,按行从大到小排序 ,找出值大于0.5所对应的行列值。

代码
import torch
x = torch.randn(3,4)
print(x)
sorted, indices = torch.sort(x, descending=True)  #按行从大到小排序 (即反序)
print(sorted)
print(indices)
# sorted[:, sorted[:,]>0.5]
mz_items = sorted[:,] > 0.5
print(mz_items)
import numpy as np
mz_items_np = mz_items.numpy()
index = np.argwhere( mz_items_np == True )
print(index)

index[:, 1] = index[:, 1] + 3  # [0, n_items) -> [n_users, n_users+n_items)
vals = sorted[mz_items]
adj = sp.coo_matrix((vals, (index[:, 0], index[:, 1])), shape=(7, 7))
print(adj)

结果:

tensor([[-1.3632,  1.2630,  1.3667,  0.1094],
        [ 0.0844, -0.3690, -0.6069, -0.0294],
        [-0.0415, -0.6528,  2.1679,  0.4352]])
tensor([[ 1.3667,  1.2630,  0.1094, -1.3632],
        [ 0.0844, -0.0294, -0.3690, -0.6069],
        [ 2.1679,  0.4352, -0.0415, -0.6528]])
tensor([[2, 1, 3, 0],
        [0, 3, 1, 2],
        [2, 3, 0, 1]])
tensor([[ True,  True, False, False],
        [False, False, False, False],
        [ True, False, False, False]])
[[0 0]
 [0 1]
 [2 0]]
  (0, 3)	1.3667
  (0, 4)	1.2630
  (2, 3)	2.1679

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498871.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号