<jupyter_start><jupyter_text>Emotion Detection from Text Loading the Dataset<jupyter_code>from mpl_toolkits.mplot3d import Axes3D
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt # plotting
from wordcloud import WordCloud, STOPWORDS
from collections import defaultdict
import numpy as np # linear algebra
import os # accessing directory structure
import pandas as pd # data processing
from sklearn.metrics import accuracy_score
from xgboost import XGBClassifier
from sklearn.svm import LinearSVC
from sklearn.ensemble import AdaBoostClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import RidgeClassifier
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.linear_model import Perceptron
from sklearn.neighbors import NearestCentroid
from sklearn.neighbors import KNeighborsClassifier
from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.pipeline import Pipeline
from time import time
from sklearn.metrics import accuracy_score
from sklearn import preprocessing
import inflect
import contractions
from bs4 import BeautifulSoup
import re, string, unicodedata
import nltk
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
from nltk import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from nltk.stem import LancasterStemmer, WordNetLemmatizer
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
data = pd.read_csv('tweet_emotions.csv')
data.head()
print(data.describe(include='all'))   #describe
data.isnull().sum()                   #mising values<jupyter_output>tweet_id sentiment  \
count   4.000000e+04     40000   
unique           NaN        13   
top              NaN   neutral   
freq             NaN      8638   
mean    1.845184e+09       NaN   
std     1.188579e+08       NaN   
min     1.693956e+09       NaN   
25%     1.751431e+09       NaN   
50%     1.855443e+09       NaN   
75%     1.962781e+09       NaN   
max     1.966441e+09       NaN   

                                                  content  
count                                               40000  
unique                                              39827  
top     I just received a mothers day card from my lov...  
freq                                                   14  
mean                                                  NaN  
std                                                   NaN  
min                                                   NaN  
25%                                                   NaN  
50%                                                [...]<jupyter_text>Pre-processing Tweets<jupyter_code>def text_preprocessing_platform(df, text_col, remove_stopwords=True):
    
    ## Define functions for individual steps
    # First function is used to denoise text
    def denoise_text(text):
        # Strip html if any. For ex. removing <html>, <p> tags
        soup = BeautifulSoup(text, "html.parser")
        text = soup.get_text()
        # Replace contractions in the text. For ex. didn't -> did not
        text = contractions.fix(text)
        return text
  
    def remove_non_ascii(words):
        """Remove non-ASCII characters from list of tokenized words"""
        new_words = []
        for word in words:
            new_word = unicodedata.normalize('NFKD', word).encode('ascii', 'ignore').decode('utf-8', 'ignore')
            new_words.append(new_word)
        return new_words
    
    
    def to_lowercase(words):
        """Convert all characters to lowercase from list of tokenized words"""
        new_words = []
        for word in words:
            new_word = word.lower()
            new_words.append(new_word)
        return new_words
    
    
    def remove_punctuation(words):
        """Remove punctuation from list of tokenized words"""
        new_words = []
        for word in words:
            new_word = re.sub(r'[^\w\s]', '', word)
            if new_word != '':
                new_words.append(new_word)
        return new_words    

    
    def replace_numbers(words):
        """Replace all interger occurrences in list of tokenized words with textual representation"""
        p = inflect.engine()
        new_words = []
        for word in words:
            if word.isdigit():
                new_word = p.number_to_words(word)
                new_words.append(new_word)
            else:
                new_words.append(word)
        return new_words
    
    
    def remove_stopwords(words):
        """Remove stop words from list of tokenized words"""
        new_words = []
        for word in words:
            if word not in stopwords.words('english'):
                new_words.append(word)
        return new_words
    
    
    def stem_words(words):
        """Stem words in list of tokenized words"""
        stemmer = LancasterStemmer()
        stems = []
        for word in words:
            stem = stemmer.stem(word)
            stems.append(stem)
        return stems
    
    
    def lemmatize_verbs(words):
        """Lemmatize verbs in list of tokenized words"""
        lemmatizer = WordNetLemmatizer()
        lemmas = []
        for word in words:
            lemma = lemmatizer.lemmatize(word, pos='v')
            lemmas.append(lemma)
        return lemmas
    
    
    ### A wrap-up function for normalization
    def normalize_text(words, remove_stopwords):
        words = remove_non_ascii(words)
        words = to_lowercase(words)
        words = remove_punctuation(words)
        words = replace_numbers(words)
        if remove_stopwords:
            words = remove_stopwords(words)
        #words = stem_words(words)
        words = lemmatize_verbs(words)
        return words
    
    # All above functions work on word tokens we need a tokenizer
    
    # Tokenize tweet into words
    def tokenize(text):
        return nltk.word_tokenize(text)
    
    
    # A overall wrap-up function
    def text_prepare(text):
        text = denoise_text(text)
        text = ' '.join([x for x in normalize_text(tokenize(text), remove_stopwords)])
        return text
    
    # run every-step
    df[text_col] = [text_prepare(x) for x in df[text_col]]
    
    
    # return processed df
    return df


