<jupyter_start><jupyter_text>This is Rank 29 solution using SGD Classifier and One Vs Rest Classifier.   I have also tried fastai but it didn't give me good results even after fine-tuning it. I have also uploaded fastai solution in this folder which gave me F1-Score of 0.4676695183 on Public leaderboard which will be good reference point for using fastai for NLP problems.  Import Libraries<jupyter_code>import re
import nltk
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings 
warnings.filterwarnings("ignore")
from nltk.corpus import stopwords
nltk_stopwords = stopwords.words('english')<jupyter_output><empty_output><jupyter_text>Import Datasets<jupyter_code>train = pd.read_csv('../input/innoplexus/innoplexus_sentiment analysis/train.csv')
test = pd.read_csv('../input/innoplexus/innoplexus_sentiment analysis/test.csv')
print('No. of samples in training set:', len(train))
print('No. of samples in test set    :', len(test))<jupyter_output>No. of samples in training set: 5279
No. of samples in test set    : 2924<jupyter_text>Text pre-processing<jupyter_code>def clean_text(text):
    text = text.lower()
    text = re.sub(r'@[a-zA-Z0-9_]+', '', text)   
    text = re.sub(r'https?://[A-Za-z0-9./]+', '', text)   
    text = re.sub(r'www.[^ ]+', '', text)  
    text = re.sub(r'[a-zA-Z0-9]*www[a-zA-Z0-9]*com[a-zA-Z0-9]*', '', text)  
    text = re.sub(r'[^a-zA-Z]', ' ', text)   
    text = [token for token in text.split() if len(token) > 2]
    text = ' '.join(text)
    return text

train['text'] = train['text'].apply(clean_text)
test['text'] = test['text'].apply(clean_text)<jupyter_output><empty_output><jupyter_text>Train test split<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_cv, y_train, y_cv = train_test_split(train['text'], train['sentiment'], test_size=0.25, stratify=train['sentiment'], 
                                                random_state=1)<jupyter_output><empty_output><jupyter_text>Feature extraction & Model building<jupyter_code>from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vect = TfidfVectorizer(analyzer='word', stop_words=None, token_pattern='(?<KEY>', ngram_range=(1, 1), max_df=1.0, 
                             min_df=3, max_features=None, binary=False, norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False)
X_train_tfidf = tfidf_vect.fit_transform(X_train)
X_cv_tfidf = tfidf_vect.transform(X_cv)
from sklearn.metrics import f1_score, accuracy_score
from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import SGDClassifier
sgd = SGDClassifier(loss='log', max_iter=200, random_state=0, class_weight='balanced')
ovr = OneVsRestClassifier(sgd)
ovr.fit(X_train_tfidf, y_train)
y_pred_class = ovr.predict(X_cv_tfidf)
print('f1_score       :', f1_score(y_cv, y_pred_class, average='macro'))
print('accuracy score :', accuracy_score(y_cv, y_pred_class))<jupyter_output>f1_score       : 0.5145775153047341
accuracy score : 0.6613636363636364<jupyter_text>Predict on test set<jupyter_code>from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vect = TfidfVectorizer(analyzer='word', stop_words=None, token_pattern='(?<KEY>', ngram_range=(1, 1), max_df=1.0, 
                             min_df=3, max_features=None, binary=False, norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False)
full_text = list(train['text'].values) + list(test['text'].values)
tfidf_vect.fit(full_text)

X_train_tfidf = tfidf_vect.transform(train['text'])
X_test_tfidf = tfidf_vect.transform(test['text'])

y_train = train['sentiment']
from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import SGDClassifier
sgd = SGDClassifier(loss='log', max_iter=200, random_state=0, class_weight='balanced')
ovr = OneVsRestClassifier(sgd)
ovr.fit(X_train_tfidf, y_train)
y_pred_class = ovr.predict(X_test_tfidf)
y_pred_class<jupyter_output><empty_output><jupyter_text>Submission<jupyter_code>test['sentiment'] = y_pred_class
test.drop(['text','drug'], axis=1,inplace=True)
test.head()
test['sentiment'].value_counts()
test.to_csv('submission.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1303827751196172, 'fraction_numerical': 0.022966507177033493, 'mean_word_length': 5.8316993464052285, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '548433', 'license': 'Apache-2.0', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1532, 'n_words': 289, 'path': 'Sentiment Analysis_Final Solution_0.5230949840.ipynb', 'repo_name': 'GJvignesh/Innoplexus-Online-Hiring-Hackathon-Sentiment-Analysis', 'size': '4200'}