<jupyter_start><jupyter_text>Importing libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import sklearn
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.metrics import classification_report

from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import SGDClassifier

from sklearn.externals import joblib
data = pd.read_csv("Data/reddit-top-flairs-cleaned.csv")
data.head()<jupyter_output><empty_output><jupyter_text>Dropping unwanted columns and shuffling the data.<jupyter_code>data = data.drop(['dirty_title','dirty_body','num_words_body','num_unique_words_title'
           ,'num_unique_words_body','num_chars_title','num_words_title','num_chars_body','url','id'], axis=1)

data.sample(frac=1)
data["body"].fillna(".", inplace = True)
data.head()
top_flairs = ["Politics", "Non-Political", "Coronavirus", "AskIndia", "Policy/Economy", 
              "Photography", "Business/Finance", "Sports", "Science/Technology"]<jupyter_output><empty_output><jupyter_text>The columns of "title" and "body" are combined as one feature as "input_features" and "cat" stores the output label.<jupyter_code>input_features = data["title"] + " "+ data["body"]
data = data.assign(input_features = input_features)

y = data.flair
data = data.drop(['title', 'body'],axis=1)
data.head()<jupyter_output><empty_output><jupyter_text>Rearranging the columns and writing the final dataset used for models.<jupyter_code>cols = data.columns.tolist()
print(cols)
cols = cols[-1:] + cols[:-1]
print(cols)
data = data[cols]

data.to_csv('Data/final-data.csv', index=False)
data.head()<jupyter_output>['flair', 'input_features']
['input_features', 'flair']<jupyter_text>Train test split is done.<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(input_features,y, test_size=0.3)

print("x_train dim:",x_train.shape, "\ty_train dim:", y_train.shape)
print("x_test dim:",x_test.shape, "\ty_test dim:", y_test.shape)<jupyter_output>x_train dim: (1409,) 	y_train dim: (1409,)
x_test dim: (604,) 	y_test dim: (604,)<jupyter_text>Logistic RegressionIt is a typical classifier and most pervasively used one too. It has great interpretability properties. Parameters:1. C = inverse regularization parameter; C = 1/λ;  It’s a penalty term meant regulate against overfitting.2. max_iter = The maximum number of passes over the training data, epochs.<jupyter_code># Logistic Regression
logistic = Pipeline([('cv', CountVectorizer()),('tfidf', TfidfTransformer()),('lr', LogisticRegression(C=1000, max_iter=1000))])
logistic.fit(x_train, y_train)

y_pred = logistic.predict(x_test)

accuracy = (accuracy_score(y_pred, y_test)*100)
print("Accuracy: %.2f" % accuracy +'%')
print(classification_report(y_test, y_pred,target_names=top_flairs))<jupyter_output>Accuracy: 82.28%
                    precision    recall  f1-score   support

          Politics       0.81      0.85      0.83        74
     Non-Political       0.78      0.69      0.73        65
       Coronavirus       0.93      0.97      0.95        69
          AskIndia       0.92      0.82      0.87        60
    Policy/Economy       0.92      0.95      0.94        62
       Photography       0.68      0.73      0.70        66
  Business/Finance       0.73      0.80      0.76        70
            Sports       0.77      0.73      0.75        64
Science/Technology       0.90      0.85      0.88        74

          accuracy                           0.82       604
         macro avg       0.83      0.82      0.82       604
      weighted avg       0.83      0.82      0.82       604<jupyter_text>Inference from Logistic Regression:1. The model has a total accuracy of **83%**, which is good.2. The most performing flair category was **"Coronavirus"**.3. The least performing flair categories was **"Non-Political"**   NAIVE BAYES CLASSIFIEROne of the most suitable variants for text is the multinomial variant.<jupyter_code># Naive Bayes
naive = Pipeline([('cv', CountVectorizer()),('tfidf', TfidfTransformer()),('nb', MultinomialNB())])
naive.fit(x_train, y_train)
y_pred = naive.predict(x_test)

accuracy = (accuracy_score(y_pred, y_test)*100)
print("Accuracy: %.2f" % accuracy +'%')
print(classification_report(y_test, y_pred,target_names=top_flairs))<jupyter_output>Accuracy: 69.04%
                    precision    recall  f1-score   support

          Politics       0.73      0.51      0.60        74
     Non-Political       0.40      0.78      0.53        65
       Coronavirus       0.83      0.97      0.89        69
          AskIndia       0.89      0.57      0.69        60
    Policy/Economy       0.85      0.90      0.88        62
       Photography       0.69      0.50      0.58        66
  Business/Finance       0.72      0.59      0.65        70
            Sports       0.56      0.70      0.62        64
Science/Technology       0.98      0.70      0.82        74

          accuracy                           0.69       604
         macro avg       0.74      0.69      0.70       604
      weighted avg       0.74      0.69      0.70       604<jupyter_text>Inference from Naive Bayes Classifier:1. The model has a total accuracy of **69%**, which is decent.2. The most performing flair category was **"Coronavirus"**.3. The least performing flair category was **"Non-Political"**.  Random Forest ClassifierRandom forest classifier suits most multi-class classification problem, also they have good interpretability and work faster. Parameters:1. n_estimators = the number of trees in the forest.<jupyter_code># Random Forest