processed_data = text_preprocessing_platform(data, 'content', remove_stopwords=False)
print("After Text Preprocessing")
display(processed_data.head()[['content']])<jupyter_output>[nltk_data] Downloading package punkt to
[nltk_data]     C:\Users\Ebru\AppData\Roaming\nltk_data...
[nltk_data]   Package punkt is already up-to-date!
[nltk_data] Downloading package stopwords to
[nltk_data]     C:\Users\Ebru\AppData\Roaming\nltk_data...
[nltk_data]   Package stopwords is already up-to-date!
[nltk_data] Downloading package wordnet to
[nltk_data]     C:\Users\Ebru\AppData\Roaming\nltk_data...
[nltk_data]   Package wordnet is already up-to-date!<jupyter_text>Data Visualization<jupyter_code>import plotly.graph_objects as go
col = 'sentiment'
explode = list((np.array(list(processed_data[col].dropna().value_counts()))/sum(list(processed_data[col].dropna().value_counts())))[::-1])[:10]
labels = list(processed_data[col].dropna().unique())[:10]
sizes = processed_data[col].value_counts()[:10]
fig = go.Figure(data=[go.Pie(labels=labels, values=sizes, textinfo='label+percent',
                             insidetextorientation='radial', pull=[0,0,0,0,0,0,0,0.1,0.2,0.3]
                            )])
fig.update_layout(title_text='Percentage of each emotion',title_x=0.45 ,title_y=0.9)
fig.show()

#########################################################################################################################

processed_data['char_length'] = processed_data['content'].apply(lambda x : len(x))
processed_data['word_length'] = processed_data['content'].apply(lambda x : len(x.split(" ")))
processed_data['sentence_length'] = processed_data["content"].apply(lambda x: len(str(x).split(".")))
processed_data['avg_word_length'] = processed_data['char_length'] / processed_data['word_length']
processed_data['avg_sentence_lenght'] = processed_data['word_length'] / processed_data['sentence_length']
processed_data.head()

x = "word_length"
y = "sentiment"
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(12,6))
fig.suptitle('Number of characters in the tweet for each sentiment', fontsize=12)
for i in processed_data[y].unique():
    sns.distplot(processed_data[processed_data[y]==i][x], hist=True, kde=False, 
                 bins=10, hist_kws={"alpha":0.8}, 
                 axlabel="histogram", ax=ax[0])
    sns.distplot(processed_data[processed_data[y]==i][x], hist=False, kde=True, 
                 kde_kws={"shade":True}, axlabel="density",   
                 ax=ax[1])
ax[0].grid(True)
ax[0].legend(processed_data[y].unique())
ax[1].grid(True)
plt.show()

#########################################################################################################################

fig, (ax1, ax2)  = plt.subplots(nrows=1, ncols=2, figsize=(12,6))
sns.distplot(processed_data['char_length'], ax=ax1, color='red')
sns.distplot(processed_data['word_length'], ax=ax2, color='red')
ax1.set_title('Number of characters in the tweet')
ax2.set_title('Number of words in the tweet')
plt.show()

#########################################################################################################################

corpus = processed_data[processed_data["sentiment"]=="worry"]["content"]
lst_tokens = nltk.tokenize.word_tokenize(corpus.str.cat(sep=" "))
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(14,6))
fig.suptitle("Most frequent words for anger 😡 ", fontsize=15)
 
    
## unigrams
dic_words_freq = nltk.FreqDist(lst_tokens)
dtf_uni = pd.DataFrame(dic_words_freq.most_common(), 
                       columns=["Word","Freq"])
dtf_uni.set_index("Word").iloc[:6,:].sort_values(by="Freq").plot(
                  kind="barh", title="Unigrams", ax=ax[0], 
                  legend=False).grid(axis='x')
ax[0].set(ylabel=None)
    
## bigrams
dic_words_freq = nltk.FreqDist(nltk.ngrams(lst_tokens, 2))
dtf_bi = pd.DataFrame(dic_words_freq.most_common(), 
                      columns=["Word","Freq"])
dtf_bi["Word"] = dtf_bi["Word"].apply(lambda x: " ".join(
                   string for string in x) )
dtf_bi.set_index("Word").iloc[:6,:].sort_values(by="Freq").plot(
                  kind="barh", title="Bigrams", ax=ax[1],
                  legend=False).grid(axis='x')
ax[1].set(ylabel=None)
plt.show()

#########################################################################################################################

corpus = processed_data[processed_data["sentiment"]=="happiness"]["content"]
lst_tokens = nltk.tokenize.word_tokenize(corpus.str.cat(sep=" "))
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(14,6))
fig.suptitle("Most frequent words for happiness 😄 ", fontsize=15)   
## unigrams
dic_words_freq = nltk.FreqDist(lst_tokens)
dtf_uni = pd.DataFrame(dic_words_freq.most_common(), 
                       columns=["Word","Freq"])
dtf_uni.set_index("Word").iloc[:6,:].sort_values(by="Freq").plot(
                  kind="barh", title="Unigrams", ax=ax[0], 
                  legend=False).grid(axis='x')
ax[0].set(ylabel=None)
## bigrams
dic_words_freq = nltk.FreqDist(nltk.ngrams(lst_tokens, 2))
dtf_bi = pd.DataFrame(dic_words_freq.most_common(), 
                      columns=["Word","Freq"])
dtf_bi["Word"] = dtf_bi["Word"].apply(lambda x: " ".join(
                   string for string in x) )
dtf_bi.set_index("Word").iloc[:6,:].sort_values(by="Freq").plot(
                  kind="barh", title="Bigrams", ax=ax[1],
                  legend=False).grid(axis='x')
ax[1].set(ylabel=None)
plt.show()<jupyter_output><empty_output><jupyter_text>Count Vectorizer vs TFIDF Vectorizer vs Bag of Words<jupyter_code>#Train-Test Split
X = processed_data.content
y = processed_data.sentiment
from sklearn.model_selection import train_test_split # data split
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=.05, random_state=1)

def accuracy_summary(pipeline, x_train, y_train, x_test, y_test):
    t0 = time()
    sentiment_fit = pipeline.fit(x_train, y_train)
    y_pred = sentiment_fit.predict(x_test)
    train_test_time = time() - t0
    accuracy = accuracy_score(y_test, y_pred)
    print ("accuracy score: {0:.2f}%".format(accuracy*100))
    print ("train and test time: {0:.2f}s".format(train_test_time))
    return accuracy, train_test_time

cvec = CountVectorizer()
lr = LogisticRegression()
n_features = np.arange(9000,15000,1000)

def nfeature_accuracy_checker(vectorizer=cvec, n_features=n_features, stop_words=None, ngram_range=(1, 1), classifier=lr):
    result = []
    print (classifier)
    print ("\n")
    for n in n_features:
        vectorizer.set_params(stop_words=stop_words, max_features=n, ngram_range=ngram_range)
        checker_pipeline = Pipeline([
            ('vectorizer', vectorizer),
            ('classifier', classifier)
        ])
        print ("Validation result for {} features".format(n))
        nfeature_accuracy,tt_time = accuracy_summary(checker_pipeline, x_train, y_train, x_test, y_test)
        result.append((n,nfeature_accuracy,tt_time))
    return result


from sklearn.feature_extraction.text import TfidfVectorizer
tvec = TfidfVectorizer()


