K-NN sample implementation
For this simple implementation of the K-NN method, we will use the NumPy and Matplotlib libraries. Also, as we will be generating a synthetic dataset for better comprehension, we will use the make_blobs
method from scikit-learn, which will generate well-defined and separated groups of information so we have a sure reference for our implementation.
Importing the required libraries:
importnumpyasnp importmatplotlib importmatplotlib.pyplotasplt fromsklearn.datasets.samples_generatorimportmake_blobs %matplotlib inline
So, it's time to generate the data samples for this example. The parameters of make_blobs
are the number of samples, the number of features or dimensions, the quantity of centers or groups, whether the samples have to be shuffled, and the standard deviation of the cluster, to control how dispersed the group samples are:
data,features=make_blobs(n_samples=100,n_features=2,centers=4, shuffle=True, cluster_std=0.8) fig,ax=plt...