def test_singular_value_helpers(n_samples, n_features, seed): # Make sure SVD and power method give approximately the same results X, Y = make_regression(n_samples, n_features, n_targets=5, random_state=seed) u1, v1, _ = _get_first_singular_vectors_power_method(X, Y, norm_y_weights=True) u2, v2 = _get_first_singular_vectors_svd(X, Y) _svd_flip_1d(u1, v1) _svd_flip_1d(u2, v2) rtol = 1e-1 assert_allclose(u1, u2, rtol=rtol) assert_allclose(v1, v2, rtol=rtol)
def test_svd_flip_1d(): # Make sure svd_flip_1d is equivalent to svd_flip u = np.array([1, -4, 2]) v = np.array([1, 2, 3]) u_expected, v_expected = svd_flip(u.reshape(-1, 1), v.reshape(1, -1)) _svd_flip_1d(u, v) # inplace assert_allclose(u, u_expected.ravel()) assert_allclose(u, [-1, 4, -2]) assert_allclose(v, v_expected.ravel()) assert_allclose(v, [-1, -2, -3])