feature_result_ug = nfeature_accuracy_checker()
feature_result_bg = nfeature_accuracy_checker(ngram_range=(1, 2))
feature_result_tg = nfeature_accuracy_checker(ngram_range=(1, 3))
feature_result_ugt = nfeature_accuracy_checker(vectorizer=tvec)
feature_result_bgt = nfeature_accuracy_checker(vectorizer=tvec,ngram_range=(1, 2))
feature_result_tgt = nfeature_accuracy_checker(vectorizer=tvec,ngram_range=(1, 3))


nfeatures_plot_tg = pd.DataFrame(feature_result_tg,columns=['nfeatures','validation_accuracy','train_test_time'])
nfeatures_plot_bg = pd.DataFrame(feature_result_bg,columns=['nfeatures','validation_accuracy','train_test_time'])
nfeatures_plot_ug = pd.DataFrame(feature_result_ug,columns=['nfeatures','validation_accuracy','train_test_time'])
nfeatures_plot_tgt = pd.DataFrame(feature_result_tgt,columns=['nfeatures','validation_accuracy','train_test_time'])
nfeatures_plot_bgt = pd.DataFrame(feature_result_bgt,columns=['nfeatures','validation_accuracy','train_test_time'])
nfeatures_plot_ugt = pd.DataFrame(feature_result_ugt,columns=['nfeatures','validation_accuracy','train_test_time'])

plt.figure(figsize=(8,6))
plt.plot(nfeatures_plot_tgt.nfeatures, nfeatures_plot_tgt.validation_accuracy,label='trigram tfidf vectorizer',color='royalblue')
plt.plot(nfeatures_plot_tg.nfeatures, nfeatures_plot_tg.validation_accuracy,label='trigram count vectorizer',linestyle=':', color='royalblue')
plt.plot(nfeatures_plot_bgt.nfeatures, nfeatures_plot_bgt.validation_accuracy,label='bigram tfidf vectorizer',color='orangered')
plt.plot(nfeatures_plot_bg.nfeatures, nfeatures_plot_bg.validation_accuracy,label='bigram count vectorizer',linestyle=':',color='orangered')
plt.plot(nfeatures_plot_ugt.nfeatures, nfeatures_plot_ugt.validation_accuracy, label='unigram tfidf vectorizer',color='gold')
plt.plot(nfeatures_plot_ug.nfeatures, nfeatures_plot_ug.validation_accuracy, label='unigram count vectorizer',linestyle=':',color='gold')
plt.title("N-gram(1~3) test result : Accuracy")
plt.xlabel("Number of features")
plt.ylabel("Validation set accuracy")
plt.legend()


# From below chart, we can see including bigram and trigram boost the model performance both in count vectorizer and TFIDF 
# vectorizer. And for every case of unigram to trigram, TFIDF yields better results than count vectorizer.<jupyter_output>LogisticRegression()


Validation result for 9000 features
accuracy score: 33.15%
train and test time: 70.48s
Validation result for 10000 features
accuracy score: 33.20%
train and test time: 41.76s
Validation result for 11000 features
accuracy score: 33.25%
train and test time: 48.35s
Validation result for 12000 features
accuracy score: 33.65%
train and test time: 92.45s
Validation result for 13000 features
accuracy score: 33.55%
train and test time: 92.57s
Validation result for 14000 features
accuracy score: 33.40%
train and test time: 72.16s
LogisticRegression()


Validation result for 9000 features
accuracy score: 31.55%
train and test time: 112.36s
Validation result for 10000 features
accuracy score: 31.35%
train and test time: 17.00s
Validation result for 11000 features
accuracy score: 31.20%
train and test time: 54.96s
Validation result for 12000 features
accuracy score: 31.15%
train and test time: 40.57s
Validation result for 13000 features
accuracy score: 31.40%
train and test [...]<jupyter_text>Bag of Words<jupyter_code>#DBOW (Distributed Bag of Words)
from sklearn.linear_model import LogisticRegression
from tqdm import tqdm
tqdm.pandas(desc="progress-bar")
from gensim.models import Doc2Vec
from gensim.models.doc2vec import TaggedDocument
import multiprocessing
from sklearn import utils

def labelize_tweets_ug(tweets,label):
    result = []
    prefix = label
    for i, t in zip(tweets.index, tweets):
        result.append(TaggedDocument(t.split(), [prefix + '_%s' % i]))
    return result
  
