You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromsklearnimportdatasetsfromsklearn.decompositionimportPCAfromsklearn.discriminant_analysisimportLinearDiscriminantAnalysisiris=datasets.load_iris()
X=iris.datay=iris.targettarget_names=iris.target_names#### dimensionality reduction using PCApca=PCA(n_components=2)
X_r=pca.fit(X).transform(X)
#### Percentage of variance explained for each componentsprint('PCA: explained variance ratio (first two components): %s'%str(pca.explained_variance_ratio_))
#### dimensionality reduction using LDAlda=LinearDiscriminantAnalysis(n_components=2)
X_r2=lda.fit(X, y).transform(X)
print('LDA: explained variance ratio (first two components): %s'%str(lda.explained_variance_ratio_))
Expected Results
The first componet of the PCA has a larger variance ratio than that from the first componet from LDA.
Actual Results
PCA: explained variance ratio (first two components): [ 0.925 0.053]
LDA: explained variance ratio (first two components): [ 0.991 0.009]
Euh, I'm really not sure explained_variance_ratio should be the same for PCA and LDA.
PCA is unsupervised, LDA is supervised. The principal components are calculated differently since LDA needs a label (y) for each point (that's why lda.fit(X, y).transform(X) and pca.fit(X).transform(X)).
Since LDA will find different principal components, I see no reason why explained_variance_ratio should be the same in both cases.
If I'm wrong please let me know, otherwise this issue should be closed because it's not a bug.
It shouldn't be the same, but I would have expected PCA to have more explained variance since that's what it's optimizing, while LDA has other things it optimizes. But I didn't think deeply about what the explained_variance_ratio in LDA means. My intuition was that the first principal component should explain more variance than any other direction. But maybe the denominator is different for the two ratios?
I got the same problem on LDA, its sum of explained_variance_ratio_ was greater than 1.
It seems related to the solver = 'eigen', as it runs ok with 'svd'. I will problably post my notebook on github later on, if it is of help I can post the link here afterwards, but for now I will leave this here:
pctVarExp= []
#Pipelines com os Escaladores e Redução de dimensionalidade:forii, clasfinenumerate(classifs):
pipelines[names[ii]] =Pipeline([('DimRed', LinearDiscriminantAnalysis(n_components=2, solver='eigen')), ( 'classifier', clasf)])
#Fits e Scoresscores= {'NN': [], 'DMC': [], 'CQG': []}
foriiinrange(500):
X_train, X_test, y_train, y_test=train_test_split(dfX,dfY,test_size=0.2, random_state=ii)
forkey, valueinpipelines.items():
value.fit(X_train, y_train)
pctVarExp.append(np.sum(value.named_steps['DimRed'].explained_variance_ratio_)) # % da variância explicada pelas componentesscores[key].append(value.score(X_test,y_test))
## Tirando a média: ##forkeyinscores:
scores[key] =np.mean(scores[key])
When I look at pctVarExp it has values like: [1.000000001356325, 1.000000001356325, 1.000000001356325, 0.9999999914732854, 0.9999999914732854, 1.0000000191251681]
Euh, I'm really not sure explained_variance_ratio should be the same for PCA and LDA.
PCA is unsupervised, LDA is supervised. The principal components are calculated differently since LDA needs a label (y) for each point (that's why lda.fit(X, y).transform(X) and pca.fit(X).transform(X)).
Since LDA will find different principal components, I see no reason why explained_variance_ratio should be the same in both cases.
If I'm wrong please let me know, otherwise this issue should be closed because it's not a bug.
Adding on to @JPFrancoia 's comment, there is indeed no reason why the two ratios should be the same since they are computed based on different data. Therefore it might not be a bug.
In particular, while both are ratios of descending eigenvalues against the sum of the eigenvalues, PCA computes eigenvalues from the entire data set, X, while LDA computes those from W^{-1}B, the multiplication of within-class variance and between-class variance calculated from X and y. For example, when X is fixed, PCA would produce the same eigenvectors and eigenvalues every time, thus the same ratio of variance explained. However, LDA would give different ratios (because of different projection directions found) for different sets of y even when X is the same.
Activity
amueller commentedon May 22, 2018
Possibly related to #9107. Thanks for the report, sorry for the slow reply.
JPFrancoia commentedon May 22, 2018
Euh, I'm really not sure
explained_variance_ratioshould be the same for PCA and LDA.PCA is unsupervised, LDA is supervised. The principal components are calculated differently since LDA needs a label (y) for each point (that's why
lda.fit(X, y).transform(X)andpca.fit(X).transform(X)).Since LDA will find different principal components, I see no reason why
explained_variance_ratioshould be the same in both cases.If I'm wrong please let me know, otherwise this issue should be closed because it's not a bug.
amueller commentedon May 24, 2018
It shouldn't be the same, but I would have expected PCA to have more explained variance since that's what it's optimizing, while LDA has other things it optimizes. But I didn't think deeply about what the
explained_variance_ratioin LDA means. My intuition was that the first principal component should explain more variance than any other direction. But maybe the denominator is different for the two ratios?ycpei commentedon Dec 4, 2018
Update on 2018-12-07: I was wrong. So please ignore this comment.
I suspect the problem has to do with the fact that each feature is normalised individually before SVD (see _solve_svd):
This was later "rectified" with the following:
where V and S are calculated using svd
I don't think this makes sense mathematically. It produces a different
scalingsfrom the following:mateuspontesm commentedon Dec 5, 2018
I got the same problem on LDA, its sum of explained_variance_ratio_ was greater than 1.
It seems related to the solver = 'eigen', as it runs ok with 'svd'. I will problably post my notebook on github later on, if it is of help I can post the link here afterwards, but for now I will leave this here:
When I look at pctVarExp it has values like: [1.000000001356325, 1.000000001356325, 1.000000001356325, 0.9999999914732854, 0.9999999914732854, 1.0000000191251681]
YangXiaozhou commentedon Mar 7, 2021
Adding on to @JPFrancoia 's comment, there is indeed no reason why the two ratios should be the same since they are computed based on different data. Therefore it might not be a bug.
In particular, while both are ratios of descending eigenvalues against the sum of the eigenvalues, PCA computes eigenvalues from the entire data set, X, while LDA computes those from W^{-1}B, the multiplication of within-class variance and between-class variance calculated from X and y. For example, when X is fixed, PCA would produce the same eigenvectors and eigenvalues every time, thus the same ratio of variance explained. However, LDA would give different ratios (because of different projection directions found) for different sets of y even when X is the same.