<jupyter_start><jupyter_text>HW 3-2~ <NAME> @ NTNU Math--- Use NumPy to do FCM clustering.  Import Data<jupyter_code>%matplotlib inline
import seaborn as sns
import pandas as pd
from sklearn import datasets

iris = datasets.load_iris()
target = iris['target']
data = pd.DataFrame(iris['data'], columns=iris['feature_names'])<jupyter_output><empty_output><jupyter_text>Plot Iris Data<jupyter_code>fig = sns.scatterplot(x = data.keys()[0], y = data.keys()[1],
                hue = iris['target_names'][target], data = data)
fig.legend(bbox_to_anchor=(1, 1), loc=2,title="Species name:")<jupyter_output><empty_output><jupyter_text>FCM Clustering with skfuzzy<jupyter_code>import skfuzzy as fuzz
import numpy as np
import time
t = time.time()
for i in range(100):
    _, u, _, _, _, _, _ = fuzz.cluster.cmeans(
        np.transpose(data), 3, 2, error=0.005, maxiter=1000, init=None)
t0 = time.time() - t<jupyter_output><empty_output><jupyter_text>Defuzzilize Membership:<jupyter_code>cluster_membership = np.argmax(u, axis=0)<jupyter_output><empty_output><jupyter_text>Plot the membership:<jupyter_code>fig1 = sns.scatterplot(
    x=data.keys()[0], y=data.keys()[1], hue=cluster_membership, data=data)
fig1.legend(bbox_to_anchor=(1, 1), loc=2, title="Cluster")<jupyter_output><empty_output><jupyter_text>Self Design FCM CodeThis FCM use matrix computation to accelerate speed:<jupyter_code>def fuzzy_c_mean(x, c, m = 2, d = 10**(-3)):
    x = np.array(x); p = x.shape[1]; n = x.shape[0]
    range = np.array(x.max() - x.min())
    a0 = np.random.randn(c,p) * range + np.array(np.mean(data))
    a = a0; a_mi1 = a; time = 0; diff = 1;
    while diff > d:
        dis_sim = (np.tile(x,(c,1,1)) - \
                       np.transpose(np.tile(a,[n,1,1]),(1,0,2)))**(2/(m-1))
        dis_sim = dis_sim.sum(axis=2)
        u = 1 / (dis_sim * sum(1 / dis_sim))
        a = np.transpose(np.transpose(np.matmul(u ** m, x)) / np.sum(u ** m, axis = 1))
        a = a[a[:,0].argsort(),]

        diff = np.linalg.norm(a-a_mi1)
        a_mi1 = a
    return u, a, a0<jupyter_output><empty_output><jupyter_text>Run iris data:<jupyter_code>t = time.time()
for i in range(100):
    u, a, _ = fuzzy_c_mean(data, 3, 2, 0.005)
t1 = time.time() - t<jupyter_output><empty_output><jupyter_text>Defuzzilize Membership:<jupyter_code>cluster_membership = np.argmax(u, axis=0)<jupyter_output><empty_output><jupyter_text>Plot the membership:<jupyter_code>fig2 = sns.scatterplot(
    x=data.keys()[0], y=data.keys()[1], hue=cluster_membership, data=data)
fig2.legend(bbox_to_anchor=(1, 1), loc=2, title="Cluster")<jupyter_output><empty_output><jupyter_text>Compare Computing time Useing different methods(In above section, we redoing FCM 100 times with same error and save the times they cost)The time using `skfuzzy` is:<jupyter_code>t0<jupyter_output><empty_output><jupyter_text>The time using self design code is:<jupyter_code>t1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15899010576595018, 'fraction_numerical': 0.027294438758103036, 'mean_word_length': 5.020533880903491, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 47, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '301914', 'license': 'Unlicense', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1167, 'n_words': 219, 'path': '20190320 NCTS AI MATH - HW3_2.ipynb', 'repo_name': 'PeterHsi/NCTS2019-AI-MATH-by-HWP', 'size': '2939'}