Debian Codegraph @

tests/test_functions/test_mixture_graph_based.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 MixtureSelection
10 from apricot import FacilityLocationSelection
11 from apricot import GraphCutSelection
12 from apricot.optimizers import *
13
14 from sklearn.datasets import load_digits
15 from sklearn.metrics import pairwise_distances
16
17 from numpy.testing import assert_almost_equal
18 from numpy.testing import assert_array_equal
19 from numpy.testing import assert_array_almost_equal
20
21 digits_data = load_digits()
22 X_digits = digits_data.data
23
24 X_digits_cosine_sparse = scipy.sparse.csr_matrix((1 - pairwise_distances(
25 X_digits, metric='cosine')) ** 2)
26 X_digits_corr_cupy = cupy.array((1 - pairwise_distances(
27 X_digits, metric='correlation')) ** 2)
28 X_digits_cosine_cupy = cupy.array((1 - pairwise_distances(
29 X_digits, metric='cosine')) ** 2)
30
31 digits_corr_ranking = [424, 615, 452, 514, 1030, 269, 1747, 1545, 1295, 148,
32 1363, 1327, 1766, 509, 852, 818, 890, 1774, 138, 1320, 945, 248, 255, 1709,
33 768, 402, 823, 899, 1658, 1069, 1647, 923, 183, 1325, 459, 168, 657, 301,
34 761, 513, 1040, 426, 478, 742, 1632, 423, 1071, 814, 1199, 649, 1617, 1423,
35 903, 1794, 114, 102, 339, 448, 1381, 1352, 1346, 254, 1340, 1781, 978, 898,
36 721, 41, 491, 816, 997, 1737, 522, 1688, 1075, 913, 1323, 332, 249, 1757,
37 244, 405, 1668, 836, 1276, 352, 74, 684, 1455, 1433, 417, 1453, 1796, 1409,
38 1639, 736, 508, 1752, 420, 1760]
39
40 digits_corr_gains = [957.5322, 330.2478, 254.6831, 251.206, 245.0905, 225.7705,
41 223.1784, 219.9972, 219.2199, 216.0972, 213.3351, 210.5622, 209.4755,
42 208.1544, 206.7405, 204.93, 204.522, 203.7405, 202.8789, 201.3152, 200.8991,
43 200.1773, 199.5643, 197.7437, 197.3154, 196.6495, 196.1545, 194.9538,
44 194.3206, 192.8184, 192.3224, 191.6689, 190.3903, 189.0685, 188.1243,
45 187.8275, 186.6411, 185.0932, 184.2903, 183.842, 183.3303, 182.6601,
46 181.9112, 180.7916, 180.3595, 179.6811, 179.4377, 178.8864, 178.0831,
47 177.3396, 176.8052, 176.3837, 175.9921, 175.4704, 174.8651, 174.4919,
48 173.9766, 173.1983, 172.7893, 172.5758, 171.5445, 171.1853, 170.9032,
49 170.4194, 169.8876, 169.1956, 168.6746, 168.3132, 167.8759, 167.4243,
50 166.7872, 165.9742, 165.5812, 165.0026, 164.6565, 164.167, 163.7766,
51 162.8939, 162.5745, 162.1263, 161.7664, 161.4983, 161.1224, 160.7753,
52 160.1076, 159.6627, 159.2873, 158.9297, 158.6049, 158.1738, 157.7336,
53 157.1569, 156.7355, 156.4312, 156.1812, 155.7283, 155.3701, 155.0292,
54 154.418, 154.0432]
55
56 digits_euclidean_ranking = [945, 1327, 448, 923, 276, 426, 1026, 651, 1583,
57 114, 148, 1423, 1295, 255, 515, 1363, 547, 232, 1544, 955, 699, 607, 404,
58 1058, 768, 1040, 296, 1491, 814, 686, 459, 668, 293, 737, 1459, 378, 654,
59 1346, 1443, 1320, 1637, 1433, 1455, 254, 674, 478, 773, 816, 1450, 183,
60 1617, 269, 482, 170, 205, 1658, 742, 1453, 120, 284, 394, 264, 1696, 248,
61 1361, 330, 1383, 829, 1537, 274, 514, 1486, 888, 40, 419, 1529, 505, 1763,
62 684, 53, 405, 997, 1532, 872, 544, 335, 933, 281, 508, 339, 574, 521, 539,
63 879, 1187, 328, 835, 697, 1123, 836]
64
65 digits_euclidean_gains = [9681446.3, 2484661.1, 2444711.8, 2293903.6, 2275176.2,
66 2235700.7, 2221801.7, 2207625.8, 2190108.0, 2177496.9, 2171467.0, 2166543.6,
67 2161585.6, 2153715.3, 2142803.6, 2134885.8, 2131767.5, 2126399.2, 2119268.2,
68 2115821.8, 2111996.6, 2107032.0, 2103319.7, 2096543.6, 2093119.3, 2087047.1,
69 2083877.7, 2080677.5, 2076910.3, 2073829.6, 2068572.5, 2065858.2, 2061714.3,
70 2057002.9, 2052908.2, 2049766.2, 2046509.7, 2043182.8, 2038671.2, 2033131.2,
71 2028417.4, 2025607.1, 2022035.2, 2018470.1, 2015333.3, 2010681.4, 2006781.6,
72 2002971.4, 1999041.2, 1995817.7, 1991182.0, 1987682.4, 1983833.3, 1980961.4,
73 1977563.7, 1974282.9, 1971292.9, 1968097.1, 1963874.4, 1959875.5, 1956835.8,
74 1953737.3, 1950422.9, 1946943.5, 1944247.4, 1940745.7, 1937767.0, 1934156.4,
75 1930520.8, 1926043.9, 1923402.3, 1920436.0, 1917812.6, 1914970.8, 1911951.5,
76 1908892.7, 1906314.3, 1903726.4, 1900097.3, 1897212.0, 1894373.1, 1889770.8,
77 1886297.2, 1883110.5, 1880186.7, 1877545.6, 1874497.6, 1871327.3, 1866850.1,
78 1863564.4, 1859998.0, 1857097.1, 1854710.4, 1850947.3, 1848393.1, 1845132.0,
79 1842794.7, 1837741.4, 1834617.1, 1831495.0]
80
81 digits_cosine_ranking = [424, 615, 1030, 402, 514, 1747, 148, 509, 1766, 818,
82 269, 1363, 768, 1295, 452, 890, 138, 1774, 852, 185, 255, 1069, 1709, 459,
83 248, 1327, 168, 513, 899, 923, 945, 423, 183, 1320, 1658, 823, 898, 491,
84 1325, 478, 1040, 742, 352, 978, 1071, 903, 1781, 426, 1796, 332, 657, 301,
85 1794, 913, 814, 417, 1075, 1647, 505, 1340, 254, 1668, 649, 1433, 420, 1423,
86 721, 854, 684, 309, 1199, 1632, 736, 836, 114, 1455, 224, 405, 1737, 1453,
87 453, 296, 500, 339, 693, 1705, 761, 1763, 997, 508, 1276, 1757, 816, 1323,
88 1026, 1596, 448, 1545, 76, 1726]
89
90 digits_cosine_gains = [1464.3202, 409.8125, 357.7419, 352.9304, 345.8749,
91 338.5499, 334.8922, 331.7992, 330.1259, 327.8876, 327.1878, 325.2868,
92 324.7755, 323.3606, 322.8546, 322.3117, 320.727, 319.4558, 317.9748,
93 316.9812, 315.9366, 314.6272, 313.954, 313.5249, 312.6064, 311.9777,
94 311.1738, 309.8506, 309.3497, 306.8928, 305.6599, 305.1896, 304.0379,
95 303.148, 302.6694, 302.075, 301.4275, 300.8761, 300.1981, 299.0383,
96 297.1896, 296.6456, 296.1498, 295.4601, 294.9541, 294.543, 293.2464,
97 292.4009, 291.8043, 291.1761, 290.7796, 289.1387, 288.6704, 288.0586,
98 287.4932, 286.8514, 286.4441, 285.8374, 285.2712, 284.6809, 284.2737,
99 283.4397, 283.055, 282.3092, 281.7608, 281.2045, 280.7276, 280.0204,
100 279.5876, 278.7367, 278.2878, 277.7987, 277.0733, 276.2545, 275.8347,
101 275.3892, 274.9112, 274.0533, 273.5174, 273.1525, 272.5565, 272.0778,
102 271.5116, 271.071, 270.6944, 270.1725, 269.7275, 269.0934, 268.5126,
103 268.0091, 267.5602, 266.9365, 266.2236, 265.8127, 265.2859, 264.8279,
104 264.4057, 263.872, 263.5162, 263.0823]
105
106 digits_cosine_greedi_ranking = [424, 1766, 148, 138, 402, 1363, 945, 1030, 509,
107 768, 1069, 1295, 514, 615, 1747, 818, 890, 513, 248, 269, 923, 852, 1774, 183,
108 452, 459, 1327, 899, 255, 426, 168, 1423, 1320, 1323, 1709, 978, 478, 1658,
109 913, 814, 352, 1325, 903, 423, 332, 742, 898, 761, 491, 420, 1796, 1433, 823,
110 419, 649, 1794, 505, 1781, 1040, 1759, 185, 997, 657, 339, 1340, 412, 1075,
111 17, 684, 854, 405, 522, 721, 1276, 1647, 1071, 301, 1682, 1545, 1726, 309, 41,
112 244, 1690, 457, 417, 1649, 453, 1428, 1793, 693, 109, 591, 1393, 948, 1704,
113 542, 52, 331, 1641]
114
115 digits_cosine_greedi_gains = [1464.3202, 407.5959, 349.2661, 330.6768,
116 344.9928, 332.7493, 321.2969, 343.6912, 330.5744, 328.727, 320.4867,
117 324.4858, 337.2288, 334.6355, 329.0956, 324.7697, 322.1504, 314.5276,
118 315.6515, 319.887, 310.8197, 316.3086, 316.9565, 308.2517, 318.3346,
119 312.5236, 311.5595, 309.844, 311.8585, 300.6039, 309.1181, 296.4878,
120 303.7425, 303.2108, 308.5394, 298.8348, 300.5579, 301.1397, 294.6515,
121 293.7151, 296.9657, 298.8378, 296.0371, 299.6898, 293.0309, 294.7472,
122 296.7244, 289.083, 296.3041, 287.9784, 291.1465, 286.9867, 294.649,
123 284.1909, 286.9657, 287.7189, 286.4448, 288.1401, 289.7308, 279.0076,
124 286.7833, 279.8429, 286.188, 280.8258, 282.8624, 277.1444, 279.7718,
125 273.966, 279.6056, 278.5453, 276.9876, 274.6063, 276.1162, 273.9862,
126 278.0959, 282.386, 275.5896, 268.7937, 270.7513, 271.8441, 274.0963,
127 265.894, 267.1621, 265.1507, 262.735, 274.3377, 258.5314, 266.2975,
128 263.023, 262.9865, 268.3422, 261.1778, 244.0778, 262.1116, 251.3201,
129 257.1708, 250.6925, 234.3935, 254.2639, 241.2629]
130
131 digits_cosine_approx_ranking = [424, 615, 452, 1295, 509, 890, 138, 185, 1030,
132 899, 1747, 269, 1766, 514, 1069, 852, 423, 923, 768, 459, 818, 1774, 491,
133 513, 352, 148, 1075, 945, 1327, 1363, 402, 1071, 823, 332, 978, 1423, 1709,
134 814, 248, 255, 254, 657, 1433, 913, 168, 1040, 309, 426, 508, 997, 1455,
135 296, 1199, 1325, 183, 686, 1647, 1453, 1658, 1737, 515, 1668, 736, 1320,
136 693, 898, 1794, 1757, 478, 742, 1781, 903, 1340, 1796, 301, 854, 453, 420,
137 649, 505, 339, 1632, 114, 761, 721, 76, 836, 224, 417, 1323, 1763, 1545,
138 1736, 405, 1276, 448, 684, 1705, 669, 419]
139
140 digits_cosine_approx_gains = [1464.3202, 409.8125, 355.8973, 337.1536, 338.603,
141 334.6332, 327.4835, 332.0559, 336.986, 324.492, 332.9814, 329.3203, 328.0008,
142 324.2584, 318.6579, 319.5194, 312.6497, 312.8434, 321.7955, 314.3747,
143 322.7808, 317.7776, 307.2386, 311.6358, 305.2872, 323.2564, 303.5155,
144 307.3318, 311.2204, 315.855, 311.7815, 302.8176, 302.4257, 298.5372,
145 299.4686, 294.7835, 307.7774, 294.6846, 306.0374, 306.9195, 293.0625,
146 295.0703, 291.1347, 292.6244, 302.8187, 295.166, 288.5487, 292.3242,
147 285.8501, 285.0467, 285.9749, 284.8052, 285.898, 293.2744, 293.7613,
148 281.2778, 286.542, 282.3119, 291.9742, 281.4734, 279.1016, 283.671,
149 280.9888, 285.1791, 278.7845, 288.5932, 282.5049, 276.7215, 286.8471,
150 283.8127, 283.0595, 283.2037, 278.5408, 281.077, 279.7131, 276.8673,
151 274.2516, 276.2174, 276.6875, 276.6524, 272.7957, 274.0943, 272.21,
152 273.1331, 270.5371, 268.6535, 270.6709, 270.2242, 273.2398, 267.4328,
153 267.6318, 266.2455, 266.4325, 267.2235, 265.6243, 264.7812, 267.3172,
154 265.0701, 262.8586, 262.9712]
155
156 digits_cosine_stochastic_ranking = [1081, 1014, 1386, 770, 567, 137, 723, 1491,
157 1274, 1492, 1728, 1456, 186, 1448, 386, 148, 891, 1759, 1424, 587, 191, 629,
158 1507, 1084, 1473, 946, 518, 638, 1739, 502, 1537, 1227, 689, 88, 238, 1667,
159 1785, 1067, 1461, 1222, 1099, 607, 364, 1572, 1195, 217, 1034, 208, 84, 1128,
160 425, 345, 626, 843, 1070, 83, 1449, 1071, 1644, 1392, 1415, 449, 802, 1348,
161 1553, 175, 1455, 1770, 1395, 1032, 879, 1220, 1137, 129, 754, 1695, 1459, 782,
162 549, 1069, 260, 834, 517, 919, 1622, 700, 424, 1685, 245, 1339, 1152, 1212,
163 1425, 937, 1665, 291, 1535, 701, 1508, 1219]
164
165 digits_cosine_stochastic_gains = [1155.9385, 254.8155, 360.3138, 282.6132,
166 236.3255, 266.8341, 350.2697, 322.4336, 206.871, 238.7681, 262.7077,
167 295.0445, 263.0537, 307.3779, 303.8213, 365.73, 213.7843, 317.021,
168 259.0621, 262.3689, 220.9726, 250.0447, 273.275, 254.7376, 236.2742,
169 206.0356, 262.7957, 240.9132, 262.5503, 242.8142, 293.9653, 283.4526,
170 193.3127, 269.5029, 235.4814, 270.887, 262.0968, 272.0255, 262.5282,
171 234.3473, 245.3481, 270.3055, 235.1375, 186.677, 210.5494, 255.2077,
172 255.2398, 288.0392, 242.4625, 240.773, 273.5747, 283.8104, 247.1762,
173 255.0, 210.2734, 232.8511, 251.2321, 298.9408, 262.5918, 246.5624,
174 241.7203, 269.0865, 277.7033, 241.7952, 248.3954, 251.7519, 286.4213,
175 245.3896, 248.4624, 232.8217, 263.4509, 225.2678, 206.3469, 246.6863,
176 231.6677, 269.3534, 268.5371, 252.8494, 244.3021, 296.5371, 221.1392,
177 263.0767, 212.6504, 204.1959, 233.5965, 266.8103, 306.1608, 221.5655,
178 207.5582, 209.8983, 217.4947, 228.2527, 244.365, 231.299, 213.8048,
179 213.5623, 248.0197, 252.3993, 174.956, 210.9899]
180
181 digits_cosine_sample_ranking = [424, 615, 1030, 514, 1747, 509, 1766, 768, 818, 269,
182 185, 1295, 890, 138, 1774, 852, 255, 1709, 1069, 522, 248, 1327, 168, 923, 423,
183 945, 183, 1658, 823, 491, 898, 1325, 478, 1075, 742, 1040, 352, 978, 903, 1071,
184 1781, 426, 1796, 332, 1794, 913, 1647, 301, 814, 417, 505, 1340, 420, 1668, 1423,
185 721, 684, 854, 309, 1632, 1199, 736, 1455, 224, 836, 114, 1453, 405, 1737, 296,
186 500, 339, 1705, 693, 761, 997, 1763, 508, 1276, 1026, 1323, 816, 448, 1596, 453,
187 419, 1726, 1545, 547, 1736, 1678, 370, 669, 686, 208, 515, 1284, 1759, 126, 74]
188
189 digits_cosine_sample_gains = [1464.3202, 409.8125, 357.7419, 349.7638, 344.019,
190 333.6616, 331.3564, 330.7733, 328.7989, 327.4326, 326.3364, 325.0169, 323.9197,
191 322.4965, 321.0188, 319.4314, 317.9578, 316.7807, 316.228, 315.0714, 314.6453,
192 313.9382, 313.1161, 310.1544, 309.103, 308.2081, 306.9797, 305.9571, 305.218,
193 304.5775, 304.0841, 303.3675, 302.3388, 301.3635, 300.06, 299.4714, 298.9754,
194 298.4509, 297.7341, 297.245, 296.1614, 295.4268, 294.7741, 294.0317, 292.4587,
195 291.8016, 291.3888, 290.926, 290.3355, 289.686, 288.8004, 288.2831, 287.6514,
196 287.0812, 286.4034, 285.4221, 284.8761, 284.2295, 283.6803, 283.0425, 282.4516,
197 281.8893, 281.3542, 280.6605, 280.1731, 279.7562, 279.0967, 278.4143, 277.7055,
198 277.2929, 276.8125, 276.2901, 275.857, 275.2616, 274.7802, 274.2633, 273.8096,
199 273.3452, 272.7006, 271.9561, 271.3491, 270.9224, 270.4983, 269.9322, 269.5459,
200 269.0294, 268.583, 267.9683, 267.4714, 266.9448, 266.634, 266.0754, 265.5448,
201 264.9206, 264.4581, 264.0199, 263.2466, 262.8614, 262.3174, 261.748]
202
203 digits_cosine_modular_ranking = [424, 148, 615, 1747, 1030, 1766, 818, 1363,
204 768, 509, 1774, 138, 1295, 402, 255, 1069, 1709, 852, 248, 1327, 168, 459,
205 890, 513, 899, 945, 452, 183, 423, 923, 1658, 1325, 491, 898, 742, 478,
206 352, 514, 823, 1040, 978, 903, 426, 1071, 1796, 269, 1794, 332, 1781, 657,
207 913, 814, 1340, 417, 254, 505, 1668, 1320, 1423, 684, 1433, 309, 114, 649,
208 420, 1455, 1453, 1647, 508, 224, 1632, 1705, 1763, 296, 301, 1737, 515,
209 836, 370, 1026, 997, 500, 1199, 736, 686, 1596, 76, 1757, 816, 693, 448,
210 339, 547, 721, 1726, 1678, 854, 405, 249, 1323]
211
212 digits_cosine_modular_gains = [1464.3202, 379.668, 373.6321, 354.1949, 345.765,
213 332.1021, 329.4361, 327.2775, 331.9908, 330.0034, 322.7988, 324.0683,
214 323.538, 335.74, 318.7241, 318.0685, 316.6991, 319.3237, 315.4338,
215 314.4588, 313.767, 327.8905, 319.4338, 313.3453, 314.5868, 309.0388,
216 317.2022, 306.7897, 309.8489, 307.1568, 308.8679, 303.1924, 304.2899,
217 302.6674, 299.7269, 303.5833, 299.1892, 304.3989, 300.4859, 297.4696,
218 296.7541, 296.2466, 294.5349, 295.234, 293.8342, 300.5235, 291.2804,
219 291.9456, 291.9794, 291.5869, 289.3387, 288.8333, 287.6984, 287.7781,
220 286.9242, 288.5137, 285.6325, 292.0567, 284.4706, 283.2179, 283.3712,
221 282.0468, 280.8433, 283.41, 281.9369, 279.4637, 278.3677, 281.9862,
222 276.8678, 277.6657, 278.8126, 275.824, 275.296, 274.9688, 278.1863, 274.86,
223 271.9642, 274.5517, 271.13, 271.3635, 271.4965, 271.554, 273.1052,
224 272.9791, 268.1288, 268.6275, 268.0731, 268.5637, 267.7105, 268.0723,
225 267.3179, 267.6352, 265.4032, 268.3584, 264.5365, 263.7466, 281.4908,
226 265.798, 260.3703, 263.9768]
227
228 # Test some similarity functions
229
230 def test_digits_euclidean_naive():
231 model1 = FacilityLocationSelection(100)
232 model2 = GraphCutSelection(100)
233 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
234 metric='euclidean', optimizer='naive')
235 model.fit(X_digits)
236 assert_array_equal(model.ranking, digits_euclidean_ranking)
237 assert_array_almost_equal(model.gains, digits_euclidean_gains, 4)
238 assert_array_almost_equal(model.subset, X_digits[model.ranking])
239
240 def test_digits_euclidean_lazy():
241 model1 = FacilityLocationSelection(100)
242 model2 = GraphCutSelection(100)
243 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
244 metric='euclidean', optimizer='lazy')
245 model.fit(X_digits)
246 assert_array_equal(model.ranking, digits_euclidean_ranking)
247 assert_array_almost_equal(model.gains, digits_euclidean_gains, 4)
248 assert_array_almost_equal(model.subset, X_digits[model.ranking])
249
250 def test_digits_euclidean_two_stage():
251 model1 = FacilityLocationSelection(100)
252 model2 = GraphCutSelection(100)
253 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
254 metric='euclidean', optimizer='two-stage')
255 model.fit(X_digits)
256 assert_array_equal(model.ranking, digits_euclidean_ranking)
257 assert_array_almost_equal(model.gains, digits_euclidean_gains, 4)
258 assert_array_almost_equal(model.subset, X_digits[model.ranking])
259
260 def test_digits_corr_naive():
261 model1 = FacilityLocationSelection(100)
262 model2 = GraphCutSelection(100)
263 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
264 metric='corr', optimizer='naive')
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_corr_lazy():
271 model1 = FacilityLocationSelection(100)
272 model2 = GraphCutSelection(100)
273 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
274 metric='corr', optimizer='lazy')
275 model.fit(X_digits)
276 assert_array_equal(model.ranking, digits_corr_ranking)
277 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
278 assert_array_almost_equal(model.subset, X_digits[model.ranking])
279
280 def test_digits_corr_two_stage():
281 model1 = FacilityLocationSelection(100)
282 model2 = GraphCutSelection(100)
283 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
284 metric='corr', optimizer='two-stage')
285 model.fit(X_digits)
286 assert_array_equal(model.ranking, digits_corr_ranking)
287 assert_array_almost_equal(model.gains, digits_corr_gains, 4)
288 assert_array_almost_equal(model.subset, X_digits[model.ranking])
289
290 def test_digits_cosine_naive():
291 model1 = FacilityLocationSelection(100)
292 model2 = GraphCutSelection(100)
293 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
294 metric='cosine', optimizer='naive')
295 model.fit(X_digits)
296 assert_array_equal(model.ranking, digits_cosine_ranking)
297 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
298 assert_array_almost_equal(model.subset, X_digits[model.ranking])
299
300 def test_digits_cosine_lazy():
301 model1 = FacilityLocationSelection(100)
302 model2 = GraphCutSelection(100)
303 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
304 metric='cosine', optimizer='lazy')
305 model.fit(X_digits)
306 assert_array_equal(model.ranking, digits_cosine_ranking)
307 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
308 assert_array_almost_equal(model.subset, X_digits[model.ranking])
309
310 def test_digits_cosine_two_stage():
311 model1 = FacilityLocationSelection(100)
312 model2 = GraphCutSelection(100)
313 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
314 metric='cosine', optimizer='two-stage')
315 model.fit(X_digits)
316 assert_array_equal(model.ranking, digits_cosine_ranking)
317 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
318 assert_array_almost_equal(model.subset, X_digits[model.ranking])
319
320 def test_digits_precomputed_naive():
321 model1 = FacilityLocationSelection(100)
322 model2 = GraphCutSelection(100)
323 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
324 metric='precomputed', optimizer='naive')
325 model.fit(X_digits_cosine_cupy)
326 assert_array_equal(model.ranking, digits_cosine_ranking)
327 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
328
329 def test_digits_precomputed_lazy():
330 model1 = FacilityLocationSelection(100)
331 model2 = GraphCutSelection(100)
332 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
333 metric='precomputed', optimizer='lazy')
334 model.fit(X_digits_cosine_cupy)
335 assert_array_equal(model.ranking, digits_cosine_ranking)
336 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
337
338 def test_digits_precomputed_two_stage():
339 model1 = FacilityLocationSelection(100)
340 model2 = GraphCutSelection(100)
341 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
342 metric='precomputed', optimizer='two-stage')
343 model.fit(X_digits_cosine_cupy)
344 assert_array_equal(model.ranking, digits_cosine_ranking)
345 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
346
347 # Test with initialization
348
349 def test_digits_euclidean_naive_init():
350 model1 = FacilityLocationSelection(100)
351 model2 = GraphCutSelection(100)
352 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
353 metric='euclidean', optimizer='naive',
354 initial_subset=digits_euclidean_ranking[:5])
355 model.fit(X_digits)
356 assert_array_equal(model.ranking[:20], digits_euclidean_ranking[5:25])
357 assert_array_almost_equal(model.gains[:20], digits_euclidean_gains[5:25], 4)
358 assert_array_almost_equal(model.subset, X_digits[model.ranking])
359
360 def test_digits_euclidean_lazy_init():
361 model1 = FacilityLocationSelection(100)
362 model2 = GraphCutSelection(100)
363 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
364 metric='euclidean', optimizer='lazy',
365 initial_subset=digits_euclidean_ranking[:5])
366 model.fit(X_digits)
367 assert_array_equal(model.ranking[:-5], digits_euclidean_ranking[5:])
368 assert_array_almost_equal(model.gains[:-5], digits_euclidean_gains[5:], 4)
369 assert_array_almost_equal(model.subset, X_digits[model.ranking])
370
371 def test_digits_euclidean_two_stage_init():
372 model1 = FacilityLocationSelection(100)
373 model2 = GraphCutSelection(100)
374 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
375 metric='euclidean', optimizer='two-stage',
376 initial_subset=digits_euclidean_ranking[:5])
377 model.fit(X_digits)
378 assert_array_equal(model.ranking[:-5], digits_euclidean_ranking[5:])
379 assert_array_almost_equal(model.gains[:-5], digits_euclidean_gains[5:], 4)
380 assert_array_almost_equal(model.subset, X_digits[model.ranking])
381
382 def test_digits_corr_naive_init():
383 model1 = FacilityLocationSelection(100)
384 model2 = GraphCutSelection(100)
385 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
386 metric='corr', optimizer='naive',
387 initial_subset=digits_corr_ranking[:5])
388 model.fit(X_digits)
389 assert_array_equal(model.ranking[:-5], digits_corr_ranking[5:])
390 assert_array_almost_equal(model.gains[:-5], digits_corr_gains[5:], 4)
391 assert_array_almost_equal(model.subset, X_digits[model.ranking])
392
393 def test_digits_corr_lazy_init():
394 model1 = FacilityLocationSelection(100)
395 model2 = GraphCutSelection(100)
396 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
397 metric='corr', optimizer='lazy',
398 initial_subset=digits_corr_ranking[:5])
399 model.fit(X_digits)
400 assert_array_equal(model.ranking[:-5], digits_corr_ranking[5:])
401 assert_array_almost_equal(model.gains[:-5], digits_corr_gains[5:], 4)
402 assert_array_almost_equal(model.subset, X_digits[model.ranking])
403
404 def test_digits_corr_two_stage_init():
405 model1 = FacilityLocationSelection(100)
406 model2 = GraphCutSelection(100)
407 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
408 metric='corr', optimizer='two-stage',
409 initial_subset=digits_corr_ranking[:5])
410 model.fit(X_digits)
411 assert_array_equal(model.ranking[:-5], digits_corr_ranking[5:])
412 assert_array_almost_equal(model.gains[:-5], digits_corr_gains[5:], 4)
413 assert_array_almost_equal(model.subset, X_digits[model.ranking])
414
415 def test_digits_cosine_naive_init():
416 model1 = FacilityLocationSelection(100)
417 model2 = GraphCutSelection(100)
418 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
419 metric='cosine', optimizer='naive',
420 initial_subset=digits_cosine_ranking[:5])
421 model.fit(X_digits)
422 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
423 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
424 assert_array_almost_equal(model.subset, X_digits[model.ranking])
425
426 def test_digits_cosine_lazy_init():
427 model1 = FacilityLocationSelection(100)
428 model2 = GraphCutSelection(100)
429 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
430 metric='cosine', optimizer='lazy',
431 initial_subset=digits_cosine_ranking[:5])
432 model.fit(X_digits)
433 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
434 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
435 assert_array_almost_equal(model.subset, X_digits[model.ranking])
436
437 def test_digits_cosine_two_stage_init():
438 model1 = FacilityLocationSelection(100)
439 model2 = GraphCutSelection(100)
440 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
441 metric='cosine', optimizer='two-stage',
442 initial_subset=digits_cosine_ranking[:5])
443 model.fit(X_digits)
444 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
445 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
446 assert_array_almost_equal(model.subset, X_digits[model.ranking])
447
448 def test_digits_precomputed_naive_init():
449 model1 = FacilityLocationSelection(100)
450 model2 = GraphCutSelection(100)
451 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
452 metric='precomputed', optimizer='naive',
453 initial_subset=digits_cosine_ranking[:5])
454 model.fit(X_digits_cosine_cupy)
455 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
456 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
457
458 def test_digits_precomputed_lazy_init():
459 model1 = FacilityLocationSelection(100)
460 model2 = GraphCutSelection(100)
461 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
462 metric='precomputed', optimizer='lazy',
463 initial_subset=digits_cosine_ranking[:5])
464 model.fit(X_digits_cosine_cupy)
465 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
466 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
467
468 def test_digits_precomputed_two_stage_init():
469 model1 = FacilityLocationSelection(100)
470 model2 = GraphCutSelection(100)
471 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
472 metric='precomputed', optimizer='two-stage',
473 initial_subset=digits_cosine_ranking[:5])
474 model.fit(X_digits_cosine_cupy)
475 assert_array_equal(model.ranking[:-5], digits_cosine_ranking[5:])
476 assert_array_almost_equal(model.gains[:-5], digits_cosine_gains[5:], 4)
477
478 # Test all optimizers
479
480 def test_digits_cosine_greedi_nn():
481 model1 = FacilityLocationSelection(100)
482 model2 = GraphCutSelection(100)
483 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
484 metric='cosine', optimizer='greedi',
485 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'naive'},
486 random_state=0)
487 model.fit(X_digits)
488 assert_array_equal(model.ranking[:50], digits_cosine_greedi_ranking[:50])
489 assert_array_almost_equal(model.gains[:50], digits_cosine_greedi_gains[:50], 4)
490 assert_array_almost_equal(model.subset, X_digits[model.ranking])
491
492 def test_digits_cosine_greedi_ll():
493 model1 = FacilityLocationSelection(100)
494 model2 = GraphCutSelection(100)
495 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
496 metric='cosine', optimizer='greedi',
497 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'lazy'},
498 random_state=0)
499 model.fit(X_digits)
500 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
501 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
502 assert_array_almost_equal(model.subset, X_digits[model.ranking])
503
504 def test_digits_cosine_greedi_ln():
505 model1 = FacilityLocationSelection(100)
506 model2 = GraphCutSelection(100)
507 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
508 metric='cosine', optimizer='greedi',
509 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'naive'},
510 random_state=0)
511 model.fit(X_digits)
512 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
513 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
514 assert_array_almost_equal(model.subset, X_digits[model.ranking])
515
516 def test_digits_cosine_greedi_nl():
517 model1 = FacilityLocationSelection(100)
518 model2 = GraphCutSelection(100)
519 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
520 metric='cosine', optimizer='greedi',
521 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'lazy'},
522 random_state=0)
523 model.fit(X_digits)
524 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
525 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
526 assert_array_almost_equal(model.subset, X_digits[model.ranking])
527
528 def test_digits_cosine_approximate():
529 model1 = FacilityLocationSelection(100)
530 model2 = GraphCutSelection(100)
531 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
532 metric='cosine', optimizer='approximate-lazy', random_state=0)
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 assert_array_almost_equal(model.subset, X_digits[model.ranking])
537
538 def test_digits_cosine_stochastic():
539 model1 = FacilityLocationSelection(100)
540 model2 = GraphCutSelection(100)
541 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
542 metric='cosine', optimizer='stochastic', random_state=0)
543 model.fit(X_digits)
544 assert_array_equal(model.ranking, digits_cosine_stochastic_ranking)
545 assert_array_almost_equal(model.gains, digits_cosine_stochastic_gains, 4)
546 assert_array_almost_equal(model.subset, X_digits[model.ranking])
547
548 def test_digits_cosine_sample():
549 model1 = FacilityLocationSelection(100)
550 model2 = GraphCutSelection(100)
551 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
552 metric='cosine', optimizer='sample', random_state=0)
553 model.fit(X_digits)
554 assert_array_equal(model.ranking, digits_cosine_sample_ranking)
555 assert_array_almost_equal(model.gains, digits_cosine_sample_gains, 4)
556 assert_array_almost_equal(model.subset, X_digits[model.ranking])
557
558 def test_digits_cosine_modular():
559 model1 = FacilityLocationSelection(100)
560 model2 = GraphCutSelection(100)
561 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
562 metric='cosine', optimizer='modular', random_state=0)
563 model.fit(X_digits)
564 assert_array_equal(model.ranking, digits_cosine_modular_ranking)
565 assert_array_almost_equal(model.gains, digits_cosine_modular_gains, 4)
566 assert_array_almost_equal(model.subset, X_digits[model.ranking])
567
568 # Using Optimizer Objects
569
570 def test_digits_cosine_naive_object():
571 model1 = FacilityLocationSelection(100)
572 model2 = GraphCutSelection(100)
573 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
574 metric='cosine', optimizer=NaiveGreedy(random_state=0))
575 model.fit(X_digits)
576 assert_array_equal(model.ranking, digits_cosine_ranking)
577 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
578 assert_array_almost_equal(model.subset, X_digits[model.ranking])
579
580 def test_digits_cosine_lazy_object():
581 model1 = FacilityLocationSelection(100)
582 model2 = GraphCutSelection(100)
583 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
584 metric='cosine', optimizer=LazyGreedy(random_state=0))
585 model.fit(X_digits)
586 assert_array_equal(model.ranking, digits_cosine_ranking)
587 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
588 assert_array_almost_equal(model.subset, X_digits[model.ranking])
589
590 def test_digits_cosine_two_stage_object():
591 model1 = FacilityLocationSelection(100)
592 model2 = GraphCutSelection(100)
593 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
594 metric='cosine', optimizer=TwoStageGreedy(random_state=0))
595 model.fit(X_digits)
596 assert_array_equal(model.ranking, digits_cosine_ranking)
597 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
598 assert_array_almost_equal(model.subset, X_digits[model.ranking])
599
600 def test_digits_cosine_greedi_nn_object():
601 model1 = FacilityLocationSelection(100)
602 model2 = GraphCutSelection(100)
603 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
604 metric='cosine', optimizer=GreeDi(optimizer1='naive',
605 optimizer2='naive', random_state=0))
606 model.fit(X_digits)
607 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
608 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
609 assert_array_almost_equal(model.subset, X_digits[model.ranking])
610
611 def test_digits_cosine_greedi_ll_object():
612 model1 = FacilityLocationSelection(100)
613 model2 = GraphCutSelection(100)
614 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
615 metric='cosine', optimizer=GreeDi(optimizer1='lazy',
616 optimizer2='lazy', random_state=0))
617 model.fit(X_digits)
618 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
619 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
620 assert_array_almost_equal(model.subset, X_digits[model.ranking])
621
622 def test_digits_cosine_greedi_ln_object():
623 model1 = FacilityLocationSelection(100)
624 model2 = GraphCutSelection(100)
625 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
626 metric='cosine', optimizer=GreeDi(optimizer1='lazy',
627 optimizer2='naive', random_state=0))
628 model.fit(X_digits)
629 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
630 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
631 assert_array_almost_equal(model.subset, X_digits[model.ranking])
632
633 def test_digits_cosine_greedi_nl_object():
634 model1 = FacilityLocationSelection(100)
635 model2 = GraphCutSelection(100)
636 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
637 metric='cosine', optimizer=GreeDi(optimizer1='naive',
638 optimizer2='lazy', random_state=0))
639 model.fit(X_digits)
640 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
641 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
642 assert_array_almost_equal(model.subset, X_digits[model.ranking])
643
644 def test_digits_cosine_approximate_object():
645 model1 = FacilityLocationSelection(100)
646 model2 = GraphCutSelection(100)
647 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
648 metric='cosine', optimizer=ApproximateLazyGreedy(random_state=0))
649 model.fit(X_digits)
650 assert_array_equal(model.ranking, digits_cosine_approx_ranking)
651 assert_array_almost_equal(model.gains, digits_cosine_approx_gains, 4)
652 assert_array_almost_equal(model.subset, X_digits[model.ranking])
653
654 def test_digits_cosine_stochastic_object():
655 model1 = FacilityLocationSelection(100)
656 model2 = GraphCutSelection(100)
657 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
658 metric='cosine', optimizer=StochasticGreedy(random_state=0))
659 model.fit(X_digits)
660 assert_array_equal(model.ranking, digits_cosine_stochastic_ranking)
661 assert_array_almost_equal(model.gains, digits_cosine_stochastic_gains, 4)
662 assert_array_almost_equal(model.subset, X_digits[model.ranking])
663
664 def test_digits_cosine_sample_object():
665 model1 = FacilityLocationSelection(100)
666 model2 = GraphCutSelection(100)
667 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
668 metric='cosine', optimizer=SampleGreedy(random_state=0))
669 model.fit(X_digits)
670 assert_array_equal(model.ranking, digits_cosine_sample_ranking)
671 assert_array_almost_equal(model.gains, digits_cosine_sample_gains, 4)
672 assert_array_almost_equal(model.subset, X_digits[model.ranking])
673
674 def test_digits_sqrt_modular_object():
675 model1 = FacilityLocationSelection(100)
676 model2 = GraphCutSelection(100)
677 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
678 metric='cosine', optimizer=ModularGreedy(random_state=0))
679 model.fit(X_digits)
680 assert_array_equal(model.ranking, digits_cosine_modular_ranking)
681 assert_array_almost_equal(model.gains, digits_cosine_modular_gains, 4)
682 assert_array_almost_equal(model.subset, X_digits[model.ranking])
683
684 # Test all optimizers on sparse data
685
686 def test_digits_cosine_naive_sparse():
687 model1 = FacilityLocationSelection(100)
688 model2 = GraphCutSelection(100)
689 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
690 metric='precomputed', optimizer='naive')
691 model.fit(X_digits_cosine_sparse)
692 assert_array_equal(model.ranking, digits_cosine_ranking)
693 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
694
695 def test_digits_cosine_lazy_sparse():
696 model1 = FacilityLocationSelection(100)
697 model2 = GraphCutSelection(100)
698 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
699 metric='precomputed', optimizer='lazy')
700 model.fit(X_digits_cosine_sparse)
701 assert_array_equal(model.ranking, digits_cosine_ranking)
702 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
703
704 def test_digits_cosine_two_stage_sparse():
705 model1 = FacilityLocationSelection(100)
706 model2 = GraphCutSelection(100)
707 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
708 metric='precomputed', optimizer='two-stage')
709 model.fit(X_digits_cosine_sparse)
710 assert_array_equal(model.ranking, digits_cosine_ranking)
711 assert_array_almost_equal(model.gains, digits_cosine_gains, 4)
712
713 def test_digits_cosine_greedi_nn_sparse():
714 model1 = FacilityLocationSelection(100)
715 model2 = GraphCutSelection(100)
716 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
717 metric='precomputed', optimizer='greedi', optimizer_kwds={
718 'optimizer1': 'naive', 'optimizer2': 'naive'}, random_state=0)
719 model.fit(X_digits_cosine_sparse)
720 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
721 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
722
723 def test_digits_cosine_greedi_ll_sparse():
724 model1 = FacilityLocationSelection(100)
725 model2 = GraphCutSelection(100)
726 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
727 metric='precomputed', optimizer='greedi', optimizer_kwds={
728 'optimizer1': 'lazy', 'optimizer2': 'lazy'}, random_state=0)
729 model.fit(X_digits_cosine_sparse)
730 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
731 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
732
733 def test_digits_cosine_greedi_ln_sparse():
734 model1 = FacilityLocationSelection(100)
735 model2 = GraphCutSelection(100)
736 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
737 metric='precomputed', optimizer='greedi',
738 optimizer_kwds={'optimizer1': 'lazy', 'optimizer2': 'naive'},
739 random_state=0)
740 model.fit(X_digits_cosine_sparse)
741 assert_array_equal(model.ranking, digits_cosine_greedi_ranking)
742 assert_array_almost_equal(model.gains, digits_cosine_greedi_gains, 4)
743
744 def test_digits_cosine_greedi_nl_sparse():
745 model1 = FacilityLocationSelection(100)
746 model2 = GraphCutSelection(100)
747 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
748 metric='precomputed', optimizer='greedi',
749 optimizer_kwds={'optimizer1': 'naive', 'optimizer2': 'lazy'},
750 random_state=0)
751 model.fit(X_digits_cosine_sparse)
752 assert_array_equal(model.ranking[:30], digits_cosine_greedi_ranking[:30])
753 assert_array_almost_equal(model.gains[:30], digits_cosine_greedi_gains[:30], 4)
754
755 def test_digits_cosine_approximate_sparse():
756 model1 = FacilityLocationSelection(100)
757 model2 = GraphCutSelection(100)
758 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
759 metric='precomputed', optimizer='approximate-lazy',
760 random_state=0)
761 model.fit(X_digits_cosine_sparse)
762 assert_array_equal(model.ranking, digits_cosine_approx_ranking)
763 assert_array_almost_equal(model.gains, digits_cosine_approx_gains, 4)
764
765 def test_digits_cosine_stochastic_sparse():
766 model1 = FacilityLocationSelection(100)
767 model2 = GraphCutSelection(100)
768 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
769 metric='precomputed', optimizer='stochastic', random_state=0)
770 model.fit(X_digits_cosine_sparse)
771 assert_array_equal(model.ranking, digits_cosine_stochastic_ranking)
772 assert_array_almost_equal(model.gains, digits_cosine_stochastic_gains, 4)
773
774 def test_digits_cosine_sample_sparse():
775 model1 = FacilityLocationSelection(100)
776 model2 = GraphCutSelection(100)
777 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
778 metric='precomputed', optimizer='sample', random_state=0)
779 model.fit(X_digits_cosine_sparse)
780 assert_array_equal(model.ranking, digits_cosine_sample_ranking)
781 assert_array_almost_equal(model.gains, digits_cosine_sample_gains, 4)
782
783 def test_digits_cosine_modular_sparse():
784 model1 = FacilityLocationSelection(100)
785 model2 = GraphCutSelection(100)
786 model = MixtureSelection(100, [model1, model2], [1.0, 0.3],
787 metric='precomputed', optimizer='modular', random_state=0)
788 model.fit(X_digits_cosine_sparse)
789 assert_array_equal(model.ranking, digits_cosine_modular_ranking)
790 assert_array_almost_equal(model.gains, digits_cosine_modular_gains, 4)