<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Python Clustering with Scikit-learn
(C) 2017-2019 by Damir Cavar
Download
Step1:  To use the 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
Step2:  We can now apply the clustering algorithm to the datapoints in $X$
Step3:  The output above shows the assignment of datapoints to clusters.
We can use the model now to make predictions about other datapoints
Step4:  We can also output the centroids of the two clusters
Step5:  If we want to use K-Nearest Neighbor in Scikit Learn, we need to import the KNeighborsClassifier from the neighbors submodule
Step6:  We instantiate a KNN-classifier
Step7:  We use the following dataset X and class-vector y
Step8:  We train the classifier
Step9:  We ask the classifier to suggest a class for an unseen vector
Step10:  It can also give us the likelihoods for the probability of a data-point being in any of the classes
Step11:  We might not have a class assignment for a sample set. If we want to use a sample set to find the closes data-point, we could use the KNeighborsClassifier as well. Here is a sample.
Step12:  We can now train for K=1, the nearest neighbor model
Step13:  We could ask for the nearest neighbor of a concrete data-point
Step14:  The returned result [[0.5]] and [[2]] means that nearest neighbor is the third sample in samples and that the distance between the two is 0.5. One can also query for the distance of multiple data-points. In this case the output of the distance is supressed
<ASSISTANT_TASK:>
Python Code:
import numpy
X = numpy.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
Explanation: Python Clustering with Scikit-learn
(C) 2017-2019 by Damir Cavar
Download: This and various other Jupyter notebooks are available from my GitHub repo.
License: Creative Commons Attribution-ShareAlike 4.0 International License (CA BY-SA 4.0)
This is a tutorial related to the discussion of clustering in the textbook Machine Learning: The Art and Science of Algorithms that Make Sense of Data by Peter Flach.
This tutorial was developed as part of my course material for the course Machine Learning for Computational Linguistics in the Computational Linguistics Program of the Department of Linguistics at Indiana University.
K-means Clustering
We will use the array objects from the Python module numpy:
End of explanation
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=2, random_state=0)
Explanation: To use the 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:
End of explanation
kmeans.fit(X)
print(kmeans.labels_)
Explanation: We can now apply the clustering algorithm to the datapoints in $X$:
End of explanation
print(kmeans.predict([[0, 0], [4, 4]]))
Explanation: The output above shows the assignment of datapoints to clusters.
We can use the model now to make predictions about other datapoints:
End of explanation
print(kmeans.cluster_centers_)
Explanation: We can also output the centroids of the two clusters:
End of explanation
from sklearn.neighbors import KNeighborsClassifier
Explanation: If we want to use K-Nearest Neighbor in Scikit Learn, we need to import the KNeighborsClassifier from the neighbors submodule:
End of explanation
KNNClassifier = KNeighborsClassifier(n_neighbors = 3)
Explanation: We instantiate a KNN-classifier:
End of explanation
X = [[0, 1], [1, 1], [2, 4], [3, 4]]
y = [0, 0, 1, 1]
Explanation: We use the following dataset X and class-vector y:
End of explanation
KNNClassifier.fit(X, y)
Explanation: We train the classifier:
End of explanation
print(KNNClassifier.predict([[1.1, 0.9]]))
Explanation: We ask the classifier to suggest a class for an unseen vector:
End of explanation
print(KNNClassifier.predict_proba([[2.9, 3.1]]))
Explanation: It can also give us the likelihoods for the probability of a data-point being in any of the classes:
End of explanation
samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
Explanation: We might not have a class assignment for a sample set. If we want to use a sample set to find the closes data-point, we could use the KNeighborsClassifier as well. Here is a sample.
End of explanation
from sklearn.neighbors import NearestNeighbors
KNNClassifier = NearestNeighbors(n_neighbors=1)
KNNClassifier.fit(samples)
Explanation: We can now train for K=1, the nearest neighbor model:
End of explanation
print(KNNClassifier.kneighbors([[1., 1., 1.]]))
Explanation: We could ask for the nearest neighbor of a concrete data-point:
End of explanation
X = [[0., 1., 0.], [1., 0., 1.]]
KNNClassifier.kneighbors(X, return_distance=False)
Explanation: The returned result [[0.5]] and [[2]] means that nearest neighbor is the third sample in samples and that the distance between the two is 0.5. One can also query for the distance of multiple data-points. In this case the output of the distance is supressed:
End of explanation
<END_TASK>