CS231n-assignment1 K-fold 交叉验证 python 中字典的用法

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

X_train_folds = []
y_train_folds = []
################################################################################
# TODO:                                                                        #
# Split up the training data into folds. After splitting, X_train_folds and    #
# y_train_folds should each be lists of length num_folds, where                #
# y_train_folds[i] is the label vector for the points in X_train_folds[i].     #
# Hint: Look up the numpy array_split function.                                #
################################################################################
# Your code
X_train_folds = np.array_split(X_train, num_folds)
y_train_folds = np.array_split(y_train, num_folds)

################################################################################
#                                 END OF YOUR CODE                             #
################################################################################

# A dictionary holding the accuracies for different values of k that we find
# when running cross-validation. After running cross-validation,
# k_to_accuracies[k] should be a list of length num_folds giving the different
# accuracy values that we found when using that value of k.
k_to_accuracies = {}

################################################################################
# TODO:                                                                        #
# Perform k-fold cross validation to find the best value of k. For each        #
# possible value of k, run the k-nearest-neighbor algorithm num_folds times,   #
# where in each case you use all but one of the folds as training data and the #
# last fold as a validation set. Store the accuracies for all fold and all     #
# values of k in the k_to_accuracies dictionary.                               #
################################################################################
# Your code
for k in k_choices:
    A = []
    for i in range(num_folds): 
        X_val_k = X_train_folds[i] #validation set
        y_val_k = y_train_folds[i] #validation set       
        X_train_k = np.concatenate(X_train_folds[:i] + X_train_folds[i+1:])
        y_train_k = np.concatenate(y_train_folds[:i] + y_train_folds[i+1:])     
        classifier = KNearestNeighbor()
        classifier.train(X_train_k, y_train_k)
        dists = classifier.compute_distances_no_loops(X_val_k)
        y_val_pred = classifier.predict_labels(dists, k=k)        
        num_correct = np.sum(y_val_pred == y_val_k)
        num_val = X_val_k.shape[0]
        accuracy = float(num_correct) / num_val
        A.append(accuracy)
    k_to_accuracies [k] = A
################################################################################
#                                 END OF YOUR CODE                             #
################################################################################
#print(k_to_accuracies)
# Print out the computed accuracies
for k in sorted(k_to_accuracies):
    for accuracy in k_to_accuracies[k]:
        print('k = %d, accuracy = %f' % (k, accuracy))

 

你可能感兴趣的:(CS231n)