AttributeError: 'CountVectorizer' object has no attribute 'get_feature_names'


The code was working before without showing any errors.
It’s for a sentimental analysis machine learning project. The code is on logistic regression model for word count:

c = CountVectorizer(stop_words = 'english')

def text_fit(X, y, model,clf_model,coef_show=1):
    X_c = model.fit_transform(X)
    print('# features: {}'.format(X_c.shape[1]))
    X_train, X_test, y_train, y_test = train_test_split(X_c, y, random_state=0)
    print('# train records: {}'.format(X_train.shape[0]))
    print('# test records: {}'.format(X_test.shape[0]))
    clf =, y_train)
    acc = clf.score(X_test, y_test)
    print ('Model Accuracy: {}'.format(acc))
    if coef_show == 1: 
        w = model.get_feature_names()
        coef = clf.coef_.tolist()[0]
        coeff_df = pd.DataFrame({'Word' : w, 'Coefficient' : coef})
        coeff_df = coeff_df.sort_values(['Coefficient', 'Word'], ascending=[0, 1])
        print('-Top 20 positive-')
        print('-Top 20 negative-')        
text_fit(X, y, c, LogisticRegression())

I deleted the project and made new one and the code was working. But after few days it again started showing the same error.

Asked By: DeValor12



According to the documentation, the method is called get_feature_names_out. Try changing the problem line to:

w = model.get_feature_names_out()
Answered By: jprebys