def test_primal_dual_relationship(): y = y_diabetes.reshape(-1, 1) coef = _solve_cholesky(X_diabetes, y, alpha=[1e-2]) K = np.dot(X_diabetes, X_diabetes.T) dual_coef = _solve_cholesky_kernel(K, y, alpha=[1e-2]) coef2 = np.dot(X_diabetes.T, dual_coef).T assert_array_almost_equal(coef, coef2)
def test_ridge_sample_weights_in_feature_space(): """Check that Cholesky solver in feature space applies sample_weights correctly. """ rng = np.random.RandomState(42) n_samples_list = [5, 6, 7] * 2 n_features_list = [7, 6, 5] * 2 n_targets_list = [1, 1, 1, 2, 2, 2] noise = 1. alpha = 2. alpha = np.atleast_1d(alpha) for n_samples, n_features, n_targets in zip(n_samples_list, n_features_list, n_targets_list): X = rng.randn(n_samples, n_features) beta = rng.randn(n_features, n_targets) Y = X.dot(beta) Y_noisy = Y + rng.randn(*Y.shape) * np.sqrt((Y**2).sum(0)) * noise K = X.dot(X.T) sample_weights = 1. + (rng.randn(n_samples)**2) * 10 coef_sample_space = _solve_cholesky_kernel( K, Y_noisy, alpha, sample_weight=sample_weights) coef_feature_space = _solve_cholesky(X, Y_noisy, alpha, sample_weight=sample_weights) assert_array_almost_equal(X.T.dot(coef_sample_space), coef_feature_space.T)
def test_primal_dual_relationship(): y = y_diabetes.reshape(-1, 1) coef = _solve_cholesky(X_diabetes, y, alpha=[1e-2]) K = np.dot(X_diabetes, X_diabetes.T) dual_coef = _solve_cholesky_kernel(K, y, alpha=[1e-2]) coef2 = np.dot(X_diabetes.T, dual_coef).T assert_array_almost_equal(coef, coef2)
def test_ridge_sample_weights_in_feature_space(): """Check that Cholesky solver in feature space applies sample_weights correctly. """ rng = np.random.RandomState(42) n_samples_list = [5, 6, 7] * 2 n_features_list = [7, 6, 5] * 2 n_targets_list = [1, 1, 1, 2, 2, 2] noise = 1. alpha = 2. alpha = np.atleast_1d(alpha) for n_samples, n_features, n_targets in zip(n_samples_list, n_features_list, n_targets_list): X = rng.randn(n_samples, n_features) beta = rng.randn(n_features, n_targets) Y = X.dot(beta) Y_noisy = Y + rng.randn(*Y.shape) * np.sqrt((Y ** 2).sum(0)) * noise K = X.dot(X.T) sample_weights = 1. + (rng.randn(n_samples) ** 2) * 10 coef_sample_space = _solve_cholesky_kernel(K, Y_noisy, alpha, sample_weight=sample_weights) coef_feature_space = _solve_cholesky(X, Y_noisy, alpha, sample_weight=sample_weights) assert_array_almost_equal(X.T.dot(coef_sample_space), coef_feature_space.T)