<filename>1018/Course Material/1018_2.py<gh_stars>0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 18 20:53:35 2017

@author: sitibanc
"""
import numpy as np
import random
from sklearn import datasets
from scipy import stats


def kmeans(sample, K, max_iter):
    N = sample.shape[0]                 # N筆資料
    D = sample.shape[1]                 # 每筆資料有N維
    C = np.zeros((K, D))                # K個中心點
    L = np.zeros((N, 1))                # Label (data屬於哪一個cluster)
    L1 = np.zeros((N, 1))               # 重新分群計算出的label
    dist = np.zeros((N, K))
    
    # Random select center
    idx = random.sample(range(N), K)
    C = sample[idx, :]
    iteration = 0
    
    while iteration <= max_iter:
        for i in range(K):
            #以整個矩陣做運算，求與C（中心點）距離
            # np.tile() --> 垂直Repeat C[i, :] N次，水平repeat 1次
            dist[:, i] = np.sum((sample - np.tile(C[i, :], (N, 1))) ** 2 , 1)
            # 取距離最短者的input為其label
            L1 = np.argmin(dist, 1)
            # 若分群後各群成員不再改變（已分完，所屬cluster已定），則跳出迴圈
            if iteration > 0 and np.array_equal(L, L1):
                break
            # Update Label L
            L = L1
            # 計算重新分群後的新center
            for i in range(K):
                # 取出屬於第i群者的index
                idx = np.nonzero(L == i)[0]    # np.nonzero()亦即True
                if len(idx) > 0:
                    C[i, :] = np.mean(sample[idx, :], 0)    # 沿垂直方向(0)計算平均  
        iteration += 1
        # Calcuate wicd（within cluster distance, 群內每筆資料與群中心的距離）
        wicd = np.sum(np.sqrt(np.sum((sample - C[L, :]) ** 2 , 1)))
    return C, L, wicd

# Practice 3 : Iris Dataset Clustering Using K-Means
data = datasets.load_iris()
feature = data.data
center, label, wicd = kmeans(feature, 3, 1000)
# Calculate Error Rate
error = 0
for i in range(len(label)):
    if i < 50:
        mode = stats.mode(label[:50])
        if label[i] != mode[0][0]:
            error += 1
    elif i < 100:
        mode = stats.mode(label[50:100])
        if label[i] != mode[0][0]:
            error += 1
    else:
        mode = stats.mode(label[100:])
        if label[i] != mode[0][0]:
            error += 1
print('Error rate :', error / len(label))
<filename>1018/Course Material/1018_2.py<gh_stars>0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 18 20:53:35 2017

@author: sitibanc
"""
import numpy as np
import random
from sklearn import datasets
from scipy import stats


def kmeans(sample, K, max_iter):
    N = sample.shape[0]                 # N筆資料
    D = sample.shape[1]                 # 每筆資料有N維
    C = np.zeros((K, D))                # K個中心點
    L = np.zeros((N, 1))                # Label (data屬於哪一個cluster)
    L1 = np.zeros((N, 1))               # 重新分群計算出的label
    dist = np.zeros((N, K))
    
    # Random select center
    idx = random.sample(range(N), K)
    C = sample[idx, :]
    iteration = 0
    
    while iteration <= max_iter:
        for i in range(K):
            #以整個矩陣做運算，求與C（中心點）距離
            # np.tile() --> 垂直Repeat C[i, :] N次，水平repeat 1次
            dist[:, i] = np.sum((sample - np.tile(C[i, :], (N, 1))) ** 2 , 1)
            # 取距離最短者的input為其label
            L1 = np.argmin(dist, 1)
            # 若分群後各群成員不再改變（已分完，所屬cluster已定），則跳出迴圈
            if iteration > 0 and np.array_equal(L, L1):
                break
            # Update Label L
            L = L1
            # 計算重新分群後的新center
            for i in range(K):
                # 取出屬於第i群者的index
                idx = np.nonzero(L == i)[0]    # np.nonzero()亦即True
                if len(idx) > 0:
                    C[i, :] = np.mean(sample[idx, :], 0)    # 沿垂直方向(0)計算平均  
        iteration += 1
        # Calcuate wicd（within cluster distance, 群內每筆資料與群中心的距離）
        wicd = np.sum(np.sqrt(np.sum((sample - C[L, :]) ** 2 , 1)))
    return C, L, wicd

# Practice 3 : Iris Dataset Clustering Using K-Means
data = datasets.load_iris()
feature = data.data
center, label, wicd = kmeans(feature, 3, 1000)
# Calculate Error Rate
error = 0
for i in range(len(label)):
    if i < 50:
        mode = stats.mode(label[:50])
        if label[i] != mode[0][0]:
            error += 1
    elif i < 100:
        mode = stats.mode(label[50:100])
        if label[i] != mode[0][0]:
            error += 1
    else:
        mode = stats.mode(label[100:])
        if label[i] != mode[0][0]:
            error += 1
print('Error rate :', error / len(label))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- Created on Wed Oct 18 20:53:35 2017  @author: sitibanc # N筆資料 # 每筆資料有N維 # K個中心點 # Label (data屬於哪一個cluster) # 重新分群計算出的label # Random select center #以整個矩陣做運算，求與C（中心點）距離 # np.tile() --> 垂直Repeat C[i, :] N次，水平repeat 1次 # 取距離最短者的input為其label # 若分群後各群成員不再改變（已分完，所屬cluster已定），則跳出迴圈 # Update Label L # 計算重新分群後的新center # 取出屬於第i群者的index # np.nonzero()亦即True # 沿垂直方向(0)計算平均 # Calcuate wicd（within cluster distance, 群內每筆資料與群中心的距離） # Practice 3 : Iris Dataset Clustering Using K-Means # Calculate Error Rate