• 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 矩阵项的列下标

    参数:

    1. dtype : dtype
    2. Data type of the matrix
    3. shape : 2-tuple
    4. Shape of the matrix
    5. ndim : int
    6. Number of dimensions (this is always 2)
    7. nnz
    8. Number of stored values, including explicit zeros
    9. data
    10. COO format data array of the matrix
    11. row
    12. COO format row index array of the matrix
    13. col
    14. COO format column index array of the matrix

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

            COO格式优势

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

                    允许重复条目(见示例)

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

    1. >>> from scipy.sparse import coo_matrix
    2. >>> import numpy as np
    3. >>> coo_matrix((3, 4), dtype=np.int8).toarray()
    4. array([[0, 0, 0, 0],
    5. [0, 0, 0, 0],
    6. [0, 0, 0, 0]], dtype=int8)
    7. >>> # Constructing a matrix using ijv format
    8. >>> row = np.array([0, 3, 1, 0])
    9. >>> col = np.array([0, 3, 1, 2])
    10. >>> data = np.array([4, 5, 7, 9])
    11. >>> coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
    12. array([[4, 0, 9, 0],
    13. [0, 7, 0, 0],
    14. [0, 0, 0, 0],
    15. [0, 0, 0, 5]])
    16. >>> # Constructing a matrix with duplicate indices
    17. >>> row = np.array([0, 0, 1, 3, 1, 0, 0])
    18. >>> col = np.array([0, 2, 1, 3, 1, 0, 0])
    19. >>> data = np.array([1, 1, 1, 1, 1, 1, 1])
    20. >>> coo = coo_matrix((data, (row, col)), shape=(4, 4))
    21. >>> coo = coo_matrix((data, (row, col)), shape=(4, 4))
    22. >>> coo.toarray()
    23. array([[3, 0, 1, 0],
    24. [0, 2, 0, 0],
    25. [0, 0, 0, 0],
    26. [0, 0, 0, 1]])
    27. >>> #保持重复索引,直到隐式或显式求和

    sp.eye()

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

    1. >>> from scipy import *
    2. >>> eye(3)
    3. [[1. 0. 0.]
    4. [0. 1. 0.]
    5. [0. 0. 1.]]
    6. >>> eye(3,3)
    7. array([[1., 0., 0.],
    8. [0., 1., 0.],
    9. [0., 0., 1.]])
    10. >>> eye(3,4)
    11. array([[1., 0., 0., 0.],
    12. [0., 1., 0., 0.],
    13. [0., 0., 1., 0.]])
    14. >>> eye(3,4,1)
    15. array([[0., 1., 0., 0.],
    16. [0., 0., 1., 0.],
    17. [0., 0., 0., 1.]])
    18. >>> eye(3,4,2)
    19. array([[0., 0., 1., 0.],
    20. [0., 0., 0., 1.],
    21. [0., 0., 0., 0.]])

  • 相关阅读:
    【项目】数据库事务与MQ发送一致性
    蓝桥杯(路径 动态规划 C++)
    【JS高级】js面向对象三大特性之继承_06
    TairSearch:加速多列索引查询
    [附源码]Python计算机毕业设计SSM科技项目在线评审系统(程序+LW)
    linux 设置默认启动图形化界面
    python中def一个方法,就一定对应一个return吗
    四十九、openlayers官网示例Immediate Rendering (Geographic)——在地图上绘制星空动画效果
    通信达股票交易接口使用步骤
    30.CSS文本悬停过渡效果
  • 原文地址:https://blog.csdn.net/qq_40671063/article/details/126687572