def test_predict_marginalized_over_instances_mocked(self, rf_mock):
"""Use mock to count the number of calls to predict()"""
class SideEffect(object):
def __call__(self, X):
# Numpy array of number 0 to X.shape[0]
rval = np.array(list(range(X.shape[0]))).reshape((-1, 1))
# Return mean and variance
return rval, rval
rf_mock.side_effect = SideEffect()
rs = np.random.RandomState(1)
F = rs.rand(10, 5)
model = RandomForestWithInstances(np.zeros((15,), dtype=np.uint),
instance_features=F,
bounds=np.array(list(map(lambda x: (0, 10), range(10))), dtype=object))
means, vars = model.predict_marginalized_over_instances(rs.rand(11, 10))
self.assertEqual(rf_mock.call_count, 11)
self.assertEqual(means.shape, (11, 1))
self.assertEqual(vars.shape, (11, 1))
for i in range(11):
self.assertEqual(means[i], 4.5)
self.assertEqual(vars[i], 4.5)
评论列表
文章目录