1
from sklearn.preprocessing import LabelEncoder
class_le = LabelEncoder()
y = class_le.fit_transform(data['10'].values)

from sklearn.preprocessing import StandardScaler
stdsc = StandardScaler()
X_train_std = stdsc.fit_transform(data.iloc[:,range(int(0),int(10))].values) 

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
lda = LinearDiscriminantAnalysis(n_components=2)
X_train_lda = lda.fit_transform(X_train_std, y)

this is the error even if am changing the dataset it is showing same error. My dataset contains 9 attributes+class. If i give n_components=1 it is taking if i give any other number it is showing error.

ValueError                                Traceback (most recent call last)
<ipython-input-8-b53ad0e7c804> in <module>()
      1 from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
      2 lda = LinearDiscriminantAnalysis(n_components=2)
----> 3 X_train_lda = lda.fit_transform(X_train_std, y)

1 frames
/usr/local/lib/python3.7/dist-packages/sklearn/discriminant_analysis.py in fit(self, X, y)
    575             if self.n_components > max_components:
    576                 raise ValueError(
--> 577                     "n_components cannot be larger than min(n_features, n_classes - 1"
    578                 )
    579             self._max_components = self.n_components

ValueError: n_components cannot be larger than min(n_features, n_classes - 1).
Progman
  • 16,827
  • 6
  • 33
  • 48

1 Answers1

2

From your error n_components cannot be larger than min(n_features, n_classes - 1), it is most likely your labels contain only two classes, so the maximum number of components can only be 2-1 = 1. My guess is that you might have mistaken it for a dimension reduction method, like this post.

You can check this post for a more theoretical explanation, and also a detailed explanation of the steps in this blog post by sebastianraschka:

In LDA, the number of linear discriminants is at most c−1 where c is the number of class labels, since the in-between scatter matrix SB is the sum of c matrices with rank 1 or less.

StupidWolf
  • 45,075
  • 17
  • 40
  • 72