scipy csr_matrix和csc_matrix函数的用法(通俗易懂版)

概述

  • scipy.sparse库中的函数
  • 为了将稀疏的np.array数据进行压缩
  • 两者一个是行一个是列,基本思想差不多

csr_matrix

>>> 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])
>>> csr_matrix((data, indices, indptr), shape=(3, 3)).toarray()
array([[1, 0, 2],
       [0, 0, 3],
       [4, 5, 6]])
解读:
 	indptr:代表的是每行有几个值(第一个值默认是0)
 		第一行:2-0=2
 		第二行:3-2=1
 		第三行:6-3=3
 	indices:代表值在每行中的位置
 		0:第0行的第一个值1在第02:第0行的第二个值2在第2(0行够了)
 		2:第1行的第一个值3在第2(1行够了)
 		0:第2行的第一个值4在第01:第2行的第二个值5在第12:第2行的第三个值6在第2(完毕)

csc_matrix

>>> 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])
>>> csc_matrix((data, indices, indptr), shape=(3, 3)).toarray()
array([[1, 0, 4],
       [0, 0, 5],
       [2, 3, 6]])
解读:
	indptr:代表的是每列有几个值(第一个值默认是0)
		第一列:2-0=2
		第二列:3-2=1
		第三列:6-3=3
	indices:代表值在每列中的位置
		0:第0列的第一个值1在第02:第0列的第二个值2在第2(0列值够了)
		2:第1列的第一个值3在第2(1列值够了)
		0:第2列的第一个值4在第01:第2列的第一个值5在第12:第2列的第一个值6在第2(完毕)

你可能感兴趣的:(python,python,scipy)