<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Hashing Vectorizer example
In this notebook, we'll experiment with HashingVectorizer by making a classifier that predicts whether a text chunk comes from the English Wikipedia articles "Anarchism" or "Anachronism".
Preamble
First, let's import everything that we'll need.
Step1:  Get the text from the API
Step2:  Build up a set of observations
Step3:  Split into train/test
Step4:  HashingVectorizer and prediction model
We'll use a GradientBoosting model because it ought to work pretty well
Step5:  Train the classifier
We'll set the "sample weight" to be proportionally stronger for "Anachronism" because we don't have as many observations.
Step6:  Test the classifier
This score() method generates a simple accuracy measure.  This result suggest that we can predict ~86% of the test set correctly.
Step7:  Statistics are great, but let's look at some example predictions.  This loop generates predictions
Step8:  Feature selection
Let's build a histogram of the feature importance measurments.
<ASSISTANT_TASK:>
Python Code:
import random
import mwapi
import mwparserfromhell as mwparser
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.feature_extraction.text import HashingVectorizer
Explanation: Hashing Vectorizer example
In this notebook, we'll experiment with HashingVectorizer by making a classifier that predicts whether a text chunk comes from the English Wikipedia articles "Anarchism" or "Anachronism".
Preamble
First, let's import everything that we'll need.
End of explanation
session = mwapi.Session("https://en.wikipedia.org", 
                        user_agent="Hashing vectorizer example <aaron.halfaker@gmail.com>")
doc = session.get(action="query", prop="revisions", titles=["Anarchism", "Anachronism"], rvprop=['content'])
anarchism_text = doc['query']['pages']['12']['revisions'][0]['*']
anarchonism_text = doc['query']['pages']['60731']['revisions'][0]['*']
Explanation: Get the text from the API
End of explanation
observations = []
for text_chunk in mwparser.parse(anarchism_text).filter_text():
    text = text_chunk.value
    if len(text) > 25:
        observations.append((text, "anarchism"))
for text_chunk in mwparser.parse(anarchonism_text).filter_text():
    text = text_chunk.value
    if len(text) > 25:
        observations.append((text, "anachronism"))
print("anarchism paragraphs:", sum(1 for _, label in observations if label == "anarchism"))
print("anachronism paragraphs:", sum(1 for _, label in observations if label == "anachronism"))
Explanation: Build up a set of observations
End of explanation
random.shuffle(observations)
train_set = observations[:int(len(observations)*0.8)]
test_set = observations[int(len(observations)*0.8):]
len(train_set), len(test_set)
Explanation: Split into train/test
End of explanation
hv = HashingVectorizer(n_features=2**16)
gbc = GradientBoostingClassifier()
Explanation: HashingVectorizer and prediction model
We'll use a GradientBoosting model because it ought to work pretty well
End of explanation
# Training
texts, labels_y = zip(*train_set)
features_X = hv.transform(texts)
gbc.fit(features_X, labels_y, 
        sample_weight=[119/(119+1433) if l == "anarchism" else 1433/(119+1433) for l in labels_y])
Explanation: Train the classifier
We'll set the "sample weight" to be proportionally stronger for "Anachronism" because we don't have as many observations.
End of explanation
# Testing
texts, labels_y = zip(*test_set)
features_X = hv.transform(texts)
gbc.score(features_X.todense(), labels_y)
Explanation: Test the classifier
This score() method generates a simple accuracy measure.  This result suggest that we can predict ~86% of the test set correctly.
End of explanation
for text, label in test_set[:10]:
    features_X = hv.transform([text])
    print("text:", repr(text[:50] + "..."), "\n",
          "\tactual:", label, "\n",
          "\tprediction:", dict(zip(gbc.classes_, 
                                    [int(v*100) for v in gbc.predict_proba(features_X.todense())[0]])))
    
Explanation: Statistics are great, but let's look at some example predictions.  This loop generates predictions
End of explanation
%matplotlib inline
import matplotlib.pyplot as plt
plt.hist(gbc.feature_importances_, bins=100, log=True)
#plt.semilogy()
plt.title("Feature importance histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
Explanation: Feature selection
Let's build a histogram of the feature importance measurments.
End of explanation
<END_TASK>