Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 101f0ee

Browse files
committed
ENH : better shrinkage range for ShrunkCovariance
1 parent 4516bf5 commit 101f0ee

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

examples/decomposition/plot_pca_vs_fa_model_selection.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,20 @@ def compute_scores(X):
7575
fa_scores.append(np.mean(cross_val_score(fa, X)))
7676

7777
return pca_scores, fa_scores
78-
79-
78+
79+
8080
def shrunk_cov_score(X):
81-
shrinkages = np.logspace(-100, 0, 30)
82-
tuned_parameters = [{'shrinkage': shrinkages}]
83-
cv = GridSearchCV(ShrunkCovariance(), tuned_parameters)
84-
return np.mean(cross_val_score(cv.fit(X).best_estimator_, X, cv=3))
81+
shrinkages = np.logspace(-2, 0, 30)
82+
cv = GridSearchCV(ShrunkCovariance(), {'shrinkage': shrinkages})
83+
a = cv.fit(X).best_estimator_
84+
return np.mean(cross_val_score(cv.fit(X).best_estimator_, X))
8585

8686

8787
def lw_score(X):
88-
return np.mean(cross_val_score(LedoitWolf(), X, cv=3))
89-
90-
88+
a = LedoitWolf().fit(X)
89+
return np.mean(cross_val_score(LedoitWolf(), X))
90+
91+
9192
for X, title in [(X_homo, 'Homoscedastic Noise'),
9293
(X_hetero, 'Heteroscedastic Noise')]:
9394
pca_scores, fa_scores = compute_scores(X)
@@ -97,7 +98,7 @@ def lw_score(X):
9798
pca = PCA(n_components='mle')
9899
pca.fit(X)
99100
n_components_pca_mle = pca.n_components_
100-
101+
101102
print("best n_components by PCA CV = %d" % n_components_pca)
102103
print("best n_components by FactorAnalysis CV = %d" % n_components_fa)
103104
print("best n_components by PCA MLE = %d" % n_components_pca_mle)
@@ -115,10 +116,10 @@ def lw_score(X):
115116

116117
# compare with other covariance estimators
117118
pl.axhline(shrunk_cov_score(X), color='violet',
118-
label='Shrunk Covariance MLE', linestyle='--')
119+
label='Shrunk Covariance MLE', linestyle='-.')
119120
pl.axhline(lw_score(X), color='orange',
120-
label='LedoitWolf MLE' % n_components_pca_mle, linestyle='--')
121-
121+
label='LedoitWolf MLE' % n_components_pca_mle, linestyle='-.')
122+
122123
pl.xlabel('nb of components')
123124
pl.ylabel('CV scores')
124125
pl.legend(loc='lower right')

0 commit comments

Comments
 (0)