0

I am really confused about this result.. the same dataset for 2 classes and the result from PCA and LDA.. is it reasonable or something may be wrong?
Thanks for any answering!

enter image description here

X = data.drop(['label'], axis=1)
y = data['label']
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
"""
PCA 
"""
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)
"""
LDA 
"""
lda = LinearDiscriminantAnalysis(n_components=1,solver='svd').fit(X_scaled,y)
X_lda = lda.transform(X_scaled)

"""
Plot
"""
N = 2
target_names = list(range(N))
colors = ['yellow', 'purple']

"""
Plot PCA
"""
plt.figure()


lw = 2
for  color, i , target_name in zip(colors,range(N), target_names):
    plt.scatter(X_pca[y == i, 0], X_pca[y == i, 1], alpha=.8,color = color, lw = lw,label=target_name)
plt.legend(loc='best', shadow=False, scatterpoints=1)
plt.xlabel('pca 1')
plt.ylabel('pca 2')
plt.title('PCA')
plt.grid()
"""
Plot LDA  
"""
plt.figure()
for color, i, target_name in zip(colors,range(N), target_names):
    plt.scatter(X_lda[y == i, 0], X_lda[y == i, 0], alpha=.8, color=color,lw = lw,label=target_name)
plt.legend(loc='best', shadow=False, scatterpoints=1)
plt.xlabel('LD 1')
plt.ylabel('LD 2')
plt.title('LDA')
plt.grid()
M. C
  • 1
  • 2
  • First of all, PCA and LDA are different methods, if you are not making a mistake on code, the results must be true and may be significantly different. Or let's use other methods of PCA such as PCAKernel. If you can, please, share a piece of code. – Musulmon Jul 13 '20 at 06:25
  • @Musulmon Thanks for the suggestion! Code was added – M. C Jul 13 '20 at 08:53
  • You should use the same number of `n_components`. In the code, you give `pca = PCA(n_components=2)` and `lda = LinearDiscriminantAnalysis(n_components=1,solver='svd')`. Let's change `lda = LinearDiscriminantAnalysis(n_components=1,solver='svd')` to `lda = LinearDiscriminantAnalysis(n_components=2,solver='svd')`. I hope it will work. – Musulmon Jul 13 '20 at 08:59
  • @Musulmon actually I used n_components=2 before, it is same.and solver also doesnt change anything... – M. C Jul 13 '20 at 09:51

0 Answers0