KNN Alogorithm | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

KNN Alogorithm

Is there anyone have any idea with this function in Python? def test_knn(train_x, train_y, test_x, num_nn): return None def main(): # Read the data file szDatasetPath = './letter-recognition.data' # Put this file in the same place as this script listClasses = [] listAttrs = [] with open(szDatasetPath) as csvFile: csvReader = csv.reader(csvFile, delimiter=',') for row in csvReader: listClasses.append(row[0]) listAttrs.append(list(map(float, row[1:]))) # Generate the mapping from class name to integer IDs mapCls2Int = dict([(y, x) for x, y in enumerate(sorted(set(listClasses)))]) # Store the dataset with numpy array dataX = np.array(listAttrs) dataY = np.array([mapCls2Int[cls] for cls in listClasses]) # Split the dataset as the training set and test set nNumTrainingExamples = 15000 trainX = dataX[:nNumTrainingExamples, :] trainY = dataY[:nNumTrainingExamples] testX = dataX[nNumTrainingExamples:, :] testY = dataY[nNumTrainingExamples:]

27th Sep 2019, 6:16 PM
Tuyen Pham
Tuyen Pham - avatar
1 Answer
0
Definition:- K Nearest Neighbour(KNN) is a simple algorithm that stores all the available cases and classifies the new data or case based on a similarity measure. It is mostly used to classifies a data point based on how its neighbours are classified. Now coming to your question, the function above simply does data preprocessing. It reads data from csv file and converted it to some numpy arrays and split based on number of records needed, which then may be passed to knn algorithm. It may not be possible to say exactly what it does without looking into data.
12th Feb 2020, 11:37 AM
Dorepalli T N S Srikanth
Dorepalli T N S Srikanth - avatar