KMeans labels_
时间: 2024-05-09 08:20:05 浏览: 9
KMeans labels_ attribute is an array that contains the cluster labels for each data point in the dataset after clustering. The labels represent the cluster assignment for each observation based on the centroid that is closest to it. The labels are integers ranging from 0 to n_clusters-1, where n_clusters is the number of clusters specified by the user.
For example, if we have a dataset with 100 data points and we perform KMeans clustering with k=5 clusters, the labels_ attribute will be an array of shape (100,) containing integers 0, 1, 2, 3, or 4 indicating the cluster assignment for each data point.
You can access the labels_ attribute of a KMeans object by calling the attribute after fitting the model to the data. For instance:
```
from sklearn.cluster import KMeans
import numpy as np
# generate random data
X = np.random.rand(100, 2)
# instantiate KMeans model
kmeans = KMeans(n_clusters=5)
# fit model to data and obtain labels
kmeans.fit(X)
labels = kmeans.labels_
print(labels)
```
Output:
```
[2 3 3 3 3 2 3 1 1 2 2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
```
In the example above, we generate random data of shape (100, 2), instantiate a KMeans model with 5 clusters, fit the model to the data and obtain the cluster labels using the labels_ attribute.