<jupyter_start><jupyter_text>Perceptron<jupyter_code>#Document classification with the perceptron
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import Perceptron
from sklearn.metrics import f1_score, classification_report

categories = ['rec.sport.hockey', 'rec.sport.baseball', 'rec.autos']
newsgroups_train = fetch_20newsgroups(subset='train', categories=categories,
                                      remove=('headers', 'footers', 'quotes'))
newsgroups_test = fetch_20newsgroups(subset='test', categories=categories,
                                      remove=('headers', 'footers', 'quotes'))

vectorizer = TfidfVectorizer()
X_train = vectorizer.fit_transform(newsgroups_train.data)
X_test = vectorizer.transform(newsgroups_test.data)
clf = Perceptron(random_state=11)
clf.fit(X_train, newsgroups_train.target )
predictions = clf.predict(X_test)
print(classification_report(newsgroups_test.target, predictions))<jupyter_output>precision    recall  f1-score   support

          0       0.81      0.92      0.86       396
          1       0.87      0.76      0.81       397
          2       0.86      0.85      0.86       399

avg / total       0.85      0.84      0.84      1192
{'fraction_non_alphanumeric': 0.09162098668754894, 'fraction_numerical': 0.0485512920908379, 'mean_word_length': 2.9813084112149535, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '291284', 'license': 'MIT', 'n_tokens_mistral': 432, 'n_tokens_neox': 390, 'n_words': 76, 'path': 'Section 10/Section 10.ipynb', 'repo_name': 'robson-koji/Machine-Learning-with-Scikit-learn', 'size': '1277'}