mirror of
https://gitee.com/TheAlgorithms/Statistical-Learning-Method_Code.git
synced 2024-12-23 05:00:20 +08:00
246 lines
24 KiB
Plaintext
246 lines
24 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Data shape: (150, 4)\n",
|
||
"Length of labels: 150\n",
|
||
"1/2\n",
|
||
"{0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 57, 60, 67, 69, 79, 80, 81, 93, 98], 1: [50, 51, 52, 54, 58, 62, 63, 65, 68, 71, 72, 73, 74, 75, 76, 77, 83, 86, 87, 91, 92, 97, 102, 103, 104, 105, 107, 108, 109, 110, 111, 112, 116, 117, 118, 119, 120, 122, 123, 124, 125, 126, 128, 129, 130, 131, 132, 133, 134, 135, 137, 139, 140, 141, 143, 144, 145, 146, 147], 2: [53, 55, 56, 59, 61, 64, 66, 70, 78, 82, 84, 85, 88, 89, 90, 94, 95, 96, 99, 100, 101, 106, 113, 114, 115, 121, 127, 136, 138, 142, 148, 149]}\n",
|
||
"2/2\n",
|
||
"{0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49], 1: [50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149], 2: []}\n",
|
||
"Time: 0.0059719085693359375\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD8CAYAAACb4nSYAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzt3Xl4VPXZ//H3Tdj3fSeEHcKiwrC57+IGKtparUitRdvy2Mc+CnGrCFaFLtrF1kKL1S5qBdSAKBU3tG6EKtkgEMIW1kDYQyDL/fsj0V+MQAayzEzm87quXNd8z3zPzP2F5DMnZ+bcMXdHRESiQ51QFyAiIjVHoS8iEkUU+iIiUUShLyISRRT6IiJRRKEvIhJFFPoiIlFEoS8iEkUU+iIiUaRuqAsor23bth4XFxfqMkREIsqKFSt2uXu7iuaFXejHxcWRlJQU6jJERCKKmW0MZp5O74iIRBGFvohIFFHoi4hEEYW+iEgUUeiLiEQRhb6ISBRR6IuIRBGFvohIGHgrfQcvLd9U7c8TdhdniYhEk10HjzAtMY1FydsYGtuSG4Z1o04dq7bnU+iLiISAu/PqF1t4ZGE6eUeKuOfSvtxxXq9qDXxQ6IuI1Litew/zwCspvJuRw9DYlsy6fgi92zerkedW6IuI1JDiYucfn23iicWrKHZ4+Op4JoyOI6aaj+7LUuiLiNSArJyDJMxP4bMNuZzduy2PXzeYbq0b13gdCn0RkWpUWFTMnz9cz5NvraFB3TrMun4INwzrilnNHd2XpdAXEakm6Vv3M2X+SlK37OeygR2YMW4Q7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7oZl92RYiBpjrpW0hgCR3TwT+QsmvgJmUHOHfGLqKKy/INf8CaAq8XPqe9SZ3j9g2GUGuuVYJcs1LgEvNLB0oAu51992hq7pyglzz/wFzzOxuSk5zTIzkgzgze4GS03NtS9+neBioB+Duz1DyvsUVQCaQB3yvSp8/gv/tRETkJEXi6R0RETlFCn0RkSii0BcRiSIKfRGRKKLQFxGJIgp9EZEootAXEYkiCn0RkSjy/wDZpjw/Gjo3LQAAAABJRU5ErkJggg==\n",
|
||
"text/plain": [
|
||
"<Figure size 432x288 with 1 Axes>"
|
||
]
|
||
},
|
||
"metadata": {
|
||
"needs_background": "light"
|
||
},
|
||
"output_type": "display_data"
|
||
}
|
||
],
|
||
"source": [
|
||
"#导入所需的库\n",
|
||
"import numpy as np\n",
|
||
"import time\n",
|
||
"import random \n",
|
||
"from scipy.special import comb\n",
|
||
"import matplotlib.pyplot as plt\n",
|
||
"%matplotlib inline\n",
|
||
"\n",
|
||
"\n",
|
||
"#定义加载数据的函数,这里以鸢尾花数据集为例\n",
|
||
"def load_data(file):\n",
|
||
" '''\n",
|
||
" INPUT:\n",
|
||
" file - (str) 数据文件的路径\n",
|
||
" \n",
|
||
" OUTPUT:\n",
|
||
" Xarray - (array) 特征数据数组\n",
|
||
" Ylist - (list) 类别标签列表\n",
|
||
" \n",
|
||
" '''\n",
|
||
" Xlist = [] #定义一个列表用来保存每条数据\n",
|
||
" Ylist = [] #定义一个列表用来保存每条数据的类别标签\n",
|
||
" fr = open(file)\n",
|
||
" for line in fr.readlines(): #逐行读取数据,鸢尾花数据集每一行表示一个鸢尾花的特征和类别标签,用逗号分隔\n",
|
||
" cur = line.split(',')\n",
|
||
" label = cur[-1]\n",
|
||
" X = [float(x) for x in cur[:-1]] #用列表来表示一条特征数据\n",
|
||
" Xlist.append(X)\n",
|
||
" Ylist.append(label)\n",
|
||
" Xarray = np.array(Xlist) #将特征数据转换为数组类型,方便之后的操作\n",
|
||
" print('Data shape:', Xarray.shape)\n",
|
||
" print('Length of labels:', len(Ylist))\n",
|
||
" return Xarray, Ylist\n",
|
||
"\n",
|
||
"\n",
|
||
"#定义标准化函数,对每一列特征进行min-max标准化,将数据缩放到0-1之间\n",
|
||
"#标准化处理对于计算距离的机器学习方法是非常重要的,因为特征的尺度不同会导致计算出来的距离倾向于尺度大的特征,为保证距离对每一列特征都是公平的,必须将所有特征缩放到同一尺度范围内\n",
|
||
"def Normalize(Xarray):\n",
|
||
" '''\n",
|
||
" INPUT:\n",
|
||
" Xarray - (array) 特征数据数组\n",
|
||
" \n",
|
||
" OUTPUT:\n",
|
||
" Xarray - (array) 标准化处理后的特征数据数组\n",
|
||
" \n",
|
||
" '''\n",
|
||
" for f in range(Xarray.shape[1]):\n",
|
||
" maxf = np.max(Xarray[:, f])\n",
|
||
" minf = np.min(Xarray[:, f])\n",
|
||
" for n in range(Xarray.shape[0]):\n",
|
||
" Xarray[n][f] = (Xarray[n][f]-minf) / (maxf-minf) \n",
|
||
" return Xarray\n",
|
||
"\n",
|
||
"\n",
|
||
"#定义计算两条数据间的距离的函数,这里计算的是欧式距离\n",
|
||
"def cal_distance(xi, xj):\n",
|
||
" '''\n",
|
||
" INPUT:\n",
|
||
" Xi - (array) 第i条特征数据\n",
|
||
" Xj - (array) 第j条特征数据\n",
|
||
" \n",
|
||
" OUTPUT:\n",
|
||
" dist - (float) 两条数据的欧式距离\n",
|
||
" \n",
|
||
" '''\n",
|
||
" dist = 0\n",
|
||
" for col in range(len(xi)):\n",
|
||
" dist += (xi[col]-xj[col]) ** 2\n",
|
||
" return dist\n",
|
||
"\n",
|
||
"\n",
|
||
"#定义计算类中心的函数,以当前类中所包含数据的各个特征均值作为新的新的类中心\n",
|
||
"def cal_groupcenter(group, Xarray):\n",
|
||
" '''\n",
|
||
" INPUT:\n",
|
||
" group - (list) 类所包含的数据列表\n",
|
||
" Xarray - (array) 特征数据数组\n",
|
||
" \n",
|
||
" OUTPUT:\n",
|
||
" center - (array) 新的类中心\n",
|
||
" \n",
|
||
" '''\n",
|
||
" center = np.zeros(Xarray.shape[1])\n",
|
||
" for i in range(Xarray.shape[1]):\n",
|
||
" for n in group:\n",
|
||
" center[i] += Xarray[n][i] #计算当前类中第i个特征的数据之和\n",
|
||
" center = center / Xarray.shape[0] #计算各个特征的均值\n",
|
||
" return center\n",
|
||
"\n",
|
||
"\n",
|
||
"#定义计算调整兰德系数(ARI)的函数,调整兰德系数是一种聚类方法的常用评估方法\n",
|
||
"def Adjusted_Rand_Index(group_dict, Ylist, k):\n",
|
||
" '''\n",
|
||
" INPUT:\n",
|
||
" group_dict - (dict) 类别字典\n",
|
||
" Ylist - (list) 类别标签列表\n",
|
||
" k - (int) 设定的类别数\n",
|
||
" \n",
|
||
" OUTPUT:\n",
|
||
" (int) 调整兰德系数\n",
|
||
" \n",
|
||
" '''\n",
|
||
" group_array = np.zeros((k, k)) #定义一个数组,用来保存聚类所产生的类别标签与给定的外部标签各类别之间共同包含的数据数量\n",
|
||
" ylabel = list(set(Ylist)) #Ylist保存的标签为字符串,用ylabel来保存各个标签,在y_dict中类别以标签在ylabel列表中的索引值来表示类\n",
|
||
" y_dict = {i:[] for i in range(k)} #定义一个空字典,用来保存外部标签中各类所包含的数据,结构与group_dict相同\n",
|
||
" for i in range(len(Ylist)):\n",
|
||
" y_dict[ylabel.index(Ylist[i])].append(i)\n",
|
||
" #循环计算group_array的值\n",
|
||
" for i in range(k):\n",
|
||
" for j in range(k):\n",
|
||
" for n in range(len(Ylist)):\n",
|
||
" if n in group_dict[i] and n in y_dict[j]:\n",
|
||
" group_array[i][j] += 1 #如果数据n同时在group_dict的类别i和y_dict的类别j中,group_array[i][j]的数值加一\n",
|
||
" RI = 0 #定义兰德系数(RI)\n",
|
||
" sum_i = np.zeros(3) #定义一个数组,用于保存聚类结果group_dict中每一类的个数\n",
|
||
" sum_j = np.zeros(3) #定义一个数组,用于保存外部标签y_dict中每一类的个数\n",
|
||
" for i in range(k):\n",
|
||
" for j in range(k):\n",
|
||
" sum_i[i] += group_array[i][j]\n",
|
||
" sum_j[j] += group_array[i][j]\n",
|
||
" if group_array[i][j] >= 2:\n",
|
||
" RI += comb(group_array[i][j], 2) #comb用于计算group_array[i][j]中两两组合的组合数\n",
|
||
" ci = 0 #ci保存聚类结果中同一类中的两两组合数之和\n",
|
||
" cj = 0 #cj保存外部标签中同一类中的两两组合数之和\n",
|
||
" for i in range(k):\n",
|
||
" if sum_i[i] >= 2:\n",
|
||
" ci += comb(sum_i[i], 2)\n",
|
||
" for j in range(k):\n",
|
||
" if sum_j[j] >= 2:\n",
|
||
" cj += comb(sum_j[j], 2)\n",
|
||
" E_RI = ci * cj / comb(len(Ylist), 2) #计算RI的期望\n",
|
||
" max_RI = (ci + cj) / 2 #计算RI的最大值\n",
|
||
" return (RI-E_RI) / (max_RI-E_RI) #返回调整兰德系数的值\n",
|
||
"\n",
|
||
"\n",
|
||
"#定义k均值聚类函数\n",
|
||
"def Kmeans(Xarray, k, iters):\n",
|
||
" '''\n",
|
||
" INPUT:\n",
|
||
" Xarray - (array) 特征数据数组\n",
|
||
" k - (int) 设定的类别数\n",
|
||
" iters - (int) 设定的迭代次数\n",
|
||
" \n",
|
||
" OUTPUT:\n",
|
||
" group_dict - (dict) 类别字典\n",
|
||
" scores - (int) 每次迭代的ARI得分列表\n",
|
||
" \n",
|
||
" '''\n",
|
||
" center_inds = random.sample(range(Xarray.shape[0]), k) #从特征数据中随机抽取k个数据索引\n",
|
||
" centers = [Xarray[ci] for ci in center_inds] #将这k个数据索引所对应的特征数据作为初始的k个聚类中心\n",
|
||
" scores = [] #定义一个空列表用来保存每次迭代的ARI得分\n",
|
||
" for i in range(iters):\n",
|
||
" group_dict = {i:[] for i in range(k)} #定义一个空字典,用于保存聚类所产生的所有类别,其中字典的键为类别标签,值为类别所包含的数据列表,以索引表示每条数据\n",
|
||
" print('{}/{}'.format(i+1, iters))\n",
|
||
" #循环计算每条数据到各个聚类中心的距离\n",
|
||
" for n in range(Xarray.shape[0]):\n",
|
||
" dists = [] #保存第n条数据到各个聚类中心的距离\n",
|
||
" for ci in range(k):\n",
|
||
" dist = cal_distance(Xarray[n], centers[ci])\n",
|
||
" dists.append(dist)\n",
|
||
" g = dists.index(min(dists)) #取距离最近的中心所在的类\n",
|
||
" group_dict[g].append(n) #将第n条数据的索引n保存到g类\n",
|
||
" print(group_dict)\n",
|
||
" for i in range(k):\n",
|
||
" centers[i] = cal_groupcenter(group_dict[i], Xarray) #根据每一类所包含的数据重新计算类中心\n",
|
||
" scores.append(Adjusted_Rand_Index(group_dict, Ylist, k)) #将该轮迭代的ARI得分保存到scores列表\n",
|
||
" return group_dict, scores\n",
|
||
"\n",
|
||
"\n",
|
||
"if __name__ == \"__main__\":\n",
|
||
" Xarray, Ylist = load_data('..\\iris.data') #加载数据\n",
|
||
" start = time.time() #保存开始时间\n",
|
||
" Xarray = Normalize(Xarray) #对特征数据进行标准化处理\n",
|
||
" k = 3 #设定聚类数为3\n",
|
||
" iters = 2 #设定迭代次数为2\n",
|
||
" group_dict, scores = Kmeans(Xarray, k, iters) #进行k均值聚类\n",
|
||
" end = time.time() #保存结束时间\n",
|
||
" print('Time:', end-start)\n",
|
||
" plt.plot(range(iters), scores) #绘制ARI得分折线图"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.7.3"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 2
|
||
}
|