<jupyter_start><jupyter_text>Model selection> Selecting models' hyper parameters  Text Classification Problem<jupyter_code>import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn import datasets
news = datasets.fetch_20newsgroups(subset='all')
X = news.data
y = news.target<jupyter_output><empty_output><jupyter_text>Split the data<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=.25)<jupyter_output><empty_output><jupyter_text>Bag of words<jupyter_code>vectorizer = CountVectorizer(stop_words='english', binary=True)
X_train_vectors = vectorizer.fit_transform(X_train)
X_test_vectors = vectorizer.transform(X_test)
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler<jupyter_output><empty_output><jupyter_text>Creating a Classifier with a Pipeline<jupyter_code>clf = Pipeline([
    ('vect', CountVectorizer(stop_words='english', binary=True)),
    ('clf', MultinomialNB())
])
clf.fit(X_train, y_train)
clf.score(X_test, y_test)
clf.score(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Creating a Model Without a pipeline<jupyter_code>model = MultinomialNB()
model.fit(X_train_vectors, y_train)
model.score(X_train_vectors, y_train)
model.score(X_test_vectors, y_test)
x_pred_vectors = vectorizer.transform(X_test[:5])
model.predict(x_pred_vectors), y_test[:5]
## The GridSearchCV
> This helps us to find the best parameters for our classifiers
from sklearn.model_selection import GridSearchCV
parameters = {
 'clf__fit_prior': np.array([True, False]),
  'clf__alpha': np.array([0,  .5, 1]),
 }
clf = Pipeline([
    ('vect', CountVectorizer()),
    ('clf', MultinomialNB())
])
gs = GridSearchCV(clf, parameters, verbose=2, refit=False, cv=3)
clf.get_params()
gs.fit(X_train, y_train)
gs.best_score_
gs.best_params_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11228070175438597, 'fraction_numerical': 0.006516290726817042, 'mean_word_length': 8.784313725490197, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '569419', 'license': 'MIT', 'n_tokens_mistral': 736, 'n_tokens_neox': 706, 'n_words': 135, 'path': 'Advanced Features/02_model_selection/Model selection.ipynb', 'repo_name': 'CrispenGari/Machine-Leaning-Scikit-learn', 'size': '1995'}