Package: caerbannog
Rabbit of CaerbannogWell, that’s no ordinary rabbit - that’s the most foul, cruel, and bad-tempered rodent you ever set eyes on!—Tim the EnchanterThis module is a high-level interface for the Vowpal Wabbit machine learning system. Currently it relies on
thewabbit_wappamodule for lower-level interaction, but strives to provide a more high-level object-oriented interface.There are currently 3 kinds ofRabbit`s you can `import fromcaerbannog:RabbitYour standard rabbit instance. By default runs Vowpal Wabbit using pipes for stdin/stdoutActiveRabbitRuns Vowpal Wabbit in active learning mode, using TCP socketOfflineRabbitThe initializer expects the argumentfpwhich is an open file with‘wt’mode.
the inputs fed toteachwill be written to this file for offline processing.Movie Review Sentiments - Active learning demo with caerbannogImport relevant modules here. We are using the ActiveRabbit for active
online learningfromcaerbannogimportActiveRabbit,Examplefromitertoolsimportisliceimportrandomimportnltkfromnltk.corpusimportmovie_reviewsCreate an active bunny, with active mellowness of 0.01rabbit=ActiveRabbit(loss_function='logistic',active_mellowness=0.01)rabbit.start()Load the documents from NLTK movie review corpus (note that you need to
download these first by nltk.download(). For each document, make a tuple
(document_words, category) where category is either ‘pos’ or ‘neg’ and
document_words is a list of words from tokenizer.documents=[(list(movie_reviews.words(fileid)),category)forcategoryinmovie_reviews.categories()forfileidinmovie_reviews.fileids(category)]random.shuffle(documents)len(documents)2000The feature extractor function. First filters out all tokens that are
non-alphanumeric. Then make the ‘w’ namespace consist of all the words
in the review; ‘n’ consists of 2-4 ngrams of the document words.defdocument_features(document_words):document_words=list(filter(str.isalnum,document_words))example=Example()example['w'].add_features(set(document_words))ngrams=set()forjinrange(2,5):ngrams.update('_'.join(i)foriinnltk.ngrams(document_words,j))example['n'].add_features(ngrams)returnexampleVowpal Wabbit expects labels to be -1 and 1 for logistic binary
classifierdefconvert_sent(sent):return{'pos':1,'neg':-1}[sent]Convert the sentiment value and extract features.examples=[(convert_sent(sent),document_features(doc))for(doc,sent)indocuments]Train with 1500 first examples and keep the remaining ones for
verificationteach,test=examples[:1500],examples[1500:]Teach the filter. We ask for prediction for each example; if the
importance is over 1 we “label” the example and teach it to the
classifier. We repeat the classification 40 times to ensure that the
classifier has had enough to adjust the weights.taught=0predicted=0labelled=set()foriinrange(10):forsent,exinteach:predicted+=1ifrabbit.predict(example=ex).importance>=1:rabbit.teach(label=sent,example=ex)taught+=1labelled.add(ex)print("Predicted{}, taught{}(ratio{}).{}unique inputs labelled".format(predicted,taught,taught/predicted,len(labelled)))Predicted 15000, taught 1057 (ratio 0.07046666666666666). 1042 unique inputs labelledTest with the testing set. For each correctly labelled example, increase
the countercorrect=0forsent,exintest:prediction=rabbit.predict(example=ex)ifprediction.label==sent:correct+=1print("{}inputs predicted.{}correct; ratio{}".format(len(test),correct,correct/len(test)))500 inputs predicted. 418 correct; ratio 0.836LicenseMIT license.