all_x = pd.concat([x_train,x_test])
all_x_w2v = labelize_tweets_ug(all_x, 'all')


cores = multiprocessing.cpu_count()
model_ug_dbow = Doc2Vec(dm=0, min_count=2, workers=cores, alpha=0.065, min_alpha=0.065)
model_ug_dbow.build_vocab([x for x in tqdm(all_x_w2v)])

for epoch in range(30):
    model_ug_dbow.train(utils.shuffle([x for x in tqdm(all_x_w2v)]), total_examples=len(all_x_w2v), epochs=1)
    model_ug_dbow.alpha -= 0.002
    model_ug_dbow.min_alpha = model_ug_dbow.alpha
    
def get_vectors(model, corpus, size):
    vecs = np.zeros((len(corpus), size))
    n = 0
    for i in corpus.index:
        prefix = 'all_' + str(i)
        vecs[n] = model.docvecs[prefix]
        n += 1
    return vecs
  
train_vecs_dbow = get_vectors(model_ug_dbow, x_train, 100)
validation_vecs_dbow = get_vectors(model_ug_dbow, x_test, 100)

clf = LogisticRegression()
clf.fit(train_vecs_dbow, y_train)
clf.score(validation_vecs_dbow, y_test)  #The accuracy tested on the test set with logistic regression is 32.4%. 
#DMC (Distributed Memory Concatenated)
cores = multiprocessing.cpu_count()
model_ug_dmc = Doc2Vec(dm=1, dm_concat=1, window=2, negative=5, min_count=2, workers=cores, alpha=0.065, min_alpha=0.065)
model_ug_dmc.build_vocab([x for x in tqdm(all_x_w2v)])

for epoch in range(30):
    model_ug_dmc.train(utils.shuffle([x for x in tqdm(all_x_w2v)]), total_examples=len(all_x_w2v), epochs=1)
    model_ug_dmc.alpha -= 0.002
    model_ug_dmc.min_alpha = model_ug_dmc.alpha
   
train_vecs_dmc = get_vectors(model_ug_dmc, x_train, 100)
validation_vecs_dmc = get_vectors(model_ug_dmc, x_test, 100)

clf = LogisticRegression()
clf.fit(train_vecs_dmc, y_train)
clf.score(validation_vecs_dmc, y_test)    #The accuracy tested on the test set with logistic regression is 27.25%. 
#DMM (Distributed Memory Mean)
cores = multiprocessing.cpu_count()
model_ug_dmm = Doc2Vec(dm=1, dm_mean=1, window=4, negative=5, min_count=2, workers=cores, alpha=0.065, min_alpha=0.065)
model_ug_dmm.build_vocab([x for x in tqdm(all_x_w2v)])

for epoch in range(30):
    model_ug_dmm.train(utils.shuffle([x for x in tqdm(all_x_w2v)]), total_examples=len(all_x_w2v), epochs=1)
    model_ug_dmm.alpha -= 0.002
    model_ug_dmm.min_alpha = model_ug_dmm.alpha
    
train_vecs_dmm = get_vectors(model_ug_dmm, x_train, 100)
validation_vecs_dmm = get_vectors(model_ug_dmm, x_test, 100)

