<jupyter_start><jupyter_text>K-means methodclustering algorithm which is executed iteratively  reference: https://pythondatascience.plavox.info/scikit-learn/%E3%82%AF%E3%83%A9%E3%82%B9%E3%82%BF%E5%88%86%E6%9E%90-k-means<jupyter_code>import numpy as np
import pandas as pd
from sklearn import cluster
from matplotlib import pyplot as plt
%matplotlib inline
# datasets
data = pd.read_csv("http://pythondatascience.plavox.info/wp-content/uploads/2016/05/Wholesale_customers_data.csv")
del(data["Channel"])
del(data["Region"])
X = np.array([data["Fresh"].tolist(),
             data["Milk"].tolist(),
             data["Grocery"].tolist(),
             data["Frozen"].tolist(),
             data["Detergents_Paper"].tolist(),
             data["Delicassen"].tolist()], np.int32)
X = np.transpose(X)
# prediction
prediction = cluster.KMeans(n_clusters=4).fit_predict(X)
prediction
# statistical value
data["cluster_id"] = prediction
data.head()
data[data["cluster_id"] == 0].mean()
data[data["cluster_id"] == 1].mean()
data[data["cluster_id"] == 2].mean()
data[data["cluster_id"] == 3].mean()
# visualize
info = pd.DataFrame()
for i in range(0, 4):
    info["cluster_"+str(i)] = data[data["cluster_id"] == i].mean()
info = info.drop("cluster_id")
cl_plot = info.T.plot(kind='bar', stacked=True)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup