<jupyter_start><jupyter_text>I have chosen 5 ML Algorithms namely:1. Naive Bayes2. Stochastic Gradient Descent3. Logistic Regression4. Random Forest5. Linear Suport Vector Machine<jupyter_code>import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm
import pickle
# Following is the list of flairs selected

flairs=["[R]eddiquette","Politics","Non-Political","Business/Finance","Unverified","Science/Technology","Sports","Photography","CAA-NRC","Food","AskIndia","Policy/Economy","Scheduled","AMA","Coronavirus"]
data = pd.read_csv('Stopwords.csv')
data.fillna('', inplace=True)
data.head(2)<jupyter_output><empty_output><jupyter_text>I have created a new column in my data 'combined'  which holds the values for columns combined from title, url , comment which gave me the highest accuracy levels, about 70% using the Logistic Regression Model<jupyter_code>data['combined']=data['title']+","+data['url']+","+data['comment']
arr = data['combined']
print(arr)
arr = data['combined']
cnt=0
for ele in arr:
    a1=''
    a = str(ele).split(',')
    for item in a:
        item1 = item.replace("[",'')
        item1 = item1.replace("]",'')
        item1 = item1.replace('"','')
        item1 = item1.replace(' ','')
        a1=a1+","+(item1)
        data.at[cnt,'combined']=a1
    cnt=cnt+1
    
arr = data['combined']
print(arr)
data.head()
data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 1500 entries, 0 to 1499
Data columns (total 7 columns):
 #   Column    Non-Null Count  Dtype 
---  ------    --------------  ----- 
 0   title     1500 non-null   object
 1   url       1500 non-null   object
 2   no_comm   1500 non-null   int64 
 3   body      1500 non-null   object
 4   comment   1500 non-null   object
 5   flair     1500 non-null   object
 6   combined  1500 non-null   object
dtypes: int64(1), object(6)
memory usage: 82.2+ KB<jupyter_text>Here I have set up the training and testing variables for my algorithm. Finally I chose a 9:1 training and testing ratio as it provided me with the highest accuracy results.I started off with a 8:2 split and the results were about 45% - 50% and then I started tweaking the split and finally settled for a 9:1 split.<jupyter_code>#Dependent and independent variables
y = data.flair
X = data.combined

#Setting training and testing dataset
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.1,random_state = 42)<jupyter_output><empty_output><jupyter_text>Using a Pipeline function combines the CountVetorizer command which cleans the data, the TfidfTransformer which computes word counts etc. and finally the Regression Model Command into single easy to understand command.<jupyter_code>#Naive Bayes
nb = Pipeline([('vect', CountVectorizer()),
               ('tfidf', TfidfTransformer()),
               ('clf', MultinomialNB()),
              ])

NB = nb.fit(X_train, y_train)
y_pred = nb.predict(X_test)


print("Accuracy is "+str(accuracy_score(y_pred, y_test)))
print(classification_report(y_test, y_pred,target_names=flairs))

#SGD
sgd = Pipeline([('vect', CountVectorizer()),
                ('tfidf', TfidfTransformer()),
                ('clf', SGDClassifier(loss='hinge', penalty='l2',alpha=1e-3, random_state=42, max_iter=5, tol=None)),
               ])
SGD = sgd.fit(X_train, y_train)
y_pred = sgd.predict(X_test)

print("Accuracy is "+str(accuracy_score(y_pred, y_test)))
print(classification_report(y_test, y_pred,target_names=flairs))<jupyter_output>Accuracy is 0.64
                    precision    recall  f1-score   support

     [R]eddiquette       0.70      1.00      0.82         7
          Politics       0.38      0.50      0.43        10
     Non-Political       0.75      0.50      0.60        12
  Business/Finance       0.42      0.83      0.56         6
        Unverified       0.77      0.83      0.80        12
Science/Technology       0.56      0.56      0.56         9
            Sports       0.67      0.22      0.33         9
       Photography       0.63      1.00      0.77        12
           CAA-NRC       0.43      0.60      0.50         5
              Food       0.62      0.62      0.62        13
          AskIndia       0.88      1.00      0.93        14
    Policy/Economy       0.75      0.43      0.55         7
         Scheduled       0.92      0.92      0.92        12
               AMA       0.33      0.09      0.14        11
       Coronavirus       0.50      0.36      0.42        11

          accuracy   [...]<jupyter_text>As the Logistic Regression Model had the largest accuracy for the 9:1 split, I selected the following model and the model for saved using the pickle function.<jupyter_code>#Logistic Regression
logreg = Pipeline([('vect', CountVectorizer()),
                ('tfidf', TfidfTransformer()),
                ('clf', LogisticRegression(n_jobs=1, C=1e20)),
               ])
LOGREG = logreg.fit(X_train, y_train)
y_pred = logreg.predict(X_test)
pickle.dump(LOGREG,open('model_final.pkl','wb'))
print("Accuracy is "+str(accuracy_score(y_pred, y_test)))
print(classification_report(y_test, y_pred,target_names=flairs))
#Random Forest
ranfor = Pipeline([('vect', CountVectorizer()),
                   ('tfidf', TfidfTransformer()),
                   ('clf', RandomForestClassifier(n_estimators = 1000, random_state = 5)),
                  ])
RM = ranfor.fit(X_train, y_train)
#pickle.dump(RM,open("model_RM.sav",'wb'))
y_pred = ranfor.predict(X_test)

print("Accuracy is "+str(accuracy_score(y_pred, y_test)))
print(classification_report(y_test, y_pred,target_names=flairs))
#SVM
svm1 = Pipeline([('vect', CountVectorizer()),
                ('tfidf', TfidfTransformer()),
                ('clf', svm.SVC(kernel='linear', C=1.0)),
               ])
SVM = svm1.fit(X_train, y_train)
y_pred = svm1.predict(X_test)

print("Accuracy is "+str(accuracy_score(y_pred, y_test)))
print(classification_report(y_test, y_pred,target_names=flairs))<jupyter_output>Accuracy is 0.6866666666666666
                    precision    recall  f1-score   support

     [R]eddiquette       1.00      1.00      1.00         7
          Politics       0.29      0.40      0.33        10
     Non-Political       0.75      0.50      0.60        12
  Business/Finance       0.62      0.83      0.71         6
        Unverified       0.77      0.83      0.80        12
Science/Technology       0.75      0.67      0.71         9
            Sports       0.71      0.56      0.63         9
       Photography       0.79      0.92      0.85        12
           CAA-NRC       0.29      0.40      0.33         5
              Food       0.69      0.69      0.69        13
          AskIndia       0.93      1.00      0.97        14
    Policy/Economy       0.46      0.86      0.60         7
         Scheduled       1.00      0.83      0.91        12
               AMA       0.57      0.36      0.44        11
       Coronavirus       0.67      0.36      0.47        11

       [...]
{'fraction_non_alphanumeric': 0.10759067008224349, 'fraction_numerical': 0.061075906700822434, 'mean_word_length': 2.2606593406593407, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '404419', 'license': 'MIT', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2387, 'n_words': 610, 'path': 'Model Training/Training and Testing.ipynb', 'repo_name': 'adityaras/reddit-flair-detector', 'size': '7417'}