当前位置: 首页 > news >正文

sp.coo_matrix(), sp.eye()

sp.coo_matrix()

坐标格式的稀疏矩阵。

也被称为“ijv”或“三重”格式。

这可以通过几种方式实例化:

        coo_matrix(D)

                密集矩阵D;

        coo_matrix(S)

                与另一个稀疏矩阵S(等价于S.tocoo())

        coo_matrix((M, N), [dtype])

                构造一个形状为(M, N)的空矩阵

        coo_matrix((data, (i, j)), [shape=(M, N)])

                从三个数组构造:

                        1. data[:] the entries of the matrix, in any order       矩阵的元素,以任何顺序  

                        2. i[:]  the row indices of the matrix entries  矩阵项的行下标

                        3. j[:]  the column indices of the matrix entries 矩阵项的列下标

参数:

 dtype : dtype
        Data type of the matrix
    shape : 2-tuple
        Shape of the matrix
    ndim : int
        Number of dimensions (this is always 2)
    nnz
        Number of stored values, including explicit zeros
    data
        COO format data array of the matrix
    row
        COO format row index array of the matrix
    col
        COO format column index array of the matrix

稀疏矩阵可以用于算术运算:它们支持加,减,乘,除,矩阵幂。

        COO格式优势

                方便稀疏格式之间的快速转换;

                允许重复条目(见示例)

                非常快速的转换CSR/CSC格式
 

>>> from scipy.sparse import coo_matrix
>>> import numpy as np
>>> coo_matrix((3, 4), dtype=np.int8).toarray()
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int8)
>>> # Constructing a matrix using ijv format
>>> row  = np.array([0, 3, 1, 0])
>>> col  = np.array([0, 3, 1, 2])
>>> data = np.array([4, 5, 7, 9])
>>> coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
array([[4, 0, 9, 0],
       [0, 7, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 5]])
>>> # Constructing a matrix with duplicate indices
>>> row  = np.array([0, 0, 1, 3, 1, 0, 0])
>>> col  = np.array([0, 2, 1, 3, 1, 0, 0])
>>> data = np.array([1, 1, 1, 1, 1, 1, 1])
>>> coo = coo_matrix((data, (row, col)), shape=(4, 4))
>>> coo = coo_matrix((data, (row, col)), shape=(4, 4))
>>> coo.toarray()
array([[3, 0, 1, 0],
       [0, 2, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 1]])
>>> #保持重复索引,直到隐式或显式求和

sp.eye()

eye(N, M=None, k=0, dtype=float) 是scipy包中的一个创建特殊矩阵(单位矩阵E)的方法

>>> from scipy import *
 
>>> eye(3)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
>>> eye(3,3)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
>>> eye(3,4)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.]])
>>> eye(3,4,1)
array([[0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
>>> eye(3,4,2)
array([[0., 0., 1., 0.],
       [0., 0., 0., 1.],
       [0., 0., 0., 0.]])

相关文章:

  • linux虚拟机未建分区的情况下对磁盘进行扩容
  • 猿创征文|【数据结构】牛客网刷题
  • 【函数式编程】Java函数式编程学习
  • 基于springboot,vue旅游信息推荐系统
  • SSLError(MaxRetryError(‘HTTPSConnectionPool(host=\‘repo.anaconda.com\‘, port
  • vs生成dll且被java通过jna调用
  • DDD - 六边形架构和CQRS架构
  • 宠物寄养小程序实战教程02
  • 【计算机网络】运输层习题(谢希仁)(1)
  • 微信小程序抓包-夜神模拟器结合BurpSuite抓包(可用于现在最新版本微信)
  • SAR/毫米波雷达 论文阅读 | Freehand, Agile, and High-Resolution Imaging, IEEE Access 2019
  • 神经网络一词在英文中是,网络词神经质什么意思
  • KOOM原理讲解(上)-JAVA内存分析
  • create-vite‘ 不是内部或外部命令,也不是可运行的程序
  • 【JS】牛客专项练习01
  • [NodeJS] 关于Buffer
  • HTML5新特性总结
  • HTTP中的ETag在移动客户端的应用
  • jdbc就是这么简单
  • markdown编辑器简评
  • MyEclipse 8.0 GA 搭建 Struts2 + Spring2 + Hibernate3 (测试)
  • python_bomb----数据类型总结
  • Spring-boot 启动时碰到的错误
  • SpriteKit 技巧之添加背景图片
  • 闭包--闭包之tab栏切换(四)
  • 分类模型——Logistics Regression
  • 跨域
  • 实现简单的正则表达式引擎
  • Hibernate主键生成策略及选择
  • 好程序员web前端教程分享CSS不同元素margin的计算 ...
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • #pragma once与条件编译
  • $.ajax中的eval及dataType
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (Redis使用系列) Springboot 使用redis实现接口幂等性拦截 十一
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (附源码)ssm户外用品商城 毕业设计 112346
  • (六)Hibernate的二级缓存
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • .[hudsonL@cock.li].mkp勒索加密数据库完美恢复---惜分飞
  • .a文件和.so文件
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .net 7 上传文件踩坑
  • .NET DataGridView数据绑定说明
  • .NET/C# 将一个命令行参数字符串转换为命令行参数数组 args
  • .NET4.0并行计算技术基础(1)
  • .NET多线程执行函数
  • .net访问oracle数据库性能问题
  • .NET上SQLite的连接
  • .php结尾的域名,【php】php正则截取url中域名后的内容
  • /etc/fstab 只读无法修改的解决办法