Skip to content

Instantly share code, notes, and snippets.

Created Sep 29, 2011
What would you like to do?
test code & dataset for scikit-learn issue #365
code demonstrating the problem seen in issue #365
to run the example:
tar -zxvf data.tgz
import numpy as np
from operator import itemgetter
from sklearn.feature_extraction.text import Vectorizer
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_files
data_train = load_files('data_train')
data_test = load_files('data_test')
categories = data_train.target_names
# split a training set and a test set
y_train, y_test =,
vectorizer = Vectorizer()
X_train = vectorizer.fit_transform(
X_test = vectorizer.transform(
vocabulary = np.array([t for t, i in sorted(vectorizer.vocabulary.iteritems(),
knnfitted = KNeighborsClassifier(n_neighbors=1000,
algorithm='brute').fit(X_train, y_train)
pred = knnfitted.predict(X_test)
print 1.0 * sum(pred) / len(pred)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment