{"id":15891,"date":"2023-01-06T22:19:05","date_gmt":"2023-01-07T03:19:05","guid":{"rendered":"https:\/\/panamahitek.com\/probando-el-clasificador-k-nearest-neighbors-con-el-dataset-mnist\/"},"modified":"2023-10-17T18:20:51","modified_gmt":"2023-10-17T23:20:51","slug":"testing-knn-algorithm-in-machine-learning","status":"publish","type":"post","link":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/","title":{"rendered":"Testing KNN algorithm in Machine Learning"},"content":{"rendered":"<p style=\"text-align: justify;\">One of the most popular classification algorithms is the KNN algorithm in Machine Learning. The <strong>K-Nearest Neighbors<\/strong> (KNN) classifier is a simple and effective method for image and general data classification. In this article, we will explore the use of the KNN classifier on the well-known MNIST dataset of handwritten digits.<\/p>\n<p style=\"text-align: justify;\">We will use the scikit-learn library in Python to train and evaluate the classifier, and we will conduct a hyperparameter search to find the best <a href=\"https:\/\/panamahitek.com\/optimizacion-de-hiperparametros-machine-learning\/\">combination of parameters for the KNN model<\/a>. By the end of this article, you will understand how to use the KNN classifier for image classification and how to optimize its performance through hyperparameter tuning.<\/p>\n<h5><strong>Features of the K-Nearest Neighbor classifier<\/strong><\/h5>\n<p style=\"text-align: justify;\">The <strong>K-Nearest Neighbors<\/strong> algorithm is a non-parametric classification method that assigns a sample a class based on the majority of the classes of its nearest neighbors in the feature space. In other words, a sample is classified according to the most common class of the <strong>K<\/strong> samples closest to it in the feature space. KNN is commonly used in classification and regression problems.<\/p>\n<p style=\"text-align: justify;\">The value of <strong>k<\/strong> is determined by the user and can affect the complexity and accuracy of the model. The parameter <strong>&#8220;k&#8221;<\/strong> is the number of nearest neighbors that are used to make a prediction. For example, if <strong>k=3<\/strong>, then the algorithm will consider the 3 nearest neighbors to a sample and make a prediction based on their classes. The value of <strong>k<\/strong> can be chosen by the user and can affect the complexity and accuracy of the model.<\/p>\n<p style=\"text-align: justify;\">A smaller value of <strong>k<\/strong> can lead to a model with lower bias but higher variance, while a larger value of <strong>k<\/strong> can lead to a model with higher bias but lower variance. In general, it is a good idea to test several values of <strong>k<\/strong> to see which gives the best performance on training and test data. It is also common to use odd values of <strong>k<\/strong> to avoid ties in class prediction.<\/p>\n<p style=\"text-align: justify;\">One of the benefits of the K-Nearest Neighbors algorithm is that it has a low training time, as it does not require a complex model to fit the training data. Instead, it simply stores the training data and makes predictions based on the K nearest neighbors to a sample when asked to classify new data. This means that the training time is essentially constant, regardless of the size of the training dataset.<\/p>\n<p style=\"text-align: justify;\">Despite its simplicity, KNN can still achieve high accuracy in many tasks, especially when the number of features is low and the classes are well separated. It is also a robust algorithm that is not sensitive to the scale of features or the presence of noise in the data. However, it can be sensitive to the value of K, and its performance may be compromised when the number of features is high or when the classes are not well separated.<\/p>\n<h5><strong>How does the KNN algorithm work?<\/strong><\/h5>\n<p style=\"text-align: justify;\">To illustrate how KNN works, I&#8217;m going to present some images with a hypothetical data classification scenario. Let&#8217;s assume we have the following dataset on some plane:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-15118 aligncenter\" src=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-18.png\" width=\"568\" height=\"302\" srcset=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-18.png 1599w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-18-300x159.png 300w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-18-1024x544.png 1024w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-18-768x408.png 768w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-18-1536x816.png 1536w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-18-696x370.png 696w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-18-1068x567.png 1068w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-18-791x420.png 791w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/p>\n<p style=\"text-align: justify;\">Each &#8220;x&#8221; represents a data point in the spatial plane. Now let&#8217;s assume that I, as a human, am asked to classify the data into groups. I think we can all agree that there are two main data groups, roughly separated by the horizontal plane&#8217;s midpoint.<\/p>\n<figure id=\"attachment_15120\" aria-describedby=\"caption-attachment-15120\" style=\"width: 567px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-15120\" src=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-19.png\" alt=\"K-Nearest Neighbors (knn)\" width=\"567\" height=\"298\" srcset=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-19.png 1040w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-19-300x158.png 300w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-19-1024x539.png 1024w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-19-768x404.png 768w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-19-696x366.png 696w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-19-799x420.png 799w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><figcaption id=\"caption-attachment-15120\" class=\"wp-caption-text\">Class A and Class B<\/figcaption><\/figure>\n<p style=\"text-align: justify;\">As we can see in the image, we have separated the data into two groups. In Machine Learning, when we deal with datasets, we don&#8217;t talk about groups or sets but classes. And each class can represent any object in the real world. In my case, I&#8217;ll consider that objects of &#8220;class A&#8221; are squares and objects of &#8220;class B&#8221; are circles.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-15122\" src=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-20.png\" alt=\"K-Nearest Neighbors (knn)\" width=\"562\" height=\"302\" srcset=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-20.png 1031w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-20-300x161.png 300w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-20-1024x550.png 1024w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-20-768x413.png 768w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-20-696x374.png 696w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-20-782x420.png 782w\" sizes=\"auto, (max-width: 562px) 100vw, 562px\" \/><\/p>\n<p style=\"text-align: justify;\">The moment I, a human, decided to classify the dataset&#8217;s objects into two classes and assigned a class to each sample, I constructed a training dataset. With this dataset, we can train a Machine Learning classifier to classify new data we haven&#8217;t seen yet.<\/p>\n<figure id=\"attachment_15124\" aria-describedby=\"caption-attachment-15124\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-15124\" src=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-21.png\" alt=\"K-Nearest Neighbors (knn)\" width=\"600\" height=\"318\" srcset=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-21.png 1040w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-21-300x159.png 300w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-21-1024x544.png 1024w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-21-768x408.png 768w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-21-696x369.png 696w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-21-791x420.png 791w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption id=\"caption-attachment-15124\" class=\"wp-caption-text\">New sample to classify<\/figcaption><\/figure>\n<p>For example, let&#8217;s assume that after training our classification algorithm, we&#8217;re asked to classify a new data point:<\/p>\n<p style=\"text-align: justify;\">We, as humans, can identify that this new data point is a square. That is, it&#8217;s surrounded by other squares. To classify it as a circle, it should be further to the right of the plane, where the other circles are. It&#8217;s not that hard, right?<\/p>\n<p style=\"text-align: justify;\">But how do we make a computer program classify this data correctly? How do we make the computer &#8220;think&#8221; like us humans? Well, the truth is, we won&#8217;t make the computer think. But we can teach it to discriminate between squares and circles using a Machine Learning algorithm.<\/p>\n<p style=\"text-align: justify;\">Each algorithm available today solves this problem differently. In KNN&#8217;s case, we&#8217;ll identify the &#8220;k&#8221; nearest neighbors to the data point we want to classify. This way, the computer can determine the data point&#8217;s proximity to the two available classes and decide whether it belongs to one or the other.<\/p>\n<figure id=\"attachment_15126\" aria-describedby=\"caption-attachment-15126\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-15126\" src=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-22.png\" alt=\"K-Nearest Neighbors (knn)\" width=\"600\" height=\"314\" srcset=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-22.png 1545w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-22-300x157.png 300w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-22-1024x536.png 1024w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-22-768x402.png 768w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-22-1536x804.png 1536w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-22-696x364.png 696w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-22-1068x559.png 1068w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-22-802x420.png 802w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption id=\"caption-attachment-15126\" class=\"wp-caption-text\">Identification of the &#8220;k&#8221; nearest neighbors. In this example, k=3, so 3 neighbors are identified<\/figcaption><\/figure>\n<p style=\"text-align: justify;\">As we can see in the image, the 3 closest &#8220;neighbors&#8221; to the data point we want to classify are squares. Thus, it&#8217;s very likely that the &#8220;x&#8221; in the image represents a square. Note that I used the word &#8220;likely&#8221; because the Machine Learning paradigm is based more on probabilities than certainties.<\/p>\n<p style=\"text-align: justify;\">Since this method is based on checking a data point&#8217;s &#8220;neighbors,&#8221; it&#8217;s logical that the KNN algorithm is named as such. K-Nearest Neighbors means k-closest neighbors. I think KNN is the computational representation of the saying &#8220;Tell me who you&#8217;re with, and I&#8217;ll tell you who you are.&#8221;<\/p>\n<p>What happens when we need to classify data that&#8217;s located between the two classes? Let&#8217;s look at the following image:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-15128\" src=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-23.png\" alt=\"K-Nearest Neighbors (knn)\" width=\"572\" height=\"301\" srcset=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-23.png 1039w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-23-300x158.png 300w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-23-1024x539.png 1024w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-23-768x404.png 768w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-23-696x366.png 696w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-23-798x420.png 798w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/p>\n<p style=\"text-align: justify;\">For the data point shown in the image, it&#8217;s not easy to determine if it belongs to one class or the other. Even I, as a human, am not sure if it&#8217;s a square or a circle. For the computer, it will check the &#8220;k&#8221; nearest neighbors, calculating the distance between the neighbors&#8217; centroids and the new data point to be classified. If we work with <strong>k=3<\/strong>, we&#8217;ll have something like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-15132\" src=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-25.png\" alt=\"K-Nearest Neighbors (knn)\" width=\"593\" height=\"310\" srcset=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-25.png 1541w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-25-300x157.png 300w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-25-1024x535.png 1024w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-25-768x401.png 768w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-25-1536x802.png 1536w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-25-696x364.png 696w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-25-1068x558.png 1068w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-25-804x420.png 804w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/p>\n<p style=\"text-align: justify;\">Clearly, the data point is a square, as two out of its three closest neighbors are squares. If there&#8217;s a tie, where two or more classes have the same number of occurrences among the K nearest neighbors, then the sample can be assigned to the class with the lowest index value.<\/p>\n<p style=\"text-align: justify;\">This classification method is known as majority voting, as the sample&#8217;s class is determined based on the majority class among its neighbors. It&#8217;s a simple and effective method for classification and is easy to understand and implement.<\/p>\n<p style=\"text-align: justify;\">Broadly speaking, this is how the KNN algorithm works. There are other important hyperparameters to consider with this algorithm, besides the number of neighbors considered in classification:<\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>weights<\/strong>: This hyperparameter controls how much weight is given to the nearest neighbors in classification, with options being &#8216;uniform&#8217; (all neighbors have the same weight) or &#8216;distance&#8217; (neighbors have weights proportional to the inverse of their distance).<\/li>\n<li style=\"text-align: justify;\"><strong>algorithm<\/strong>: This hyperparameter controls the algorithm used for the nearest neighbor search. Options include &#8216;brute&#8217;, which uses a brute force search, &#8216;kd_tree&#8217;, which uses a KD-tree data structure for faster search, and &#8216;ball_tree&#8217;, which uses a search tree for faster search.<\/li>\n<li><strong>leaf_size<\/strong>: This hyperparameter controls the number of samples in a KD or ball tree leaf. It can affect the speed of the nearest neighbor search.<\/li>\n<li><strong>p<\/strong>: This hyperparameter controls the distance metric used for the nearest neighbor search. Options include p=1 for Manhattan distance and p=2 for Euclidean distance.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">In general, that&#8217;s what the KNN algorithm is about for classification in Machine Learning.<\/p>\n<h5><strong>Hyperparameter Optimization<\/strong><\/h5>\n<p style=\"text-align: justify;\">My interest in K-Nearest Neighbors arose after my <a href=\"https:\/\/panamahitek.com\/seleccion-de-algoritmos-de-clasificacion-de-machine-learning-en-python\/\">previous publication<\/a> in which we conducted tests with different types of classifiers on the MNIST dataset. KNN achieved a relatively high accuracy (96.82%), although it was lower than other algorithms such as <strong>Support Vector Machine<\/strong> (97.85%) and <strong>Random Forest<\/strong> (96.97%). However, there was one area where KNN proved to be one of the best:<\/p>\n<ul>\n<li><span style=\"font-size: 12px;\"><strong>KNeighborsClassifier<\/strong> | training time: <strong>0.18 seconds<\/strong>, testing time: 6.30 seconds, 96.64%<\/span><\/li>\n<li><span style=\"font-size: 12px;\"><strong>RandomForestClassifier<\/strong> | training time: 46.06 seconds, testing time: 0.43 seconds, 96.97%<\/span><\/li>\n<li><span style=\"font-size: 12px;\"><strong>SVC<\/strong> | training time: 167.80 seconds, testing time: 85.12 seconds, 97.85%<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify;\">The training time is significantly shorter than the algorithms that achieved better performance on MNIST. This piqued my interest in this algorithm, as it&#8217;s possible that with some hyperparameter optimization, better results can be achieved.<\/p>\n<p style=\"text-align: justify;\">So I wrote the following script, based on our publication about <a href=\"https:\/\/panamahitek.com\/optimizacion-de-hiperparametros-machine-learning\/\">hyperparameter optimization<\/a>:<\/p>\n<pre class=\"lang:python decode:true\">from sklearn.model_selection import GridSearchCV\r\nimport pandas as pd\r\nimport numpy as np\r\nfrom sklearn.neighbors import KNeighborsClassifier\r\n\r\n\"\"\"\r\nHere I set the global variables, which\r\nwill be used to test the computing time\r\nfor both training and testing\r\n\"\"\"\r\ndef loadDataset(fileName, samples):  # A function for loading the data from a dataset (MNIST)\r\n    x = []  # Array for data inputs\r\n    y = []  # Array for labels (expected outputs)\r\n    train_data = pd.read_csv(fileName)  # Data has to be stored in a CSV file, separated by commas\r\n    y = np.array(train_data.iloc[0:samples, 0])  # Labels column\r\n    x = np.array(train_data.iloc[0:samples, 1:]) \/ 255  # Division by 255 is used for data normalization\r\n    return x, y\r\n\r\n# Define the grid of hyperparameter values to search over\r\nparam_grid = {\r\n    'n_neighbors': [3, 5, 7, 9, 11],\r\n    'weights': ['uniform', 'distance'],\r\n    'leaf_size': [5, 10, 20, 30, 35, 40, 45, 50]\r\n}\r\n\r\n# Define the scoring metric to use for evaluation\r\nscoring = 'accuracy'\r\n\r\n# Create a GridSearchCV object\r\nclf = KNeighborsClassifier()\r\ngrid_search = GridSearchCV(estimator=clf, param_grid=param_grid, scoring=scoring, cv=2, verbose=2)\r\n\r\ntrain_x,train_y = loadDataset(\"..\/..\/..\/..\/datasets\/mnist\/mnist_train.csv\",50000)\r\ntest_x,test_y = loadDataset(\"..\/..\/..\/..\/datasets\/mnist\/mnist_test.csv\",10000)\r\n\r\n# Fit the grid search object to the training data\r\ngrid_search.fit(train_x, train_y)\r\n\r\n# Print the best hyperparameters and the best score\r\nprint(\"Best hyperparameters:\", grid_search.best_params_)\r\nprint(\"Best score:\", grid_search.best_score_)\r\n\r\n# Re-train the model with the best hyperparameters\r\nbest_clf = grid_search.best_estimator_\r\nbest_clf.fit(train_x, train_y)\r\n\r\n# Test the model with the best hyperparameters on the testing data\r\naccuracy = best_clf.score(test_x, test_y)\r\nprint(\"Testing accuracy:\", accuracy)<\/pre>\n<p>You can download this algorithm from our <a href=\"https:\/\/github.com\/PanamaHitek\/machine_learning_tutorials\/blob\/main\/python\/machine_learning_examples\/sklearn\/kneighbor_example\/main.py\">Github repository<\/a>. The outcome of this algorithm should look something like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-15134 size-full\" src=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-26.png\" alt=\"mnist result\" width=\"823\" height=\"252\" srcset=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-26.png 823w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-26-300x92.png 300w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-26-768x235.png 768w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-26-696x213.png 696w\" sizes=\"auto, (max-width: 823px) 100vw, 823px\" \/><\/p>\n<p style=\"text-align: justify;\">After testing various parameter combinations, we achieved a 96.92% accuracy in MNIST classification, using the parameters shown in the image.<\/p>\n<p style=\"text-align: justify;\">The time each test takes is relatively short, about 9 seconds. I tested up to 200 combinations, and as I decreased the number of neighbors, I obtained better results. It is what it is.<\/p>\n<h5><strong>Conclusions<\/strong><\/h5>\n<p style=\"text-align: justify;\">In summary, in this post, we&#8217;ve explored the use of the K-Nearest Neighbors algorithm for image classification on the MNIST dataset. We&#8217;ve seen that KNN is a non-parametric classification method that has a short training time and can achieve high accuracy in many tasks. We&#8217;ve also seen how to use the scikit-learn library in Python to train and evaluate a KNN model and conduct a hyperparameter search to find the best parameter combination.<\/p>\n<p style=\"text-align: justify;\">I hope you enjoyed reading this post and learned something new about using the K-Nearest Neighbors algorithm for image classification. If you have any questions or comments, please feel free to leave a message below. Thank you for reading!<\/p>\n<p>Let me know if you need any further assistance!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most popular classification algorithms is the KNN algorithm in Machine Learning. The K-Nearest Neighbors (KNN) classifier is a simple and effective method for image and general data classification. In this article, we will explore the use of the KNN classifier on the well-known MNIST dataset of handwritten digits. We will use the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":15137,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2955,1985],"tags":[3145,3110,3115,3117,3134,3130,3129,3135,3143,3119,3127,3142,3137,2966,3144,3121,3132,3133,1990,3128,3136,3138,3124,3141,3139,3140,3131],"class_list":{"0":"post-15891","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-machine-learning-en","8":"category-python-en","9":"tag-accuracy-en","10":"tag-busqueda-de-hiperparametros-en","11":"tag-combinacion-de-parametros-en","12":"tag-conjuntos-de-datos-con-muchas-caracteristicas-en","13":"tag-datasets-with-many-features","14":"tag-features","15":"tag-hyperparameter-search","16":"tag-hyperparameters","17":"tag-image-classification-en","18":"tag-knn-en","19":"tag-knn-algorithm","20":"tag-knn-algorithm-in-machine-learning","21":"tag-knn-model","22":"tag-machine-learning-en","23":"tag-mnist-dataset-en","24":"tag-modelo-knn-en","25":"tag-non-parametric-classification","26":"tag-parameter-combination","27":"tag-python-en","28":"tag-robust-algorithm","29":"tag-scikit-learn-library","30":"tag-sensitivity-to-the-value-of-k","31":"tag-sklearn-en","32":"tag-testing-knn-algorithm-in-machine-learning","33":"tag-training-time","34":"tag-value-of-k","35":"tag-well-separated-classes"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Testing KNN algorithm in Machine Learning - Panama Hitek<\/title>\n<meta name=\"description\" content=\"In this post, we will learn how to use the K-Nearest Neighbors (KNN) algorithm in Machine Learning for image classification\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Testing KNN algorithm in Machine Learning - Panama Hitek\" \/>\n<meta property=\"og:description\" content=\"In this post, we will learn how to use the K-Nearest Neighbors (KNN) algorithm in Machine Learning for image classification\" \/>\n<meta property=\"og:url\" content=\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/\" \/>\n<meta property=\"og:site_name\" content=\"Panama Hitek\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/antony.garcia.gonzalez\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-07T03:19:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-17T23:20:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/K_NEAREST_NEIGHBORS.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Antony Garc\u00eda Gonz\u00e1lez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/antony_garcia_g\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Antony Garc\u00eda Gonz\u00e1lez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/\"},\"author\":{\"name\":\"Antony Garc\u00eda Gonz\u00e1lez\",\"@id\":\"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e\"},\"headline\":\"Testing KNN algorithm in Machine Learning\",\"datePublished\":\"2023-01-07T03:19:05+00:00\",\"dateModified\":\"2023-10-17T23:20:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/\"},\"wordCount\":1687,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/K_NEAREST_NEIGHBORS.jpg\",\"keywords\":[\"accuracy.\",\"b\u00fasqueda de hiperpar\u00e1metros\",\"combinaci\u00f3n de par\u00e1metros\",\"conjuntos de datos con muchas caracter\u00edsticas\",\"datasets with many features\",\"features\",\"hyperparameter search\",\"hyperparameters\",\"image classification\",\"knn\",\"KNN algorithm\",\"KNN algorithm in Machine Learning\",\"KNN model\",\"machine learning\",\"MNIST dataset\",\"modelo KNN\",\"non-parametric classification\",\"parameter combination\",\"Python\",\"robust algorithm\",\"scikit-learn library\",\"sensitivity to the value of K\",\"Sklearn\",\"Testing KNN algorithm in Machine Learning\",\"training time\",\"value of K.\",\"well-separated classes\"],\"articleSection\":[\"Machine Learning\",\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/\",\"url\":\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/\",\"name\":\"Testing KNN algorithm in Machine Learning - Panama Hitek\",\"isPartOf\":{\"@id\":\"https:\/\/panamahitek.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/K_NEAREST_NEIGHBORS.jpg\",\"datePublished\":\"2023-01-07T03:19:05+00:00\",\"dateModified\":\"2023-10-17T23:20:51+00:00\",\"author\":{\"@id\":\"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e\"},\"description\":\"In this post, we will learn how to use the K-Nearest Neighbors (KNN) algorithm in Machine Learning for image classification\",\"breadcrumb\":{\"@id\":\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#primaryimage\",\"url\":\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/K_NEAREST_NEIGHBORS.jpg\",\"contentUrl\":\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/K_NEAREST_NEIGHBORS.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Portada\",\"item\":\"https:\/\/panamahitek.com\/en\/home\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Testing KNN algorithm in Machine Learning\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/panamahitek.com\/en\/#website\",\"url\":\"https:\/\/panamahitek.com\/en\/\",\"name\":\"Panama Hitek\",\"description\":\"Conocimiento libre, de Panam\u00e1 para el mundo\",\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e\",\"name\":\"Antony Garc\u00eda Gonz\u00e1lez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/panamahitek.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d7a10397454e66de561db7fc2ed37dd2bd115478c378b22eaafd410de973dfd1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d7a10397454e66de561db7fc2ed37dd2bd115478c378b22eaafd410de973dfd1?s=96&d=mm&r=g\",\"caption\":\"Antony Garc\u00eda Gonz\u00e1lez\"},\"description\":\"Ingeniero Electromec\u00e1nico, egresado de la Universidad Tecnol\u00f3gica de Panam\u00e1. Miembro fundador de Panama Hitek. Entusiasta de la electr\u00f3nica y la programaci\u00f3n.\",\"sameAs\":[\"https:\/\/www.facebook.com\/antony.garcia.gonzalez\",\"https:\/\/www.instagram.com\/antony.garcia.gonzalez\",\"https:\/\/www.linkedin.com\/in\/antony-garcia-gonzalez-96539461\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/antony_garcia_g\"],\"url\":\"https:\/\/panamahitek.com\/en\/author\/agarciag-2\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Testing KNN algorithm in Machine Learning - Panama Hitek","description":"In this post, we will learn how to use the K-Nearest Neighbors (KNN) algorithm in Machine Learning for image classification","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/","og_locale":"en_US","og_type":"article","og_title":"Testing KNN algorithm in Machine Learning - Panama Hitek","og_description":"In this post, we will learn how to use the K-Nearest Neighbors (KNN) algorithm in Machine Learning for image classification","og_url":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/","og_site_name":"Panama Hitek","article_author":"https:\/\/www.facebook.com\/antony.garcia.gonzalez","article_published_time":"2023-01-07T03:19:05+00:00","article_modified_time":"2023-10-17T23:20:51+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/K_NEAREST_NEIGHBORS.jpg","type":"image\/jpeg"}],"author":"Antony Garc\u00eda Gonz\u00e1lez","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/antony_garcia_g","twitter_misc":{"Written by":"Antony Garc\u00eda Gonz\u00e1lez","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#article","isPartOf":{"@id":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/"},"author":{"name":"Antony Garc\u00eda Gonz\u00e1lez","@id":"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e"},"headline":"Testing KNN algorithm in Machine Learning","datePublished":"2023-01-07T03:19:05+00:00","dateModified":"2023-10-17T23:20:51+00:00","mainEntityOfPage":{"@id":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/"},"wordCount":1687,"commentCount":0,"image":{"@id":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#primaryimage"},"thumbnailUrl":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/K_NEAREST_NEIGHBORS.jpg","keywords":["accuracy.","b\u00fasqueda de hiperpar\u00e1metros","combinaci\u00f3n de par\u00e1metros","conjuntos de datos con muchas caracter\u00edsticas","datasets with many features","features","hyperparameter search","hyperparameters","image classification","knn","KNN algorithm","KNN algorithm in Machine Learning","KNN model","machine learning","MNIST dataset","modelo KNN","non-parametric classification","parameter combination","Python","robust algorithm","scikit-learn library","sensitivity to the value of K","Sklearn","Testing KNN algorithm in Machine Learning","training time","value of K.","well-separated classes"],"articleSection":["Machine Learning","Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/","url":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/","name":"Testing KNN algorithm in Machine Learning - Panama Hitek","isPartOf":{"@id":"https:\/\/panamahitek.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#primaryimage"},"image":{"@id":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#primaryimage"},"thumbnailUrl":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/K_NEAREST_NEIGHBORS.jpg","datePublished":"2023-01-07T03:19:05+00:00","dateModified":"2023-10-17T23:20:51+00:00","author":{"@id":"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e"},"description":"In this post, we will learn how to use the K-Nearest Neighbors (KNN) algorithm in Machine Learning for image classification","breadcrumb":{"@id":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#primaryimage","url":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/K_NEAREST_NEIGHBORS.jpg","contentUrl":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/K_NEAREST_NEIGHBORS.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/panamahitek.com\/en\/testing-knn-algorithm-in-machine-learning\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Portada","item":"https:\/\/panamahitek.com\/en\/home\/"},{"@type":"ListItem","position":2,"name":"Testing KNN algorithm in Machine Learning"}]},{"@type":"WebSite","@id":"https:\/\/panamahitek.com\/en\/#website","url":"https:\/\/panamahitek.com\/en\/","name":"Panama Hitek","description":"Conocimiento libre, de Panam\u00e1 para el mundo","inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e","name":"Antony Garc\u00eda Gonz\u00e1lez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/panamahitek.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d7a10397454e66de561db7fc2ed37dd2bd115478c378b22eaafd410de973dfd1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d7a10397454e66de561db7fc2ed37dd2bd115478c378b22eaafd410de973dfd1?s=96&d=mm&r=g","caption":"Antony Garc\u00eda Gonz\u00e1lez"},"description":"Ingeniero Electromec\u00e1nico, egresado de la Universidad Tecnol\u00f3gica de Panam\u00e1. Miembro fundador de Panama Hitek. Entusiasta de la electr\u00f3nica y la programaci\u00f3n.","sameAs":["https:\/\/www.facebook.com\/antony.garcia.gonzalez","https:\/\/www.instagram.com\/antony.garcia.gonzalez","https:\/\/www.linkedin.com\/in\/antony-garcia-gonzalez-96539461\/","https:\/\/x.com\/https:\/\/twitter.com\/antony_garcia_g"],"url":"https:\/\/panamahitek.com\/en\/author\/agarciag-2\/"}]}},"jetpack_featured_media_url":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/K_NEAREST_NEIGHBORS.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/posts\/15891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/comments?post=15891"}],"version-history":[{"count":0,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/posts\/15891\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/media\/15137"}],"wp:attachment":[{"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/media?parent=15891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/categories?post=15891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/tags?post=15891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}