def setUp(self): np.random.seed(1) self.A = np.random.uniform(0, 1, size=(20, 10)) self.b = np.random.uniform(0, 1, size=20) prob = blitzml.LassoProblem(self.A, self.b) self.lam = 0.02 * prob.compute_max_l1_penalty() self.sol = prob.solve(self.lam)
def test_sparse_linear_lambda_max_basic(self): A = np.array([[1, -2], [1, 0]]).T b = np.array([3, -1]) prob = blitzml.LassoProblem(A, b) lammax = prob.compute_max_l1_penalty() self.assertEqual(lammax, 6.0) lammax2 = prob.compute_max_l1_penalty(include_bias_term=False) self.assertEqual(lammax2, 5.0)
def test_lasso_empty(self): A = sp.csc_matrix((100, 1000)) b = np.ones(100) with captured_output() as out: prob = blitzml.LassoProblem(A, b) self.assertEqual(prob.compute_max_l1_penalty(), 0.) sol = prob.solve(0.) self.assertEqual(np.linalg.norm(sol.weights), 0.) self.assertEqual(sol.bias, 1.0) self.assertEqual(sol.objective_value, 0.) self.assertEqual(sol.duality_gap, 0.)
def test_lasso_bad_initial_conditions(self): n = 6 d = 4 A = np.arange(n * d).reshape(n, d) b = np.arange(n) prob = blitzml.LassoProblem(A, b) lammax = prob.compute_max_l1_penalty() weights0 = -100 * np.arange(d) lam = 0.02 * lammax sol = prob.solve(lam, initial_weights=weights0, stopping_tolerance=1e-5) self.assertEqual(is_solution(sol, A, b, lam), True)
def test_lasso_good_initial_conditions(self): n = 7 d = 30 np.random.seed(0) A = np.random.randn(n, d) b = np.random.randn(n) prob = blitzml.LassoProblem(A, b) lammax = prob.compute_max_l1_penalty() lam = 0.005 * lammax sol0 = prob.solve(lam, stopping_tolerance=1e-5) sol = prob.solve(lam, initial_weights=sol0.weights, max_time=-1.0) self.assertEqual(is_solution(sol, A, b, lam), True)
def test_lasso_empty_cols(self): np.random.seed(1) A = np.random.randn(20, 30) A[:, 0] = 0. A[:, 10] = 0. A[:, 29] = 0. A_sparse = sp.csc_matrix(A) b = np.random.randn(20) prob = blitzml.LassoProblem(A_sparse, b) lammax = prob.compute_max_l1_penalty() lam = 0.05 * lammax sol = prob.solve(lam, stopping_tolerance=1e-5) self.assertEqual(is_solution(sol, A, b, lam), True)
def setUp(self): A = np.array([[2]], dtype=np.float) b = np.array([10], dtype=np.double) with captured_output() as out: self.prob = blitzml.LassoProblem(A, b)
def test_lasso_csr_matrix_warning(self): self.A = sp.csr_matrix(self.A) with captured_output() as out: prob = blitzml.LassoProblem(self.A, self.b) message = out[0] self.assertTrue("Warning" in message)
def test_lasso_dense_copy_warning_F_contiguous(self): self.A = np.asfortranarray(self.A) with captured_output() as out: prob = blitzml.LassoProblem(self.A, self.b) message = out[0] self.assertTrue("Warning" not in message)
def setUp(self): A = np.arange(20).reshape(5, 4) b = np.arange(5).astype(np.float64) self.prob = blitzml.LassoProblem(A, b)