replace apply with good old for-loop. Fix training/test data split bug