def reshape(self, new_shape): """ Reshapes current matrix. Overwrites the current matrix with a new matrix of the given shape! Args: shape: length 2 tuple or pair of integers Raises: ValueError: if shape is not an integer pair or if new shape is inconsistent with the total size of the current matrix. """ if not isinstance(new_shape, tuple) or len(new_shape) != 2: raise ValueError("shape must be integer pair") no_rows, no_cols = self.mat.shape new_no_rows, new_no_cols = new_shape if not is_integer(new_no_rows) or not is_integer(new_no_cols): raise ValueError("shape must be integer pair") if no_rows * no_cols != new_no_rows * new_no_cols: raise ValueError("total size of new matrix must be unchanged.") # TODO: change here if we want a copy!! mat = self.mat.tocoo(copy=False) # upcast mat.row and mat.col if no_rows * no_cols >= 2 ** 31 - 1: linear_pos = np.array(mat.row, dtype=np.int64) * no_cols + mat.col else: linear_pos = mat.row * no_cols + mat.col mat.row = linear_pos // new_no_cols mat.col = linear_pos - (mat.row * new_no_cols) # NOTE: change here if we want a copy!! self.mat = csr_matrix((mat.data, (mat.row, mat.col)), shape=new_shape)
def set_min_samples(self, min_samples): if not is_integer(min_samples): raise ValueError("expected %s min_samples value, received %s" % ("integer", type(min_samples))) self._MIN_SAMPLES = min_samples