def test_accuracy_runner(): # Set up data that should deliver accuracy of 0.20 if all goes right class MockAlgo: def fit(self, X, y): return def predict(self, X): nr = X.shape[0] res = np.zeros(nr) res[0:int(nr / 5.0)] = 1.0 return res pair = algorithms.AlgorithmPair( MockAlgo, MockAlgo, shared_args={}, name="Mock", accuracy_function=metrics.accuracy_score, ) runner = AccuracyComparisonRunner([20], [5], dataset_name='zeros', test_fraction=0.20) results = runner.run(pair)[0] assert results["cuml_acc"] == pytest.approx(0.80)
def test_speedup_runner(): class MockAlgo: def __init__(self, t): self.t = t def fit(self, X, y): time.sleep(self.t) return def predict(self, X): nr = X.shape[0] res = np.zeros(nr) res[0:int(nr / 5.0)] = 1.0 return res class FastMockAlgo(MockAlgo): def __init__(self): MockAlgo.__init__(self, 0.1) class SlowMockAlgo(MockAlgo): def __init__(self): MockAlgo.__init__(self, 2) pair = algorithms.AlgorithmPair( SlowMockAlgo, FastMockAlgo, shared_args={}, name="Mock", bench_func=fit_predict, accuracy_function=metrics.accuracy_score, ) runner = SpeedupComparisonRunner( [20], [5], dataset_name='zeros' ) results = runner.run(pair)[0] expected_speedup = SlowMockAlgo().t / FastMockAlgo().t assert results["speedup"] == pytest.approx(expected_speedup, 0.4)
def test_multi_reps(): class CountingAlgo: tot_reps = 0 def fit(self, X, y): CountingAlgo.tot_reps += 1 pair = algorithms.AlgorithmPair( CountingAlgo, CountingAlgo, shared_args={}, name="Counting", ) runner = AccuracyComparisonRunner([20], [5], dataset_name='zeros', test_fraction=0.20, n_reps=4) runner.run(pair) # Double the n_reps since it is used in cpu and cuml versions assert CountingAlgo.tot_reps == 8