<jupyter_start><jupyter_text>作業 用 iris (dataset.load_iris()) 資料嘗試跑 kmeans (可以測試不同的群數 , init 等)<jupyter_code>from sklearn import datasets
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
# Though the following import is not directly being used, it is required
# for 3D projection to work
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans

iris = datasets.load_iris()
X = iris.data
y = iris.target<jupyter_output><empty_output><jupyter_text>載入 相關套件 並 執行 kmean 實驗 ...- 實驗測試不同的群數- 實驗測試不同的初始值- 呈現結果<jupyter_code>#不同群10、5、3,不同初始值 initialization 1、2、3
estimators = [('k_means_10', KMeans(n_clusters=10)),
              ('k_means_10', KMeans(n_clusters=5)),
              ('k_means_3', KMeans(n_clusters=3)),
              ('k_means_bad_init', KMeans(n_clusters=3, n_init=1,init='random')),
              ('k_means_bad_init', KMeans(n_clusters=3, n_init=1,init='random')),
              ('k_means_bad_init', KMeans(n_clusters=3, n_init=1,init='random')),
             ]
fignum = 1
titles = ['10 clusters', '5 clusters','3 clusters', '3 clusters, initialization 1','3 clusters, initialization 2','3 clusters, initialization 3']
for name, est in estimators:
    fig = plt.figure(fignum, figsize=(4, 3))
    ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
    
    ## fit data
    est.fit(X)
    
    labels = est.labels_

    ax.scatter(X[:, 3], X[:, 0], X[:, 2],
               c=labels.astype(np.float), edgecolor='k')

    ax.w_xaxis.set_ticklabels([])
    ax.w_yaxis.set_ticklabels([])
    ax.w_zaxis.set_ticklabels([])
    ax.set_title(titles[fignum - 1])
    ax.dist = 12
    fignum = fignum + 1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1426865671641791, 'fraction_numerical': 0.03343283582089552, 'mean_word_length': 3.872093023255814, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '299459', 'license': 'MIT', 'n_tokens_mistral': 723, 'n_tokens_neox': 711, 'n_words': 137, 'path': 'Day_055_kmeanSample.ipynb', 'repo_name': 'build0220/homework', 'size': '1675'}