from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
ks = range(1, 6)
inertias = []
for k in ks:
model = KMeans(n_clusters=k) # Create a KMeans instance with k clusters
model.fit(samples) # Fit model to samples
inertias.append(model.inertia_) # Append the inertia to the list of inertias
plt.plot(ks, inertias, '-o') # Plot ks vs inertias
plt.xlabel('number of clusters, k')
plt.ylabel('inertia')
plt.xticks(ks)
plt.show()
import pandas as pd
from sklearn.cluster import KMeans
model = KMeans(n_clusters=3) # Create a KMeans model with 3 clusters
labels = model.fit_predict(samples) # Use fit_predict to fit model and obtain cluster labels
df = pd.DataFrame({'labels': labels, 'varieties': varieties}) # Create a DataFrame with labels and varieties as columns
ct = pd.crosstab(df['labels'],df['varieties']) # Create crosstab
print(ct)