Debian Codegraph @

tests/test_functions/test_facility_location.py

raw
1 import scipy
2 import numpy
3
4 try:
5 import cupy
6 except:
7 import numpy as cupy
8
9 from apricot import FacilityLocationSelection
10 from apricot.optimizers import *
11
12 from sklearn.datasets import load_digits
13 from sklearn.metrics import pairwise_distances
14
15 from numpy.testing import assert_almost_equal
16 from numpy.testing import assert_array_equal
17 from numpy.testing import assert_array_almost_equal
18
19 digits_data = load_digits()
20 X_digits = digits_data.data
21
22 X_digits_cosine_sparse = scipy.sparse.csr_matrix((1 - pairwise_distances(
23 X_digits, metric='cosine')) ** 2)
24 X_digits_corr_cupy = cupy.array((1 - pairwise_distances(
25 X_digits, metric='correlation')) ** 2)
26 X_digits_cosine_cupy = cupy.array((1 - pairwise_distances(
27 X_digits, metric='cosine')) ** 2)
28
29 digits_corr_ranking = [424, 1647, 396, 339, 1030, 331, 983, 1075, 1482,
30 1539, 1282, 493, 885, 823, 1051, 236, 537, 1161, 345, 1788, 1432, 1634,
31 1718, 1676, 146, 1286, 655, 1292, 556, 533, 1545, 520, 1711, 1428, 620,
32 1276, 305, 438, 1026, 183, 2, 384, 1012, 798, 213, 1291, 162, 1206, 227,
33 1655, 233, 1508, 410, 1295, 1312, 1350, 514, 938, 579, 1066, 82, 164, 948,
34 1588, 1294, 1682, 943, 517, 959, 1429, 762, 898, 1556, 881, 1470, 1549, 1325,
35 1568, 937, 347, 1364, 126, 732, 1168, 241, 573, 731, 815, 864, 1639, 1570, 411,
36 1086, 696, 870, 1156, 353, 160, 1381, 326]
37
38 digits_corr_gains = [736.794, 114.2782, 65.4154, 61.3037, 54.5428, 38.7506, 34.097,
39 32.6649, 30.2813, 27.8395, 24.1843, 17.568, 16.6615, 15.2973, 13.2629, 9.7685,
40 9.5183, 7.9484, 7.8094, 7.0224, 6.623, 6.061, 6.0469, 5.323, 5.167, 5.0563,
41 4.8848, 4.7694, 4.4766, 4.4577, 4.3198, 3.9347, 3.5501, 3.3284, 3.0123, 2.9994,
42 2.9739, 2.8233, 2.7572, 2.558, 2.5281, 2.4412, 2.4328, 2.3452, 2.2498, 2.2457,
43 2.2127, 2.1542, 2.1416, 2.0876, 2.0715, 2.0482, 2.0053, 1.9996, 1.9912, 1.973,
44 1.8029, 1.7865, 1.7284, 1.7255, 1.7201, 1.7169, 1.6614, 1.6445, 1.6147, 1.5874,
45 1.5827, 1.5822, 1.5784, 1.5164, 1.4876, 1.4319, 1.4288, 1.3736, 1.3485, 1.3039,
46 1.2872, 1.2771, 1.2587, 1.2391, 1.2279, 1.2006, 1.1654, 1.1491, 1.1445, 1.137,
47 1.1122, 1.0785, 1.0771, 1.0402, 1.0321, 1.0192, 1.0158, 0.9734, 0.9627, 0.9612,
48 0.9401, 0.9291, 0.912, 0.8924]
49
50 digits_euclidean_ranking = [945, 392, 1507, 793, 1417, 1039, 97, 1107, 1075,
51 867, 360, 186, 1584, 1422, 885, 1084, 1327, 1696, 991, 146, 181, 765,
52 175, 1513, 1120, 877, 1201, 1764, 1711, 1447, 1536, 1286, 438, 612, 6,
53 514, 410, 1545, 384, 1053, 1485, 983, 310, 51, 654, 1312, 708, 157, 259,
54 1168, 117, 1634, 1537, 1188, 1364, 1713, 579, 582, 69, 200, 1678, 798, 183,
55 520, 1011, 1295, 1291, 938, 1276, 501, 696, 948, 925, 558, 269, 1066, 573,
56 762, 1294, 1588, 732, 1387, 1568, 1026, 1156, 79, 1222, 1414, 864, 1549,
57 1236, 213, 411, 151, 233, 924, 126, 345, 1421, 1562]
58
59 digits_euclidean_gains = [7448636.0, 384346.0, 250615.0, 224118.0, 166266.0,
60 127456.0, 122986.0, 109483.0, 93463.0, 67173.0, 55997.0, 54721.0, 51497.0,
61 47765.0, 45073.0, 33857.0, 30100.0, 25043.0, 22260.0, 19700.0, 19135.0,
62 17545.0, 17000.0, 15462.0, 15315.0, 14996.0, 14819.0, 13244.0, 12529.0,
63 12474.0, 11702.0, 11639.0, 11612.0, 11266.0, 11187.0, 9722.0, 9244.0,
64 8645.0, 8645.0, 8461.0, 8404.0, 8115.0, 7998.0, 7351.0, 7153.0, 6992.0,
65 6956.0, 6919.0, 6711.0, 6684.0, 6526.0, 6348.0, 6099.0, 5969.0, 5460.0,
66 5433.0, 5163.0, 5141.0, 5090.0, 4900.0, 4842.0, 4683.0, 4165.0, 4104.0,
67 4099.0, 4099.0, 3998.0, 3959.0, 3912.0, 3807.0, 3703.0, 3675.0, 3670.0,
68 3636.0, 3564.0, 3407.0, 3395.0, 3196.0, 3188.0, 3168.0, 3156.0, 3144.0,
69 3093.0, 3078.0, 3059.0, 2997.0, 2944.0, 2891.0, 2886.0, 2865.0, 2804.0,
70 2779.0, 2756.0, 2748.0, 2709.0, 2696.0, 2651.0, 2637.0, 2619.0, 2602.0]
71
72 digits_cosine_ranking = [424, 615, 1545, 1385, 1482, 112, 1539, 1075, 331,
73 493, 885, 345, 1282, 823, 1432, 1051, 537, 1788, 1549, 1622, 834, 1634,
74 1718, 1474, 1185, 655, 1292, 396, 1711, 1676, 2, 183, 1536, 983, 438,
75 1276, 305, 1353, 620, 384, 1026, 162, 1012, 798, 213, 227, 1291, 533,
76 1655, 1485, 410, 1206, 157, 1387, 556, 29, 1588, 1320, 1295, 164, 514,
77 938, 948, 517, 579, 1325, 1682, 1222, 82, 959, 943, 520, 762, 1066, 898,
78 1227, 1568, 1086, 881, 1678, 1470, 1364, 732, 937, 1168, 347, 241, 573,
79 1156, 1570, 908, 1584, 126, 1639, 815, 411, 151, 846, 696, 233]
80
81 digits_cosine_gains = [1126.631, 76.1809, 43.868, 35.4606, 33.088, 32.6405,
82 27.5345, 23.2181, 20.0442, 15.384, 10.8113, 9.5884, 8.3655, 7.7554, 7.6675,
83 7.3457, 5.2902, 5.0039, 4.6488, 4.2388, 4.2165, 4.1855, 3.7814, 3.4738,
84 3.1826, 3.1456, 3.1258, 3.0707, 2.9035, 2.8744, 2.788, 2.4376, 2.3306,
85 1.9438, 1.8711, 1.8376, 1.7047, 1.6453, 1.5747, 1.5644, 1.5371, 1.4879,
86 1.4603, 1.4256, 1.4233, 1.4098, 1.3889, 1.3861, 1.3012, 1.2986, 1.278,
87 1.2779, 1.2775, 1.2059, 1.1914, 1.1437, 1.1221, 1.1031, 1.0829, 1.0708,
88 1.0427, 1.0116, 1.0099, 0.9978, 0.9943, 0.98, 0.9717, 0.945, 0.927, 0.9064,
89 0.8899, 0.8746, 0.8651, 0.8649, 0.859, 0.8526, 0.84, 0.8284, 0.8269, 0.8067,
90 0.8066, 0.7645, 0.7517, 0.7409, 0.7396, 0.7383, 0.7237, 0.7209, 0.7043,
91 0.7021, 0.6945, 0.6806, 0.6585, 0.6539, 0.6198, 0.6161, 0.6078, 0.5963,
92 0.5945, 0.5899]
93
94 digits_cosine_greedi_ranking = [424, 1030, 468, 1111, 1385, 41, 201, 81, 396,
95 186, 537, 938, 1009, 655, 1071, 1736, 345, 1482, 331, 1788, 1536, 543, 1104,
96 1120, 1535, 1201, 1053, 236, 1282, 339, 1718, 148, 115, 1046, 1203, 530,
97 1185, 494, 155, 621, 1542, 280, 732, 561, 900, 1276, 191, 554, 1051, 275,
98 1134, 1223, 504, 410, 1624, 982, 1364, 241, 692, 1047, 1428, 312, 1709,
99 160, 816, 768, 402, 131, 1265, 1483, 635, 1554, 1614, 923, 552, 188, 1410,
100 493, 32, 264, 1532, 1478, 1244, 675, 976, 235, 262, 132, 809, 668, 1555,
101 1332, 924, 138, 1295, 1533, 165, 822, 1184, 463]
102
103 digits_cosine_greedi_gains = [1126.631, 73.1793, 46.4627, 29.5523, 34.0967,
104 22.7932, 21.2345, 19.1614, 37.8457, 10.9945, 6.7468, 10.4479, 8.1564,
105 3.4669, 9.0943, 6.8193, 9.0551, 7.4868, 7.4788, 4.6044, 5.3755, 2.0625,
106 3.1779, 4.5603, 3.5798, 2.3906, 1.7633, 3.2745, 3.2062, 2.0717, 2.8894,
107 2.3138, 1.7417, 1.4141, 1.6926, 1.0587, 2.5893, 1.7456, 1.0624, 1.278,
108 1.4662, 0.8194, 0.7861, 1.0377, 1.0657, 1.2889, 0.5415, 1.1421, 1.082,
109 1.0706, 2.0516, 1.4782, 0.4165, 2.6358, 1.2067, 0.9377, 0.7095, 0.9775,
110 1.3211, 0.758, 1.699, 0.8115, 0.6292, 1.8336, 0.54, 0.6586, 0.4637, 0.813,
111 0.3427, 0.2793, 0.8045, 0.1796, 0.9699, 0.7823, 1.132, 0.8785, 0.5449,
112 1.0747, 1.3588, 0.3345, 0.3211, 0.6306, 0.4073, 0.139, 0.668, 0.2053,
113 0.3902, 0.8324, 0.1153, 0.5048, 1.1628, 0.4711, 0.8476, 0.8886, 0.6292,
114 0.6492, 0.6842, 0.639, 0.2356, 0.2605]
115
116 digits_cosine_approx_ranking = [424, 615, 1545, 1385, 983, 1482, 1539, 1075, 331,
117 493, 885, 345, 823, 1282, 1051, 236, 537, 1788, 1161, 655, 195, 1634, 438,
118 1718, 1474, 1676, 1292, 533, 396, 1185, 183, 1711, 2, 640, 1353, 1276, 305,
119 384, 1442, 1026, 1012, 213, 162, 197, 798, 227, 1291, 410, 1485, 1655, 1206,
120 29, 1588, 556, 1682, 1536, 1325, 1295, 517, 514, 579, 1461, 1320, 1294, 948,
121 567, 1222, 898, 943, 762, 550, 1556, 1066, 1350, 1568, 881, 689, 1678, 1470,
122 347, 82, 1364, 1168, 1549, 1156, 959, 241, 126, 1628, 149, 652, 411, 1639, 937,
123 216, 1250, 1257, 815, 151, 846]
124
125 digits_cosine_approx_gains = [1126.631, 76.1809, 43.868, 35.4606, 32.6168,
126 33.0709, 28.6118, 23.568, 20.2298, 15.2965, 10.8862, 9.3698, 8.3121, 8.3655,
127 7.3398, 6.4778, 5.2902, 5.0039, 4.7001, 4.4474, 4.1603, 4.1855, 3.8457, 3.7814,
128 3.4738, 3.0503, 2.9736, 2.8744, 3.0707, 2.8541, 2.7273, 2.6049, 2.4984, 2.3956,
129 2.138, 1.8376, 1.7047, 1.5309, 1.4743, 1.5371, 1.5433, 1.4296, 1.5101, 1.4607,
130 1.4256, 1.4098, 1.3889, 1.281, 1.2986, 1.3012, 1.2779, 1.1437, 1.1221, 1.1914,
131 1.0978, 1.0958, 1.0816, 0.987, 0.9978, 1.0427, 0.9943, 0.9759, 1.0199, 1.0072,
132 0.9435, 0.8643, 0.9121, 0.859, 0.8899, 0.8651, 0.8293, 0.8497, 0.8649, 0.7972,
133 0.7962, 0.7702, 0.7473, 0.7945, 0.8066, 0.7383, 0.7807, 0.7645, 0.734, 0.7296,
134 0.7043, 0.728, 0.7237, 0.6585, 0.6398, 0.6063, 0.6387, 0.6161, 0.6539, 0.6395,
135 0.5825, 0.5886, 0.6205, 0.6031, 0.6078, 0.5963]
136
137 digits_cosine_stochastic_ranking = [1081, 1014, 1386, 770, 567, 137, 723, 1491,
138 1274, 1492, 1728, 1456, 186, 1448, 386, 148, 891, 1759, 1424, 587, 191, 629,
139 1507, 1084, 1473, 946, 518, 638, 1739, 502, 1537, 1227, 689, 88, 238, 1667,
140 1785, 1067, 1461, 1222, 1099, 607, 364, 1572, 1195, 217, 1034, 208, 84, 1128,
141 425, 345, 626, 843, 1070, 83, 1449, 1071, 1644, 1392, 1415, 449, 802, 1348,
142 1553, 175, 1455, 1770, 1395, 1032, 879, 1220, 1137, 129, 754, 1695, 1459, 782,
143 549, 1069, 260, 834, 517, 919, 1622, 700, 424, 1685, 245, 1339, 1152, 1212,
144 1425, 937, 1665, 291, 1535, 701, 1508, 1219]
145
146 digits_cosine_stochastic_gains = [889.4143, 20.4744, 103.4191, 46.2267, 4.3305,
147 18.5211, 54.3538, 35.5077, 6.551, 5.7077, 5.6756, 37.63, 17.5565, 34.278,
148 42.5224, 35.5992, 1.2107, 19.8488, 3.8072, 3.4409, 2.7019, 1.3549, 1.5089,
149 1.0551, 14.8608, 2.5029, 0.4053, 1.4568, 4.6182, 1.0255, 2.0205, 3.7699,
150 2.0694, 12.8589, 2.3583, 3.3821, 3.8403, 1.1118, 1.6831, 4.3867, 1.2917,
151 1.8872, 0.5247, 1.1549, 1.0258, 6.2302, 10.167, 3.4695, 4.1669, 1.0842,
152 2.3784, 11.029, 1.6765, 0.7525, 0.7335, 0.454, 2.307, 4.0194, 0.7664, 0.3671,
153 0.9314, 3.1446, 1.2759, 2.6267, 0.4132, 0.6536, 0.5142, 0.4766, 0.7555,
154 1.1265, 0.1483, 0.5388, 0.6339, 0.4821, 3.1597, 0.6725, 2.7096, 0.2288,
155 0.335, 0.8554, 2.0427, 0.6971, 4.5681, 2.0573, 0.6791, 0.703, 1.0025,
156 0.3316, 2.9545, 1.2529, 0.736, 0.7703, 0.2727, 0.7296, 1.1756, 0.2375,
157 1.4499, 0.0924, 1.4348, 0.2389]
158
159 digits_cosine_sample_ranking = [424, 615, 1545, 1385, 1482, 112, 1539, 1075,
160 331, 493, 885, 345, 1282, 823, 1432, 1051, 537, 1788, 1549, 1622, 834,
161 1634, 1718, 1474, 1185, 655, 1292, 396, 1711, 1676, 2, 183, 1536, 983,
162 1276, 305, 384, 403, 1353, 620, 1026, 162, 1012, 798, 213, 227, 1291,
163 533, 1655, 1485, 410, 1206, 157, 1387, 556, 29, 1588, 1682, 1295, 164,
164 514, 948, 579, 972, 1325, 1461, 1312, 1222, 82, 959, 943, 520, 762,
165 1066, 898, 1227, 1568, 1086, 881, 1678, 1470, 1364, 732, 1156, 241,
166 573, 1570, 908, 1504, 1584, 126, 1639, 1158, 937, 815, 411, 151, 149,
167 846, 696]
168
169 digits_cosine_sample_gains = [1126.631, 76.1809, 43.868, 35.4606, 33.088,
170 32.6405, 27.5345, 23.2181, 20.0442, 15.384, 10.8113, 9.5884, 8.3655,
171 7.7554, 7.6675, 7.3457, 5.2902, 5.0039, 4.6488, 4.2388, 4.2165, 4.1855,
172 3.7814, 3.4738, 3.1826, 3.1456, 3.1258, 3.0707, 2.9035, 2.8744, 2.788,
173 2.4376, 2.3306, 1.9438, 1.8376, 1.7047, 1.6997, 1.6733, 1.6453, 1.5747,
174 1.5371, 1.4879, 1.4603, 1.4256, 1.4233, 1.4098, 1.3889, 1.3861, 1.3246,
175 1.2986, 1.278, 1.2779, 1.2775, 1.2059, 1.1914, 1.1437, 1.1221, 1.0978,
176 1.0829, 1.0708, 1.0427, 1.0099, 0.9943, 0.981, 0.98, 0.9759, 0.9456,
177 0.945, 0.927, 0.9064, 0.8899, 0.8746, 0.8651, 0.8649, 0.859, 0.8526,
178 0.84, 0.8284, 0.8269, 0.8067, 0.8066, 0.7645, 0.7517, 0.7295, 0.7237,
179 0.7209, 0.7021, 0.6945, 0.6898, 0.6806, 0.6585, 0.6576, 0.6469, 0.6395,
180 0.6198, 0.6111, 0.6078, 0.5964, 0.5963, 0.5945]
181
182 digits_cosine_modular_ranking = [424, 148, 615, 1747, 1030, 1766, 818, 1363,
183 768, 509, 1774, 138, 1295, 402, 255, 1069, 1709, 852, 248, 1327, 168, 459,
184 890, 513, 899, 945, 452, 183, 423, 923, 1658, 1325, 491, 898, 742, 478,
185 352, 514, 823, 1040, 978, 903, 426, 1071, 1796, 269, 1794, 332, 1781, 657,
186 913, 814, 1340, 417, 254, 505, 1668, 1320, 1423, 684, 1433, 309, 114, 649,
187 420, 1455, 1453, 1647, 508, 224, 1632, 1705, 1763, 296, 301, 1737, 515,
188 836, 370, 1026, 997, 500, 1199, 736, 686, 1596, 76, 1757, 816, 693, 448,
189 339, 547, 721, 1726, 1678, 854, 405, 249, 1323]
190
191 digits_cosine_modular_gains = [1126.631, 44.738, 40.5236, 21.8344, 14.9058,
192 2.0543, 0.301, 1.4012, 6.271, 5.6542, 0.6256, 2.3966, 2.8463, 16.5448,
193 0.4742, 1.412, 0.4318, 3.4858, 0.7938, 0.5358, 0.3774, 14.1052, 7.2849,
194 1.9459, 3.8649, 1.2624, 9.6255, 1.0289, 4.0384, 2.9969, 5.4019, 1.0495,
195 1.2719, 1.6337, 0.3053, 3.4975, 0.7808, 5.4622, 2.994, 0.5453, 0.2372,
196 0.3399, 0.1372, 0.7602, 0.4801, 6.4833, 0.1203, 0.2693, 0.5488, 1.5296,
197 0.1185, 0.3899, 0.6843, 0.2927, 0.5842, 2.3053, 0.3581, 6.3265, 0.3343,
198 0.3976, 0.1859, 0.1083, 0.4559, 1.2732, 0.4012, 0.2193, 0.0957, 2.9362,
199 0.1251, 0.7071, 1.123, 0.1063, 0.1448, 0.1763, 2.1166, 0.467, 0.1193,
200 1.0707, 0.0863, 0.133, 0.0905, 0.1449, 2.0452, 2.1527, 0.061, 0.2085,
201 0.29, 0.7726, 0.1614, 1.1461, 0.649, 0.8119, 0.3708, 3.3097, 0.8278,
202 0.5271, 15.1531, 2.0343, 0.0976, 0.6053]
203
204 digits_cosine_sieve_ranking = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13,
205 14, 15, 16, 17, 18, 20, 22, 25, 26, 27, 29, 32, 34, 39, 40, 41, 44, 52,
206 56, 58, 62, 65, 69, 74, 76, 79, 84, 94, 99, 117, 126, 128, 131, 136,
207 138, 144, 146, 148, 149, 152, 154, 157, 164, 165, 174, 180, 220, 251,
208 257, 259, 260, 262, 264, 275, 281, 287, 297, 301, 324, 331, 345, 349,
209 358, 366, 375, 387, 393, 424, 425, 436, 442, 450, 460, 470, 517, 518,
210 519, 520, 522, 528, 531, 533, 534, 540, 543, 545, 546]
211
212 digits_cosine_sieve_gains = [0.4861, 0.1082, 0.0127, 0.0322, 0.0112, 0.011,
213 0.0236, 0.0145, 0.0084, 0.0029, 0.0059, 0.0078, 0.0082, 0.0094, 0.0139,
214 0.0017, 0.0091, 0.0026, 0.0015, 0.0069, 0.0015, 0.0014, 0.0014, 0.0018,
215 0.0052, 0.0015, 0.0051, 0.0026, 0.0026, 0.0031, 0.002, 0.0014, 0.0024,
216 0.0012, 0.0028, 0.001, 0.0011, 0.0022, 0.0021, 0.0031, 0.0019, 0.0019,
217 0.0013, 0.001, 0.0017, 0.0012, 0.0017, 0.0009, 0.0009, 0.0011, 0.0016,
218 0.001, 0.0011, 0.0009, 0.0016, 0.0008, 0.0009, 0.0009, 0.0012, 0.001,
219 0.001, 0.0009, 0.0008, 0.001, 0.0008, 0.0009, 0.0009, 0.0009, 0.0008,
220 0.0009, 0.0011, 0.0008, 0.001, 0.0009, 0.0008, 0.0008, 0.0009, 0.0014,
221 0.0007, 0.0011, 0.0007, 0.0008, 0.001, 0.0018, 0.0006, 0.0006, 0.0007,
222 0.0008, 0.0008, 0.0006, 0.001, 0.0006, 0.0004, 0.0005, 0.0005, 0.0005,
223 0.0004, 0.0005, 0.0004, 0.0004]
224
225 # Test some similarity functions
226
227 def test_digits_euclidean_naive():
228 model = FacilityLocationSelection(100, 'euclidean', optimizer='naive')
229 model.fit(X_digits)
230 assert_array_equal(model.ranking[:30], digits_euclidean_ranking[:30])
231 assert_array_equal(model.ranking[-30:], digits_euclidean_ranking[-30:])
232 assert_array_almost_equal(model.gains, digits_euclidean_gains, 4)
233 assert_array_almost_equal(model.subset, X_digits[model.ranking])
234
235 def test_digits_euclidean_lazy():
236 model = FacilityLocationSelection(100, 'euclidean', optimizer='lazy')
237 model.fit(X_digits)
238 assert_array_equal(model.ranking, digits_euclidean_ranking)
239 assert_array_almost_equal(model.gains, digits_euclidean_gains, 4)
240 assert_array_almost_equal(model.subset, X_digits[model.ranking])
241
242 def test_digits_euclidean_two_stage():
243 model = FacilityLocationSelection(100, 'euclidean', optimizer='two-stage')
244 model.fit(X_digits)
245 assert_array_equal(model.ranking, digits_euclidean_ranking)
246 assert_array_almost_equal(model.gains, digits_euclidean_gains, 4)
247 assert_array_almost_equal(model.subset, X_digits[model.ranking])
248
249 def test_digits_corr_naive():
250 model = FacilityLocationSelection(100, 'corr', optimizer='naive')
251 model.fit(X_digits)
252 assert_array_equal(model.ranking, digits_corr_ranking)
253 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
254 assert_array_almost_equal(model.subset, X_digits[model.ranking])
255
256 def test_digits_corr_lazy():
257 model = FacilityLocationSelection(100, 'corr', optimizer='lazy')
258 model.fit(X_digits)
259 assert_array_equal(model.ranking, digits_corr_ranking)
260 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
261 assert_array_almost_equal(model.subset, X_digits[model.ranking])
262
263 def test_digits_corr_two_stage():
264 model = FacilityLocationSelection(100, 'corr', optimizer='two-stage')
265 model.fit(X_digits)
266 assert_array_equal(model.ranking, digits_corr_ranking)
267 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
268 assert_array_almost_equal(model.subset, X_digits[model.ranking])
269
270 def test_digits_cosine_naive():
271 model = FacilityLocationSelection(100, 'cosine', optimizer='naive')
272 model.fit(X_digits)
273 assert_array_equal(model.ranking, digits_cosine_ranking)
274 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
275 assert_array_almost_equal(model.subset, X_digits[model.ranking])
276
277 def test_digits_cosine_lazy():
278 model = FacilityLocationSelection(100, 'cosine', optimizer='lazy')
279 model.fit(X_digits)
280 assert_array_equal(model.ranking, digits_cosine_ranking)
281 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
282 assert_array_almost_equal(model.subset, X_digits[model.ranking])
283
284 def test_digits_cosine_two_stage():
285 model = FacilityLocationSelection(100, 'cosine', optimizer='two-stage')
286 model.fit(X_digits)
287 assert_array_equal(model.ranking, digits_cosine_ranking)
288 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
289 assert_array_almost_equal(model.subset, X_digits[model.ranking])
290
291 def test_digits_precomputed_naive():
292 model = FacilityLocationSelection(100, 'precomputed', optimizer='naive')
293 model.fit(X_digits_corr_cupy)
294 assert_array_equal(model.ranking, digits_corr_ranking)
295 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
296
297 def test_digits_precomputed_lazy():
298 model = FacilityLocationSelection(100, 'precomputed', optimizer='lazy')
299 model.fit(X_digits_corr_cupy)
300 assert_array_equal(model.ranking, digits_corr_ranking)
301 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
302
303 def test_digits_precomputed_two_stage():
304 model = FacilityLocationSelection(100, 'precomputed', optimizer='two-stage')
305 model.fit(X_digits_corr_cupy)
306 assert_array_equal(model.ranking, digits_corr_ranking)
307 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
308
309 # Test with initialization
310
311 def test_digits_euclidean_naive_init():
312 model = FacilityLocationSelection(100, 'euclidean', optimizer='naive',
313 initial_subset=digits_euclidean_ranking[:5])
314 model.fit(X_digits)
315 assert_array_equal(model.ranking[:20], digits_euclidean_ranking[5:25])
316 assert_array_almost_equal(model.gains[:20], digits_euclidean_gains[5:25], 4)
317 assert_array_almost_equal(model.subset, X_digits[model.ranking])
318
319 def test_digits_euclidean_lazy_init():
320 model = FacilityLocationSelection(100, 'euclidean', optimizer='lazy',
321 initial_subset=digits_euclidean_ranking[:5])
322 model.fit(X_digits)
323 assert_array_equal(model.ranking[:-5], digits_euclidean_ranking[5:])
324 assert_array_almost_equal(model.gains[:-5], digits_euclidean_gains[5:], 4)
325 assert_array_almost_equal(model.subset, X_digits[model.ranking])
326
327 def test_digits_euclidean_two_stage_init():
328 model = FacilityLocationSelection(100, 'euclidean', optimizer='two-stage',
329 initial_subset=digits_euclidean_ranking[:5])
330 model.fit(X_digits)
331 assert_array_equal(model.ranking[:-5], digits_euclidean_ranking[5:])
332 assert_array_almost_equal(model.gains[:-5], digits_euclidean_gains[5:], 4)
333 assert_array_almost_equal(model.subset, X_digits[model.ranking])
334
335 def test_digits_corr_naive_init():
336 model = FacilityLocationSelection(100, 'corr', optimizer='naive',
337 initial_subset=digits_corr_ranking[:5])
338 model.fit(X_digits)
339 assert_array_equal(model.ranking[:-5], digits_corr_ranking[5:])
340 assert_array_almost_equal(model.gains[:-5], digits_corr_gains[5:], 4)
341 assert_array_almost_equal(model.subset, X_digits[model.ranking])
342
343 def test_digits_corr_lazy_init():
344 model = FacilityLocationSelection(100, 'corr', optimizer='lazy',
345 initial_subset=digits_corr_ranking[:5])
346 model.fit(X_digits)
347 assert_array_equal(model.ranking[:-5], digits_corr_ranking[5:])
348 assert_array_almost_equal(model.gains[:-5], digits_corr_gains[5:], 4)
349 assert_array_almost_equal(model.subset, X_digits[model.ranking])
350
351 def test_digits_corr_two_stage_init():
352 model = FacilityLocationSelection(100, 'corr', optimizer='two-stage',
353 initial_subset=digits_corr_ranking[:5])
354 model.fit(X_digits)
355 assert_array_equal(model.ranking[:-5], digits_corr_ranking[5:])
356 assert_array_almost_equal(model.gains[:-5], digits_corr_gains[5:], 4)
357 assert_array_almost_equal(model.subset, X_digits[model.ranking])
358
359 def test_digits_cosine_naive_init():
360 model = FacilityLocationSelection(100, 'cosine', optimizer='naive',
361 initial_subset=digits_cosine_ranking[:5])
362 model.fit(X_digits)
363 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
364 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
365 assert_array_almost_equal(model.subset, X_digits[model.ranking])
366
367 def test_digits_cosine_lazy_init():
368 model = FacilityLocationSelection(100, 'cosine', optimizer='lazy',
369 initial_subset=digits_cosine_ranking[:5])
370 model.fit(X_digits)
371 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
372 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
373 assert_array_almost_equal(model.subset, X_digits[model.ranking])
374
375 def test_digits_cosine_two_stage_init():
376 model = FacilityLocationSelection(100, 'cosine', optimizer='two-stage',
377 initial_subset=digits_cosine_ranking[:5])
378 model.fit(X_digits)
379 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
380 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
381 assert_array_almost_equal(model.subset, X_digits[model.ranking])
382
383 def test_digits_precomputed_naive_init():
384 model = FacilityLocationSelection(100, 'precomputed', optimizer='naive',
385 initial_subset=digits_cosine_ranking[:5])
386 model.fit(X_digits_cosine_cupy)
387 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
388 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
389
390 def test_digits_precomputed_lazy_init():
391 model = FacilityLocationSelection(100, 'precomputed', optimizer='lazy',
392 initial_subset=digits_cosine_ranking[:5])
393 model.fit(X_digits_cosine_cupy)
394 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
395 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
396
397 def test_digits_precomputed_two_stage_init():
398 model = FacilityLocationSelection(100, 'precomputed', optimizer='two-stage',
399 initial_subset=digits_cosine_ranking[:5])
400 model.fit(X_digits_cosine_cupy)
401 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
402 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
403
404 # Test all optimizers
405
406 def test_digits_cosine_greedi_nn():
407 model = FacilityLocationSelection(100, 'cosine', optimizer='greedi',
408 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'naive'},
409 random_state=0)
410 model.fit(X_digits)
411 assert_array_equal(model.ranking[:50], digits_cosine_greedi_ranking[:50])
412 assert_array_almost_equal(model.gains[:50], digits_cosine_greedi_gains[:50], 4)
413 assert_array_almost_equal(model.subset, X_digits[model.ranking])
414
415 def test_digits_cosine_greedi_ll():
416 model = FacilityLocationSelection(100, 'cosine', optimizer='greedi',
417 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'lazy'},
418 random_state=0)
419 model.fit(X_digits)
420 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
421 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
422 assert_array_almost_equal(model.subset, X_digits[model.ranking])
423
424 def test_digits_cosine_greedi_ln():
425 model = FacilityLocationSelection(100, 'cosine', optimizer='greedi',
426 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'naive'},
427 random_state=0)
428 model.fit(X_digits)
429 assert_array_equal(model.ranking[:50], digits_cosine_greedi_ranking[:50])
430 assert_array_almost_equal(model.gains[:50], digits_cosine_greedi_gains[:50], 4)
431 assert_array_almost_equal(model.subset, X_digits[model.ranking])
432
433 def test_digits_cosine_greedi_nl():
434 model = FacilityLocationSelection(100, 'cosine', optimizer='greedi',
435 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'lazy'},
436 random_state=0)
437 model.fit(X_digits)
438 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
439 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
440 assert_array_almost_equal(model.subset, X_digits[model.ranking])
441
442 def test_digits_cosine_approximate():
443 model = FacilityLocationSelection(100, 'cosine', optimizer='approximate-lazy')
444 model.fit(X_digits)
445 assert_array_equal(model.ranking, digits_cosine_approx_ranking)
446 assert_array_almost_equal(model.gains, digits_cosine_approx_gains, 4)
447 assert_array_almost_equal(model.subset, X_digits[model.ranking])
448
449 def test_digits_cosine_stochastic():
450 model = FacilityLocationSelection(100, 'cosine', optimizer='stochastic',
451 random_state=0)
452 model.fit(X_digits)
453 assert_array_equal(model.ranking, digits_cosine_stochastic_ranking)
454 assert_array_almost_equal(model.gains, digits_cosine_stochastic_gains, 4)
455 assert_array_almost_equal(model.subset, X_digits[model.ranking])
456
457 def test_digits_cosine_sample():
458 model = FacilityLocationSelection(100, 'cosine', optimizer='sample',
459 random_state=0)
460 model.fit(X_digits)
461 assert_array_equal(model.ranking, digits_cosine_sample_ranking)
462 assert_array_almost_equal(model.gains, digits_cosine_sample_gains, 4)
463 assert_array_almost_equal(model.subset, X_digits[model.ranking])
464
465 def test_digits_cosine_modular():
466 model = FacilityLocationSelection(100, 'cosine', optimizer='modular',
467 random_state=0)
468 model.fit(X_digits)
469 assert_array_equal(model.ranking, digits_cosine_modular_ranking)
470 assert_array_almost_equal(model.gains, digits_cosine_modular_gains, 4)
471 assert_array_almost_equal(model.subset, X_digits[model.ranking])
472
473 # Using the partial_fit method
474
475 def test_digits_cosine_sieve_batch():
476 model = FacilityLocationSelection(100, 'cosine', random_state=0,
477 reservoir=X_digits)
478 model.partial_fit(X_digits)
479 assert_array_equal(model.ranking, digits_cosine_sieve_ranking)
480 assert_array_almost_equal(model.gains, digits_cosine_sieve_gains, 4)
481 assert_array_almost_equal(model.subset, X_digits[model.ranking])
482
483 def test_digits_cosine_sieve_minibatch():
484 model = FacilityLocationSelection(100, 'cosine', random_state=0,
485 reservoir=X_digits)
486 model.partial_fit(X_digits[:300])
487 model.partial_fit(X_digits[300:500])
488 model.partial_fit(X_digits[500:])
489 assert_array_equal(model.ranking, digits_cosine_sieve_ranking)
490 assert_array_almost_equal(model.gains, digits_cosine_sieve_gains, 4)
491 assert_array_almost_equal(model.subset, X_digits[model.ranking])
492
493 # Using Optimizer Objects
494
495 def test_digits_cosine_naive_object():
496 model = FacilityLocationSelection(100, 'cosine', optimizer=NaiveGreedy())
497 model.fit(X_digits)
498 assert_array_equal(model.ranking, digits_cosine_ranking)
499 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
500 assert_array_almost_equal(model.subset, X_digits[model.ranking])
501
502 def test_digits_cosine_lazy_object():
503 model = FacilityLocationSelection(100, 'cosine', optimizer=LazyGreedy())
504 model.fit(X_digits)
505 assert_array_equal(model.ranking, digits_cosine_ranking)
506 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
507 assert_array_almost_equal(model.subset, X_digits[model.ranking])
508
509 def test_digits_cosine_two_stage_object():
510 model = FacilityLocationSelection(100, 'cosine', optimizer=TwoStageGreedy())
511 model.fit(X_digits)
512 assert_array_equal(model.ranking, digits_cosine_ranking)
513 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
514 assert_array_almost_equal(model.subset, X_digits[model.ranking])
515
516 def test_digits_cosine_greedi_nn_object():
517 model = FacilityLocationSelection(100, 'cosine', optimizer=GreeDi(
518 optimizer1='naive', optimizer2='naive', random_state=0))
519 model.fit(X_digits)
520 assert_array_equal(model.ranking[:50], digits_cosine_greedi_ranking[:50])
521 assert_array_almost_equal(model.gains[:50], digits_cosine_greedi_gains[:50], 4)
522 assert_array_almost_equal(model.subset, X_digits[model.ranking])
523
524 def test_digits_cosine_greedi_ll_object():
525 model = FacilityLocationSelection(100, 'cosine', optimizer=GreeDi(
526 optimizer1='lazy', optimizer2='lazy', random_state=0))
527 model.fit(X_digits)
528 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
529 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
530 assert_array_almost_equal(model.subset, X_digits[model.ranking])
531
532 def test_digits_cosine_greedi_ln_object():
533 model = FacilityLocationSelection(100, 'cosine', optimizer=GreeDi(
534 optimizer1='lazy', optimizer2='naive', random_state=0))
535 model.fit(X_digits)
536 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
537 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
538 assert_array_almost_equal(model.subset, X_digits[model.ranking])
539
540 def test_digits_cosine_greedi_nl_object():
541 model = FacilityLocationSelection(100, 'cosine', optimizer=GreeDi(
542 optimizer1='naive', optimizer2='lazy', random_state=0))
543 model.fit(X_digits)
544 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
545 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
546 assert_array_almost_equal(model.subset, X_digits[model.ranking])
547
548 def test_digits_cosine_approximate_object():
549 model = FacilityLocationSelection(100, 'cosine',
550 optimizer=ApproximateLazyGreedy())
551 model.fit(X_digits)
552 assert_array_equal(model.ranking, digits_cosine_approx_ranking)
553 assert_array_almost_equal(model.gains, digits_cosine_approx_gains, 4)
554 assert_array_almost_equal(model.subset, X_digits[model.ranking])
555
556 def test_digits_cosine_stochastic_object():
557 model = FacilityLocationSelection(100, 'cosine',
558 optimizer=StochasticGreedy(random_state=0))
559 model.fit(X_digits)
560 assert_array_equal(model.ranking, digits_cosine_stochastic_ranking)
561 assert_array_almost_equal(model.gains, digits_cosine_stochastic_gains, 4)
562 assert_array_almost_equal(model.subset, X_digits[model.ranking])
563
564 def test_digits_cosine_sample_object():
565 model = FacilityLocationSelection(100, 'cosine',
566 optimizer=SampleGreedy(random_state=0))
567 model.fit(X_digits)
568 assert_array_equal(model.ranking, digits_cosine_sample_ranking)
569 assert_array_almost_equal(model.gains, digits_cosine_sample_gains, 4)
570 assert_array_almost_equal(model.subset, X_digits[model.ranking])
571
572 def test_digits_sqrt_modular_object():
573 model = FacilityLocationSelection(100, 'cosine',
574 optimizer=ModularGreedy(random_state=0))
575 model.fit(X_digits)
576 assert_array_equal(model.ranking, digits_cosine_modular_ranking)
577 assert_array_almost_equal(model.gains, digits_cosine_modular_gains, 4)
578 assert_array_almost_equal(model.subset, X_digits[model.ranking])
579
580 # Test all optimizers on sparse data
581
582 def test_digits_cosine_naive_sparse():
583 model = FacilityLocationSelection(100, 'precomputed', optimizer='naive')
584 model.fit(X_digits_cosine_sparse)
585 assert_array_equal(model.ranking, digits_cosine_ranking)
586 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
587
588 def test_digits_cosine_lazy_sparse():
589 model = FacilityLocationSelection(100, 'precomputed', optimizer='lazy')
590 model.fit(X_digits_cosine_sparse)
591 assert_array_equal(model.ranking, digits_cosine_ranking)
592 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
593
594 def test_digits_cosine_two_stage_sparse():
595 model = FacilityLocationSelection(100, 'precomputed', optimizer='two-stage')
596 model.fit(X_digits_cosine_sparse)
597 assert_array_equal(model.ranking, digits_cosine_ranking)
598 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
599
600 def test_digits_cosine_greedi_nn_sparse():
601 model = FacilityLocationSelection(100, 'precomputed', optimizer='greedi',
602 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'naive'},
603 random_state=0)
604 model.fit(X_digits_cosine_sparse)
605 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
606 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
607
608 def test_digits_cosine_greedi_ll_sparse():
609 model = FacilityLocationSelection(100, 'precomputed', optimizer='greedi',
610 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'lazy'},
611 random_state=0)
612 model.fit(X_digits_cosine_sparse)
613 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
614 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
615
616 def test_digits_cosine_greedi_ln_sparse():
617 model = FacilityLocationSelection(100, 'precomputed', optimizer='greedi',
618 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'naive'},
619 random_state=0)
620 model.fit(X_digits_cosine_sparse)
621 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
622 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
623
624 def test_digits_cosine_greedi_nl_sparse():
625 model = FacilityLocationSelection(100, 'precomputed', optimizer='greedi',
626 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'lazy'},
627 random_state=0)
628 model.fit(X_digits_cosine_sparse)
629 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
630 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
631
632 def test_digits_cosine_approximate_sparse():
633 model = FacilityLocationSelection(100, 'precomputed', optimizer='approximate-lazy')
634 model.fit(X_digits_cosine_sparse)
635 assert_array_equal(model.ranking, digits_cosine_approx_ranking)
636 assert_array_almost_equal(model.gains, digits_cosine_approx_gains, 4)
637
638 def test_digits_cosine_stochastic_sparse():
639 model = FacilityLocationSelection(100, 'precomputed', optimizer='stochastic',
640 random_state=0)
641 model.fit(X_digits_cosine_sparse)
642 assert_array_equal(model.ranking, digits_cosine_stochastic_ranking)
643 assert_array_almost_equal(model.gains, digits_cosine_stochastic_gains, 4)
644
645 def test_digits_cosine_sample_sparse():
646 model = FacilityLocationSelection(100, 'precomputed', optimizer='sample',
647 random_state=0)
648 model.fit(X_digits_cosine_sparse)
649 assert_array_equal(model.ranking, digits_cosine_sample_ranking)
650 assert_array_almost_equal(model.gains, digits_cosine_sample_gains, 4)
651
652 def test_digits_sqrt_modular_sparse():
653 model = FacilityLocationSelection(100, 'precomputed', optimizer='modular',
654 random_state=0)
655 model.fit(X_digits_cosine_sparse)
656 assert_array_equal(model.ranking, digits_cosine_modular_ranking)
657 assert_array_almost_equal(model.gains, digits_cosine_modular_gains, 4)
658