python访问矩阵某一元素_在python中访问特定矩阵行中的非零元素

本文探讨了如何在Python中使用Scipy的sparse模块访问和操作稀疏矩阵,包括创建Compressed Sparse Row (CSR) 矩阵,并通过矩阵乘法和nonzero函数来获取非零元素及其索引。示例展示了如何利用这些功能进行逐行运算和求平方和。
摘要由CSDN通过智能技术生成

关于第一个元组代表什么,我有点困惑 . 它是稀疏数组的索引和值吗?例如

In [4]: arrays=(np.array([0, 1, 2], dtype=int), np.array([2, 0, 0], dtype=int), np.array([2, 1, 3], dtype=float))

...

In [6]: from scipy import sparse

In [7]: M=sparse.csr_matrix((arrays[2],(arrays[0],arrays[1])))

In [8]: M

Out[8]:

<3x3 sparse matrix of type ''

with 3 stored elements in Compressed Sparse Row format>

In [9]: M.A

Out[9]:

array([[ 0., 0., 2.],

[ 1., 0., 0.],

[ 3., 0., 0.]])

In [10]: print(M)

(0, 2) 2.0

(1, 0) 1.0

(2, 0) 3.0

矩阵乘法是为这样的数组定义的:

In [12]: M*M.T

Out[12]:

<3x3 sparse matrix of type ''

with 5 stored elements in Compressed Sparse Row format>

In [13]: (M*M.T).A

Out[13]:

array([[ 4., 0., 0.],

[ 0., 1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值