clf = LogisticRegression()
clf.fit(train_vecs_dmm, y_train)
clf.score(validation_vecs_dmm, y_test)  #The accuracy tested on the test set with logistic regression is 31.6%.<jupyter_output>100%|██████████| 40000/40000 [00:00<00:00, 1822956.55it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1604262.42it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1822976.36it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1909800.56it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1823075.40it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1909931.01it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1823035.78it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1909909.27it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1909887.53it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1822936.74it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1823075.40it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1823035.78it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1909887.53it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1822956.55it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1823115.02it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1743775.83it/s]
100%|██████████| 40000/40000 [00:00<00:00, 1823015.97it/[...]<jupyter_text>Handling Imbalanced Data Original Imbalanced Data<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import precision_score, recall_score, f1_score

tvec = TfidfVectorizer(stop_words=None, max_features=100000, ngram_range=(1, 3))
lr = LogisticRegression()


def lr_cv(splits, X, Y, pipeline, average_method):
    
    kfold = StratifiedKFold(n_splits=splits, shuffle=True, random_state=5)
    accuracy = []
    precision = []
    recall = []
    f1 = []
    for train, test in kfold.split(X, Y):
        lr_fit = pipeline.fit(X[train], Y[train])
        prediction = lr_fit.predict(X[test])
        scores = lr_fit.score(X[test],Y[test])
        
        accuracy.append(scores * 100)
        precision.append(precision_score(Y[test], prediction, average=average_method)*100)
        print('precision:',precision_score(Y[test], prediction, average=None))
        recall.append(recall_score(Y[test], prediction, average=average_method)*100)
        print('recall:   ',recall_score(Y[test], prediction, average=None))
        f1.append(f1_score(Y[test], prediction, average=average_method)*100)
        print('f1 score: ',f1_score(Y[test], prediction, average=None))
        print('-'*50)

    print("accuracy: %.2f%% (+/- %.2f%%)" % (np.mean(accuracy), np.std(accuracy)))
    print("precision: %.2f%% (+/- %.2f%%)" % (np.mean(precision), np.std(precision)))
    print("recall: %.2f%% (+/- %.2f%%)" % (np.mean(recall), np.std(recall)))
    print("f1 score: %.2f%% (+/- %.2f%%)" % (np.mean(f1), np.std(f1)))
    
from sklearn.pipeline import Pipeline
original_pipeline = Pipeline([
    ('vectorizer', tvec),
    ('classifier', lr)
])
lr_cv(5, processed_data.content, processed_data.sentiment, original_pipeline, 'macro')<jupyter_output>precision: [0.         0.         0.         0.         0.08       0.33470734
 0.47692308 0.48592593 0.34720062 0.3        0.34699104 0.33333333
 0.32649109]
recall:    [0.         0.         0.         0.         0.0056338  0.39000961
 0.11698113 0.42652796 0.51708164 0.0295082  0.26234269 0.04347826
 0.49822695]
f1 score:  [0.         0.         0.         0.         0.01052632 0.36024845
 0.18787879 0.45429363 0.41544545 0.05373134 0.29878721 0.07692308
 0.39447824]
--------------------------------------------------
precision: [0.         0.         0.33333333 0.         0.19354839 0.34112554
 0.5        0.47761194 0.34376222 0.4        0.325      0.46666667
 0.32493318]
recall:    [0.         0.         0.0060241  0.         0.01690141 0.378119
 0.15151515 0.41612484 0.50868056 0.05245902 0.25169409 0.04805492
 0.50295508]
f1 score:  [0.         0.         0.01183432 0.         0.03108808 0.35867091
 0.23255814 0.4447533  0.41026838 0.09275362 0.28368794 0.08713693
 0.39480399]
---[...]<jupyter_text>Oversampling<jupyter_code>#RandomOverSampler
from imblearn.pipeline import make_pipeline
from imblearn.over_sampling import ADASYN, SMOTE, RandomOverSampler
ROS_pipeline = make_pipeline(tvec, RandomOverSampler(random_state=5),lr)
SMOTE_pipeline = make_pipeline(tvec, SMOTE(random_state=5),lr)
tv = TfidfVectorizer(stop_words=None, max_features=10000,analyzer='word', token_pattern=r'\w{1,}')
testing_tfidf = tv.fit_transform(processed_data.content)
ros = RandomOverSampler(random_state=5)
X_ROS, y_ROS = ros.fit_resample(testing_tfidf, processed_data.sentiment)
lr_cv(5, processed_data.content, processed_data.sentiment, ROS_pipeline, 'macro')                 # best accuracy
#SMOTE (Synthetic Minority Over-Sampling Technique)
smt = SMOTE(random_state=5, k_neighbors=1)
X_SMOTE, y_SMOTE = smt.fit_resample(testing_tfidf, processed_data.sentiment)
lr_cv(5, processed_data.content, processed_data.sentiment, SMOTE_pipeline, 'macro')       # second best<jupyter_output>precision: [0.03571429 0.05769231 0.05586592 0.0326087  0.11753372 0.33643772
 0.19036145 0.42391304 0.34525547 0.10688836 0.34980989 0.1557377
 0.35311227]
recall:    [0.04545455 0.08333333 0.12048193 0.05921053 0.17183099 0.27761768
 0.29811321 0.45643693 0.27388535 0.14754098 0.26718296 0.13043478
 0.35874704]
