def setUp(self): self.dir_ = data_dir + "/space_test_resources/" self.init_test_cases = [(DenseMatrix(np.array([[1,2],[3,4]])), ["car", "man"], ["feat1", "feat2"], {"man":1, "car":0}, {"feat1":0, "feat2":1}, [ScalingOperation(EpmiWeighting())]), (DenseMatrix(np.array([[1,2],[3,4]])), ["car", "man"], [], {"man":1, "car":0}, {}, [ScalingOperation(EpmiWeighting())])] self.m1 = np.array([[1,2,3]]) self.row1 = ["a"] self.row2 = ["a", "b", "c"] self.ft1 = ["f1","f2","f3"] self.space1 = Space(DenseMatrix(self.m1),self.row1, self.ft1) self.x = np.mat([[1,2,3],[2,4,6],[4,675,43]]) self.us = np.mat([[ 2.19272110e+00, 3.03174768e+00], [ 4.38544220e+00, 6.06349536e+00], [ 6.76369708e+02, -4.91431927e-02]]) self.space2 = Space(DenseMatrix(self.x), self.row2, self.ft1)
def test_apply_weighting_operation(self): test_cases = [(self.m1, np.array([[0,0,0]])), (self.m2, np.array([[0]]))] w = PpmiWeighting() for in_mat, expected_mat in test_cases: op = ScalingOperation(w) tmp_mat = in_mat.copy() out_mat = op.apply(DenseMatrix(in_mat)).mat np.testing.assert_array_almost_equal(expected_mat, out_mat, 7) np.testing.assert_array_equal(in_mat, tmp_mat) self.assertRaises(IllegalStateError, op.apply, DenseMatrix(in_mat))
def test_project_weighting_operation(self): test_cases = [(self.m1, self.m3, np.array([[0.69314718,0,0]])), (self.m2, self.m4, np.array([[0]]))] w = PpmiWeighting() for (core_mat, per_mat, expected_mat) in test_cases: op = ScalingOperation(w) tmp_mat = per_mat.copy() self.assertRaises(IllegalStateError, op.project, DenseMatrix(per_mat)) op.apply(DenseMatrix(core_mat)) out_mat = op.project(DenseMatrix(per_mat)).mat np.testing.assert_array_almost_equal(expected_mat, out_mat, 7) np.testing.assert_array_equal(per_mat, tmp_mat) out_mat = op.project(DenseMatrix(per_mat)).mat np.testing.assert_array_almost_equal(expected_mat, out_mat, 7)
def create_operation(self): return ScalingOperation(self)