@@ -293,8 +293,8 @@ def _solve_lsqr(self, X, y, shrinkage):
293
293
self .means_ = _class_means (X , y )
294
294
self .covariance_ = _class_cov (X , y , self .priors_ , shrinkage )
295
295
self .coef_ = linalg .lstsq (self .covariance_ , self .means_ .T )[0 ].T
296
- self .intercept_ = (- 0.5 * np .diag (np .dot (self .means_ , self .coef_ .T ))
297
- + np .log (self .priors_ ))
296
+ self .intercept_ = (- 0.5 * np .diag (np .dot (self .means_ , self .coef_ .T )) +
297
+ np .log (self .priors_ ))
298
298
299
299
def _solve_eigen (self , X , y , shrinkage ):
300
300
"""Eigenvalue solver.
@@ -336,15 +336,16 @@ class scatter). This solver supports both classification and
336
336
Sb = St - Sw # between scatter
337
337
338
338
evals , evecs = linalg .eigh (Sb , Sw )
339
- self .explained_variance_ratio_ = np .sort (evals / np .sum (evals ))[::- 1 ]
339
+ self .explained_variance_ratio_ = np .sort (evals / np .sum (evals )
340
+ )[::- 1 ][:self ._max_components ]
340
341
evecs = evecs [:, np .argsort (evals )[::- 1 ]] # sort eigenvectors
341
342
# evecs /= np.linalg.norm(evecs, axis=0) # doesn't work with numpy 1.6
342
343
evecs /= np .apply_along_axis (np .linalg .norm , 0 , evecs )
343
344
344
345
self .scalings_ = evecs
345
346
self .coef_ = np .dot (self .means_ , evecs ).dot (evecs .T )
346
- self .intercept_ = (- 0.5 * np .diag (np .dot (self .means_ , self .coef_ .T ))
347
- + np .log (self .priors_ ))
347
+ self .intercept_ = (- 0.5 * np .diag (np .dot (self .means_ , self .coef_ .T )) +
348
+ np .log (self .priors_ ))
348
349
349
350
def _solve_svd (self , X , y ):
350
351
"""SVD solver.
@@ -400,12 +401,12 @@ def _solve_svd(self, X, y):
400
401
_ , S , V = linalg .svd (X , full_matrices = 0 )
401
402
402
403
self .explained_variance_ratio_ = (S ** 2 / np .sum (
403
- S ** 2 ))[:self .n_components ]
404
+ S ** 2 ))[:self ._max_components ]
404
405
rank = np .sum (S > self .tol * S [0 ])
405
406
self .scalings_ = np .dot (scalings , V .T [:, :rank ])
406
407
coef = np .dot (self .means_ - self .xbar_ , self .scalings_ )
407
- self .intercept_ = (- 0.5 * np .sum (coef ** 2 , axis = 1 )
408
- + np .log (self .priors_ ))
408
+ self .intercept_ = (- 0.5 * np .sum (coef ** 2 , axis = 1 ) +
409
+ np .log (self .priors_ ))
409
410
self .coef_ = np .dot (coef , self .scalings_ .T )
410
411
self .intercept_ -= np .dot (self .xbar_ , self .coef_ .T )
411
412
@@ -457,6 +458,13 @@ def fit(self, X, y, store_covariance=None, tol=None):
457
458
UserWarning )
458
459
self .priors_ = self .priors_ / self .priors_ .sum ()
459
460
461
+ # Get the maximum number of components
462
+ if self .n_components is None :
463
+ self ._max_components = len (self .classes_ ) - 1
464
+ else :
465
+ self ._max_components = min (len (self .classes_ ) - 1 ,
466
+ self .n_components )
467
+
460
468
if self .solver == 'svd' :
461
469
if self .shrinkage is not None :
462
470
raise NotImplementedError ('shrinkage not supported' )
@@ -497,9 +505,8 @@ def transform(self, X):
497
505
X_new = np .dot (X - self .xbar_ , self .scalings_ )
498
506
elif self .solver == 'eigen' :
499
507
X_new = np .dot (X , self .scalings_ )
500
- n_components = X .shape [1 ] if self .n_components is None \
501
- else self .n_components
502
- return X_new [:, :n_components ]
508
+
509
+ return X_new [:, :self ._max_components ]
503
510
504
511
def predict_proba (self , X ):
505
512
"""Estimate probability.
0 commit comments