<p>from sklearn.datasets import fetch_20newsgroups</p>
<p>from sklearn.feature_extraction.text import CountVectorizer</p>
<p>from sklearn.feature_extraction.text import TfidfTransformer</p>
<p>from sklearn import svm</p>
<p>from sklearn import metrics</p>
<p>import numpy as np</p>
<p>&nbsp;</p>
<p># specify the categories to training with from the list of 20</p>
<p>categories = ['alt.atheism', 'soc.religion.christian', 'comp.graphics', 'sci.med']</p>
<p>twenty_train = fetch_20newsgroups(subset='train', categories=categories, shuffle=True, random_state=42)</p>
<p>print(twenty_train.target_names)</p>
<p>&nbsp;</p>
<p>count_vect = CountVectorizer()</p>
<p>X_train_counts = count_vect.fit_transform(twenty_train.data)</p>
<p>&nbsp;</p>
<p>tfidf_transformer = TfidfTransformer()</p>
<p>X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)</p>
<p>&nbsp;</p>
<p>clf = svm.SVC(kernel = 'linear')</p>
<p>clf.fit(X_train_tfidf, twenty_train.target)</p>
<p>&nbsp;</p>
<p># we will write two sentences to test the model.</p>
<p>docs_new = ['Abuse of antibiotics is very common',&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'OpenGL on the GPU is fast']</p>
<p>X_new_counts = count_vect.transform(docs_new)</p>
<p>X_new_tfidf = tfidf_transformer.transform(X_new_counts)</p>
<p>&nbsp;</p>
<p># show the category predicted by the model</p>
<p>predicted = clf.predict(X_new_tfidf)</p>
<p>print(predicted)</p>
<p>&nbsp;</p>
<p>for doc, category in zip(docs_new, predicted):</p>
<p>&nbsp; &nbsp; print('{} =&gt; {}'.format(doc, twenty_train.target_names[category]))</p>
<p>&nbsp;</p>
<p># get the test data from test dataset</p>
<p>twenty_test = fetch_20newsgroups(</p>
<p>&nbsp; &nbsp; subset='test',&nbsp;</p>
<p>&nbsp; &nbsp; categories=categories,&nbsp;</p>
<p>&nbsp; &nbsp; shuffle=True,&nbsp;</p>
<p>&nbsp; &nbsp; random_state=42</p>
<p>)</p>
<p>docs_test = twenty_test.data</p>
<p>&nbsp;</p>
<p># vectorize test data</p>
<p>X_test_counts = count_vect.transform(docs_test)</p>
<p>&nbsp;</p>
<p># extract feature of test data</p>
<p>X_test_tfidf = tfidf_transformer.transform(X_test_counts)</p>
<p>&nbsp;</p>
<p># use the model to predict the category&nbsp;</p>
<p>predicted = clf.predict(X_test_tfidf)</p>
<p>&nbsp;</p>
<p># get the precision, recall, f1-score and support of this model</p>
<p>print(metrics.classification_report(</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; twenty_test.target,&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; predicted,target_names=twenty_test.target_names)</p>
<p>&nbsp; &nbsp; &nbsp;)</p>
<p>&nbsp;</p>
<p># get the accuracy of the model</p>
<p>print("Accuracy:\t {}".format((np.mean(predicted == twenty_test.target))))</p>