9951 explained code solutions for 126 technologies


python-scikit-learnK means clustering example


from sklearn import datasets, cluster

X, _ = datasets.load_iris(return_X_y=True)
model = cluster.KMeans(n_clusters = 3)
model.fit(X)

clusters = model.predict(X)ctrl + c
from sklearn import

import module from scikit-learn

.KMeans(

create KMeans clustering model

n_clusters

number of clusters we want to see

.fit(

train model with a given features and target variable dataset

.predict(

predict target variable based on given features dataset