Python源码示例:sklearn.datasets.make_spd_matrix()

示例1
def random_model(n, seed=None):
    """Generate random model (mu, sigma) for portfolio optimization problem.

    Args:
        n (int): number of assets.
        seed (int or None): random seed - if None, will not initialize.

    Returns:
        numpy.narray: expected return vector
        numpy.ndarray: covariance matrix

    """
    if seed:
        aqua_globals.random_seed = seed

    # draw random return values between [0, 1]
    m_u = aqua_globals.random.uniform(size=n, low=0, high=1)

    # construct positive semi-definite covariance matrix
    sigma = make_spd_matrix(n)

    return m_u, sigma 
示例2
def make_covar_matrix(covariance_type, n_components, n_features,
                      random_state=None):
    mincv = 0.1
    prng = check_random_state(random_state)
    if covariance_type == 'spherical':
        return (mincv + mincv * prng.random_sample((n_components,))) ** 2
    elif covariance_type == 'tied':
        return (make_spd_matrix(n_features)
                + mincv * np.eye(n_features))
    elif covariance_type == 'diag':
        return (mincv + mincv *
                prng.random_sample((n_components, n_features))) ** 2
    elif covariance_type == 'full':
        return np.array([
            (make_spd_matrix(n_features, random_state=prng)
             + mincv * np.eye(n_features))
            for x in range(n_components)
        ]) 
示例3
def test_make_spd_matrix():
    X = make_spd_matrix(n_dim=5, random_state=0)

    assert_equal(X.shape, (5, 5), "X shape mismatch")
    assert_array_almost_equal(X, X.T)

    from numpy.linalg import eig
    eigenvalues, _ = eig(X)
    assert_array_equal(eigenvalues > 0, np.array([True] * 5),
                       "X is not positive-definite") 
示例4
def test_Metric(self):
        np.random.seed(28)
        for d in [iris, wine, breast_cancer]:
            X, y = d()
            n, d = X.shape
            M = make_spd_matrix(d)

            metric = Metric(M)
            metric.fit(X, y)
            L = metric.transformer()
            assert_array_almost_equal(L.T.dot(L), M)

            LX1 = metric.transform()
            LX2 = metric.transform(X)

            dl1 = pdist(LX1)
            dl2 = pdist(LX2)
            dm = pdist(X, metric='mahalanobis', VI=M)  # CHecking that d_M = d_L

            assert_array_almost_equal(dm, dl1)
            assert_array_almost_equal(dm, dl2)

            d_, d = L.shape
            e_, e = M.shape

            assert_equal(d, e_)
            assert_equal(d, e)
            assert_equal(d, X.shape[1]) 
示例5
def test_make_spd_matrix():
    X = make_spd_matrix(n_dim=5, random_state=0)

    assert_equal(X.shape, (5, 5), "X shape mismatch")
    assert_array_almost_equal(X, X.T)

    from numpy.linalg import eig
    eigenvalues, _ = eig(X)
    assert_array_equal(eigenvalues > 0, np.array([True] * 5),
                       "X is not positive-definite")