fixing predict function to handle sparse matrices properly