Optimizing numpy's ensue function.