In [1]:
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cross_validation import StratifiedKFold

import numpy as np


/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.py:43: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)

In [2]:
mc = ["Human machine interface for lab abc computer applications",
              "A survey of user opinion of computer system response time",
              "The EPS user interface management system",
              "System and human system engineering testing of EPS",
              "Relation of user perceived response time to error measurement",
              "The generation of random binary unordered trees",
              "The intersection graph of paths in trees",
              "Graph minors IV Widths of trees and well quasi ordering",
              "Graph minors A survey"]

In [3]:
def multiclass_matthews_corrcoef(y_true,y_pred):
    cov_mat = np.cov(y_true,y_pred)
    mcc = cov_mat[0][1]/np.sqrt(cov_mat[0][0]*cov_mat[1][1])
    return mcc

In [4]:
def split_mc_corpus(corpus):
    words = corpus
    return [word for word in words]

bow_transformer = CountVectorizer(analyzer=split_mc_corpus).fit(mc)

messages_bow = bow_transformer.transform(mc)
tfidf_transformer = TfidfTransformer().fit(messages_bow)

vectors = tfidf_transformer.transform(messages_bow)
klasses = np.array([1,1,1,2,2,2,3,3,3])

In [14]:
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cross_validation import KFold

kf = StratifiedKFold(klasses, n_folds=2)
knn = KNeighborsClassifier(n_neighbors=2)


mccs = []
for train, test in kf:
    knn.fit(vectors[train],klasses[train])
    preds = [knn.predict(vectors[t])[0] for t in test]
    mccs.append(multiclass_matthews_corrcoef(klasses[test],preds))
    
print "Mean MCC", np.mean(mccs)


Mean MCC -0.433012701892

In [10]:
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn import cross_validation


X = vectors
y = klasses
skf = StratifiedKFold(n_folds=2)
skf.get_n_splits(X, y)

for train_index, test_index in skf.split(X, y):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]

mccs = []
classifiers = {'KNN' : KNeighborsClassifier(1)               
               }

for name, clf in classifiers.items():    
    clf.fit(X_test,y_test)
    preds = [clf.predict(X_test)[t[0]] for t in enumerate(X_test)]
    mccs.append(multiclass_matthews_corrcoef(y_test,preds))    
    print name, "MCC: %0.3f"% np.mean(mccs)
    mccs = []


KNN MCC: 1.000

In [ ]: