machine learning - Tensorflow KNN : How can we assign the K parameter for defining number of neighbors in KNN? -


i have started working on machine learning project using k-nearest-neighbors method on python tensorflow library. have no experience working tensorflow tools, found code in github , modified data.

my dataset this:

2,2,2,2,0,0,3 2,2,2,2,0,1,0 2,2,2,4,2,2,1 ... 2,2,2,4,2,0,0 

and code works fine:

import tensorflow tf import numpy np  # whole dataset => 1428 samples dataset = 'car-eval-data-1.csv' # samples train, remaining test samples = 1300 reader = np.loadtxt(open(dataset, "rb"), delimiter=",", skiprows=1, dtype=np.int32)  train_x, train_y = reader[:samples,:5], reader[:samples,6] test_x, test_y = reader[samples:, :5], reader[samples:, 6]  # placeholder can assign values in future. kind of variable #  v = ("variable type",[none,4])  -- can have multidimensional values here training_values = tf.placeholder("float",[none,len(train_x[0])]) test_values     = tf.placeholder("float",[len(train_x[0])])  # manhattan distance distance = tf.abs(tf.reduce_sum(tf.square(tf.subtract(training_values,test_values)),reduction_indices=1))  prediction = tf.arg_min(distance, 0) init = tf.global_variables_initializer()  accuracy = 0.0  tf.session() sess:     sess.run(init)     # looping through test set compare against training set     in range (len(test_x)):         # tensor flow method prediction near test parameters in training set.         index_in_trainingset = sess.run(prediction, feed_dict={training_values:train_x,test_values:test_x[i]})              print("test %d, , prediction %s, real value %s"%(i,train_y[index_in_trainingset],test_y[i]))         if train_y[index_in_trainingset] == test_y[i]:         # if prediction right accuracy increases.             accuracy += 1. / len(test_x)  print('accuracy -> ', accuracy * 100, ' %') 

the thing not understand if it's knn method there has k parameter defines number of neighbors predicting label each test sample.
how can assign k parameter tune number of nearest neighbors code?
there way modify code make use of k parameter?

you're right example above not have provision select k-nearest neighbours. in code below, have added ability add such parameter(knn_size) along other corrections

import tensorflow tf import numpy np  # whole dataset => 1428 samples dataset = 'path_to_dataset_csv' knn_size = 1 # samples train, remaining test samples = 1300 reader = np.loadtxt(open(dataset, "rb"), delimiter=",", skiprows=1, dtype=np.int32)  train_x, train_y = reader[:samples,:6], reader[:samples,6] test_x, test_y = reader[samples:, :6], reader[samples:, 6]  # placeholder can assign values in future. kind of variable #  v = ("variable type",[none,4])  -- can have multidimensional values here training_values = tf.placeholder("float",[none, len(train_x[0])]) test_values     = tf.placeholder("float",[len(train_x[0])])   # manhattan distance distance = tf.abs(tf.reduce_sum(tf.square(tf.subtract(training_values,test_values)),reduction_indices=1))  # here, multiply distance -1 reverse magnitude of distances, i.e. largest distance becomes smallest distance # tf.nn.top_k returns top k values , indices, here k controlled parameter knn_size  k_nearest_neighbour_values, k_nearest_neighbour_indices = tf.nn.top_k(tf.scalar_mul(-1,distance),k=knn_size)  #based on indices obtain previous step, locate exact class label set of k closest matches in training data best_training_labels = tf.gather(train_y,k_nearest_neighbour_indices)  if knn_size==1:     prediction = tf.squeeze(best_training_labels) else:     # make our prediction based on class label appears     # tf.unique_with_counts() gives unique values appear in 1-d tensor along indices , counts      values, indices, counts = tf.unique_with_counts(best_training_labels)     # gives index of class label has repeated     max_count_index = tf.argmax(counts,0)     #retrieve required class label     prediction = tf.gather(values,max_count_index)     init = tf.global_variables_initializer()  accuracy = 0.0  tf.session() sess:     sess.run(init)     # looping through test set compare against training set     in range (len(test_x)):           # tensor flow method prediction near test parameters in training set.         prediction_value = sess.run([prediction], feed_dict={training_values:train_x,test_values:test_x[i]})          print("test %d, , prediction %s, real value %s"%(i,prediction_value[0],test_y[i]))         if prediction_value[0] == test_y[i]:         # if prediction right accuracy increases.             accuracy += 1. / len(test_x)  print('accuracy -> ', accuracy * 100, ' %') 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -