Debian Codegraph @

tests/test_functions/test_sum_redundancy.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 import scipy
2 import numpy
3
4 try:
5 import cupy
6 except:
7 import numpy as cupy
8
9 from apricot import SumRedundancySelection
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 = [0, 1631, 1308, 1259, 1024, 673, 734, 1595, 75, 1078,
30 958, 447, 1195, 1221, 576, 1660, 998, 1274, 632, 1671, 1572, 1180, 1742,
31 1779, 713, 1514, 813, 988, 1576, 757, 1000, 1025, 1419, 1499, 133, 1551,
32 1580, 1316, 689, 1589, 756, 1462, 889, 413, 599, 1311, 1275, 1163, 54,
33 1657, 1400, 1001, 1264, 1495, 1302, 1154, 1473, 317, 1488, 194, 527, 1283,
34 675, 1604, 1077, 215, 766, 946, 1165, 403, 1115, 972, 1079, 1754, 914, 617,
35 57, 1182, 751, 1717, 876, 31, 103, 941, 1467, 218, 1440, 637, 982, 1708,
36 1272, 7, 966, 981, 163, 1562, 440, 341, 1301, 1216]
37
38 digits_corr_gains = [-1.0, -1.0053, -1.2226, -1.2786, -1.5351, -1.5826,
39 -1.7436, -2.2005, -2.2884, -2.5087, -2.7581, -2.8347, -3.0216, -3.3001,
40 -3.3921, -4.1251, -4.2028, -4.4885, -4.8339, -4.8857, -5.1816, -5.3797,
41 -5.7385, -6.03, -6.0674, -6.4577, -6.8554, -6.9494, -7.0362, -7.4869,
42 -7.6198, -8.117, -8.5624, -8.7402, -8.9086, -9.2393, -9.372, -9.7991,
43 -10.003, -10.3158, -10.7446, -10.8657, -11.3165, -11.518, -11.8186,
44 -11.8664, -12.24, -12.5102, -12.865, -13.1717, -13.5104, -13.611, -14.0063,
45 -14.3125, -14.4315, -14.9093, -15.4115, -15.5949, -15.8784, -15.9565,
46 -16.2537, -16.4321, -16.8201, -17.01, -17.5617, -17.6755, -18.0406,
47 -18.2885, -19.0408, -19.1925, -19.3148, -19.7779, -19.8668, -20.3401,
48 -20.4092, -21.0325, -21.2272, -21.472, -21.9653, -22.1756, -22.5028,
49 -22.8417, -23.1781, -23.34, -23.5919, -24.1087, -24.2592, -24.4434,
50 -24.7991, -25.0295, -25.2185, -25.8405, -26.3942, -26.5413, -26.8281,
51 -27.0728, -27.2003, -27.7439, -27.9688, -28.2785]
52
53 digits_euclidean_ranking = [0, 623, 163, 1572, 215, 241, 988, 1635, 1505, 766,
54 673, 67, 1296, 283, 947, 1589, 172, 832, 1001, 1259, 1495, 1111, 919, 1576,
55 141, 1710, 1437, 1106, 1308, 1595, 982, 317, 1274, 131, 1375, 204, 998, 788,
56 732, 756, 1462, 916, 155, 1086, 77, 851, 1574, 680, 263, 1436, 1205, 609,
57 1660, 1221, 1275, 1035, 1514, 953, 1754, 1604, 853, 1562, 1094, 1457, 1264,
58 1685, 218, 447, 1492, 689, 951, 235, 1272, 734, 1657, 494, 517, 1317, 210,
59 629, 1467, 1587, 1290, 926, 1735, 565, 1419, 1522, 801, 660, 1499, 1220,
60 757, 1000, 312, 1470, 135, 1070, 889, 1426]
61
62 digits_euclidean_gains = [-5935.0, -9777.0, -13543.0, -17943.0, -22857.0,
63 -27221.0, -33987.0, -37289.0, -42655.0, -44649.0, -50719.0, -54275.0,
64 -61769.0, -67425.0, -69983.0, -75243.0, -78809.0, -86093.0, -91183.0,
65 -95109.0, -100791.0, -106791.0, -111741.0, -116985.0, -121935.0,
66 -128081.0, -132819.0, -138007.0, -142801.0, -148589.0, -153975.0,
67 -157323.0, -164121.0, -168921.0, -174335.0, -179963.0, -184405.0,
68 -190255.0, -196089.0, -201213.0, -206047.0, -210323.0, -215441.0,
69 -221839.0, -227461.0, -233779.0, -238253.0, -243317.0, -248155.0,
70 -254819.0, -259613.0, -263847.0, -270369.0, -274331.0, -281473.0,
71 -286875.0, -292657.0, -296343.0, -303373.0, -308247.0, -314769.0,
72 -317825.0, -324991.0, -330459.0, -336565.0, -342469.0, -347659.0,
73 -353903.0, -358113.0, -365203.0, -370033.0, -376423.0, -379817.0,
74 -386027.0, -391541.0, -397755.0, -402413.0, -406833.0, -413851.0,
75 -420295.0, -425313.0, -430743.0, -437069.0, -442009.0, -445999.0,
76 -453759.0, -460717.0, -467225.0, -470579.0, -478611.0, -481381.0,
77 -486279.0, -492717.0, -501067.0, -507697.0, -511129.0, -517415.0,
78 -523749.0, -527895.0, -533187.0]
79
80 digits_cosine_ranking = [0, 1626, 1308, 1259, 1078, 673, 1595, 958, 75,
81 447, 1660, 1221, 1180, 734, 1589, 1462, 632, 1671, 1779, 1717, 133,
82 966, 914, 1274, 1514, 1000, 1035, 1580, 1373, 1311, 403, 1272, 946,
83 766, 215, 1611, 813, 876, 1572, 1316, 1631, 713, 1467, 1499, 1508,
84 941, 1264, 1275, 194, 1024, 527, 1079, 998, 1473, 1182, 990, 131,
85 1585, 204, 617, 675, 67, 1001, 1200, 31, 413, 341, 1025, 1213, 1301,
86 1576, 1400, 218, 317, 442, 1734, 192, 1551, 801, 1495, 1708, 889, 561,
87 1604, 4, 751, 704, 1314, 994, 155, 151, 981, 1125, 498, 57, 1480, 1742,
88 1562, 1195, 988]
89
90 digits_cosine_gains = [-1.0, -1.2608, -1.8589, -2.2945, -2.9785, -3.273,
91 -4.0554, -4.5342, -5.1995, -5.9721, -6.2496, -6.8113, -7.5169, -8.1488,
92 -9.0023, -9.2591, -10.006, -10.618, -11.3895, -11.7181, -12.7751, -13.2362,
93 -14.0317, -14.7067, -15.1397, -15.7079, -16.3536, -17.0946, -17.5568,
94 -18.2507, -19.4527, -20.1545, -20.7871, -21.4178, -21.7413, -22.5283,
95 -23.3034, -23.7622, -24.2979, -25.3123, -25.7659, -26.4916, -27.6042,
96 -28.0639, -28.5733, -29.3231, -30.0786, -30.4752, -31.2483, -31.8044,
97 -32.8744, -33.6345, -34.0621, -34.7931, -35.4228, -36.1701, -36.9265,
98 -37.5654, -38.2502, -38.9728, -39.8254, -40.4355, -41.1174, -41.7559,
99 -42.4764, -43.3304, -43.9796, -44.5423, -45.3739, -46.0711, -46.7337,
100 -47.2139, -47.7222, -48.8554, -49.4269, -50.3968, -51.0522, -51.5147,
101 -52.5101, -52.9929, -53.7409, -54.115, -55.3053, -55.9741, -56.6847,
102 -57.6243, -58.176, -58.5817, -59.7514, -60.3535, -60.7568, -61.6063,
103 -62.5969, -63.1069, -64.0203, -64.638, -65.5255, -66.1212, -66.8677,
104 -67.4475]
105
106 digits_cosine_greedi_ranking = [16, 617, 1660, 673, 1000, 1589, 1308, 1221,
107 1595, 1078, 447, 1626, 1514, 1259, 632, 133, 1671, 1717, 914, 75, 946,
108 734, 1779, 766, 1462, 1580, 1272, 1473, 1311, 1611, 1180, 1572, 876, 1024,
109 1631, 1274, 215, 1035, 403, 958, 1316, 966, 1182, 1373, 1508, 1025, 1467,
110 1499, 194, 1275, 1264, 813, 998, 67, 527, 1079, 131, 204, 990, 1585, 675,
111 713, 1200, 1301, 413, 192, 1001, 1400, 1576, 218, 1213, 981, 1734, 31, 341,
112 317, 1495, 889, 1708, 801, 1125, 442, 751, 1059, 1314, 4, 12, 1604, 1562,
113 704, 107, 561, 163, 988, 498, 1551, 1195, 1480, 994, 151]
114
115 digits_cosine_greedi_gains = [-1.0, -1.3478, -1.9394, -2.3069, -3.1291, -3.4523,
116 -3.811, -4.7084, -5.3596, -5.7415, -6.1306, -6.9337, -7.5382, -7.9192,
117 -8.7388, -9.5881, -10.0552, -10.5168, -11.4115, -12.0015, -12.4559, -13.359,
118 -13.957, -14.6866, -14.849, -15.9155, -16.5696, -17.298, -18.0227, -18.5558,
119 -19.1504, -19.9612, -20.7278, -21.2489, -22.0421, -22.5731, -23.1782, -23.8589,
120 -24.283, -25.1361, -25.8376, -26.5521, -27.0485, -28.0133, -28.5404, -29.4877,
121 -30.0386, -30.6349, -31.0334, -32.2181, -32.7685, -33.3918, -34.193, -34.753,
122 -35.6558, -36.0367, -36.7902, -37.8717, -38.4027, -38.9991, -39.478, -40.4198,
123 -41.1552, -41.7612, -42.363, -43.2004, -44.1362, -44.6401, -45.3337, -45.9022,
124 -46.5817, -47.0738, -48.3477, -48.9314, -49.5341, -50.1541, -51.1846, -51.5936,
125 -52.3633, -52.9651, -53.8957, -54.671, -55.369, -55.8204, -56.6435, -57.4165,
126 -58.2005, -59.0945, -59.66, -60.1562, -61.0494, -62.0454, -62.5839, -63.0563,
127 -64.1159, -64.557, -65.2267, -66.0529, -66.849, -67.6248]
128
129 digits_cosine_stochastic_ranking = [1081, 1014, 1386, 770, 567, 137, 723, 1491,
130 1274, 1492, 1728, 1456, 186, 1448, 386, 148, 891, 1759, 1424, 587, 191, 629,
131 1507, 1084, 1473, 946, 518, 638, 1739, 502, 1537, 1227, 689, 88, 238, 1667,
132 1785, 1067, 1461, 1222, 1099, 607, 364, 1572, 1195, 217, 1034, 208, 84,
133 1128, 425, 345, 626, 843, 1070, 83, 1449, 1071, 1644, 1392, 1415, 449, 802,
134 1348, 1553, 175, 1455, 1770, 1395, 1032, 879, 1220, 1137, 129, 754, 1695,
135 1459, 782, 549, 1069, 260, 834, 517, 919, 1622, 700, 424, 1685, 245, 1339,
136 1152, 1212, 1425, 937, 1665, 291, 1535, 701, 1508, 1219]
137
138 digits_cosine_stochastic_gains = [-1.0, -2.5269, -2.7995, -3.2931, -5.5216,
139 -6.0637, -7.7541, -8.1408, -7.2975, -10.4072, -11.2111, -10.3373, -10.6228,
140 -12.2424, -13.1709, -18.8132, -14.5518, -19.0894, -18.2777, -18.8472,
141 -17.3115, -23.1732, -23.7351, -26.0553, -18.8892, -17.7293, -29.2051,
142 -23.7703, -27.484, -27.9689, -34.4357, -33.4325, -23.4861, -31.3646,
143 -30.627, -35.9673, -36.9382, -38.8354, -37.5974, -33.8208, -38.1725,
144 -43.8267, -39.2434, -31.1408, -34.4416, -44.662, -43.0794, -51.7522,
145 -45.9076, -45.5261, -53.8562, -54.4264, -51.2669, -53.0412, -44.3672,
146 -52.5772, -53.4106, -66.2475, -61.2402, -58.9847, -59.9399, -65.6418,
147 -67.2847, -61.22, -63.0781, -66.7774, -76.5662, -67.3135, -69.2438,
148 -66.9263, -77.0899, -69.448, -57.6908, -71.3056, -69.0238, -82.4863,
149 -82.4559, -78.3968, -79.779, -96.6816, -69.961, -84.4731, -70.0465,
150 -69.5608, -81.6036, -94.3322, -109.4369, -80.056, -72.2626, -79.6127,
151 -80.3194, -86.0693, -95.2923, -89.2698, -82.4462, -84.8242, -99.9732,
152 -102.3375, -72.4036, -91.7773]
153
154 digits_cosine_sample_ranking = [0, 1626, 1308, 1589, 673, 734, 914, 75, 1660,
155 447, 1717, 1671, 1078, 133, 1221, 958, 1514, 1373, 1580, 766, 966, 1272,
156 1462, 1035, 1275, 1311, 632, 1180, 1631, 1576, 946, 1024, 215, 1274, 403,
157 527, 876, 1611, 617, 813, 998, 1499, 1508, 1079, 1473, 194, 317, 1264,
158 1316, 67, 1562, 1585, 204, 990, 675, 1314, 1301, 889, 218, 941, 413, 994,
159 1604, 4, 1025, 801, 341, 31, 131, 704, 442, 689, 155, 192, 751, 1163, 1400,
160 1552, 57, 1731, 561, 151, 163, 1734, 1648, 1495, 1587, 498, 972, 728, 1195,
161 988, 1125, 1304, 800, 1721, 7, 19, 1754, 981]
162
163 digits_cosine_sample_gains = [-1.0, -1.2608, -1.8589, -2.3718, -2.9235,
164 -3.3502, -4.1682, -4.7961, -5.3775, -5.8901, -6.4477, -6.8991, -7.7067,
165 -8.1207, -8.9752, -9.6247, -10.2187, -10.4458, -11.617, -12.2125, -12.9059,
166 -13.5264, -14.323, -14.9052, -15.3206, -16.1262, -16.5538, -17.6517,
167 -18.1862, -18.9705, -19.4022, -20.3438, -20.9218, -21.26, -22.1514,
168 -22.8178, -23.5192, -24.0593, -24.7916, -25.6778, -26.448, -27.0072,
169 -27.5813, -28.1622, -28.9791, -29.6623, -30.212, -30.8247, -31.4657,
170 -32.7805, -33.1168, -33.913, -34.5045, -35.0171, -35.7662, -36.9909,
171 -37.4624, -38.2663, -38.7272, -39.5656, -40.1986, -40.7718, -41.6487,
172 -42.6081, -43.0435, -43.8426, -44.2991, -45.0666, -45.9207, -46.4336,
173 -47.4731, -47.8693, -48.9462, -49.5734, -50.278, -51.0253, -51.7427,
174 -52.3166, -53.3424, -54.1884, -54.6391, -55.5147, -55.9356, -56.7033,
175 -57.3959, -58.1553, -58.8327, -59.6321, -60.1963, -60.9938, -61.5477,
176 -62.3124, -62.7948, -63.7351, -64.9801, -65.5436, -66.2565, -66.9823,
177 -67.8899, -68.5351]
178
179 digits_cosine_modular_ranking = [1796, 672, 590, 591, 592, 593, 594, 595,
180 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609,
181 610, 611, 612, 613, 614, 589, 588, 587, 573, 562, 563, 564, 565, 566,
182 567, 568, 569, 570, 571, 572, 574, 586, 575, 576, 577, 578, 579, 580,
183 581, 582, 583, 584, 585, 615, 616, 617, 658, 647, 648, 649, 650, 651,
184 652, 653, 654, 655, 656, 657, 659, 645, 660, 661, 662, 663, 664, 665,
185 666, 667, 668, 669, 670, 646, 644, 618, 630, 619, 620, 621, 622, 623,
186 624, 625, 626, 627, 628, 629, 631, 643]
187
188 digits_cosine_modular_gains = [-1.0, -2.3079, -2.789, -3.6932, -4.9088,
189 -5.7819, -6.3563, -8.4147, -8.0702, -8.105, -10.8682, -11.1989,
190 -12.8727, -14.6083, -12.2928, -15.4387, -18.6001, -18.2984, -19.3362,
191 -22.3696, -22.8144, -21.7067, -20.4596, -27.9798, -28.2063, -24.4073,
192 -28.4632, -27.6832, -28.5308, -27.8148, -31.0751, -30.0901, -36.1829,
193 -34.3228, -35.1394, -35.3716, -37.906, -31.2499, -40.2782, -41.9238,
194 -38.3051, -39.5617, -48.0495, -46.3859, -46.4503, -42.0932, -37.1236,
195 -54.9001, -52.8341, -44.635, -51.3953, -59.3985, -53.5856, -52.8414,
196 -52.4711, -68.5418, -52.824, -50.2893, -62.4484, -56.4849, -61.7257,
197 -70.4068, -50.5291, -64.1149, -61.7033, -60.5719, -78.3484, -55.4513,
198 -64.7636, -79.5183, -75.3014, -71.657, -61.1447, -70.4666, -67.1287,
199 -59.2271, -81.2681, -58.394, -83.9439, -86.8982, -80.2988, -90.9462,
200 -68.0999, -86.0352, -95.3056, -76.3657, -92.3353, -95.5004, -101.6048,
201 -100.966, -106.9129, -85.5593, -87.2622, -86.1141, -91.5066, -88.5264,
202 -78.4808, -99.1651, -102.2596, -101.259]
203
204 # Test some similarity functions
205
206 def test_digits_euclidean_naive():
207 model = SumRedundancySelection(100, 'euclidean', optimizer='naive')
208 model.fit(X_digits)
209 assert_array_equal(model.ranking, digits_euclidean_ranking)
210 assert_array_almost_equal(model.gains, digits_euclidean_gains, 4)
211 assert_array_almost_equal(model.subset, X_digits[model.ranking])
212
213 def test_digits_euclidean_lazy():
214 model = SumRedundancySelection(100, 'euclidean', optimizer='lazy')
215 model.fit(X_digits)
216 assert_array_equal(model.ranking, digits_euclidean_ranking)
217 assert_array_almost_equal(model.gains, digits_euclidean_gains, 4)
218 assert_array_almost_equal(model.subset, X_digits[model.ranking])
219
220 def test_digits_euclidean_two_stage():
221 model = SumRedundancySelection(100, 'euclidean', optimizer='two-stage')
222 model.fit(X_digits)
223 assert_array_equal(model.ranking, digits_euclidean_ranking)
224 assert_array_almost_equal(model.gains, digits_euclidean_gains, 4)
225 assert_array_almost_equal(model.subset, X_digits[model.ranking])
226
227 def test_digits_corr_naive():
228 model = SumRedundancySelection(100, 'corr', optimizer='naive')
229 model.fit(X_digits)
230 assert_array_equal(model.ranking, digits_corr_ranking)
231 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
232 assert_array_almost_equal(model.subset, X_digits[model.ranking])
233
234 def test_digits_corr_lazy():
235 model = SumRedundancySelection(100, 'corr', optimizer='lazy')
236 model.fit(X_digits)
237 assert_array_equal(model.ranking, digits_corr_ranking)
238 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
239 assert_array_almost_equal(model.subset, X_digits[model.ranking])
240
241 def test_digits_corr_two_stage():
242 model = SumRedundancySelection(100, 'corr', optimizer='two-stage')
243 model.fit(X_digits)
244 assert_array_equal(model.ranking, digits_corr_ranking)
245 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
246 assert_array_almost_equal(model.subset, X_digits[model.ranking])
247
248 def test_digits_cosine_naive():
249 model = SumRedundancySelection(100, 'cosine', optimizer='naive')
250 model.fit(X_digits)
251 assert_array_equal(model.ranking, digits_cosine_ranking)
252 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
253 assert_array_almost_equal(model.subset, X_digits[model.ranking])
254
255 def test_digits_cosine_lazy():
256 model = SumRedundancySelection(100, 'cosine', optimizer='lazy')
257 model.fit(X_digits)
258 assert_array_equal(model.ranking, digits_cosine_ranking)
259 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
260 assert_array_almost_equal(model.subset, X_digits[model.ranking])
261
262 def test_digits_cosine_two_stage():
263 model = SumRedundancySelection(100, 'cosine', optimizer='two-stage')
264 model.fit(X_digits)
265 assert_array_equal(model.ranking, digits_cosine_ranking)
266 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
267 assert_array_almost_equal(model.subset, X_digits[model.ranking])
268
269 def test_digits_precomputed_naive():
270 model = SumRedundancySelection(100, 'precomputed', optimizer='naive')
271 model.fit(X_digits_corr_cupy)
272 assert_array_equal(model.ranking, digits_corr_ranking)
273 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
274
275 def test_digits_precomputed_lazy():
276 model = SumRedundancySelection(100, 'precomputed', optimizer='lazy')
277 model.fit(X_digits_corr_cupy)
278 assert_array_equal(model.ranking, digits_corr_ranking)
279 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
280
281 def test_digits_precomputed_two_stage():
282 model = SumRedundancySelection(100, 'precomputed', optimizer='two-stage')
283 model.fit(X_digits_corr_cupy)
284 assert_array_equal(model.ranking, digits_corr_ranking)
285 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
286
287 # Test with initialization
288
289 def test_digits_euclidean_naive_init():
290 model = SumRedundancySelection(100, 'euclidean', optimizer='naive',
291 initial_subset=digits_euclidean_ranking[:5])
292 model.fit(X_digits)
293 assert_array_equal(model.ranking[:20], digits_euclidean_ranking[5:25])
294 assert_array_almost_equal(model.gains[:20], digits_euclidean_gains[5:25], 4)
295 assert_array_almost_equal(model.subset, X_digits[model.ranking])
296
297 def test_digits_euclidean_lazy_init():
298 model = SumRedundancySelection(100, 'euclidean', optimizer='lazy',
299 initial_subset=digits_euclidean_ranking[:5])
300 model.fit(X_digits)
301 assert_array_equal(model.ranking[:-5], digits_euclidean_ranking[5:])
302 assert_array_almost_equal(model.gains[:-5], digits_euclidean_gains[5:], 4)
303 assert_array_almost_equal(model.subset, X_digits[model.ranking])
304
305 def test_digits_euclidean_two_stage_init():
306 model = SumRedundancySelection(100, 'euclidean', optimizer='two-stage',
307 initial_subset=digits_euclidean_ranking[:5])
308 model.fit(X_digits)
309 assert_array_equal(model.ranking[:-5], digits_euclidean_ranking[5:])
310 assert_array_almost_equal(model.gains[:-5], digits_euclidean_gains[5:], 4)
311 assert_array_almost_equal(model.subset, X_digits[model.ranking])
312
313 def test_digits_corr_naive_init():
314 model = SumRedundancySelection(100, 'corr', optimizer='naive',
315 initial_subset=digits_corr_ranking[:5])
316 model.fit(X_digits)
317 assert_array_equal(model.ranking[:-5], digits_corr_ranking[5:])
318 assert_array_almost_equal(model.gains[:-5], digits_corr_gains[5:], 4)
319 assert_array_almost_equal(model.subset, X_digits[model.ranking])
320
321 def test_digits_corr_lazy_init():
322 model = SumRedundancySelection(100, 'corr', optimizer='lazy',
323 initial_subset=digits_corr_ranking[:5])
324 model.fit(X_digits)
325 assert_array_equal(model.ranking[:-5], digits_corr_ranking[5:])
326 assert_array_almost_equal(model.gains[:-5], digits_corr_gains[5:], 4)
327 assert_array_almost_equal(model.subset, X_digits[model.ranking])
328
329 def test_digits_corr_two_stage_init():
330 model = SumRedundancySelection(100, 'corr', optimizer='two-stage',
331 initial_subset=digits_corr_ranking[:5])
332 model.fit(X_digits)
333 assert_array_equal(model.ranking[:-5], digits_corr_ranking[5:])
334 assert_array_almost_equal(model.gains[:-5], digits_corr_gains[5:], 4)
335 assert_array_almost_equal(model.subset, X_digits[model.ranking])
336
337 def test_digits_cosine_naive_init():
338 model = SumRedundancySelection(100, 'cosine', optimizer='naive',
339 initial_subset=digits_cosine_ranking[:5])
340 model.fit(X_digits)
341 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
342 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
343 assert_array_almost_equal(model.subset, X_digits[model.ranking])
344
345 def test_digits_cosine_lazy_init():
346 model = SumRedundancySelection(100, 'cosine', optimizer='lazy',
347 initial_subset=digits_cosine_ranking[:5])
348 model.fit(X_digits)
349 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
350 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
351 assert_array_almost_equal(model.subset, X_digits[model.ranking])
352
353 def test_digits_cosine_two_stage_init():
354 model = SumRedundancySelection(100, 'cosine', optimizer='two-stage',
355 initial_subset=digits_cosine_ranking[:5])
356 model.fit(X_digits)
357 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
358 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
359 assert_array_almost_equal(model.subset, X_digits[model.ranking])
360
361 def test_digits_precomputed_naive_init():
362 model = SumRedundancySelection(100, 'precomputed', optimizer='naive',
363 initial_subset=digits_cosine_ranking[:5])
364 model.fit(X_digits_cosine_cupy)
365 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
366 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
367
368 def test_digits_precomputed_lazy_init():
369 model = SumRedundancySelection(100, 'precomputed', optimizer='lazy',
370 initial_subset=digits_cosine_ranking[:5])
371 model.fit(X_digits_cosine_cupy)
372 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
373 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
374
375 def test_digits_precomputed_two_stage_init():
376 model = SumRedundancySelection(100, 'precomputed', optimizer='two-stage',
377 initial_subset=digits_cosine_ranking[:5])
378 model.fit(X_digits_cosine_cupy)
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
382 # Test all optimizers
383
384 def test_digits_cosine_greedi_nn():
385 model = SumRedundancySelection(100, 'cosine', optimizer='greedi',
386 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'naive'},
387 random_state=0)
388 model.fit(X_digits)
389 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
390 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
391 assert_array_almost_equal(model.subset, X_digits[model.ranking])
392
393 def test_digits_cosine_greedi_ll():
394 model = SumRedundancySelection(100, 'cosine', optimizer='greedi',
395 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'lazy'},
396 random_state=0)
397 model.fit(X_digits)
398 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
399 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
400 assert_array_almost_equal(model.subset, X_digits[model.ranking])
401
402 def test_digits_cosine_greedi_ln():
403 model = SumRedundancySelection(100, 'cosine', optimizer='greedi',
404 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'naive'},
405 random_state=0)
406 model.fit(X_digits)
407 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
408 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
409 assert_array_almost_equal(model.subset, X_digits[model.ranking])
410
411 def test_digits_cosine_greedi_nl():
412 model = SumRedundancySelection(100, 'cosine', optimizer='greedi',
413 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'lazy'},
414 random_state=0)
415 model.fit(X_digits)
416 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
417 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
418 assert_array_almost_equal(model.subset, X_digits[model.ranking])
419
420 def test_digits_cosine_approximate():
421 pass
422 #model = SumRedundancySelection(100, 'cosine', optimizer='approximate-lazy')
423 #model.fit(X_digits)
424 #assert_array_equal(model.ranking, digits_cosine_approx_ranking)
425 #assert_array_almost_equal(model.gains, digits_cosine_approx_gains, 4)
426
427 def test_digits_cosine_stochastic():
428 model = SumRedundancySelection(100, 'cosine', optimizer='stochastic',
429 random_state=0)
430 model.fit(X_digits)
431 assert_array_equal(model.ranking, digits_cosine_stochastic_ranking)
432 assert_array_almost_equal(model.gains, digits_cosine_stochastic_gains, 4)
433 assert_array_almost_equal(model.subset, X_digits[model.ranking])
434
435 def test_digits_cosine_sample():
436 model = SumRedundancySelection(100, 'cosine', optimizer='sample',
437 random_state=0)
438 model.fit(X_digits)
439 assert_array_equal(model.ranking, digits_cosine_sample_ranking)
440 assert_array_almost_equal(model.gains, digits_cosine_sample_gains, 4)
441 assert_array_almost_equal(model.subset, X_digits[model.ranking])
442
443 def test_digits_sqrt_modular():
444 model = SumRedundancySelection(100, 'cosine', optimizer='modular',
445 random_state=0)
446 model.fit(X_digits)
447 assert_array_equal(model.ranking, digits_cosine_modular_ranking)
448 assert_array_almost_equal(model.gains, digits_cosine_modular_gains, 4)
449 assert_array_almost_equal(model.subset, X_digits[model.ranking])
450
451 # Using the partial_fit method
452
453 def test_digits_cosine_sieve_batch():
454 return
455 model = SumRedundancySelection(100, 'cosine', random_state=0,
456 reservoir=X_digits)
457 model.partial_fit(X_digits)
458 print("[" + ", ".join(map(str, model.ranking)) + "]")
459 print("[" + ", ".join([str(round(gain, 4)) for gain in model.gains]) + "]")
460 assert_array_equal(model.ranking, digits_cosine_sieve_ranking)
461 assert_array_almost_equal(model.gains, digits_cosine_sieve_gains, 4)
462 assert_array_almost_equal(model.subset, X_digits[model.ranking])
463
464 def test_digits_cosine_sieve_minibatch():
465 return
466 model = SumRedundancySelection(100, 'cosine', random_state=0,
467 reservoir=X_digits)
468 model.partial_fit(X_digits[:300])
469 model.partial_fit(X_digits[300:500])
470 model.partial_fit(X_digits[500:])
471 assert_array_equal(model.ranking, digits_cosine_sieve_ranking)
472 assert_array_almost_equal(model.gains, digits_cosine_sieve_gains, 4)
473 assert_array_almost_equal(model.subset, X_digits[model.ranking])
474
475 # Using Optimizer Objects
476
477 def test_digits_cosine_naive_object():
478 model = SumRedundancySelection(100, 'cosine', optimizer=NaiveGreedy())
479 model.fit(X_digits)
480 assert_array_equal(model.ranking, digits_cosine_ranking)
481 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
482 assert_array_almost_equal(model.subset, X_digits[model.ranking])
483
484 def test_digits_cosine_lazy_object():
485 model = SumRedundancySelection(100, 'cosine', optimizer=LazyGreedy())
486 model.fit(X_digits)
487 assert_array_equal(model.ranking, digits_cosine_ranking)
488 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
489 assert_array_almost_equal(model.subset, X_digits[model.ranking])
490
491 def test_digits_cosine_two_stage_object():
492 model = SumRedundancySelection(100, 'cosine', optimizer=TwoStageGreedy())
493 model.fit(X_digits)
494 assert_array_equal(model.ranking, digits_cosine_ranking)
495 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
496 assert_array_almost_equal(model.subset, X_digits[model.ranking])
497
498 def test_digits_cosine_greedi_nn_object():
499 model = SumRedundancySelection(100, 'cosine', optimizer=GreeDi(
500 optimizer1='naive', optimizer2='naive', random_state=0))
501 model.fit(X_digits)
502 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
503 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
504 assert_array_almost_equal(model.subset, X_digits[model.ranking])
505
506 def test_digits_cosine_greedi_ll_object():
507 model = SumRedundancySelection(100, 'cosine', optimizer=GreeDi(
508 optimizer1='lazy', optimizer2='lazy', random_state=0))
509 model.fit(X_digits)
510 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
511 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
512 assert_array_almost_equal(model.subset, X_digits[model.ranking])
513
514 def test_digits_cosine_greedi_ln_object():
515 model = SumRedundancySelection(100, 'cosine', optimizer=GreeDi(
516 optimizer1='lazy', optimizer2='naive', random_state=0))
517 model.fit(X_digits)
518 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
519 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
520 assert_array_almost_equal(model.subset, X_digits[model.ranking])
521
522 def test_digits_cosine_greedi_nl_object():
523 model = SumRedundancySelection(100, 'cosine', optimizer=GreeDi(
524 optimizer1='naive', optimizer2='lazy', random_state=0))
525 model.fit(X_digits)
526 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
527 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
528 assert_array_almost_equal(model.subset, X_digits[model.ranking])
529
530 def test_digits_cosine_approximate_object():
531 #model = SumRedundancySelection(100, 'cosine',
532 # optimizer=ApproximateLazyGreedy())
533 #model.fit(X_digits)
534 #assert_array_equal(model.ranking, digits_cosine_approx_ranking)
535 #assert_array_almost_equal(model.gains, digits_cosine_approx_gains, 4)
536 pass
537
538 def test_digits_cosine_stochastic_object():
539 model = SumRedundancySelection(100, 'cosine',
540 optimizer=StochasticGreedy(random_state=0))
541 model.fit(X_digits)
542 assert_array_equal(model.ranking, digits_cosine_stochastic_ranking)
543 assert_array_almost_equal(model.gains, digits_cosine_stochastic_gains, 4)
544 assert_array_almost_equal(model.subset, X_digits[model.ranking])
545
546 def test_digits_cosine_sample_object():
547 model = SumRedundancySelection(100, 'cosine',
548 optimizer=SampleGreedy(random_state=0))
549 model.fit(X_digits)
550 assert_array_equal(model.ranking, digits_cosine_sample_ranking)
551 assert_array_almost_equal(model.gains, digits_cosine_sample_gains, 4)
552 assert_array_almost_equal(model.subset, X_digits[model.ranking])
553
554 def test_digits_sqrt_modular_object():
555 model = SumRedundancySelection(100, 'cosine',
556 optimizer=ModularGreedy(random_state=0))
557 model.fit(X_digits)
558 assert_array_equal(model.ranking, digits_cosine_modular_ranking)
559 assert_array_almost_equal(model.gains, digits_cosine_modular_gains, 4)
560 assert_array_almost_equal(model.subset, X_digits[model.ranking])
561
562 # Test all optimizers on sparse data
563
564 def test_digits_cosine_naive_sparse():
565 model = SumRedundancySelection(100, 'precomputed', optimizer='naive')
566 model.fit(X_digits_cosine_sparse)
567 assert_array_equal(model.ranking, digits_cosine_ranking)
568 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
569
570 def test_digits_cosine_lazy_sparse():
571 model = SumRedundancySelection(100, 'precomputed', optimizer='lazy')
572 model.fit(X_digits_cosine_sparse)
573 assert_array_equal(model.ranking, digits_cosine_ranking)
574 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
575
576 def test_digits_cosine_two_stage_sparse():
577 model = SumRedundancySelection(100, 'precomputed', optimizer='two-stage')
578 model.fit(X_digits_cosine_sparse)
579 assert_array_equal(model.ranking, digits_cosine_ranking)
580 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
581
582 def test_digits_cosine_greedi_nn_sparse():
583 model = SumRedundancySelection(100, 'precomputed', optimizer='greedi',
584 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'naive'},
585 random_state=0)
586 model.fit(X_digits_cosine_sparse)
587 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
588 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
589
590 def test_digits_cosine_greedi_ll_sparse():
591 model = SumRedundancySelection(100, 'precomputed', optimizer='greedi',
592 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'lazy'},
593 random_state=0)
594 model.fit(X_digits_cosine_sparse)
595 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
596 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
597
598 def test_digits_cosine_greedi_ln_sparse():
599 model = SumRedundancySelection(100, 'precomputed', optimizer='greedi',
600 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'naive'},
601 random_state=0)
602 model.fit(X_digits_cosine_sparse)
603 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
604 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
605
606 def test_digits_cosine_greedi_nl_sparse():
607 model = SumRedundancySelection(100, 'precomputed', optimizer='greedi',
608 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'lazy'},
609 random_state=0)
610 model.fit(X_digits_cosine_sparse)
611 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
612 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
613
614 def test_digits_cosine_approximate_sparse():
615 pass
616 # Approximate doesn't work with supermodular functions.
617
618 #model = SumRedundancySelection(100, 'precomputed', optimizer='approximate-lazy')
619 #model.fit(X_digits_cosine_sparse)
620 #assert_array_equal(model.ranking, digits_cosine_approx_ranking)
621 #assert_array_almost_equal(model.gains, digits_cosine_approx_gains, 4)
622
623 def test_digits_cosine_stochastic_sparse():
624 model = SumRedundancySelection(100, 'precomputed', optimizer='stochastic',
625 random_state=0)
626 model.fit(X_digits_cosine_sparse)
627 assert_array_equal(model.ranking, digits_cosine_stochastic_ranking)
628 assert_array_almost_equal(model.gains, digits_cosine_stochastic_gains, 4)
629
630 def test_digits_cosine_sample_sparse():
631 model = SumRedundancySelection(100, 'precomputed', optimizer='sample',
632 random_state=0)
633 model.fit(X_digits_cosine_sparse)
634 assert_array_equal(model.ranking, digits_cosine_sample_ranking)
635 assert_array_almost_equal(model.gains, digits_cosine_sample_gains, 4)
636
637 def test_digits_sqrt_modular_sparse():
638 model = SumRedundancySelection(100, 'precomputed', optimizer='modular',
639 random_state=0)
640 model.fit(X_digits_cosine_sparse)
641 assert_array_equal(model.ranking, digits_cosine_modular_ranking)
642 assert_array_almost_equal(model.gains, digits_cosine_modular_gains, 4)