cross validation to find optimal value of K