# 2.5 SciPy中稀疏矩陣
In?[3]:
```
%matplotlib inline
import numpy as np
```
## 2.5.1 介紹
(密集) 矩陣是:
* 數據對象
* 存儲二維值數組的數據結構
重要特征:
* 一次分配所有項目的內存
* 通常是一個連續組塊,想一想Numpy數組
* _快速_訪問個項目(*)
### 2.5.1.1 為什么有稀疏矩陣?
* 內存,增長是n**2
* 小例子(雙精度矩陣):
In?[5]:
```
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 1e6, 10)
plt.plot(x, 8.0 * (x**2) / 1e6, lw=5)
plt.xlabel('size n')
plt.ylabel('memory [MB]')
```
Out[5]:
```
<matplotlib.text.Text at 0x105b08dd0>
```

### 2.5.1.2 稀疏矩陣 vs. 稀疏矩陣存儲方案
* 稀疏矩陣是一個矩陣,巨大多數是空的
* 存儲所有的0是浪費 -> 只存儲非0項目
* 想一下**壓縮**
* 有利: 巨大的內存節省
* 不利: 依賴實際的存儲方案, (*) 通常并不能滿足
### 2.5.1.3 典型應用
* 偏微分方程(PDES)的解
* 有限元素法
* 機械工程、電子、物理...
* 圖論
* (i,j)不是0表示節點i與節點j是聯接的
* ...
### 2.5.1.4 先決條件
最新版本的
* `numpy`
* `scipy`
* `matplotlib` (可選)
* `ipython` (那些增強很方便)
### 2.5.1.5 稀疏結構可視化
* matplotlib中的`spy()`
* 樣例繪圖:
  
