# Text%20Classification
course: Academy — 54-Next-Gen-AI-GenAI-Agents-Future-Trends
module: Academy/54-Next-Gen-AI-GenAI-Agents-Future-Trends
type: notebook
source_url: https://personal-learn.armco.dev/files/Academy/54-Next-Gen-AI-GenAI-Agents-Future-Trends/folders/Session-2-NLP_Folder/Text%20Classification.ipynb
---
[cell 2 markdown]
# Text Classification
[cell 3 markdown]
## Import libraries
[cell 4 code]
## Import Libraries
import numpy as np
import pandas as pd
import string
import spacy
from collections import Counter
from sklearn.decomposition import LatentDirichletAllocation
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import LinearSVC
# import warnings
# warnings.filterwarnings('ignore')
[cell 5 markdown]
## Load data
[cell 6 markdown]
Sample:
__label__2 Stuning even for the non-gamer: This sound ...
__label__2 The best soundtrack ever to anything.: I'm ...
__label__2 Amazing!: This soundtrack is my favorite m ...
__label__2 Excellent Soundtrack: I truly like this so ...
__label__2 Remember, Pull Your Jaw Off The Floor Afte ...
__label__2 an absolute masterpiece: I am quite sure a ...
__label__1 Buyer beware: This is a self-published boo ...
. . .
There are only two **labels**:
- `__label__1`
- `__label__2`
[cell 7 code]
## Loading the data
trainDF = pd.read_fwf(
filepath_or_buffer = '../DATA/DATA/corpus.txt',
colspecs = [(9, 10), # label: get only the numbers 1 or 2
(11, 9000) # text: makes the it big enough to get to the end of the line
],
header = 0,
names = ['label', 'text'],
lineterminator = '\n'
)
# convert label from [1, 2] to [0, 1]
trainDF['label'] = trainDF['label'] - 1
[cell 8 markdown]
## Inspect the data
[cell 9 code]
print(trainDF.info())
print(trainDF.sample(10))
[cell 10 markdown]
## Split the data into train and test
[cell 11 code]
## split the dataset
X_train, X_test, y_train, y_test = train_test_split(
trainDF['text'],
trainDF['label'],
test_size = 0.2,
random_state = 42
)
[cell 12 markdown]
## Feature Engineering
[cell 13 markdown]
### Count Vectors as features
[cell 14 code]
# create a count vectorizer object
count_vect = CountVectorizer(token_pattern = r'\w{1,}')
# Learn a vocabulary dictionary of all tokens in the raw documents
count_vect.fit(trainDF['text'])
# Transform documents to document-term matrix.
X_train_count = count_vect.transform(X_train)
X_test_count = count_vect.transform(X_test)
[cell 15 markdown]
### TF-IDF Vectors as features
- Word level
- N-Gram level
- Character level
[cell 16 code]
%%time
# word level tf-idf
tfidf_vect = TfidfVectorizer(analyzer = 'word',
token_pattern = r'\w{1,}',
max_features = 5000)
print(tfidf_vect)
tfidf_vect.fit(trainDF['text'])
X_train_tfidf = tfidf_vect.transform(X_train)
X_test_tfidf = tfidf_vect.transform(X_test)
[cell 17 code]
%%time
# ngram level tf-idf
tfidf_vect_ngram = TfidfVectorizer(analyzer = 'word',
token_pattern = r'\w{1,}',
ngram_range = (2, 3),
max_features = 5000)
print(tfidf_vect_ngram)
tfidf_vect_ngram.fit(trainDF['text'])
X_train_tfidf_ngram = tfidf_vect_ngram.transform(X_train)
X_test_tfidf_ngram = tfidf_vect_ngram.transform(X_test)
[cell 18 code]
%%time
# characters level tf-idf
tfidf_vect_ngram_chars = TfidfVectorizer(analyzer = 'char',
token_pattern = r'\w{1,}',
ngram_range = (2, 3),
max_features = 5000)
print(tfidf_vect_ngram_chars)
tfidf_vect_ngram_chars.fit(trainDF['text'])
X_train_tfidf_ngram_chars = tfidf_vect_ngram_chars.transform(X_train)
X_test_tfidf_ngram_chars = tfidf_vect_ngram_chars.transform(X_test)
[cell 19 markdown]
### Text / NLP based features
[cell 20 code]
%%time
trainDF['char_count'] = trainDF['text'].apply(len)
trainDF['word_count'] = trainDF['text'].apply(lambda x: len(x.split()))
trainDF['word_density'] = trainDF['char_count'] / (trainDF['word_count'] + 1)
trainDF['punctuation_count'] = trainDF['text'].apply(lambda x: len(''.join(_ for _ in x if _ in string.punctuation)))
trainDF['title_word_count'] = trainDF['text'].apply(lambda x: len([w for w in x.split() if w.istitle()]))
trainDF['uppercase_word_count'] = trainDF['text'].apply(lambda x: len([w for w in x.split() if w.isupper()]))
[cell 21 code]
trainDF.sample(5)
[cell 22 code]
## load spaCy
nlp = spacy.load("en_core_web_sm")
[cell 23 markdown]
Part of Speech in **SpaCy**
POS DESCRIPTION EXAMPLES
----- ------------------------- ---------------------------------------------
ADJ adjective big, old, green, incomprehensible, first
ADP adposition in, to, during
ADV adverb very, tomorrow, down, where, there
AUX auxiliary is, has (done), will (do), should (do)
CONJ conjunction and, or, but
CCONJ coordinating conjunction and, or, but
DET determiner a, an, the
INTJ interjection psst, ouch, bravo, hello
NOUN noun girl, cat, tree, air, beauty
NUM numeral 1, 2017, one, seventy-seven, IV, MMXIV
PART particle 's, not,
PRON pronoun I, you, he, she, myself, themselves, somebody
PROPN proper noun Mary, John, London, NATO, HBO
PUNCT punctuation ., (, ), ?
SCONJ subordinating conjunction if, while, that
SYM symbol $, %, §, ©, +, −, ×, ÷, =, :), 😝
VERB verb run, runs, running, eat, ate, eating
X other sfpksdpsxmsa
SPACE space
[cell 24 code]
# Initialise some columns for feature's counts
trainDF['adj_count'] = 0
trainDF['adv_count'] = 0
trainDF['noun_count'] = 0
trainDF['num_count'] = 0
trainDF['pron_count'] = 0
trainDF['propn_count'] = 0
trainDF['verb_count'] = 0
[cell 25 code]
%%time
# for each text
for i in range(trainDF.shape[0]):
# convert into a spaCy document
doc = nlp(trainDF.iloc[i]['text'])
# initialise feature counters
c = Counter([t.pos_ for t in doc])
trainDF.at[i, 'adj_count'] = c['ADJ']
trainDF.at[i, 'adv_count'] = c['ADV']
trainDF.at[i, 'noun_count'] = c['NOUN']
trainDF.at[i, 'num_count'] = c['NUM']
trainDF.at[i, 'pron_count'] = c['PRON']
trainDF.at[i, 'propn_count'] = c['PROPN']
trainDF.at[i, 'verb_count'] = c['VERB']
[cell 26 code]
cols = [
'char_count', 'word_count', 'word_density',
'punctuation_count', 'title_word_count',
'uppercase_word_count', 'adj_count',
'adv_count', 'noun_count', 'num_count',
'pron_count', 'propn_count', 'verb_count']
trainDF[cols].sample(5)
[cell 27 markdown]
### Topic Models as features
[cell 28 code]
%%time
# train a LDA Model
lda_model = LatentDirichletAllocation(n_components = 20, learning_method = 'online', max_iter = 20)
X_topics = lda_model.fit_transform(X_train_count)
topic_word = lda_model.components_
vocab = count_vect.get_feature_names_out()
[cell 29 code]
# view the topic models
n_top_words = 10
topic_summaries = []
print('Group Top Words')
print('-----', '-'*80)
for i, topic_dist in enumerate(topic_word):
topic_words = np.array(vocab)[np.argsort(topic_dist)][:-(n_top_words+1):-1]
top_words = ' '.join(topic_words)
topic_summaries.append(top_words)
print(' %3d %s' % (i, top_words))
[cell 30 markdown]
## Modeling
[cell 31 code]
## helper function
def train_model(classifier, feature_vector_train, label, feature_vector_valid):
# fit the training dataset on the classifier
classifier.fit(feature_vector_train, label)
# predict the labels on validation dataset
predictions = classifier.predict(feature_vector_valid)
return accuracy_score(predictions, y_test)
[cell 32 code]
# Keep the results in a dataframe
results = pd.DataFrame(columns = ['Count Vectors',
'WordLevel TF-IDF',
'N-Gram Vectors',
'CharLevel Vectors'])
[cell 33 markdown]
### Naive Bayes Classifier
[cell 34 code]
%%time
# Naive Bayes on Count Vectors
accuracy1 = train_model(MultinomialNB(), X_train_count, y_train, X_test_count)
print('NB, Count Vectors : %.4f\n' % accuracy1)
[cell 35 code]
%%time
# Naive Bayes on Word Level TF IDF Vectors
accuracy2 = train_model(MultinomialNB(), X_train_tfidf, y_train, X_test_tfidf)
print('NB, WordLevel TF-IDF : %.4f\n' % accuracy2)
[cell 36 code]
%%time
# Naive Bayes on Ngram Level TF IDF Vectors
accuracy3 = train_model(MultinomialNB(), X_train_tfidf_ngram, y_train, X_test_tfidf_ngram)
print('NB, N-Gram Vectors : %.4f\n' % accuracy3)
[cell 37 code]
%%time
# # Naive Bayes on Character Level TF IDF Vectors
accuracy4 = train_model(MultinomialNB(), X_train_tfidf_ngram_chars, y_train, X_test_tfidf_ngram_chars)
print('NB, CharLevel Vectors: %.4f\n' % accuracy4)
[cell 38 code]
results.loc['Naïve Bayes'] = {
'Count Vectors': accuracy1,
'WordLevel TF-IDF': accuracy2,
'N-Gram Vectors': accuracy3,
'CharLevel Vectors': accuracy4}
[cell 39 markdown]
### Linear Classifier
[cell 40 code]
%%time
# Linear Classifier on Count Vectors
accuracy1 = train_model(LogisticRegression(solver = 'lbfgs', max_iter = 350), X_train_count, y_train, X_test_count)
print('LR, Count Vectors : %.4f\n' % accuracy1)
[cell 41 code]
%%time
# Linear Classifier on Word Level TF IDF Vectors
accuracy2 = train_model(LogisticRegression(solver = 'lbfgs', max_iter = 100), X_train_tfidf, y_train, X_test_tfidf)
print('LR, WordLevel TF-IDF : %.4f\n' % accuracy2)
[cell 42 code]
%%time
# Linear Classifier on Ngram Level TF IDF Vectors
accuracy3 = train_model(LogisticRegression(solver = 'lbfgs', max_iter = 100), X_train_tfidf_ngram, y_train, X_test_tfidf_ngram)
print('LR, N-Gram Vectors : %.4f\n' % accuracy3)
[cell 43 code]
%%time
# Linear Classifier on Character Level TF IDF Vectors
accuracy4 = train_model(LogisticRegression(solver = 'lbfgs', max_iter = 100), X_train_tfidf_ngram_chars, y_train, X_test_tfidf_ngram_chars)
print('LR, CharLevel Vectors: %.4f\n' % accuracy4)
[cell 44 code]
results.loc['Logistic Regression'] = {
'Count Vectors': accuracy1,
'WordLevel TF-IDF': accuracy2,
'N-Gram Vectors': accuracy3,
'CharLevel Vectors': accuracy4}
[cell 45 markdown]
### Support Vector Machine
[cell 46 code]
%%time
# Support Vector Machine on Count Vectors
accuracy1 = train_model(LinearSVC(), X_train_count, y_train, X_test_count)
print('SVM, Count Vectors : %.4f\n' % accuracy1)
[cell 47 code]
%%time
# Support Vector Machine on Word Level TF IDF Vectors
accuracy2 = train_model(LinearSVC(), X_train_tfidf, y_train, X_test_tfidf)
print('SVM, WordLevel TF-IDF : %.4f\n' % accuracy2)
[cell 48 code]
%%time
# Support Vector Machine on Ngram Level TF IDF Vectors
accuracy3 = train_model(LinearSVC(), X_train_tfidf_ngram, y_train, X_test_tfidf_ngram)
print('SVM, N-Gram Vectors : %.4f\n' % accuracy3)
[cell 49 code]
%%time
# Support Vector Machine on Character Level TF IDF Vectors
accuracy4 = train_model(LinearSVC(), X_train_tfidf_ngram_chars, y_train, X_test_tfidf_ngram_chars)
print('SVM, CharLevel Vectors: %.4f\n' % accuracy4)
[cell 50 code]
results.loc['Support Vector Machine'] = {
'Count Vectors': accuracy1,
'WordLevel TF-IDF': accuracy2,
'N-Gram Vectors': accuracy3,
'CharLevel Vectors': accuracy4}
[cell 51 code]
results