首頁  >  文章  >  後端開發  >  如何在Python中建立稀疏矩陣?

如何在Python中建立稀疏矩陣?

PHPz
PHPz轉載
2023-09-05 15:49:061183瀏覽

如何在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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除