Elbow Method - Finding the number of components required to preserve maximum variance.

My code:

pca = decomposition.PCA()

vectorizer = TfidfVectorizer(min_df=10)
preprocessed_essay_tfidf = vectorizer.fit_transform(preprocessed_essay)
pca_data = preprocessed_essay_tfidf
pca.n_components = 3000

percentage_var_explained = pca.explained_variance_ / np.sum(pca.explained_variance_);
cum_var_explained = np.cumsum(percentage_var_explained)

I get the attribute error:

AttributeError: 'PCA' object has no attribute 'explained_variance_

Why is this?

I can find explained_variance_ present here.


1 Answer 1


PCA is an estimator and by that you need to call the fit() method in order to calculate the principal components and all the statistics related to them, such as the variances of the projections en hence the explained_variance_ratio.
pca.fit(preprocessed_essay_tfidf) or pca.fit_transform(preprocessed_essay_tfidf)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.