random = Pipeline([('cv', CountVectorizer()),('tfidf', TfidfTransformer()),('rf', RandomForestClassifier(n_estimators = 600))])
random.fit(x_train, y_train)

y_pred = random.predict(x_test)

accuracy = (accuracy_score(y_pred, y_test)*100)
print("Accuracy: %.2f" % accuracy +'%')
print(classification_report(y_test, y_pred,target_names=top_flairs))<jupyter_output>Accuracy: 82.28%
                    precision    recall  f1-score   support

          Politics       0.86      0.91      0.88        74
     Non-Political       0.82      0.72      0.77        65
       Coronavirus       0.85      0.97      0.91        69
          AskIndia       0.98      0.97      0.97        60
    Policy/Economy       0.95      0.95      0.95        62
       Photography       0.76      0.68      0.72        66
  Business/Finance       0.53      0.74      0.62        70
            Sports       0.84      0.72      0.77        64
Science/Technology       1.00      0.76      0.86        74

          accuracy                           0.82       604
         macro avg       0.84      0.82      0.83       604
      weighted avg       0.84      0.82      0.83       604<jupyter_text>Inference from Random Forest Classifier:1. The model has a total accuracy of **82%**, which is goood.2. The most performing flair category was **"AskIndia"**.3. The least performing flair category was **"Business/Finance"**.<jupyter_code>joblib.dump(random, 'Pickle_files/random-forest.pkl')<jupyter_output><empty_output><jupyter_text>k- Nearest Neighbours Classifier Parameters:1. n_neighbours = number of neighbours to take into consideration.<jupyter_code># k-Nearest Neighbours
neighbours = Pipeline([('vect', CountVectorizer()),('tfidf', TfidfTransformer()),('knn', KNeighborsClassifier(n_neighbors=10))])
neighbours.fit(x_train, y_train)
y_pred = neighbours.predict(x_test)

accuracy = (accuracy_score(y_pred, y_test)*100)
print("Accuracy: %.2f" % accuracy +'%')
print(classification_report(y_test, y_pred,target_names=top_flairs))<jupyter_output>Accuracy: 62.58%
                    precision    recall  f1-score   support

          Politics       0.70      0.43      0.53        74
     Non-Political       0.49      0.65      0.56        65
       Coronavirus       0.70      0.94      0.80        69
          AskIndia       0.68      0.63      0.66        60
    Policy/Economy       0.73      0.87      0.79        62
       Photography       0.44      0.53      0.48        66
  Business/Finance       0.59      0.41      0.49        70
            Sports       0.51      0.52      0.51        64
Science/Technology       0.89      0.68      0.77        74

          accuracy                           0.63       604
         macro avg       0.64      0.63      0.62       604
      weighted avg       0.64      0.63      0.62       604<jupyter_text>Inference from k- Nearest Neighbours Classifier:1. The model has a total accuracy of **63%**, which is goood.2. The most performing flair category was **"Coronavirus"**.3. The least performing flair category was **"Photography"**.  Linear Support Vector Machine Parameters:1. loss = 'hinge' : The loss function to be used.‘hinge’ gives a linear SVM.2. penalyty = regularisation term. 'l2' for linear SVM.2. alpha = Constant that multiples the regularisation term.3. max_iter = The maximum number of passes over the training data, epochs.<jupyter_code>svm = Pipeline([('cv', CountVectorizer()),('tfidf', TfidfTransformer()),('svm', SGDClassifier(loss='hinge', 
                penalty='l2',alpha=0.001, max_iter=20))])
svm.fit(x_train, y_train)
y_pred = svm.predict(x_test)

accuracy = (accuracy_score(y_pred, y_test)*100)
print("Accuracy: %.2f" % accuracy +'%')
print(classification_report(y_test, y_pred,target_names=top_flairs))<jupyter_output>Accuracy: 83.77%
                    precision    recall  f1-score   support

          Politics       0.86      0.91      0.88        74
     Non-Political       0.77      0.71      0.74        65
       Coronavirus       0.83      0.97      0.89        69
          AskIndia       0.93      0.90      0.92        60
    Policy/Economy       0.91      0.98      0.95        62
       Photography       0.74      0.73      0.73        66
  Business/Finance       0.77      0.73      0.75        70
            Sports       0.80      0.77      0.78        64
Science/Technology       0.93      0.85      0.89        74

          accuracy                           0.84       604
         macro avg       0.84      0.84      0.84       604
      weighted avg       0.84      0.84      0.84       604<jupyter_text>Inference from Linear SVM Classifier:1. The model has a total accuracy of **84%**, which is gooood.2. The most performing flair category was **"Policy/Economy"**.3. The least performing flair category was **"Photography"**.<jupyter_code>joblib.dump(random, 'Pickle_files/linear-svm.pkl')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10065263351410975, 'fraction_numerical': 0.07041088335324938, 'mean_word_length': 2.2959709179036656, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '509220', 'license': 'MIT', 'n_tokens_mistral': 3884, 'n_tokens_neox': 3475, 'n_words': 880, 'path': 'Models/Part III - Building a Flare Detector.ipynb', 'repo_name': 'sgouda789/Reddit-Flair-Detection', 'size': '10879'}