def test_vstack_with_single_vector(self): expected = SparseMatrix.from_list([[1, 0, 2, 0, 3, 4, 0, 5]]) result = SparseMatrix.vstack( [SparseVector.from_list([1, 0, 2, 0, 3, 4, 0, 5])]) self.assertEqual(result.shape, (1, 8)) self.assertEqual(result, expected)
def test_vstack_with_multiple_vectors(self): arrays = [np.random.randint(0, 10, 30) for _ in range(20)] vectors = [SparseVector.from_list(arrays[i]) for i in range(20)] expected = SparseMatrix.from_list(arrays) result = SparseMatrix.vstack(vectors) self.assertEqual(result, expected)
def select(self, classz): """ Computes the sub-matrix that represents all training examples whose classification is the specified class. :param classz: The class to select. :return: A sparse matrix of data specific to a class. """ indices = np.where(self.classes == classz)[0] return SparseMatrix.vstack([self.counts.get_row(i) for i in indices])
def shuffle(self): """ Shuffles this training database, re-arranging the order of both the classes and data by row only. """ rows = [row for row in self.counts.get_rows()] state = np.random.get_state() for item in [self.classes, rows]: np.random.set_state(state) np.random.shuffle(item) self.counts = SparseMatrix.vstack(rows, dtype=self.counts.data.dtype)