data.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. """Base class for sparse matrice with a .data attribute
  2. subclasses must provide a _with_data() method that
  3. creates a new matrix with the same sparsity pattern
  4. as self but with a different data array
  5. """
  6. from __future__ import division, print_function, absolute_import
  7. import numpy as np
  8. from .base import spmatrix, _ufuncs_with_fixed_point_at_zero
  9. from .sputils import isscalarlike, validateaxis
  10. __all__ = []
  11. # TODO implement all relevant operations
  12. # use .data.__methods__() instead of /=, *=, etc.
  13. class _data_matrix(spmatrix):
  14. def __init__(self):
  15. spmatrix.__init__(self)
  16. def _get_dtype(self):
  17. return self.data.dtype
  18. def _set_dtype(self, newtype):
  19. self.data.dtype = newtype
  20. dtype = property(fget=_get_dtype, fset=_set_dtype)
  21. def _deduped_data(self):
  22. if hasattr(self, 'sum_duplicates'):
  23. self.sum_duplicates()
  24. return self.data
  25. def __abs__(self):
  26. return self._with_data(abs(self._deduped_data()))
  27. def _real(self):
  28. return self._with_data(self.data.real)
  29. def _imag(self):
  30. return self._with_data(self.data.imag)
  31. def __neg__(self):
  32. if self.dtype.kind == 'b':
  33. raise NotImplementedError('negating a sparse boolean '
  34. 'matrix is not supported')
  35. return self._with_data(-self.data)
  36. def __imul__(self, other): # self *= other
  37. if isscalarlike(other):
  38. self.data *= other
  39. return self
  40. else:
  41. return NotImplemented
  42. def __itruediv__(self, other): # self /= other
  43. if isscalarlike(other):
  44. recip = 1.0 / other
  45. self.data *= recip
  46. return self
  47. else:
  48. return NotImplemented
  49. def astype(self, dtype, casting='unsafe', copy=True):
  50. dtype = np.dtype(dtype)
  51. if self.dtype != dtype:
  52. return self._with_data(
  53. self._deduped_data().astype(dtype, casting=casting, copy=copy),
  54. copy=copy)
  55. elif copy:
  56. return self.copy()
  57. else:
  58. return self
  59. astype.__doc__ = spmatrix.astype.__doc__
  60. def conj(self, copy=True):
  61. if np.issubdtype(self.dtype, np.complexfloating):
  62. return self._with_data(self.data.conj(), copy=copy)
  63. elif copy:
  64. return self.copy()
  65. else:
  66. return self
  67. conj.__doc__ = spmatrix.conj.__doc__
  68. def copy(self):
  69. return self._with_data(self.data.copy(), copy=True)
  70. copy.__doc__ = spmatrix.copy.__doc__
  71. def count_nonzero(self):
  72. return np.count_nonzero(self._deduped_data())
  73. count_nonzero.__doc__ = spmatrix.count_nonzero.__doc__
  74. def power(self, n, dtype=None):
  75. """
  76. This function performs element-wise power.
  77. Parameters
  78. ----------
  79. n : n is a scalar
  80. dtype : If dtype is not specified, the current dtype will be preserved.
  81. """
  82. if not isscalarlike(n):
  83. raise NotImplementedError("input is not scalar")
  84. data = self._deduped_data()
  85. if dtype is not None:
  86. data = data.astype(dtype)
  87. return self._with_data(data ** n)
  88. ###########################
  89. # Multiplication handlers #
  90. ###########################
  91. def _mul_scalar(self, other):
  92. return self._with_data(self.data * other)
  93. # Add the numpy unary ufuncs for which func(0) = 0 to _data_matrix.
  94. for npfunc in _ufuncs_with_fixed_point_at_zero:
  95. name = npfunc.__name__
  96. def _create_method(op):
  97. def method(self):
  98. result = op(self._deduped_data())
  99. return self._with_data(result, copy=True)
  100. method.__doc__ = ("Element-wise %s.\n\n"
  101. "See numpy.%s for more information." % (name, name))
  102. method.__name__ = name
  103. return method
  104. setattr(_data_matrix, name, _create_method(npfunc))
  105. def _find_missing_index(ind, n):
  106. for k, a in enumerate(ind):
  107. if k != a:
  108. return k
  109. k += 1
  110. if k < n:
  111. return k
  112. else:
  113. return -1
  114. class _minmax_mixin(object):
  115. """Mixin for min and max methods.
  116. These are not implemented for dia_matrix, hence the separate class.
  117. """
  118. def _min_or_max_axis(self, axis, min_or_max):
  119. N = self.shape[axis]
  120. if N == 0:
  121. raise ValueError("zero-size array to reduction operation")
  122. M = self.shape[1 - axis]
  123. mat = self.tocsc() if axis == 0 else self.tocsr()
  124. mat.sum_duplicates()
  125. major_index, value = mat._minor_reduce(min_or_max)
  126. not_full = np.diff(mat.indptr)[major_index] < N
  127. value[not_full] = min_or_max(value[not_full], 0)
  128. mask = value != 0
  129. major_index = np.compress(mask, major_index)
  130. value = np.compress(mask, value)
  131. from . import coo_matrix
  132. if axis == 0:
  133. return coo_matrix((value, (np.zeros(len(value)), major_index)),
  134. dtype=self.dtype, shape=(1, M))
  135. else:
  136. return coo_matrix((value, (major_index, np.zeros(len(value)))),
  137. dtype=self.dtype, shape=(M, 1))
  138. def _min_or_max(self, axis, out, min_or_max):
  139. if out is not None:
  140. raise ValueError(("Sparse matrices do not support "
  141. "an 'out' parameter."))
  142. validateaxis(axis)
  143. if axis is None:
  144. if 0 in self.shape:
  145. raise ValueError("zero-size array to reduction operation")
  146. zero = self.dtype.type(0)
  147. if self.nnz == 0:
  148. return zero
  149. m = min_or_max.reduce(self._deduped_data().ravel())
  150. if self.nnz != np.product(self.shape):
  151. m = min_or_max(zero, m)
  152. return m
  153. if axis < 0:
  154. axis += 2
  155. if (axis == 0) or (axis == 1):
  156. return self._min_or_max_axis(axis, min_or_max)
  157. else:
  158. raise ValueError("axis out of range")
  159. def _arg_min_or_max_axis(self, axis, op, compare):
  160. if self.shape[axis] == 0:
  161. raise ValueError("Can't apply the operation along a zero-sized "
  162. "dimension.")
  163. if axis < 0:
  164. axis += 2
  165. zero = self.dtype.type(0)
  166. mat = self.tocsc() if axis == 0 else self.tocsr()
  167. mat.sum_duplicates()
  168. ret_size, line_size = mat._swap(mat.shape)
  169. ret = np.zeros(ret_size, dtype=int)
  170. nz_lines, = np.nonzero(np.diff(mat.indptr))
  171. for i in nz_lines:
  172. p, q = mat.indptr[i:i + 2]
  173. data = mat.data[p:q]
  174. indices = mat.indices[p:q]
  175. am = op(data)
  176. m = data[am]
  177. if compare(m, zero) or q - p == line_size:
  178. ret[i] = indices[am]
  179. else:
  180. zero_ind = _find_missing_index(indices, line_size)
  181. if m == zero:
  182. ret[i] = min(am, zero_ind)
  183. else:
  184. ret[i] = zero_ind
  185. if axis == 1:
  186. ret = ret.reshape(-1, 1)
  187. return np.asmatrix(ret)
  188. def _arg_min_or_max(self, axis, out, op, compare):
  189. if out is not None:
  190. raise ValueError("Sparse matrices do not support "
  191. "an 'out' parameter.")
  192. validateaxis(axis)
  193. if axis is None:
  194. if 0 in self.shape:
  195. raise ValueError("Can't apply the operation to "
  196. "an empty matrix.")
  197. if self.nnz == 0:
  198. return 0
  199. else:
  200. zero = self.dtype.type(0)
  201. mat = self.tocoo()
  202. mat.sum_duplicates()
  203. am = op(mat.data)
  204. m = mat.data[am]
  205. if compare(m, zero):
  206. return mat.row[am] * mat.shape[1] + mat.col[am]
  207. else:
  208. size = np.product(mat.shape)
  209. if size == mat.nnz:
  210. return am
  211. else:
  212. ind = mat.row * mat.shape[1] + mat.col
  213. zero_ind = _find_missing_index(ind, size)
  214. if m == zero:
  215. return min(zero_ind, am)
  216. else:
  217. return zero_ind
  218. return self._arg_min_or_max_axis(axis, op, compare)
  219. def max(self, axis=None, out=None):
  220. """
  221. Return the maximum of the matrix or maximum along an axis.
  222. This takes all elements into account, not just the non-zero ones.
  223. Parameters
  224. ----------
  225. axis : {-2, -1, 0, 1, None} optional
  226. Axis along which the sum is computed. The default is to
  227. compute the maximum over all the matrix elements, returning
  228. a scalar (i.e. `axis` = `None`).
  229. out : None, optional
  230. This argument is in the signature *solely* for NumPy
  231. compatibility reasons. Do not pass in anything except
  232. for the default value, as this argument is not used.
  233. Returns
  234. -------
  235. amax : coo_matrix or scalar
  236. Maximum of `a`. If `axis` is None, the result is a scalar value.
  237. If `axis` is given, the result is a sparse.coo_matrix of dimension
  238. ``a.ndim - 1``.
  239. See Also
  240. --------
  241. min : The minimum value of a sparse matrix along a given axis.
  242. np.matrix.max : NumPy's implementation of 'max' for matrices
  243. """
  244. return self._min_or_max(axis, out, np.maximum)
  245. def min(self, axis=None, out=None):
  246. """
  247. Return the minimum of the matrix or maximum along an axis.
  248. This takes all elements into account, not just the non-zero ones.
  249. Parameters
  250. ----------
  251. axis : {-2, -1, 0, 1, None} optional
  252. Axis along which the sum is computed. The default is to
  253. compute the minimum over all the matrix elements, returning
  254. a scalar (i.e. `axis` = `None`).
  255. out : None, optional
  256. This argument is in the signature *solely* for NumPy
  257. compatibility reasons. Do not pass in anything except for
  258. the default value, as this argument is not used.
  259. Returns
  260. -------
  261. amin : coo_matrix or scalar
  262. Minimum of `a`. If `axis` is None, the result is a scalar value.
  263. If `axis` is given, the result is a sparse.coo_matrix of dimension
  264. ``a.ndim - 1``.
  265. See Also
  266. --------
  267. max : The maximum value of a sparse matrix along a given axis.
  268. np.matrix.min : NumPy's implementation of 'min' for matrices
  269. """
  270. return self._min_or_max(axis, out, np.minimum)
  271. def argmax(self, axis=None, out=None):
  272. """Return indices of maximum elements along an axis.
  273. Implicit zero elements are also taken into account. If there are
  274. several maximum values, the index of the first occurrence is returned.
  275. Parameters
  276. ----------
  277. axis : {-2, -1, 0, 1, None}, optional
  278. Axis along which the argmax is computed. If None (default), index
  279. of the maximum element in the flatten data is returned.
  280. out : None, optional
  281. This argument is in the signature *solely* for NumPy
  282. compatibility reasons. Do not pass in anything except for
  283. the default value, as this argument is not used.
  284. Returns
  285. -------
  286. ind : np.matrix or int
  287. Indices of maximum elements. If matrix, its size along `axis` is 1.
  288. """
  289. return self._arg_min_or_max(axis, out, np.argmax, np.greater)
  290. def argmin(self, axis=None, out=None):
  291. """Return indices of minimum elements along an axis.
  292. Implicit zero elements are also taken into account. If there are
  293. several minimum values, the index of the first occurrence is returned.
  294. Parameters
  295. ----------
  296. axis : {-2, -1, 0, 1, None}, optional
  297. Axis along which the argmin is computed. If None (default), index
  298. of the minimum element in the flatten data is returned.
  299. out : None, optional
  300. This argument is in the signature *solely* for NumPy
  301. compatibility reasons. Do not pass in anything except for
  302. the default value, as this argument is not used.
  303. Returns
  304. -------
  305. ind : np.matrix or int
  306. Indices of minimum elements. If matrix, its size along `axis` is 1.
  307. """
  308. return self._arg_min_or_max(axis, out, np.argmin, np.less)