## 2.5.2 存儲機制
* scipy.sparse中有七類稀疏矩陣:
1. csc_matrix: 壓縮列格式
2. csr_matrix: 壓縮行格式
3. bsr_matrix: 塊壓縮行格式
4. lil_matrix: 列表的列表格式
5. dok_matrix: 值的字典格式
6. coo_matrix: 座標格式 (即 IJV, 三維格式)
7. dia_matrix: 對角線格式
* 每一個類型適用于一些任務
* 許多都利用了由Nathan Bell提供的稀疏工具 C ++ 模塊
* 假設導入了下列模塊:
In?[1]:
```
import numpy as np
import scipy.sparse as sparse
import matplotlib.pyplot as plt
```
* 給Numpy用戶的**warning**:
* 使用'*'的乘是_矩陣相乘_ (點積)
* 并不是Numpy的一部分!
* 向Numpy函數傳遞一個稀疏矩陣希望一個ndarray/矩陣是沒用的
### 2.5.2.1 通用方法
* 所有scipy.sparse類都是spmatrix的子類
* 算術操作的默認實現
* 通常轉化為CSR
* 為了效率而子類覆蓋
* 形狀、數據類型設置/獲取
* 非0索引
* 格式轉化、與Numpy交互(toarray(), todense())
* ...
* 屬性:
* mtx.A - 與mtx.toarray()相同
* mtx.T - 轉置 (與mtx.transpose()相同)
* mtx.H - Hermitian (列舉) 轉置
* mtx.real - 復矩陣的真部
* mtx.imag - 復矩陣的虛部
* mtx.size - 非零數 (與self.getnnz()相同)
* mtx.shape - 行數和列數 (元組)
* 數據通常儲存在Numpy數組中
### 2.5.2.2 稀疏矩陣類
#### 2.5.2.2.1 對角線格式 (DIA))
* 非常簡單的格式
* 形狀 (n_diag, length) 的密集Numpy數組的對角線
* 固定長度 -> 當離主對角線比較遠時會浪費空間
* _data_matrix的子類 (帶數據屬性的稀疏矩陣類)
* 每個對角線的偏移
* 0 是主對角線
* 負偏移 = 下面
* 正偏移 = 上面
* 快速矩陣 * 向量 (sparsetools)
* 快速方便的關于項目的操作
* 直接操作數據數組 (快速的NumPy機件)
* 構建器接受 :
* 密集矩陣 (數組)
* 稀疏矩陣
* 形狀元組 (創建空矩陣)
* (數據, 偏移) 元組
* 沒有切片、沒有單個項目訪問
* 用法 :
* 非常專業
* 通過有限微分解偏微分方程
* 有一個迭代求解器 ##### 2.5.2.2.1.1 示例
* 創建一些DIA矩陣 :
In?[3]:
```
data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0)
data
```
Out[3]:
```
array([[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]])
```
In?[6]:
```
offsets = np.array([0, -1, 2])
mtx = sparse.dia_matrix((data, offsets), shape=(4, 4))
mtx
```
Out[6]:
```
<4x4 sparse matrix of type '<type 'numpy.int64'>'
with 9 stored elements (3 diagonals) in DIAgonal format>
```
In?[7]:
```
mtx.todense()
```
Out[7]:
```
matrix([[1, 0, 3, 0],
[1, 2, 0, 4],
[0, 2, 3, 0],
[0, 0, 3, 4]])
```
In?[9]:
```
data = np.arange(12).reshape((3, 4)) + 1
data
```
Out[9]:
```
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
```
In?[10]:
```
mtx = sparse.dia_matrix((data, offsets), shape=(4, 4))
mtx.data
```
Out[10]:
```
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
```
In?[11]:
```
mtx.offsets
```
Out[11]:
```
array([ 0, -1, 2], dtype=int32)
```
In?[12]:
```
print mtx
```
```
(0, 0) 1
(1, 1) 2
(2, 2) 3
(3, 3) 4
(1, 0) 5
(2, 1) 6
(3, 2) 7
(0, 2) 11
(1, 3) 12
```
In?[13]:
```
mtx.todense()
```
Out[13]:
```
matrix([[ 1, 0, 11, 0],
[ 5, 2, 0, 12],
[ 0, 6, 3, 0],
[ 0, 0, 7, 4]])
```
* 機制的解釋 :
偏移: 行
```
2: 9
1: --10------
0: 1 . 11 .
-1: 5 2 . 12
-2: . 6 3 .
-3: . . 7 4
---------8
```
* 矩陣-向量相乘
In?[15]:
```
vec = np.ones((4, ))
vec
```
Out[15]:
```
array([ 1., 1., 1., 1.])
```
In?[16]:
```
mtx * vec
```
Out[16]:
```
array([ 12., 19., 9., 11.])
```
In?[17]:
```
mtx.toarray() * vec
```
Out[17]:
```
array([[ 1., 0., 11., 0.],
[ 5., 2., 0., 12.],
[ 0., 6., 3., 0.],
[ 0., 0., 7., 4.]])
```
#### 2.5.2.2.2 列表的列表格式 (LIL))
* 基于行的聯接列表
* 每一行是一個Python列表(排序的)非零元素的列索引
* 行存儲在Numpy數組中 (dtype=np.object)
* 非零值也近似存儲
* 高效增量構建稀疏矩陣
* 構建器接受 :
* 密集矩陣 (數組)
* 稀疏矩陣
* 形狀元組 (創建一個空矩陣)
* 靈活切片、高效改變稀疏結構
* 由于是基于行的,算術和行切片慢
* 用途 :
* 當稀疏模式并不是已知的邏輯或改變
* 例子:從一個文本文件讀取稀疏矩陣 ##### 2.5.2.2.2.1 示例
* 創建一個空的LIL矩陣 :
In?[2]:
```
mtx = sparse.lil_matrix((4, 5))
```
* 準備隨機數據:
In?[4]:
```
from numpy.random import rand
data = np.round(rand(2, 3))
data
```
Out[4]:
```
array([[ 0., 0., 0.],
[ 1., 0., 0.]])
```
* 使用象征所以分配數據:
In?[6]:
```
mtx[:2, [1, 2, 3]] = data
mtx
```
Out[6]:
```
<4x5 sparse matrix of type '<type 'numpy.float64'>'
with 3 stored elements in LInked List format>
```
In?[7]:
```
print mtx
```
```
(0, 1) 1.0
(0, 3) 1.0
(1, 2) 1.0
```
In?[8]:
```
mtx.todense()
```
Out[8]:
```
matrix([[ 0., 1., 0., 1., 0.],
[ 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])
```
In?[9]:
```
mtx.toarray()
```
Out[9]:
```
array([[ 0., 1., 0., 1., 0.],
[ 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])
```
更多的切片和索引:
In?[10]:
```
mtx = sparse.lil_matrix([[0, 1, 2, 0], [3, 0, 1, 0], [1, 0, 0, 1]])
mtx.todense()
```
Out[10]:
```
matrix([[0, 1, 2, 0],
[3, 0, 1, 0],
[1, 0, 0, 1]])
```
In?[11]:
```
print mtx
```
```
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 2) 1
(2, 0) 1
(2, 3) 1
```
In?[12]:
```
mtx[:2, :]
```
Out[12]:
```
<2x4 sparse matrix of type '<type 'numpy.int64'>'
with 4 stored elements in LInked List format>
```
In?[13]:
```
mtx[:2, :].todense()
```
Out[13]:
```
matrix([[0, 1, 2, 0],
[3, 0, 1, 0]])
```
In?[14]:
```
mtx[1:2, [0,2]].todense()
```
Out[14]:
```
matrix([[3, 1]])
```
In?[15]:
```
mtx.todense()
```
Out[15]:
```
matrix([[0, 1, 2, 0],
[3, 0, 1, 0],
[1, 0, 0, 1]])
```
#### 2.5.2.2.3 值的字典格式 (DOK))
* Python字典的子類
* 鍵是 (行, 列) 索引元組 (不允許重復的條目)
* 值是對應的非零值
* 高效增量構建稀疏矩陣
* 構建器支持:
* 密集矩陣 (數組)
* 稀疏矩陣
* 形狀元組 (創建空矩陣)
* 高效 O(1) 對單個元素的訪問
* 靈活索引,改變稀疏結構是高效
* 一旦創建完成后可以被高效轉換為coo_matrix
* 算術很慢 (循環用`dict.iteritems()`)
* 用法:
* 當稀疏模式是未知的假設或改變時
##### 2.5.2.2.3.1 示例
* 逐個元素創建一個DOK矩陣:
In?[16]:
```
mtx = sparse.dok_matrix((5, 5), dtype=np.float64)
mtx
```
Out[16]:
```
<5x5 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements in Dictionary Of Keys format>
```
In?[17]:
```
for ir in range(5):
for ic in range(5):
mtx[ir, ic] = 1.0 * (ir != ic)
mtx
```
Out[17]:
```
<5x5 sparse matrix of type '<type 'numpy.float64'>'
with 20 stored elements in Dictionary Of Keys format>
```
In?[18]:
```
mtx.todense()
```
Out[18]:
```
matrix([[ 0., 1., 1., 1., 1.],
[ 1., 0., 1., 1., 1.],
[ 1., 1., 0., 1., 1.],
[ 1., 1., 1., 0., 1.],
[ 1., 1., 1., 1., 0.]])
```
* 切片與索引:
In?[19]:
```
mtx[1, 1]
```
Out[19]:
```
0.0
```
In?[20]:
```
mtx[1, 1:3]
```
Out[20]:
```
<1x2 sparse matrix of type '<type 'numpy.float64'>'
with 1 stored elements in Dictionary Of Keys format>
```
In?[21]:
```
mtx[1, 1:3].todense()
```
Out[21]:
```
matrix([[ 0., 1.]])
```
In?[22]:
```
mtx[[2,1], 1:3].todense()
```
Out[22]:
```
matrix([[ 1., 0.],
[ 0., 1.]])
```
#### 2.5.2.2.4 座標格式 (COO))
* 也被稱為 ‘ijv’ 或 ‘triplet’ 格式
* 三個NumPy數組: row, col, data
* `data[i]`是在 (row[i], col[i]) 位置的值
* 允許重復值
* `\_data\_matrix`的子類 (帶有`data`屬性的稀疏矩陣類)
* 構建稀疏矩陣的高速模式
* 構建器接受:
* 密集矩陣 (數組)
* 稀疏矩陣
* 形狀元組 (創建空數組)
* `(data, ij)`元組
* 與CSR/CSC格式非常快的互相轉換
* 快速的矩陣 * 向量 (sparsetools)
* 快速而簡便的逐項操作
* 直接操作數據數組 (快速NumPy機制)
* 沒有切片,沒有算術 (直接)
* 使用:
* 在各種稀疏格式間的靈活轉換
* 當轉化到其他形式 (通常是 CSR 或 CSC), 重復的條目被加總到一起
* 有限元素矩陣的快速高效創建
##### 2.5.2.2.4.1 示例
* 創建空的COO矩陣:
In?[23]:
```
mtx = sparse.coo_matrix((3, 4), dtype=np.int8)
mtx.todense()
```
Out[23]:
```
matrix([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int8)
```
* 用 (data, ij) 元組創建:
In?[24]:
```
row = np.array([0, 3, 1, 0])
col = np.array([0, 3, 1, 2])
data = np.array([4, 5, 7, 9])
mtx = sparse.coo_matrix((data, (row, col)), shape=(4, 4))
mtx
```
Out[24]:
```
<4x4 sparse matrix of type '<type 'numpy.int64'>'
with 4 stored elements in COOrdinate format>
```
In?[25]:
```
mtx.todense()
```
Out[25]:
```
matrix([[4, 0, 9, 0],
[0, 7, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 5]])
```
#### 2.5.2.2.5 壓縮稀疏行格式 (CSR))
* 面向行
* 三個Numpy數組: `indices`, `indptr`, `data`
* `indices`是列索引的數組
* `data`是對應的非零值數組
* `indptr`指向行開始的所以和數據
* 長度是`n_row + 1`, 最后一個項目 = 值數量 = `indices`和`data`的長度
* i-th行的非零值是列索引為`indices[indptr[i]:indptr[i+1]]`的`data[indptr[i]:indptr[i+1]]`
* 項目 (i, j) 可以通過`data[indptr[i]+k]`, k是j在`indices[indptr[i]:indptr[i+1]]`的位置來訪問
* `_cs_matrix` (常規 CSR/CSC 功能) 的子類
* `_data_matrix` (帶有`data`屬性的稀疏矩陣類) 的子類
* 快速矩陣向量相乘和其他算術 (sparsetools)
* 構建器接受:
* 密集矩陣 (數組)
* 稀疏矩陣
* 形狀元組 (創建空矩陣)
* `(data, ij)` 元組
* `(data, indices, indptr)` 元組
* 高效行切片,面向行的操作
* 較慢的列切片,改變稀疏結構代價昂貴
* 用途:
* 實際計算 (大多數線性求解器都支持這個格式)
##### 2.5.2.2.5.1 示例
* 創建空的CSR矩陣:
In?[26]:
```
mtx = sparse.csr_matrix((3, 4), dtype=np.int8)
mtx.todense()
```
Out[26]:
```
matrix([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int8)
```
* 用`(data, ij)`元組創建:
In?[27]:
```
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
mtx = sparse.csr_matrix((data, (row, col)), shape=(3, 3))
mtx
```
Out[27]:
```
<3x3 sparse matrix of type '<type 'numpy.int64'>'
with 6 stored elements in Compressed Sparse Row format>
```
In?[28]:
```
mtx.todense()
```
Out[28]:
```
matrix([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
```
In?[29]:
```
mtx.data
```
Out[29]:
```
array([1, 2, 3, 4, 5, 6])
```
In?[30]:
```
mtx.indices
```
Out[30]:
```
array([0, 2, 2, 0, 1, 2], dtype=int32)
```
In?[31]:
```
mtx.indptr
```
Out[31]:
```
array([0, 2, 3, 6], dtype=int32)
```
用`(data, indices, indptr)`元組創建:
In?[32]:
```
data = np.array([1, 2, 3, 4, 5, 6])
indices = np.array([0, 2, 2, 0, 1, 2])
indptr = np.array([0, 2, 3, 6])
mtx = sparse.csr_matrix((data, indices, indptr), shape=(3, 3))
mtx.todense()
```
Out[32]:
```
matrix([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
```
#### 2.5.2.2.6 壓縮稀疏列格式 (CSC))
* 面向列
* 三個Numpy數組: `indices`、`indptr`、`data`
* `indices`是行索引的數組
* `data`是對應的非零值
* `indptr`指向`indices`和`data`開始的列
* 長度是`n_col + 1`, 最后一個條目 = 值數量 = `indices`和`data`的長度
* 第i列的非零值是行索引為`indices[indptr[i]:indptr[i+1]]`的`data[indptr[i]:indptr[i+1]]`
* 項目 (i, j) 可以作為`data[indptr[j]+k]`訪問, k是i在`indices[indptr[j]:indptr[j+1]]`的位置
* `_cs_matrix`的子類 (通用的 CSR/CSC 功能性)
* `_data_matrix`的子類 (帶有`data`屬性的稀疏矩陣類)
* 快速的矩陣和向量相乘及其他數學 (sparsetools)
* 構建器接受:
* 密集矩陣 (數組)
* 稀疏矩陣
* 形狀元組 (創建空矩陣)
* `(data, ij)`元組
* `(data, indices, indptr)`元組
* 高效列切片、面向列的操作
* 較慢的行切片、改變稀疏結構代價昂貴
* 用途:
* 實際計算 (巨大多數線性求解器支持這個格式)
##### 2.5.2.2.6.1 示例
* 創建空CSC矩陣:
In?[33]:
```
mtx = sparse.csc_matrix((3, 4), dtype=np.int8)
mtx.todense()
```
Out[33]:
```
matrix([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int8)
```
* 用`(data, ij)`元組創建:
In?[34]:
```
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
mtx = sparse.csc_matrix((data, (row, col)), shape=(3, 3))
mtx
```
Out[34]:
```
<3x3 sparse matrix of type '<type 'numpy.int64'>'
with 6 stored elements in Compressed Sparse Column format>
```
In?[35]:
```
mtx.todense()
```
Out[35]:
```
matrix([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
```
In?[36]:
```
mtx.data
```
Out[36]:
```
array([1, 4, 5, 2, 3, 6])
```
In?[37]:
```
mtx.indices
```
Out[37]:
```
array([0, 2, 2, 0, 1, 2], dtype=int32)
```
In?[38]:
```
mtx.indptr
```
Out[38]:
```
array([0, 2, 3, 6], dtype=int32)
```
* 用`(data, indices, indptr)`元組創建:
In?[39]:
```
data = np.array([1, 4, 5, 2, 3, 6])
indices = np.array([0, 2, 2, 0, 1, 2])
indptr = np.array([0, 2, 3, 6])
mtx = sparse.csc_matrix((data, indices, indptr), shape=(3, 3))
mtx.todense()
```
Out[39]:
```
matrix([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
```
#### 2.5.2.2.7 塊壓縮行格式 (BSR))
* 本質上,CSR帶有密集的固定形狀的子矩陣而不是純量的項目
* 塊大小`(R, C)`必須可以整除矩陣的形狀`(M, N)`
* 三個Numpy數組: `indices`、`indptr`、`data`
* `indices`是每個塊列索引的數組
* `data`是形狀為(nnz, R, C)對應的非零值
* ...
* `_cs_matrix`的子類 (通用的CSR/CSC功能性)
* `_data_matrix`的子類 (帶有`data`屬性的稀疏矩陣類)
* 快速矩陣向量相乘和其他的算術 (sparsetools)
* 構建器接受:
* 密集矩陣 (數組)
* 稀疏矩陣
* 形狀元組 (創建空的矩陣)
* `(data, ij)`元組
* `(data, indices, indptr)`元組
* 許多對于帶有密集子矩陣的稀疏矩陣算術操作比CSR更高效很多
* 用途:
* 類似CSR
* 有限元素向量值離散化 ##### 2.5.2.2.7.1 示例
* 創建空的`(1, 1)`塊大小的(類似CSR...)的BSR矩陣:
In?[40]:
```
mtx = sparse.bsr_matrix((3, 4), dtype=np.int8)
mtx
```
Out[40]:
```
<3x4 sparse matrix of type '<type 'numpy.int8'>'
with 0 stored elements (blocksize = 1x1) in Block Sparse Row format>
```
In?[41]:
```
mtx.todense()
```
Out[41]:
```
matrix([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]], dtype=int8)
```
* 創建塊大小`(3, 2)`的空BSR矩陣:
In?[42]:
```
mtx = sparse.bsr_matrix((3, 4), blocksize=(3, 2), dtype=np.int8)
mtx
```
Out[42]:
```
<3x4 sparse matrix of type '<type 'numpy.int8'>'
with 0 stored elements (blocksize = 3x2) in Block Sparse Row format>
```
```
- 一個bug?
```
* 用`(1, 1)`塊大小 (類似 CSR...)`(data, ij)`的元組創建:
In?[43]:
```
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
mtx = sparse.bsr_matrix((data, (row, col)), shape=(3, 3))
mtx
```
Out[43]:
```
<3x3 sparse matrix of type '<type 'numpy.int64'>'
with 6 stored elements (blocksize = 1x1) in Block Sparse Row format>
```
In?[44]:
```
mtx.todense()
```
Out[44]:
```
matrix([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
```
In?[45]:
```
mtx.indices
```
Out[45]:
```
array([0, 2, 2, 0, 1, 2], dtype=int32)
```
In?[46]:
```
mtx.indptr
```
Out[46]:
```
array([0, 2, 3, 6], dtype=int32)
```
* 用`(2, 1)`塊大小`(data, indices, indptr)`的元組創建:
In?[47]:
```
indptr = np.array([0, 2, 3, 6])
indices = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6]).repeat(4).reshape(6, 2, 2)
mtx = sparse.bsr_matrix((data, indices, indptr), shape=(6, 6))
mtx.todense()
```
Out[47]:
```
matrix([[1, 1, 0, 0, 2, 2],
[1, 1, 0, 0, 2, 2],
[0, 0, 0, 0, 3, 3],
[0, 0, 0, 0, 3, 3],
[4, 4, 5, 5, 6, 6],
[4, 4, 5, 5, 6, 6]])
```
In?[48]:
```
data
```
Out[48]:
```
array([[[1, 1],
[1, 1]],
[[2, 2],
[2, 2]],
[[3, 3],
[3, 3]],
[[4, 4],
[4, 4]],
[[5, 5],
[5, 5]],
[[6, 6],
[6, 6]]])
```
### 2.5.2.3 總結
存儲機制的總結
| 格式 | 矩陣 \* 向量 | 提取項目 | 靈活提取 | 設置項目 | 靈活設置 | 求解器 | 備注 |
| --- | --- | --- | --- | --- | --- | --- | --- |
| DIA | sparsetools | . | . | . | . | 迭代 | 有數據數組,專門化 |
| LIL | 通過 CSR | 是 | 是 | 是 | 是 | 迭代 | 通過CSR的算術, 增量構建 |
| DOK | python | 是 | 只有一個軸 | 是 | 是 | 迭代 | `O(1)`條目訪問, 增量構建 |
| COO | sparsetools | . | . | . | . | 迭代 | 有數據數組, 便利的快速轉換 |
| CSR | sparsetools | 是 | 是 | 慢 | . | 任何 | 有數據數組, 快速以行為主的操作 |
| CSC | sparsetools | 是 | 是 | 慢 | . | 任何 | 有數據數組, 快速以列為主的操作 |
| BSR | sparsetools | . | . | . | . | 專門化 | 有數據數組,專門化 |
- 介紹
- 1.1 科學計算工具及流程
- 1.2 Python語言
- 1.3 NumPy:創建和操作數值數據
- 1.4 Matplotlib:繪圖
- 1.5 Scipy:高級科學計算
- 1.6 獲取幫助及尋找文檔
- 2.1 Python高級功能(Constructs)
- 2.2 高級Numpy
- 2.3 代碼除錯
- 2.4 代碼優化
- 2.5 SciPy中稀疏矩陣
- 2.6 使用Numpy和Scipy進行圖像操作及處理
- 2.7 數學優化:找到函數的最優解
- 2.8 與C進行交互
- 3.1 Python中的統計學
- 3.2 Sympy:Python中的符號數學
- 3.3 Scikit-image:圖像處理
- 3.4 Traits:創建交互對話
- 3.5 使用Mayavi進行3D作圖
- 3.6 scikit-learn:Python中的機器學習