mirror of
https://gitee.com/TheAlgorithms/Statistical-Learning-Method_Code.git
synced 2024-12-23 05:00:20 +08:00
568 lines
30 KiB
Plaintext
568 lines
30 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 57,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Data shape: (150, 4)\n",
|
||
"Length of labels: 150\n",
|
||
"[[0. 0.21561354 0.16810102 ... 1.08257132 1.14907064 0.96462829]\n",
|
||
" [0.21561354 0. 0.10157824 ... 1.08390691 1.17619813 0.95649502]\n",
|
||
" [0.16810102 0.10157824 0. ... 1.12088708 1.19544459 0.98859665]\n",
|
||
" ...\n",
|
||
" [1.08257132 1.08390691 1.12088708 ... 0. 0.226928 0.18710825]\n",
|
||
" [1.14907064 1.17619813 1.19544459 ... 0.226928 0. 0.28409587]\n",
|
||
" [0.96462829 0.95649502 0.98859665 ... 0.18710825 0.28409587 0. ]]\n",
|
||
"Number of groups: 150\n",
|
||
"150 [142, 101]\n",
|
||
"Number of groups: 149\n",
|
||
"151 [37, 34]\n",
|
||
"Number of groups: 148\n",
|
||
"152 [37, 34, 9]\n",
|
||
"Number of groups: 147\n",
|
||
"153 [39, 7]\n",
|
||
"Number of groups: 146\n",
|
||
"154 [48, 10]\n",
|
||
"Number of groups: 145\n",
|
||
"155 [40, 17]\n",
|
||
"Number of groups: 144\n",
|
||
"156 [138, 127]\n",
|
||
"Number of groups: 143\n",
|
||
"157 [47, 2]\n",
|
||
"Number of groups: 142\n",
|
||
"158 [39, 7, 28]\n",
|
||
"Number of groups: 141\n",
|
||
"159 [27, 0]\n",
|
||
"Number of groups: 140\n",
|
||
"160 [27, 0, 40, 17]\n",
|
||
"Number of groups: 139\n",
|
||
"161 [132, 128]\n",
|
||
"Number of groups: 138\n",
|
||
"162 [25, 1]\n",
|
||
"Number of groups: 137\n",
|
||
"163 [143, 120]\n",
|
||
"Number of groups: 136\n",
|
||
"164 [47, 2, 29]\n",
|
||
"Number of groups: 135\n",
|
||
"165 [27, 0, 40, 17, 39, 7, 28]\n",
|
||
"Number of groups: 134\n",
|
||
"166 [46, 19]\n",
|
||
"Number of groups: 133\n",
|
||
"167 [81, 80]\n",
|
||
"Number of groups: 132\n",
|
||
"168 [47, 2, 29, 3]\n",
|
||
"Number of groups: 131\n",
|
||
"169 [92, 82]\n",
|
||
"Number of groups: 130\n",
|
||
"170 [99, 96]\n",
|
||
"Number of groups: 129\n",
|
||
"171 [38, 8]\n",
|
||
"Number of groups: 128\n",
|
||
"172 [27, 0, 40, 17, 39, 7, 28, 49]\n",
|
||
"Number of groups: 127\n",
|
||
"173 [91, 63]\n",
|
||
"Number of groups: 126\n",
|
||
"174 [47, 2, 29, 3, 30]\n",
|
||
"Number of groups: 125\n",
|
||
"175 [137, 116]\n",
|
||
"Number of groups: 124\n",
|
||
"176 [93, 57]\n",
|
||
"Number of groups: 123\n",
|
||
"177 [27, 0, 40, 17, 39, 7, 28, 49, 4]\n",
|
||
"Number of groups: 122\n",
|
||
"178 [25, 1, 45]\n",
|
||
"Number of groups: 121\n",
|
||
"179 [25, 1, 45, 12]\n",
|
||
"Number of groups: 120\n",
|
||
"180 [75, 65]\n",
|
||
"Number of groups: 119\n",
|
||
"181 [24, 11]\n",
|
||
"Number of groups: 118\n",
|
||
"182 [126, 123]\n",
|
||
"Number of groups: 117\n",
|
||
"183 [81, 80, 69]\n",
|
||
"Number of groups: 116\n",
|
||
"184 [99, 96, 94]\n",
|
||
"Number of groups: 115\n",
|
||
"185 [139, 112]\n",
|
||
"Number of groups: 114\n",
|
||
"186 [47, 2, 29, 3, 30, 37, 34, 9]\n",
|
||
"Number of groups: 113\n",
|
||
"187 [47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12]\n",
|
||
"Number of groups: 112\n",
|
||
"188 [95, 88]\n",
|
||
"Number of groups: 111\n",
|
||
"189 [95, 88, 99, 96, 94]\n",
|
||
"Number of groups: 110\n",
|
||
"190 [27, 0, 40, 17, 39, 7, 28, 49, 4, 35]\n",
|
||
"Number of groups: 109\n",
|
||
"191 [84, 66]\n",
|
||
"Number of groups: 108\n",
|
||
"192 [97, 74]\n",
|
||
"Number of groups: 107\n",
|
||
"193 [138, 127, 149]\n",
|
||
"Number of groups: 106\n",
|
||
"194 [47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42]\n",
|
||
"Number of groups: 105\n",
|
||
"195 [27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11]\n",
|
||
"Number of groups: 104\n",
|
||
"196 [46, 19, 21]\n",
|
||
"Number of groups: 103\n",
|
||
"197 [148, 136]\n",
|
||
"Number of groups: 102\n",
|
||
"198 [91, 63, 78]\n",
|
||
"Number of groups: 101\n",
|
||
"199 [38, 8, 13]\n",
|
||
"Number of groups: 100\n",
|
||
"200 [86, 52]\n",
|
||
"Number of groups: 99\n",
|
||
"201 [95, 88, 99, 96, 94, 55]\n",
|
||
"Number of groups: 98\n",
|
||
"202 [86, 52, 75, 65]\n",
|
||
"Number of groups: 97\n",
|
||
"203 [26, 23]\n",
|
||
"Number of groups: 96\n",
|
||
"204 [16, 5]\n",
|
||
"Number of groups: 95\n",
|
||
"205 [86, 52, 75, 65, 58]\n",
|
||
"Number of groups: 94\n",
|
||
"206 [91, 63, 78, 61]\n",
|
||
"Number of groups: 93\n",
|
||
"207 [97, 74, 71]\n",
|
||
"Number of groups: 92\n",
|
||
"208 [46, 19, 21, 48, 10]\n",
|
||
"Number of groups: 91\n",
|
||
"209 [145, 141]\n",
|
||
"Number of groups: 90\n",
|
||
"210 [137, 116, 103]\n",
|
||
"Number of groups: 89\n",
|
||
"211 [95, 88, 99, 96, 94, 55, 92, 82]\n",
|
||
"Number of groups: 88\n",
|
||
"212 [26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11]\n",
|
||
"Number of groups: 87\n",
|
||
"213 [56, 51]\n",
|
||
"Number of groups: 86\n",
|
||
"214 [95, 88, 99, 96, 94, 55, 92, 82, 90]\n",
|
||
"Number of groups: 85\n",
|
||
"215 [86, 52, 75, 65, 58, 50]\n",
|
||
"Number of groups: 84\n",
|
||
"216 [130, 107]\n",
|
||
"Number of groups: 83\n",
|
||
"217 [86, 52, 75, 65, 58, 50, 97, 74, 71]\n",
|
||
"Number of groups: 82\n",
|
||
"218 [26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20]\n",
|
||
"Number of groups: 81\n",
|
||
"219 [38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42]\n",
|
||
"Number of groups: 80\n",
|
||
"220 [26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6]\n",
|
||
"Number of groups: 79\n",
|
||
"221 [46, 19, 21, 48, 10, 44]\n",
|
||
"Number of groups: 78\n",
|
||
"222 [26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6, 38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42]\n",
|
||
"Number of groups: 77\n",
|
||
"223 [143, 120, 140]\n",
|
||
"Number of groups: 76\n",
|
||
"224 [86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61]\n",
|
||
"Number of groups: 75\n",
|
||
"225 [95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69]\n",
|
||
"Number of groups: 74\n",
|
||
"226 [89, 53]\n",
|
||
"Number of groups: 73\n",
|
||
"227 [26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6, 38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42, 36]\n",
|
||
"Number of groups: 72\n",
|
||
"228 [126, 123, 111]\n",
|
||
"Number of groups: 71\n",
|
||
"229 [147, 110]\n",
|
||
"Number of groups: 70\n",
|
||
"230 [142, 101, 121]\n",
|
||
"Number of groups: 69\n",
|
||
"231 [86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76]\n",
|
||
"Number of groups: 68\n",
|
||
"232 [26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6, 38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42, 36, 46, 19, 21, 48, 10, 44]\n",
|
||
"Number of groups: 67\n",
|
||
"233 [138, 127, 149, 70]\n",
|
||
"Number of groups: 66\n",
|
||
"234 [89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69]\n",
|
||
"Number of groups: 65\n",
|
||
"235 [138, 127, 149, 70, 126, 123, 111]\n",
|
||
"Number of groups: 64\n",
|
||
"236 [143, 120, 140, 145, 141]\n",
|
||
"Number of groups: 63\n",
|
||
"237 [148, 136, 100]\n",
|
||
"Number of groups: 62\n",
|
||
"238 [89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67]\n",
|
||
"Number of groups: 61\n",
|
||
"239 [26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6, 38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42, 36, 46, 19, 21, 48, 10, 44, 31]\n",
|
||
"Number of groups: 60\n",
|
||
"240 [26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6, 38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42, 36, 46, 19, 21, 48, 10, 44, 31, 43]\n",
|
||
"Number of groups: 59\n",
|
||
"241 [86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73]\n",
|
||
"Number of groups: 58\n",
|
||
"242 [89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79]\n",
|
||
"Number of groups: 57\n",
|
||
"243 [132, 128, 104]\n",
|
||
"Number of groups: 56\n",
|
||
"244 [89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64]\n",
|
||
"Number of groups: 55\n",
|
||
"245 [143, 120, 140, 145, 141, 144]\n",
|
||
"Number of groups: 54\n",
|
||
"246 [138, 127, 149, 70, 126, 123, 111, 146]\n",
|
||
"Number of groups: 53\n",
|
||
"247 [93, 57, 98]\n",
|
||
"Number of groups: 52\n",
|
||
"248 [86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54]\n",
|
||
"Number of groups: 51\n",
|
||
"249 [147, 110, 137, 116, 103]\n",
|
||
"Number of groups: 50\n",
|
||
"250 [143, 120, 140, 145, 141, 144, 139, 112]\n",
|
||
"Number of groups: 49\n",
|
||
"251 [86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66]\n",
|
||
"Number of groups: 48\n",
|
||
"252 [87, 68]\n",
|
||
"Number of groups: 47\n",
|
||
"253 [142, 101, 121, 113]\n",
|
||
"Number of groups: 46\n",
|
||
"254 [122, 105]\n",
|
||
"Number of groups: 45\n",
|
||
"255 [86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51]\n",
|
||
"Number of groups: 44\n",
|
||
"256 [86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85]\n",
|
||
"Number of groups: 43\n",
|
||
"257 [26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6, 38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42, 36, 46, 19, 21, 48, 10, 44, 31, 43, 16, 5]\n",
|
||
"Number of groups: 42\n",
|
||
"258 [86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133]\n",
|
||
"Number of groups: 41\n",
|
||
"259 [87, 68, 119]\n",
|
||
"Number of groups: 40\n",
|
||
"260 [148, 136, 100, 115]\n",
|
||
"Number of groups: 39\n",
|
||
"261 [86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83]\n",
|
||
"Number of groups: 38\n",
|
||
"262 [26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6, 38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42, 36, 46, 19, 21, 48, 10, 44, 31, 43, 16, 5, 18]\n",
|
||
"Number of groups: 37\n",
|
||
"263 [143, 120, 140, 145, 141, 144, 139, 112, 124]\n",
|
||
"Number of groups: 36\n",
|
||
"264 [86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64]\n",
|
||
"Number of groups: 35\n",
|
||
"265 [33, 32]\n",
|
||
"Number of groups: 34\n",
|
||
"266 [86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77]\n",
|
||
"Number of groups: 33\n",
|
||
"267 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103]\n",
|
||
"Number of groups: 32\n",
|
||
"268 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104]\n",
|
||
"Number of groups: 31\n",
|
||
"269 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146]\n",
|
||
"Number of groups: 30\n",
|
||
"270 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102]\n",
|
||
"Number of groups: 29\n",
|
||
"271 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77]\n",
|
||
"Number of groups: 28\n",
|
||
"272 [131, 117]\n",
|
||
"Number of groups: 27\n",
|
||
"273 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115]\n",
|
||
"Number of groups: 26\n",
|
||
"274 [26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6, 38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42, 36, 46, 19, 21, 48, 10, 44, 31, 43, 16, 5, 18, 22]\n",
|
||
"Number of groups: 25\n",
|
||
"275 [122, 105, 135]\n",
|
||
"Number of groups: 24\n",
|
||
"276 [33, 32, 14]\n",
|
||
"Number of groups: 23\n",
|
||
"277 [129, 125]\n",
|
||
"Number of groups: 22\n",
|
||
"278 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59]\n",
|
||
"Number of groups: 21\n",
|
||
"279 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134]\n",
|
||
"Number of groups: 20\n",
|
||
"280 [129, 125, 130, 107]\n",
|
||
"Number of groups: 19\n",
|
||
"281 [33, 32, 14, 26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6, 38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42, 36, 46, 19, 21, 48, 10, 44, 31, 43, 16, 5, 18, 22]\n",
|
||
"Number of groups: 18\n",
|
||
"282 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72]\n",
|
||
"Number of groups: 17\n",
|
||
"283 [93, 57, 98, 60]\n",
|
||
"Number of groups: 16\n"
|
||
]
|
||
},
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"284 [33, 32, 14, 26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6, 38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42, 36, 46, 19, 21, 48, 10, 44, 31, 43, 16, 5, 18, 22, 15]\n",
|
||
"Number of groups: 15\n",
|
||
"285 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113]\n",
|
||
"Number of groups: 14\n",
|
||
"286 [129, 125, 130, 107, 122, 105, 135]\n",
|
||
"Number of groups: 13\n",
|
||
"287 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113, 87, 68, 119]\n",
|
||
"Number of groups: 12\n",
|
||
"288 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113, 87, 68, 119, 129, 125, 130, 107, 122, 105, 135]\n",
|
||
"Number of groups: 11\n",
|
||
"289 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113, 87, 68, 119, 129, 125, 130, 107, 122, 105, 135, 108]\n",
|
||
"Number of groups: 10\n",
|
||
"290 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113, 87, 68, 119, 129, 125, 130, 107, 122, 105, 135, 108, 118]\n",
|
||
"Number of groups: 9\n",
|
||
"291 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113, 87, 68, 119, 129, 125, 130, 107, 122, 105, 135, 108, 118, 93, 57, 98, 60]\n",
|
||
"Number of groups: 8\n",
|
||
"292 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113, 87, 68, 119, 129, 125, 130, 107, 122, 105, 135, 108, 118, 93, 57, 98, 60, 62]\n",
|
||
"Number of groups: 7\n",
|
||
"293 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113, 87, 68, 119, 129, 125, 130, 107, 122, 105, 135, 108, 118, 93, 57, 98, 60, 62, 114]\n",
|
||
"Number of groups: 6\n",
|
||
"294 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113, 87, 68, 119, 129, 125, 130, 107, 122, 105, 135, 108, 118, 93, 57, 98, 60, 62, 114, 109]\n",
|
||
"Number of groups: 5\n",
|
||
"295 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113, 87, 68, 119, 129, 125, 130, 107, 122, 105, 135, 108, 118, 93, 57, 98, 60, 62, 114, 109, 106]\n",
|
||
"Number of groups: 4\n",
|
||
"296 [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113, 87, 68, 119, 129, 125, 130, 107, 122, 105, 135, 108, 118, 93, 57, 98, 60, 62, 114, 109, 106, 131, 117]\n",
|
||
"{41: [41], 284: [33, 32, 14, 26, 23, 27, 0, 40, 17, 39, 7, 28, 49, 4, 35, 24, 11, 20, 6, 38, 8, 13, 47, 2, 29, 3, 30, 37, 34, 9, 25, 1, 45, 12, 42, 36, 46, 19, 21, 48, 10, 44, 31, 43, 16, 5, 18, 22, 15], 296: [143, 120, 140, 145, 141, 144, 139, 112, 124, 147, 110, 137, 116, 103, 132, 128, 104, 138, 127, 149, 70, 126, 123, 111, 146, 102, 86, 52, 75, 65, 58, 50, 97, 74, 71, 91, 63, 78, 61, 76, 73, 54, 84, 66, 56, 51, 85, 133, 83, 89, 53, 95, 88, 99, 96, 94, 55, 92, 82, 90, 81, 80, 69, 67, 79, 64, 77, 148, 136, 100, 115, 59, 134, 72, 142, 101, 121, 113, 87, 68, 119, 129, 125, 130, 107, 122, 105, 135, 108, 118, 93, 57, 98, 60, 62, 114, 109, 106, 131, 117]}\n",
|
||
"Adjusted Rand Index: 0.5583714437541352\n",
|
||
"Time: 177.01405954360962\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"#导入所需的库\n",
|
||
"import numpy as np\n",
|
||
"import math\n",
|
||
"import time\n",
|
||
"from scipy.special import comb\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",
|
||
" dist = math.sqrt(dist)\n",
|
||
" return dist\n",
|
||
"\n",
|
||
"\n",
|
||
"#定义计算所有特征数据两两之间距离的函数\n",
|
||
"def Distances(Xarray):\n",
|
||
" '''\n",
|
||
" INPUT:\n",
|
||
" Xarray - (array) 特征数据数组\n",
|
||
" \n",
|
||
" OUTPUT:\n",
|
||
" dists - (array) 两两数据的欧式距离数组\n",
|
||
" \n",
|
||
" '''\n",
|
||
" dists = np.zeros((Xarray.shape[0], Xarray.shape[0])) #定义一个数组用来保存两两数据的距离\n",
|
||
" for n1 in range(Xarray.shape[0]):\n",
|
||
" for n2 in range(n1):\n",
|
||
" dists[n1][n2] = cal_distance(Xarray[n1], Xarray[n2])\n",
|
||
" dists[n2][n1] = dists[n1][n2]\n",
|
||
" return dists\n",
|
||
"\n",
|
||
"\n",
|
||
"#定义计算两类的类间距离的函数,这里计算的是最短距离\n",
|
||
"def cal_groupdist(g1, g2, group_dict, dists):\n",
|
||
" '''\n",
|
||
" INPUT:\n",
|
||
" g1 - (int) 类别1的标签\n",
|
||
" g2 - (int) 类别2的标签\n",
|
||
" group_dict - (dict) 类别字典\n",
|
||
" dists - (array) 两两数据的欧式距离数组\n",
|
||
" \n",
|
||
" OUTPUT:\n",
|
||
" (int) 类间最短距离\n",
|
||
" \n",
|
||
" '''\n",
|
||
" d = []\n",
|
||
" #循环计算两类之间两两数据的距离\n",
|
||
" for xi in group_dict[g1]:\n",
|
||
" for xj in group_dict[g2]:\n",
|
||
" if xi != xj:\n",
|
||
" d.append(dists[xi][xj])\n",
|
||
" return min(d)\n",
|
||
"\n",
|
||
"\n",
|
||
"#定义层次聚类函数\n",
|
||
"def Clustering(Xarray, k, dists):\n",
|
||
" '''\n",
|
||
" INPUT:\n",
|
||
" Xarray - (array) 特征数据数组\n",
|
||
" k - (int) 设定的类别数\n",
|
||
" dists - (array) 两两数据的欧式距离数组\n",
|
||
" \n",
|
||
" OUTPUT:\n",
|
||
" group_dict - (dict) 类别字典\n",
|
||
" \n",
|
||
" '''\n",
|
||
" group_dict = {} #定义一个空字典,用于保存聚类所产生的所有类别\n",
|
||
" for n in range(Xarray.shape[0]): #层次聚类是一种聚合聚类方法,首先将每条数据都分到不同的类,数据的类别标签为0-(N-1),其中N为数据条数\n",
|
||
" group_dict[n] = [n]\n",
|
||
" newgroup = Xarray.shape[0] #newgroup表示新的类别标签,此时下一个类别标签为N\n",
|
||
" while len(group_dict.keys()) > k: #当类别数大于我们所设定的类别数k时,不断循环进行聚类\n",
|
||
" print('Number of groups:', len(group_dict.keys()))\n",
|
||
" group_dists = {} #定义一个空字典,用于保存两两类之间的间距,其中字典的值为元组(g1, g2),表示两个类别标签,字典的键为这两个类别的间距\n",
|
||
" #循环计算group_dict中两两类别之间的间距,保存到group_dists中\n",
|
||
" for g1 in group_dict.keys():\n",
|
||
" for g2 in group_dict.keys():\n",
|
||
" if g1 != g2:\n",
|
||
" if (g1, g2) not in group_dists.values():\n",
|
||
" d = cal_groupdist(g1, g2, group_dict, dists)\n",
|
||
" group_dists[d] = (g1, g2)\n",
|
||
" group_mindist = min(list(group_dists.keys())) #取类别之间的最小间距\n",
|
||
" mingroups = group_dists[group_mindist] #取间距最小的两个类别\n",
|
||
" new = [] #定义一个列表,用于保存所产生的新类中包含的数据,这里用之前对每条数据给的类别标签0-(N-1)来表示\n",
|
||
" for g in mingroups:\n",
|
||
" new.extend(group_dict[g]) #将间距最小的两类中包含的数据保存在new列表中\n",
|
||
" del group_dict[g] #然后在group_dict中移去这两类\n",
|
||
" print(newgroup, new)\n",
|
||
" group_dict[newgroup] = new #此时聚类所产生的新类中包含的数据即为以上两类的中包含的数据的聚合,给新类贴上类别标签为newgroup,保存到group_dict中\n",
|
||
" newgroup += 1 #产生下一个类别标签\n",
|
||
" return group_dict\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",
|
||
" y_dict = {} #定义一个空字典,用来保存外部标签中各类所包含的数据,结构与group_dict相同\n",
|
||
" for i in range(len(Ylist)):\n",
|
||
" if Ylist[i] not in y_dict:\n",
|
||
" y_dict[Ylist[i]] = [i]\n",
|
||
" else:\n",
|
||
" y_dict[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[list(group_dict.keys())[i]] and n in y_dict[list(y_dict.keys())[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",
|
||
"if __name__ == \"__main__\":\n",
|
||
" Xarray, Ylist = load_data('..\\iris.data') #加载数据\n",
|
||
" start = time.time() #保存开始时间\n",
|
||
" Xarray = Normalize(Xarray) #对特征数据进行标准化处理\n",
|
||
" k = 3 #设定聚类数为3\n",
|
||
" dists = Distances(Xarray) #计算特征数据的距离数组\n",
|
||
" print(dists)\n",
|
||
" group_dict = Clustering(Xarray, k, dists) #进行层次聚类\n",
|
||
" end = time.time() #保存结束时间\n",
|
||
" print(group_dict)\n",
|
||
" ARI = Adjusted_Rand_Index(group_dict, Ylist, k) #计算ARI用来评估聚类结果\n",
|
||
" print('Adjusted Rand Index:', ARI)\n",
|
||
" print('Time:', end-start)"
|
||
]
|
||
},
|
||
{
|
||
"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
|
||
}
|