<jupyter_start><jupyter_text>模式识别·第五次作业·聚类算法 纪泽西 17375338 Last Modified:10th,April,2020           Run in Google Colab  Part1: Kmeans<jupyter_code>%tensorflow_version 2.x
import tensorflow as tf
import keras.backend as K
from keras.engine.topology import Layer, InputSpec
from keras.layers import Dense, Input
from keras.models import Model
from keras.optimizers import SGD
from keras import callbacks
from keras.initializers import VarianceScaling
from keras.utils import plot_model

import sklearn
from sklearn.metrics import confusion_matrix
from sklearn.cluster import KMeans
from sklearn.metrics import accuracy_score

import glob
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from time import *
import pandas as pd

import os
import scipy.io as sio
from IPython.display import Image

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train.reshape((x_train.shape[0],-1))
x_test = x_test.reshape((x_test.shape[0],-1))
n_clusters = len(np.unique(y_test))
kmeans = KMeans(n_clusters=n_clusters+2,n_init=15,n_jobs=4)<jupyter_output><empty_output><jupyter_text>由于数据集中1的书写方式有印刷体和手写体两种，且差距过大导致聚类将其分为两类，考虑增加聚类个数，之后手动整合<jupyter_code># 考虑直接在测试集上聚类
st=time()
y_hat = kmeans.fit_predict(x_test)
et=time()
print("amount time: {:.4f} s".format(et-st))
# 考虑在训练集上聚类，利用聚类结果分析数据集
kmeans1 = KMeans(n_clusters=n_clusters+2,n_init=15,n_jobs=4)
st=time()
kmeans1.fit(x_train)
y_hat2 = kmeans1.predict(x_test)
et=time()
print("amount time: {:.4f} s".format(et-st))
# 可视化结果并展示分类准确性
def draw_confusionmatrix(ytest, yhat):
    plt.figure(figsize=(10,7))
    cm = confusion_matrix(ytest, yhat)
    ax = sns.heatmap(cm, annot=True, fmt="d")
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

    acc = accuracy_score(ytest, yhat)
    print(f"Sum Axis-1 as Classification accuracy: {acc}")<jupyter_output><empty_output><jupyter_text>第一组测试<jupyter_code>draw_confusionmatrix(y_test,y_hat)<jupyter_output>Sum Axis-1 as Classification accuracy: 0.1105<jupyter_text>手动对聚类标签进行修正，依据的原则进行修正<jupyter_code>temp=[5,1,8,9,6,4,0,1,0,7,2,3]
y_hat1=np.zeros(10000)
for i in range(0,10000):
  y_hat1[i] = temp[y_hat[i]]
draw_confusionmatrix(y_test,y_hat1)<jupyter_output>Sum Axis-1 as Classification accuracy: 0.6181<jupyter_text>第二组测试<jupyter_code>draw_confusionmatrix(y_test,y_hat2)
temp=[5,4,9,0,3,2,0,1,1,8,7,6]
y_hat3=np.zeros(10000)
for i in range(0,10000):
  y_hat3[i] = temp[y_hat2[i]]
draw_confusionmatrix(y_test,y_hat3)<jupyter_output>Sum Axis-1 as Classification accuracy: 0.6035
{'fraction_non_alphanumeric': 0.12110726643598616, 'fraction_numerical': 0.0457516339869281, 'mean_word_length': 7.234177215189874, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '482322', 'license': 'Apache-2.0', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1200, 'n_words': 195, 'path': 'KMeans.ipynb', 'repo_name': 'Phantom-Ren/PR_TH', 'size': '2601'}