f1 score:  [0.04       0.06818182 0.07633588 0.04205607 0.1395881  0.30421053
 0.23235294 0.4395742  0.30545689 0.12396694 0.30296378 0.14196762
 0.35590736]
--------------------------------------------------
precision: [0.         0.02       0.06497175 0.04885993 0.12522361 0.34246575
 0.23096447 0.44413793 0.30938416 0.11736334 0.34275184 0.17365269
 0.35423007]
recall:    [0.         0.02777778 0.13855422 0.09933775 0.1971831  0.26391555
 0.34469697 0.41872562 0.24421296 0.23934426 0.27008712 0.13272311
 0.34397163]
f1 score:  [0.         0.02325581 0.08846154 0.06550218 0.15317287 0.29810298
 0.27659574 0.43105756 0.27296248 0.1574973  0.30211153 0.15045396
 0.34902549]
--[...]<jupyter_text>Undersampling<jupyter_code>#RandomUnderSampler
from imblearn.under_sampling import NearMiss, RandomUnderSampler
RUS_pipeline = make_pipeline(tvec, RandomUnderSampler(random_state=5),lr)
rus = RandomUnderSampler(random_state=5)
X_RUS, y_RUS = rus.fit_resample(testing_tfidf, processed_data.sentiment)
lr_cv(5, processed_data.content, processed_data.sentiment, RUS_pipeline, 'macro')
#NearMiss 
NM1_pipeline = make_pipeline(tvec, NearMiss(sampling_strategy='not minority', version = 1),lr)
NM2_pipeline = make_pipeline(tvec, NearMiss(sampling_strategy='not minority', version = 2),lr)
NM3_pipeline = make_pipeline(tvec, NearMiss(sampling_strategy='not minority', version = 3, n_neighbors_ver3=4),lr)
nm = NearMiss(sampling_strategy='not minority', version=1, n_neighbors=1)
X_nm, y_nm = nm.fit_resample(testing_tfidf, processed_data.sentiment)
#NearMiss-1
lr_cv(5, processed_data.content, processed_data.sentiment, NM1_pipeline, 'macro')      #It selects samples of the majority
                         #class for which average distances to the k closest instances of the minority class is smallest. 
#NearMiss-2
lr_cv(5, processed_data.content, processed_data.sentiment, NM2_pipeline, 'macro')    #It selects samples of the majority 
                        #class for which average distances to the k farthest instances of the minority class is smallest.
#NearMiss-3
lr_cv(5,  processed_data.content, processed_data.sentiment, NM3_pipeline, 'macro')  #The majority class instances are selected 
                                                   #for which the average distance to the N nearest-neighbors is the largest.<jupyter_output>precision: [0.00323974 0.01112656 0.         0.01985981 0.07605178 0.
 0.12603306 0.         0.24823274 0.         0.         0.
 0.        ]
recall:    [0.13636364 0.44444444 0.         0.11184211 0.13239437 0.
 0.23018868 0.         0.52866242 0.         0.         0.
 0.        ]
f1 score:  [0.00632911 0.02170963 0.         0.03373016 0.09660843 0.
 0.16288385 0.         0.33783534 0.         0.         0.
 0.        ]
--------------------------------------------------
precision: [0.00211864 0.02072539 0.         0.04358974 0.07738998 0.
 0.08796895 0.         0.272      0.0533099  0.         0.
 0.        ]
recall:    [0.04545455 0.55555556 0.         0.11258278 0.28732394 0.
 0.25757576 0.         0.37384259 0.29836066 0.         0.
 0.        ]
f1 score:  [0.00404858 0.03996004 0.         0.06284658 0.12193664 0.
 0.13114754 0.         0.31489154 0.09045726 0.         0.
 0.        ]
--------------------------------------------------
precision: [0.00245098 0.01957831 0.         0[...]<jupyter_text>Modeling<jupyter_code>###########################################################################################################################

#After implementing vectorization using tf-idf, we will use SMOTE or Random Over Sampler to handle our imbalanced data set. 

###########################################################################################################################


def get_feature_vector(train_fit):
    vector = TfidfVectorizer(sublinear_tf=True,stop_words=None,analyzer='word', token_pattern=r'\w{1,}')
    vector.fit(train_fit)
    return vector

tf_vector = get_feature_vector(np.array(processed_data.iloc[:, 2]).ravel())
X = tf_vector.transform(np.array(processed_data.iloc[:, 2]).ravel())
y = np.array(processed_data.iloc[:, 1]).ravel()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=30)
smt = SMOTE(random_state=3)
X_train, y_train =smt.fit_resample(X_train, y_train) 


