mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-01 05:51:01 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -23,7 +23,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.sparse import *
|
||||
import numpy as np
|
||||
|
||||
@@ -33,14 +33,14 @@ import numpy as np
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
coo_matrix((2,3))
|
||||
|
||||
```
|
||||
|
||||
Out[2]:
|
||||
|
||||
```
|
||||
```py
|
||||
<2x3 sparse matrix of type '<type 'numpy.float64'>'
|
||||
with 0 stored elements in COOrdinate format>
|
||||
```
|
||||
@@ -49,13 +49,13 @@ Out[2]:
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
A = coo_matrix([[1,2,0],[0,0,3],[4,0,5]])
|
||||
print A
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
(0, 0) 1
|
||||
(0, 1) 2
|
||||
(1, 2) 3
|
||||
@@ -68,20 +68,20 @@ print A
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
type(A)
|
||||
|
||||
```
|
||||
|
||||
Out[4]:
|
||||
|
||||
```
|
||||
```py
|
||||
scipy.sparse.coo.coo_matrix
|
||||
```
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
B = A.tocsr()
|
||||
type(B)
|
||||
|
||||
@@ -89,7 +89,7 @@ type(B)
|
||||
|
||||
Out[5]:
|
||||
|
||||
```
|
||||
```py
|
||||
scipy.sparse.csr.csr_matrix
|
||||
```
|
||||
|
||||
@@ -97,7 +97,7 @@ scipy.sparse.csr.csr_matrix
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
C = A.todense()
|
||||
C
|
||||
|
||||
@@ -105,7 +105,7 @@ C
|
||||
|
||||
Out[6]:
|
||||
|
||||
```
|
||||
```py
|
||||
matrix([[1, 2, 0],
|
||||
[0, 0, 3],
|
||||
[4, 0, 5]])
|
||||
@@ -115,7 +115,7 @@ matrix([[1, 2, 0],
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
v = np.array([1,0,-1])
|
||||
A.dot(v)
|
||||
|
||||
@@ -123,7 +123,7 @@ A.dot(v)
|
||||
|
||||
Out[7]:
|
||||
|
||||
```
|
||||
```py
|
||||
array([ 1, -3, -1])
|
||||
```
|
||||
|
||||
@@ -131,7 +131,7 @@ array([ 1, -3, -1])
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
I = np.array([0,3,1,0])
|
||||
J = np.array([0,3,1,2])
|
||||
V = np.array([4,5,7,9])
|
||||
@@ -141,12 +141,12 @@ A = coo_matrix((V,(I,J)),shape=(4,4))
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
print A
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
(0, 0) 4
|
||||
(3, 3) 5
|
||||
(1, 1) 7
|
||||
@@ -158,7 +158,7 @@ COO 格式的稀疏矩阵在构建的时候只是简单的将坐标和值加到
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
I = np.array([0,0,1,3,1,0,0])
|
||||
J = np.array([0,2,1,3,1,0,0])
|
||||
V = np.array([1,1,1,1,1,1,1])
|
||||
@@ -167,7 +167,7 @@ print B
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
(0, 0) 1
|
||||
(0, 2) 1
|
||||
(1, 1) 1
|
||||
@@ -182,13 +182,13 @@ print B
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
C = B.tocsr()
|
||||
print C
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
(0, 0) 3
|
||||
(0, 2) 1
|
||||
(1, 1) 2
|
||||
@@ -200,7 +200,7 @@ print C
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.sparse import lil_matrix
|
||||
from scipy.sparse.linalg import spsolve
|
||||
from numpy.linalg import solve, norm
|
||||
@@ -212,7 +212,7 @@ from numpy.random import rand
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
A = lil_matrix((1000, 1000))
|
||||
A[0, :100] = rand(100)
|
||||
A[1, 100:200] = A[0, :100]
|
||||
@@ -224,7 +224,7 @@ A.setdiag(rand(1000))
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
A = A.tocsr()
|
||||
b = rand(1000)
|
||||
x = spsolve(A, b)
|
||||
@@ -235,7 +235,7 @@ x = spsolve(A, b)
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
x_ = solve(A.toarray(), b)
|
||||
|
||||
```
|
||||
@@ -244,7 +244,7 @@ x_ = solve(A.toarray(), b)
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
err = norm(x-x_)
|
||||
err
|
||||
|
||||
@@ -252,7 +252,7 @@ err
|
||||
|
||||
Out[16]:
|
||||
|
||||
```
|
||||
```py
|
||||
6.4310987107687431e-13
|
||||
```
|
||||
|
||||
@@ -262,7 +262,7 @@ Out[16]:
|
||||
|
||||
In [17]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy import sparse
|
||||
|
||||
row, col, val = sparse.find(C)
|
||||
@@ -270,7 +270,7 @@ print row, col, val
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
[0 0 1 3] [0 2 1 3] [3 1 2 1]
|
||||
|
||||
```
|
||||
@@ -281,14 +281,14 @@ print row, col, val
|
||||
|
||||
In [18]:
|
||||
|
||||
```
|
||||
```py
|
||||
sparse.issparse(B)
|
||||
|
||||
```
|
||||
|
||||
Out[18]:
|
||||
|
||||
```
|
||||
```py
|
||||
True
|
||||
```
|
||||
|
||||
@@ -296,14 +296,14 @@ True
|
||||
|
||||
In [19]:
|
||||
|
||||
```
|
||||
```py
|
||||
sparse.isspmatrix(B.todense())
|
||||
|
||||
```
|
||||
|
||||
Out[19]:
|
||||
|
||||
```
|
||||
```py
|
||||
False
|
||||
```
|
||||
|
||||
@@ -311,26 +311,26 @@ False
|
||||
|
||||
In [20]:
|
||||
|
||||
```
|
||||
```py
|
||||
sparse.isspmatrix_coo(B)
|
||||
|
||||
```
|
||||
|
||||
Out[20]:
|
||||
|
||||
```
|
||||
```py
|
||||
True
|
||||
```
|
||||
|
||||
In [21]:
|
||||
|
||||
```
|
||||
```py
|
||||
sparse.isspmatrix_csr(B)
|
||||
|
||||
```
|
||||
|
||||
Out[21]:
|
||||
|
||||
```
|
||||
```py
|
||||
False
|
||||
```
|
||||
Reference in New Issue
Block a user