本文整理汇总了Python中sklearn.datasets.make_spd_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python datasets.make_spd_matrix方法的具体用法?Python datasets.make_spd_matrix怎么用?Python datasets.make_spd_matrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.datasets
的用法示例。
在下文中一共展示了datasets.make_spd_matrix方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: random_model
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import make_spd_matrix [as 别名]
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: make_covar_matrix
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import make_spd_matrix [as 别名]
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: test_make_spd_matrix
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import make_spd_matrix [as 别名]
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: test_Metric
# 需要导入模块: from sklearn import datasets [as 别名]
# 或者: from sklearn.datasets import make_spd_matrix [as 别名]
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])