# Training Logistics Regression model
LR_model = LogisticRegression(solver='lbfgs')
LR_model.fit(X_train, y_train)
y_predict_lr = LR_model.predict(X_test)
print(accuracy_score(y_test, y_predict_lr))     #0.284

# Training Naive Bayes model
NB_model = MultinomialNB()
NB_model.fit(X_train, y_train)
y_predict_nb = NB_model.predict(X_test)
print(accuracy_score(y_test, y_predict_nb)) 

# Training AdaBoost model
AB_model = AdaBoostClassifier()
AB_model.fit(X_train, y_train)
y_predict_ab = AB_model.predict(X_test)
print(accuracy_score(y_test, y_predict_ab)) 

# Training SVM model
SVM_model = LinearSVC()
SVM_model.fit(X_train, y_train)
y_predict_svm = SVM_model.predict(X_test)
print(accuracy_score(y_test, y_predict_svm)) 

# Training Ridge model
R_model = RidgeClassifier()
R_model.fit(X_train, y_train)
y_predict_r = R_model.predict(X_test)
print(accuracy_score(y_test, y_predict_r))  

# Training Passive Aggressive Classifier model
PA_model = PassiveAggressiveClassifier()
PA_model.fit(X_train, y_train)
y_predict_pa = PA_model.predict(X_test)
print(accuracy_score(y_test, y_predict_pa)) 

# Training Nearest Centroid model
NC_model = NearestCentroid()
NC_model.fit(X_train, y_train)
y_predict_nc = NC_model.predict(X_test)
print(accuracy_score(y_test, y_predict_nc)) 

# Training Perceptron model
P_model = Perceptron()
P_model.fit(X_train, y_train)
y_predict_p = P_model.predict(X_test)
print(accuracy_score(y_test, y_predict_p)) 

# Training XGBClassifier model
XGB_model = XGBClassifier()
XGB_model.fit(X_train, y_train)
y_predict_xgb = XGB_model.predict(X_test)
print(accuracy_score(y_test, y_predict_xgb))                # 0.33275  best accuracy :|

# Training KNeighborsClassifier model
KNN_model = KNeighborsClassifier()
KNN_model.fit(X_train, y_train)
y_predict_knn = KNN_model.predict(X_test)
print(accuracy_score(y_test, y_predict_knn))<jupyter_output>0.284
0.196125
0.247875
0.254125
0.246125
0.2245
0.274
0.220125
[18:45:26] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.5.0/src/learner.cc:1115: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'multi:softprob' was changed from 'merror' to 'mlogloss'. Explicitly set eval_metric if you'd like to restore the old behavior.
0.33275
0.153375<jupyter_text>Forecasting with XGBClassifier<jupyter_code>newdata = pd.read_csv('newdata_nc.csv')
newdata.head()

processed_newdata = text_preprocessing_platform(newdata, 'DocText', remove_stopwords=False)

# Creating text feature
newdata_feature = tf_vector.transform(np.array(processed_newdata.iloc[:, 0]).ravel())

# Using XGBClassifier model for prediction
newdata_prediction_xgb = XGB_model.predict(newdata_feature)

newdata['newdata_prediction_xgb'] = ""
newdata.iloc[:,2]=pd.DataFrame(newdata_prediction_xgb)
newdata.to_csv("newdata_nc1.csv", index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1458765279282067, 'fraction_numerical': 0.08420238279436794, 'mean_word_length': 3.8734730809832603, 'pattern_counts': {'":': 2, '<': 52, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '444174', 'license': 'Naumen', 'n_tokens_mistral': 13156, 'n_tokens_neox': 11191, 'n_words': 2242, 'path': 'Case Study-checkpoint.ipynb', 'repo_name': 'ebrusakar/Emotion-Detection-from-Text', 'size': '32315'}