如何在 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 關鍵字,匯入numpy 模組並使用別名 (np)。

  • 使用 import 關鍵字,從 scipy 模組匯入 csr_matrix 函式。

  • 使用csr_matrix() 函式建立一個 3 * 3 的稀疏矩陣(格式)int 資料型別,並使用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 關鍵字,匯入numpy 模組並使用別名 (np)。

  • 使用 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 關鍵字,匯入numpy 模組並使用別名 (np)。

  • 使用 import 關鍵字,從 scipy 模組匯入csc_matrix 函式。

  • 使用csc_matrix() 函式建立一個 3 * 3 的稀疏矩陣(格式)int 資料型別,並使用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 陣列生成稀疏矩陣。

更新於: 2022 年 10 月 20 日

8K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.