**(C) 2017-2019 by Damir Cavar**

**Download:** This and various other Jupyter notebooks are available from my GitHub repo.

We will use the *array* objects from the Python module *numpy*:

```
In [1]:
```import numpy
X = numpy.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])

*K-means* clustering algorithm from Scikit-learn, we import it and specify the number of clusters (that is the *k*), and the random state to initialize the centroid centers of the clusters. We assume that the data can be grouped into two clusters:

```
In [2]:
```from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=2, random_state=0)

We can now apply the clustering algorithm to the datapoints in $X$:

```
In [3]:
```kmeans.fit(X)
print(kmeans.labels_)

```
```

The output above shows the assignment of datapoints to clusters.

We can use the model now to make predictions about other datapoints:

```
In [4]:
```print(kmeans.predict([[0, 0], [4, 4]]))

```
```

We can also output the centroids of the two clusters:

```
In [5]:
```print(kmeans.cluster_centers_)

```
```

```
In [6]:
```from sklearn.neighbors import KNeighborsClassifier

We instantiate a KNN-classifier:

```
In [7]:
```KNNClassifier = KNeighborsClassifier(n_neighbors = 3)

We use the following dataset X and class-vector y:

```
In [8]:
```X = [[0, 1], [1, 1], [2, 4], [3, 4]]
y = [0, 0, 1, 1]

We train the classifier:

```
In [9]:
```KNNClassifier.fit(X, y)

```
Out[9]:
```

We ask the classifier to suggest a class for an unseen vector:

```
In [10]:
```print(KNNClassifier.predict([[1.1, 0.9]]))

```
```

It can also give us the likelihoods for the probability of a data-point being in any of the classes:

```
In [11]:
```print(KNNClassifier.predict_proba([[2.9, 3.1]]))

```
```

```
In [12]:
```samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]

We can now train for K=1, the nearest neighbor model:

```
In [13]:
```from sklearn.neighbors import NearestNeighbors
KNNClassifier = NearestNeighbors(n_neighbors=1)
KNNClassifier.fit(samples)

```
Out[13]:
```

We could ask for the nearest neighbor of a concrete data-point:

```
In [14]:
```print(KNNClassifier.kneighbors([[1., 1., 1.]]))

```
```

```
In [15]:
```X = [[0., 1., 0.], [1., 0., 1.]]
KNNClassifier.kneighbors(X, return_distance=False)

```
Out[15]:
```