Reputation: 81
I've got imported the Dataset from this URL in the pandas Dataframe called df: https://www.kaggle.com/jakeshbohaju/brain-tumor?select=Brain+Tumor.csv
But, while running a linear discriminant analysis, I always get the error on the bottom.
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
X = df.drop(['label','Image'], axis=1)
y = df[['label']]
lda = LinearDiscriminantAnalysis(n_components=2)
X_r2 = lda.fit(X, y).transform(X)
Error:
ValueError Traceback (most recent call last) <ipython-input-41-f7a0f19db224> in <module>
25 lda = LinearDiscriminantAnalysis(n_components=2)
26 lda2 = LinearDiscriminantAnalysis(n_components=2)
---> 27 X_r3 = lda2.fit(X_train,y_train.values.ravel()).transform(X_train)
28 X_r2 = lda.fit(X, y).transform(X)
29
~/miniforge3/envs/pyM1/lib/python3.8/site-packages/sklearn/discriminant_analysis.py in fit(self, X, y)
537 else:
538 if self.n_components > max_components:
--> 539 raise ValueError(
540 "n_components cannot be larger than min(n_features, "
541 "n_classes - 1)."
ValueError: n_components cannot be larger than min(n_features, n_classes - 1).
Upvotes: 3
Views: 1172
Reputation: 7353
lda.fit(X, y)
does not return anything and hence you cannot call the method called .transform()
on it. The API will allow you to call a method only if it is already defined. Please see the documentation.
Change it to this. I would also encourage you to spend more time on the documentation.
lda = LinearDiscriminantAnalysis(n_components=2)
# either use: lda.fit_transform(X, y)
X_r2 = lda.fit_transform(X, y)
## PREFERRED WAY
# or, use: lda.fit(X, y)
# followed by lda.transform(X)
lda.fit(X, y)
X_r2 = lda.transform(X)
Upvotes: 1