<jupyter_start><jupyter_text>MODELS<jupyter_code>X = df['body']
y = df['subreddit']
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify = y)<jupyter_output><empty_output><jupyter_text><jupyter_code>#null model
y.value_counts(normalize=True)<jupyter_output><empty_output><jupyter_text>Count Vectorizer & Logistic Regression model<jupyter_code>cvect = CountVectorizer(stop_words = text.ENGLISH_STOP_WORDS.union(['roast']), max_features= 300)
ss = StandardScaler(with_mean=False)
logit = LogisticRegression(max_iter= 10_000)
pipe = make_pipeline(cvect, ss, logit)
pipe.fit(X_train, y_train)
pipe.score(X_test, y_test)
pipe.score(X_train, y_train)
word_coefs = pipe.named_steps['logisticregression'].coef_[0]
words = pipe.named_steps['countvectorizer'].get_feature_names()
word_coef_df = pd.DataFrame({'coefs': word_coefs, 'word': words})
word_coef_df.nlargest(12, 'coefs').plot(kind = 'barh')
plt.yticks([i for i in range(12)], word_coef_df.nlargest(12, 'coefs')['word'])
plt.title('Words that add to the score');
word_coef_df.nlargest(12, 'coefs').plot(kind = 'barh')
plt.yticks([i for i in range(12)], word_coef_df.nsmallest(12, 'coefs')['word'])
plt.title('Words that add to the score');<jupyter_output><empty_output><jupyter_text>Grid Search - Count Vectorizer & Logistic Regression (with Lemmatizer)<jupyter_code>def split_lem(text):
    text = text.lower()
    lem = WordNetLemmatizer()
    return ' '.join([lem.lematize(word) for word in text.split()])
cvect = CountVectorizer(stop_words = text.ENGLISH_STOP_WORDS.union(['roast']))
ss = StandardScaler(with_mean=False)
logit = LogisticRegression(max_iter= 10_000)
pipe = make_pipeline(cvect, ss, logit)

params = {
         'countvectorizer__max_features': [100, 200, 300],
         'countvectorizer__min_df': [1, 2, 6],
         'countvectorizer__max_df': [0.01, 0.5],
         'countvectorizer__ngram_range': [(1, 2), (1, 3)],
         'countvectorizer__preprocessor': [None, split_lem]
}

grid = GridSearchCV(pipe, params, n_jobs=-1)
grid.fit(X_train, y_train)
grid.best_params_
grid.score(X_test, y_test)
grid.score(X_train, y_train)<jupyter_output><empty_output><jupyter_text>TF-IDF Vectorizer & Logistic Regression model<jupyter_code>tvect = TfidfVectorizer(stop_words = text.ENGLISH_STOP_WORDS.union(['roast']), max_features= 400)
ss = StandardScaler(with_mean=False)
logit = LogisticRegression(max_iter= 10_000)
pipe = make_pipeline(tvect, ss, logit)
pipe.fit(X_train, y_train)
pipe.score(X_test, y_test)
pipe.score(X_train, y_train)
word_coefs = pipe.named_steps['logisticregression'].coef_[0]
words = pipe.named_steps['tfidfvectorizer'].get_feature_names()
word_coef_df = pd.DataFrame({'coefs': word_coefs, 'word': words})
word_coef_df.nlargest(12, 'coefs').plot(kind = 'barh', figsize = (11,10))
plt.yticks([i for i in range(12)], word_coef_df.nlargest(12, 'coefs')['word'], fontsize = 15)
plt.title('Words that add to the score', fontsize = 17);
word_coef_df.nlargest(12, 'coefs').plot(kind = 'barh', figsize = (11,10))
plt.yticks([i for i in range(12)], word_coef_df.nsmallest(12, 'coefs')['word'], fontsize = 15)
plt.title('Words that add to the score', fontsize = 17);
pipe.predict(['you look beautiful'])
pipe.predict(['you look hideous'])
preds = pipe.predict(X_test)
plot_confusion_matrix(pipe, X_test, y_test, cmap='Blues', values_format='d');
tn, fp, fn, tp = confusion_matrix(y_test, preds).ravel()
specificity = tn/(tn + fp)
specificity<jupyter_output><empty_output><jupyter_text>Grid Search - TF-IDF Vectorizer & Logistic Regression (with Lemmatizer)<jupyter_code>tvect = TfidfVectorizer(stop_words = text.ENGLISH_STOP_WORDS.union(['roast']))
pipe = make_pipeline(tvect, ss, logit)

params = {
         'tfidfvectorizer__max_features': [300, 400, 500],
         'tfidfvectorizer__min_df': [1, 2, 6],
         'tfidfvectorizer__max_df': [0.1, 0.5],
         'tfidfvectorizer__ngram_range': [(1, 2), (1, 3)],
         'tfidfvectorizer__preprocessor': [None, split_lem]
}

grid = GridSearchCV(pipe, params, n_jobs=-1)
grid.fit(X_train, y_train)
grid.best_params_
grid.score(X_test, y_test)
grid.score(X_train, y_train)
plot_confusion_matrix(grid, X_test, y_test, cmap='Blues', values_format='d');
preds = pipe.predict(X_test)
tn, fp, fn, tp = confusion_matrix(y_test, preds).ravel()
specificity = tn/(tn + fp)
specificity<jupyter_output><empty_output><jupyter_text>Naive Bayes<jupyter_code>cvect = CountVectorizer(stop_words = text.ENGLISH_STOP_WORDS.union(['roast']), max_features= 500)
nbayes = MultinomialNB()
pipe = make_pipeline(cvect, nbayes)
pipe.fit(X_train, y_train)
pipe.score(X_test, y_test)
pipe.score(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Grid Search with Naive Bayes<jupyter_code>cvect = CountVectorizer(stop_words = text.ENGLISH_STOP_WORDS.union(['roast']))
nbayes = MultinomialNB()
pipe = make_pipeline(cvect, nbayes)
params = {'countvectorizer__max_features': [200, 300, 400],
         'countvectorizer__min_df': [1, 2, 6, 10],
         'countvectorizer__max_df': [0.5, 0.8, .9],
         'countvectorizer__ngram_range': [(1, 1), (1, 2), (1, 3)],
         'countvectorizer__preprocessor': [None, split_lem]}
grid = GridSearchCV(pipe, params, n_jobs=-1)
grid.fit(X_train, y_train)
grid.best_params_
grid.score(X_test, y_test)
grid.score(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Naive Bayes with TfidfVectorizer<jupyter_code>tvect = TfidfVectorizer(stop_words = text.ENGLISH_STOP_WORDS.union(['roast']), max_features= 300)
nbayes = MultinomialNB()
pipe = make_pipeline(cvect, nbayes)
pipe.fit(X_train, y_train)
pipe.score(X_test, y_test)
pipe.score(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Grid Search with TF-IDFVectorizer & Naive Bayes<jupyter_code>tvect = TfidfVectorizer(stop_words = 'english')
pipe = make_pipeline(tvect, nbayes)
params = {
         'tfidfvectorizer__max_features': [50, 100, 150, 200],
         'tfidfvectorizer__min_df': [1, 2, 6, 10],
         'tfidfvectorizer__max_df': [0.5, 0.8, .9],
         'tfidfvectorizer__ngram_range': [(1, 1), (1, 2), (1, 3)],
         'tfidfvectorizer__preprocessor': [None, split_lem]}
grid = GridSearchCV(pipe, params, n_jobs=-1)
grid.fit(X_train, y_train)
grid.best_params_
grid.score(X_test, y_test)
grid.score(X_train, y_train)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.150111429481057, 'fraction_numerical': 0.026424705507800062, 'mean_word_length': 6.85375, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '231744', 'license': 'CC0-1.0', 'n_tokens_mistral': 2634, 'n_tokens_neox': 2472, 'n_words': 425, 'path': 'code/NLP-Models.ipynb', 'repo_name': 'kevinrenois/Subreddit-Classification', 'size': '6282'}