@@ -75,19 +75,20 @@ def compute_scores(X):
75
75
fa_scores .append (np .mean (cross_val_score (fa , X )))
76
76
77
77
return pca_scores , fa_scores
78
-
79
-
78
+
79
+
80
80
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 ))
85
85
86
86
87
87
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
+
91
92
for X , title in [(X_homo , 'Homoscedastic Noise' ),
92
93
(X_hetero , 'Heteroscedastic Noise' )]:
93
94
pca_scores , fa_scores = compute_scores (X )
@@ -97,7 +98,7 @@ def lw_score(X):
97
98
pca = PCA (n_components = 'mle' )
98
99
pca .fit (X )
99
100
n_components_pca_mle = pca .n_components_
100
-
101
+
101
102
print ("best n_components by PCA CV = %d" % n_components_pca )
102
103
print ("best n_components by FactorAnalysis CV = %d" % n_components_fa )
103
104
print ("best n_components by PCA MLE = %d" % n_components_pca_mle )
@@ -115,10 +116,10 @@ def lw_score(X):
115
116
116
117
# compare with other covariance estimators
117
118
pl .axhline (shrunk_cov_score (X ), color = 'violet' ,
118
- label = 'Shrunk Covariance MLE' , linestyle = '-- ' )
119
+ label = 'Shrunk Covariance MLE' , linestyle = '-. ' )
119
120
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
+
122
123
pl .xlabel ('nb of components' )
123
124
pl .ylabel ('CV scores' )
124
125
pl .legend (loc = 'lower right' )
0 commit comments