Python 如何创建稀疏矩阵

Python 如何创建稀疏矩阵

在本文中,我们将向您展示什么是稀疏矩阵以及如何在Python中创建稀疏矩阵。

什么是稀疏矩阵

稀疏矩阵是指大部分元素都是 0 的矩阵。也就是说,矩阵只包含少数位置上的数据。而稀疏矩阵所占用的大部分内存都是由零构成的。

例如:

M = [
   [1, 0, 0, 0],
   [0, 0, 3, 0],
   [0, 0, 0, 0],
   [0, 0, 0, 2]
]

使用2D数组来表示稀疏矩阵会浪费很多内存,因为矩阵中的零在大多数情况下是没有意义的。因此,我们不会将零和非零元素一起保留,而只是存储非零元素。这涉及使用三元组来存储非零元素(行,列,值)。

自然语言处理(NLP)和数据编码都大量使用稀疏矩阵。如果矩阵的大多数元素都是0,在存储方面存储所有矩阵元素将变得昂贵。

这是因为我们只有少量的数据点,大部分存储空间都被冗余的零占据。

稀疏矩阵的优势

与简单矩阵相比,使用稀疏矩阵有两个主要优势:

  • 存储 ——因为非零元素比零元素少,所以可以使用更少的内存来存储只有这些元素。
  • 计算时间 ——通过逻辑地创建一个只遍历非零元素的数据结构,可以节省计算时间。

如何在Python中创建稀疏矩阵

Python中的SciPy提供了使用各种数据结构创建稀疏矩阵的工具,以及将密集矩阵转换为稀疏矩阵的工具。

在Python中,我们可以使用以下函数创建稀疏矩阵:

  • csr_matrix()函数 ——在压缩的 稀疏行 格式中创建稀疏矩阵。
  • csc_matrix()函数 ——在压缩的 稀疏列 格式中创建稀疏矩阵。

方法1. 使用csr_matrix()函数创建稀疏矩阵

它在压缩的稀疏 格式中创建稀疏矩阵。

语法

scipy.sparse.csr_matrix(shape=None, dtype=None)

参数

  • shape − 矩阵的形状

  • dtype − 矩阵的数据类型

步骤

以下是执行所需任务的算法/步骤−

  • 使用import关键字,导入numpy模块并给它取一个别名np。

  • 使用import关键字,从scipy模块中导入csr_matrix函数。

  • 使用csr_matrix()函数创建一个3 * 3的稀疏矩阵(行格式),数据类型为int,并使用toarray()函数将其转换为数组。

  • 打印结果稀疏矩阵。

示例

以下程序使用csr_matrix()函数返回一个3×3的稀疏矩阵−

# importing numpy module with an alias name
import numpy as np

# importing csr_matrix function from scipy module
from scipy.sparse import csr_matrix

# Using csr_matrix function to create a 3 * 3 sparse matrix of int datatype
# and converting into array
sparse_matrix = csr_matrix((3, 3), dtype = np.int8).toarray()

# printing the resultant sparse matrix
print("The resultant sparse matrix:\n", sparse_matrix)

输出

在执行之后,上述程序将产生以下输出 –

The resultant sparse matrix:
 [[0 0 0]
 [0 0 0]
 [0 0 0]]

方法2:使用给定的Numpy数组的csr_matrix()函数创建稀疏矩阵

步骤

执行所需任务的算法/步骤如下:

  • 使用import关键字导入numpy模块,并将其别名命名为np。

  • 使用import关键字从scipy模块导入csr_matrix函数。

  • 使用numpy.array()函数创建数组(返回一个ndarray,满足给定的要求)。

示例

# importing numpy module with alias name
import numpy as np

# importing csr_matrix function from scipy module
from scipy.sparse import csr_matrix

# Giving rows and columns values
rows = np.array([0, 1, 0, 2, 1, 1])
columns = np.array([1, 0, 0, 2, 1, 2])

# Giving array data
arrayData = np.array([1, 3, 2, 5, 7, 6])

# Using csr_matrix function to create a 3x3 sparse matrix
sparse_matrix = csr_matrix((arrayData, (rows, columns)),
   shape = (3, 3)).toarray()

# print the resultant sparse matrix
print("The resultant sparse matrix:\n", sparse_matrix)

输出

在执行程序时,上述程序将生成以下输出-

The resultant sparse matrix:
 [[2 1 0]
 [3 7 6]
 [0 0 5]]

方法3. 使用csc_matrix()函数创建稀疏矩阵

它以压缩稀疏列格式创建稀疏矩阵。

语法

scipy.sparse.csc_matrix(shape=None, dtype=None)

参数

  • shape −矩阵的形状

  • dtype −矩阵的数据类型

步骤

以下是执行所需任务的算法/步骤−

  • 使用import关键字,将numpy模块导入,并使用别名np。

  • 使用import关键字,从scipy模块中导入csc_matrix函数。

  • 使用csc_matrix()函数创建一个3*3的稀疏矩阵(列格式),数据类型为int,并使用toarray()函数将其转换为数组。

  • 打印结果稀疏矩阵。

示例

以下程序使用csc_matrix()函数以列格式返回稀疏矩阵(3×3)−

# importing numpy module with an alias name
import numpy as np

# importing csc_matrix function from scipy module
from scipy.sparse import csc_matrix

# Using csc_matrix function to create a 3 * 3 sparse matrix of int datatype
# and converting into array
sparse_matrix = csc_matrix((3, 3), dtype = np.int8).toarray()

# printing the resultant sparse matrix
print("The resultant sparse matrix:\n", sparse_matrix)

输出

在执行时,上述程序将生成以下输出−

The resultant sparse matrix:
 [[0 0 0]
 [0 0 0]
 [0 0 0]]

方法4:使用给定的Numpy数组的csc_matrix()函数创建稀疏矩阵

示例

以下程序使用csc_matrix()函数以整数的列格式返回稀疏矩阵(3×3)-

import numpy as np
# importing csc_matrix function from scipy module
from scipy.sparse import csc_matrix

# Giving rows and columns values
rows = np.array([0, 1, 0, 2, 1, 1])
columns = np.array([1, 0, 0, 2, 1, 2])

# Giving array data
arrayData = np.array([1, 3, 2, 5, 7, 6])

# Using csc_matrix function to create a 3x3 sparse matrix in column format
sparse_matrix = csc_matrix((arrayData, (rows, columns)),
   shape = (3, 3)).toarray()

# print the resultant sparse matrix
print("The resultant sparse matrix:\n", sparse_matrix)

输出

执行后,以上程序将生成以下输出 –

The resultant sparse matrix:
 [[2 1 0]
 [3 7 6]
 [0 0 5]]

结论

在本教程中,我们学习了四种不同的在Python中生成稀疏矩阵的方法。我们还学习了如何从numpy数组生成稀疏矩阵。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程