Basic operations when using sparse matrices in Scipy I wrote it a long time ago, so maybe something is wrong
Matrix calculation
import scipy.sparse as sp
import numpy as np
a = sp.lil_matrix((1, 10000)) # 1*10000 sparse matrices are created
b = sp.lil_matrix((1, 10000))
# a.shape => (1, 10000)
for i in xrange(a.shape[1]):
r = np.random.rand()
if r < 0.9:
r = 0.0
a[0, i] = r
#Randomly stored numerical values in each element of a
a
# => <1x10000 sparse matrix of type '<type 'numpy.float64'>'
with 947 stored elements in LInked List format>
#b did the same
conversion
ca = a.tocsr()
ca
# => <1x10000 sparse matrix of type '<type 'numpy.float64'>'
with 947 stored elements in Compressed Sparse Row format>
#lil =>became csr
Matrix product
#Transpose matrix
ta = a.T
#Matrix multiplication
print a.dot(ta) # (1,1)Matrix, but this is also represented by a sparse matrix
# => (0, 0) 853.19504342
Vector magnitude
v = np.array([[1, 1]])
math.sqrt(np.dot(v, v.T))
# => 1.4142135623730951
np.linalg.norm(v)
# => 1.4142135623730951
np.linalg.norm(a)
# =>Error occurs
np.linalg.norm(a.todense())
np.linalg.norm(a.toarray())
# => 29.209502621916037
#Cosine similarity
import scipy.spatial.distance as dis
dis.cosine(a.todense(), b.todense())
# => 0.91347774109309299
Euclidean distance of sparse matrix
# -*- encoding: utf-8 -*-
import scipy.spatial.distance as dis
import scipy.sparse as sp
import numpy as np, scipy.io as io
import math
def sparse_distance(v1, v2):
"""1*Find the Euclidean distance between N vectors
args:
v1, v2 : 1 *Of N(Sparse)queue
"""
if not sp.issparse(v1) or not sp.issparse(v2):
#Use the built-in euclidean if it is not a sparse matrix
if v1.size != v2.size:
raise ValueError
return dis.euclidean(v1, v2)
indexes1 = v1.rows.item()[:]
indexes2 = v2.rows.item()[:]
if indexes1.length != indexes2.length:
raise ValueError
indexes = indexes1 + indexes2 #Index where the two vectors are not sparse
euc_dis = 0.0
for index in indexes:
_dis = v1[0, index] - v2[0, index]
euc_dis += _dis ** 2
return math.sqrt(euc_dis)
Recommended Posts