In [2]:
from sklearn import preprocessing
from sklearn import cross_validation
from sklearn.tree import DecisionTreeClassifier
from sklearn.feature_extraction.text import CountVectorizer
preprocess is a multi-language preprocessor The sklearn.preprocessing package provides several common utility functions and transformer classes to change raw feature vectors into a representation that is more suitable for the downstream estimators.
sklearn(scikit-learn) package can't handle gender data because it has string (male and female inside), so need to be convered to boolean (0 and 1).
In scikit-learn a random split into training and test (to avoid the cases of overfitting) sets can be quickly computed with the train_test_split helper function.
In [3]:
########## STEP 1: DATA IMPORT AND PREPROCESSING ##########
# Here we're taking in the training data and splitting it into two lists: One with the text of
# each bill title, and the second with each bill title's corresponding category. Order is important.
# The first bill in list 1 should also be the first category in list 2.
training = [line.strip().split('|') for line in open('bills_training.txt', 'r', encoding="utf8").readlines()]
text = [t[0] for t in training if len(t) > 1]
labels = [t[1] for t in training if len(t) > 1]
# A little bit of cleanup for scikit-learn's benefit. Scikit-learn models wants our categories to
# be numbers, not strings. The LabelEncoder performs this transformation.
encoder = preprocessing.LabelEncoder()
correct_labels = encoder.fit_transform(labels)
In [4]:
# is it a random number or number of occurrence ?
correct_labels
Out[4]:
In [6]:
########## STEP 2: FEATURE EXTRACTION ##########
vectorizer = CountVectorizer(stop_words='english')
data = vectorizer.fit_transform(text)
In [12]:
# what exactly is the number corresponding to the category 0,1,2.... what's number 1 stands for which is outside the tuple
print(data)
In [9]:
########## STEP 3: MODEL BUILDING ##########
model = DecisionTreeClassifier()
fit_model = model.fit(data, correct_labels)
In [11]:
print(fit_model)
In [10]:
# ########## STEP 4: EVALUATION ##########
# Evaluate our model with 10-fold cross-validation
scores = cross_validation.cross_val_score(model, data, correct_labels, cv=5)
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
In [11]:
# ########## STEP 5: APPLYING THE MODEL ##########
docs_new = ["Public postsecondary education: executive officer compensation.",
"An act to add Section 236.3 to the Education code, related to the pricing of college textbooks.",
"Political Reform Act of 1974: campaign disclosures.",
"An act to add Section 236.3 to the Penal Code, relating to human trafficking."
]
test_data = vectorizer.transform(docs_new)
for i in range(len(docs_new)):
print('%s -> %s' % (docs_new[i], encoder.classes_[model.predict(test_data.toarray()[i])]))
In [ ]: