_data.py 13 KB

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