SHARE
TWEET

Untitled

a guest Nov 22nd, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. for num_training in train_data_sizes:
  3.  
  4.     try:
  5.         del X_train
  6.     except:
  7.         pass
  8.  
  9.     try:
  10.         del y_train
  11.     except:
  12.         pass
  13.  
  14.     try:
  15.         del X_test
  16.     except:
  17.         pass
  18.  
  19.     try:
  20.         del y_test
  21.     except:
  22.         pass
  23.  
  24.     mask = np.arange(num_training,dtype=int)
  25.     X_train = X_train_orig[mask]
  26.     y_train = y_train_orig[mask]
  27.  
  28.     num_test = num_training / 10
  29.     mask = np.arange(num_test,dtype=int)
  30.     X_test = X_test_orig[mask]
  31.     y_test = y_test_orig[mask]
  32.    
  33.     # Reshape the image data into rows
  34.     X_train = np.reshape(X_train, (X_train.shape[0], -1))
  35.     X_test = np.reshape(X_test, (X_test.shape[0], -1))
  36.    
  37.     classifier = KNearestNeighbor()
  38.     classifier.train(X_train, y_train)
  39.    
  40.    
  41.     num_folds = 5
  42.     k_choices = [1, 3, 5, 8, 10, 12, 15, 20, 50, 100]
  43.  
  44.     X_train_folds = []
  45.     y_train_folds = []
  46.  
  47.     X_train_folds = np.array_split(X_train, num_folds)
  48.     y_train_folds = np.array_split(y_train, num_folds)
  49.  
  50.     k_to_accuracies = {}
  51.  
  52.     for k in k_choices:
  53.         k_to_accuracies[k] = []
  54.         for i in range(num_folds):
  55.             # prepare training data for the current fold
  56.             X_train_fold = np.concatenate([ fold for j, fold in enumerate(X_train_folds) if i != j ])
  57.             y_train_fold = np.concatenate([ fold for j, fold in enumerate(y_train_folds) if i != j ])
  58.  
  59.             # use of k-nearest-neighbor algorithm
  60.             classifier.train(X_train_fold, y_train_fold)
  61.             y_pred_fold = classifier.predict(X_train_folds[i], k=k, num_loops=0)
  62.  
  63.             # Compute the fraction of correctly predicted examples
  64.             num_correct = np.sum(y_pred_fold == y_train_folds[i])
  65.             accuracy = float(num_correct) / X_train_folds[i].shape[0]
  66.             k_to_accuracies[k].append(accuracy)
  67.    
  68.     accuracies_mean = np.array([np.mean(v) for k,v in sorted(k_to_accuracies.items())])
  69.     accuracies_std = np.array([np.std(v) for k,v in sorted(k_to_accuracies.items())])
  70.  
  71.     best_k = k_choices[accuracies_mean.argmax()]
  72.  
  73.     classifier = KNearestNeighbor()
  74.     classifier.train(X_train, y_train)
  75.     y_test_pred = classifier.predict(X_test, k=best_k)
  76.  
  77.     # Compute and display the accuracy
  78.     num_correct = np.sum(y_test_pred == y_test)
  79.     accuracy = float(num_correct) / num_test
  80.     num_train_to_accuracy[num_training] = accuracy
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top