コード例 #1
0
def test_KNN_predict_labels_shape(sample_train, sample_test):
    Xtrain, ytrain = sample_train(count=40)
    Xtest, ytest   = sample_test(count=10)

    Xtrain = np.reshape(Xtrain, (Xtrain.shape[0], -1))
    Xtest = np.reshape(Xtest, (Xtest.shape[0], -1))

    knn = KNearestNeighbor()
    knn.train(Xtrain,ytrain)

    dist_no  = knn.compute_distances_no_loops(Xtest)
    assert knn.predict_labels(dist_no, k=1).shape == ytest.shape
    assert knn.predict_labels(dist_no, k=2).shape == ytest.shape
    assert knn.predict_labels(dist_no, k=3).shape == ytest.shape
    assert knn.predict_labels(dist_no, k=4).shape == ytest.shape
コード例 #2
0
ファイル: knn.py プロジェクト: zmzmhhhxyj/CS231n
# Remember that training a kNN classifier is a noop:
# the Classifier simply remembers the data and does no further processing
classifier = KNearestNeighbor()
classifier.train(X_train, y_train)
"""
dists = classifier.compute_distances_two_loops(X_test)
pickle.dump(dists,open(r"D:\python\CS231n\assignment1\tmp.txt","wb"))
print(dists.shape)
print(dists)

plt.imshow(dists, interpolation='none')
plt.show()
"""
with open(r"D:\python\CS231n\assignment1\tmp.txt", "rb") as file:
    dists = pickle.load(file)
y_test_pred = classifier.predict_labels(dists, k=1)

# Compute and print the fraction of correctly predicted examples
""" num_correct = np.sum(y_test_pred == y_test)
accuracy = float(num_correct) / num_test
print('Got %d / %d correct => accuracy: %f' % (num_correct, num_test, accuracy))

y_test_pred = classifier.predict_labels(dists, k=5)
num_correct = np.sum(y_test_pred == y_test)
accuracy = float(num_correct) / num_test
print('Got %d / %d correct => accuracy: %f' % (num_correct, num_test, accuracy))
 """
"""
dists_one = classifier.compute_distances_one_loop(X_test)
pickle.dump(dists,open(r"D:\python\CS231n\assignment1\tmp_one.txt","wb"))
"""
コード例 #3
0
classifier.train(X_train, y_train)

# Open cs231n/classifiers/k_nearest_neighbor.py and implement
# compute_distances_two_loops.

# Test your implementation:
dists = classifier.compute_distances_two_loops(X_test)

# We can visualize the distance matrix: each row is a single test example and
# its distances to training examples
plt.imshow(dists, interpolation='none')
plt.show()

# Now implement the function predict_labels and run the code below:
# We use k = 1 (which is Nearest Neighbor).
y_test_pred = classifier.predict_labels(dists, k=1)

# Compute and print the fraction of correctly predicted examples
num_correct = np.sum(y_test_pred == y_test)
accuracy = float(num_correct) / num_test
print('Got %d / %d correct => accuracy: %f' %
      (num_correct, num_test, accuracy))

y_test_pred = classifier.predict_labels(dists, k=5)
num_correct = np.sum(y_test_pred == y_test)
accuracy = float(num_correct) / num_test
print('Got %d / %d correct => accuracy: %f' %
      (num_correct, num_test, accuracy))

# Now lets speed up distance matrix computation by using partial vectorization
# with one loop. Implement the function compute_distances_one_loop and run the
コード例 #4
0
#%%
# We can visualize the distance matrix: each row is a single test example and
# its distances to training examples
plt.figure()
plt.subplot(2, 1, 1)
plt.imshow(dists, interpolation='none')
plt.ylabel('one loop')
plt.show()
plt.subplot(2, 1, 2)
plt.imshow(dists, interpolation='none')
plt.show()
plt.ylabel('no loop')
#%%
# Now implement the function predict_labels and run the code below:
# We use k = 1 (which is Nearest Neighbor).
y_test_pred = classifier.predict_labels(dists, k=5)

# Compute and print the fraction of correctly predicted examples
num_correct = np.sum(y_test_pred == y_test)
accuracy = float(num_correct) / num_test
print('Got %d / %d correct => accuracy: %f' %
      (num_correct, num_test, accuracy))

#%%

num_folds = 5
k_choices = [1, 3, 5, 8, 10, 12, 15, 20, 50, 100]

X_train_folds = []
y_train_folds = []
################################################################################