Search code examples
countvectorizer

CountVectorizer() : AttributeError: 'numpy.float64' object has no attribute 'lower'


I am trying to fit a dataset which has event_type and notes (free text) columns. before calling the MultinomialNB model , I processed the text and converted it to array to vectorize it and calculate the tfidf here below the code provided:

Convert Event types from string to integer for easy processing

ACLED['category_id'] = ACLED['event_type'].factorize()[0]
category_id_ACLED = ACLED[['event_type', 'category_id']].drop_d

uplicates().sort_values('category_id')
category_to_id = dict(category_id_ACLED.values)
id_to_category = dict(category_id_ACLED[['category_id', 'event_type']].values)

Text Representation

I also converted notes and category_id into features and labels as follows:

tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='latin-1', ngram_range=(1, 2), stop_words='english')
features = tfidf.fit_transform(ACLED.notes).toarray()
labels = ACLED.category_id
print(features.shape)

Then I split the dataset to training and testing sets using features and labels :

X_train, X_test, y_train, y_test = train_test_split(features ,labels, random_state=0)
print('Original dataset shape {}'.format(Counter(y_train)))

output

Original dataset shape Counter({1: 1280, 2: 819, 0: 676, 3: 593, 4: 138, 5: 53, 7: 50, 6: 21, 8: 10})

Since the classes are imbalanced , I used SMOTE to resolve the minority issue and creating synthethic copies

Apply the random over-sampling to overcome imbalanced classes

sm = SMOTE(random_state=42)
X_resampled, y_resampled = sm.fit_sample(X_train, y_train)
print('Resampled dataset shape {}'.format(Counter(y_resampled)))

output after oversampling

Resampled dataset shape Counter({3: 1280, 1: 1280, 2: 1280, 0: 1280, 7: 1280, 6: 1280, 4: 1280, 5: 1280, 8: 1280})

Everything until now is working fine , until I tried to calculate the terms frequency using CountVectorizer() as follows :

count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(X_resampled)
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)

Output error :

'numpy.ndarray' object has no attribute 'lower'

I tried to use ravel() function to flatten the array but the error persists, any ideas, thanks in advance


Solution

  • I found the solution for this issue, instead of using features and labels I performed a subset on the dataset directly :

    X_train, X_test, y_train, y_test = train_test_split(ACLED['notes'] ,ACLED['event_type'], random_state=0)
    

    then I moved SMOTE after the counVectorizer since SMOTE has it is own pipeline :

    Vactorize the notes column of the training set

    count_vect = CountVectorizer()
    X_train_counts = count_vect.fit_transform(X_train)
    tfidf_transformer = TfidfTransformer()
    X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
    

    Apply the random over-sampling to overcome imbalanced classes

    sm = SMOTE(random_state=42)
    X_resampled, y_resampled = sm.fit_sample(X_train_tfidf, y_train)
    print('Resampled dataset shape {}'.format(Counter(y_resampled)))
    

    output

    Original dataset shape Counter({'Riots/Protests': 1280, 'Battle-No change of territory': 819, 'Remote violence': 676, 'Violence against civilians': 593, 'Strategic development': 138, 'Battle-Government regains territory': 53, 'Battle-Non-state actor overtakes territory': 50, 'Non-violent transfer of territory': 21, 'Headquarters or base established': 10})
    Resampled dataset shape Counter({'Violence against civilians': 1280, 'Riots/Protests': 1280, 'Battle-No change of territory': 1280, 'Remote violence': 1280, 'Battle-Non-state actor overtakes territory': 1280, 'Non-violent transfer of territory': 1280, 'Strategic development': 1280, 'Battle-Government regains territory': 1280, 'Headquarters or base established': 1280})