如何在Python中创建稀疏矩阵?
在本文中,我们将向您展示什么是稀疏矩阵以及如何在 python 中创建稀疏矩阵。
什么是稀疏矩阵?
稀疏矩阵是大多数元素为0的矩阵。也就是说,矩阵仅包含少数位置的数据。稀疏矩阵消耗的大部分内存都是由零组成的。
例如 -
M = [ [1, 0, 0, 0], [0, 0, 3, 0], [0, 0, 0, 0], [0, 0, 0, 2] ]
使用二维数组来表示稀疏矩阵会浪费大量内存,因为矩阵中的零在大多数情况下都是无用的。因此,我们不是将零与非零元素一起保存,而是只存储非零元素。这涉及使用三元组来存储非零元素(行、列、值)。
自然语言处理(NLP)和数据编码都大量使用稀疏矩阵。如果大多数矩阵元素为 0,则存储所有矩阵元素的存储成本会很高。
这是因为我们只有几个数据点,并且大部分存储空间都被冗余零占用。
稀疏矩阵的优点
以下是使用稀疏矩阵而不是简单矩阵的两个主要优点 -
存储 - 因为非零元素比零少,所以可以使用更少的内存来单独存储这些元素。
计算时间 - 通过逻辑创建仅遍历非零元素的数据结构可以节省计算时间。
如何在Python中创建稀疏矩阵?
Python 中的 SciPy 提供了使用各种数据结构创建稀疏矩阵以及将稠密矩阵转换为稀疏矩阵的工具。
在Python中,我们可以使用以下函数创建稀疏矩阵 -
csr_matrix() 函数 - 以压缩稀疏行格式创建稀疏矩阵,
csc_matrix() 函数 - 以压缩稀疏列格式创建稀疏矩阵。,,
方法1.使用csr_matrix()函数创建稀疏矩阵
它以压缩稀疏行格式创建稀疏矩阵。
语法
scipy.sparse.csr_matrix(shape=None, dtype=None)
参数
shape - 它是矩阵的形状
dtype - 它是矩阵的数据类型
算法(步骤)
以下是执行所需任务所需遵循的算法/步骤 -
使用 import 关键字,导入带有别名 (np) 的 numpy 模块。
使用 import 关键字,从 scipy 模块导入 csr_matrix 函数。
使用csr_matrix()函数创建int数据类型的3 * 3稀疏矩阵(row格式)并使用toarray转换为数组() 函数。
打印生成的稀疏矩阵。
示例
以下程序使用 csr_matrix() 函数返回稀疏矩阵 (3x3) -
# 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 关键字,导入带有别名 (np) 的 numpy 模块。
使用 import 关键字,从 scipy 模块导入 csr_matrix 函数。
使用numpy.array()函数创建数组(返回一个ndarray。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 关键字,导入带有别名 (np) 的 numpy 模块。
使用 import 关键字,从 scipy 模块导入 csc_matrix 函数。
使用csc_matrix()函数创建int数据类型的3 * 3稀疏矩阵(列格式)并使用toarray转换为数组() 函数。
打印生成的稀疏矩阵。
示例
以下程序使用 csc_matrix() 函数以列格式返回稀疏矩阵 (3x3) -
# 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() 函数以整数列格式返回稀疏矩阵 (3x3) -
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 数组生成稀疏矩阵。
以上是如何在Python中创建稀疏矩阵?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

在 VS Code 中,可以通过以下步骤在终端运行程序:准备代码和打开集成终端确保代码目录与终端工作目录一致根据编程语言选择运行命令(如 Python 的 python your_file_name.py)检查是否成功运行并解决错误利用调试器提升调试效率

VS Code可以在Windows 8上运行,但体验可能不佳。首先确保系统已更新到最新补丁,然后下载与系统架构匹配的VS Code安装包,按照提示安装。安装后,注意某些扩展程序可能与Windows 8不兼容,需要寻找替代扩展或在虚拟机中使用更新的Windows系统。安装必要的扩展,检查是否正常工作。尽管VS Code在Windows 8上可行,但建议升级到更新的Windows系统以获得更好的开发体验和安全保障。

VS Code 可用于编写 Python,并提供许多功能,使其成为开发 Python 应用程序的理想工具。它允许用户:安装 Python 扩展,以获得代码补全、语法高亮和调试等功能。使用调试器逐步跟踪代码,查找和修复错误。集成 Git,进行版本控制。使用代码格式化工具,保持代码一致性。使用 Linting 工具,提前发现潜在问题。

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

VS Code 扩展存在恶意风险,例如隐藏恶意代码、利用漏洞、伪装成合法扩展。识别恶意扩展的方法包括:检查发布者、阅读评论、检查代码、谨慎安装。安全措施还包括:安全意识、良好习惯、定期更新和杀毒软件。
