title: Optimization of K-fold cross validation for implicit recommendation systems
body: <p>I have been trying to test my recommendation system using k-fold cross validation. My recommendation system is based on implicit feedback.Since, I am trying to implement k-fold cross validation on my user-item matrix, I can't use scikit-learn's native k-fold methods(can I?). I am having trouble implementing my k-fold cross validation technique. I seem to be using a lot of for loops, and it's becoming very slow. I have gone through these links : <a href="https://stackoverflow.com/questions/31168115/optimize-this-function-with-numpy-or-other-vectorization-methods?lq=1">Optimize this function with numpy (or other vectorization methods)</a> and <a href="https://stackoverflow.com/questions/26403884/speed-up-for-loop-with-numpy?rq=1">Speed up for loop with numpy</a> but I can't seem to apply it to my code. Can somebody help me out?</p>
  
  <p>My code:</p>
  
  <pre><code> def TrainRepeat2(counts,FinalArr,k=3):
  
  
    """
    parameters:
    -------------------------------------------
  
    counts : user-item matrix
    k  :  No of folds
    FinalArr : shuffled indices
  
    Example:
    if k = 3
    FinalArr will be a list containing 3 lists with randomly shuffled indices
  """
  
  
     # No of factors
     num_factors= [10,20]
     PartitionList = range(k)
  
  
     # Iterating over the number of factors
     for i in range(len(num_factors)):
  
  
         # iterating over the folds
         for partition in PartitionList:
  
            # Keep one fold for testing
            validation = counts[FinalArr[partition],:]
  
           # CKeep the rest for training
            validation_list = [x for x in PartitionList if x != partition]
  
             # Train over the rest
             for t in validation_list:
  
               train = counts[FinalArr[t],:]
               train = sparse.csr_matrix(train)
               print "THe evaluation is being done for factor no %d" %(num_factors[i])
               reg_param = 5
  
               MF_als = ImplicitMF(train,validation,num_factors = num_factors[i],num_iterations=80,reg_param = reg_param,num_threads=14)
               user_vectors,item_vectors=  MF_als.train_model(flag,leaveone=False)
  </code></pre>
  
  <p>Specifically, the algorithm is o(N^3). I want to somehow remove the for loops and optimize the code.</p>
  
  <p>Any help would be appreciated</p>
  
  <p>Thanks!</p>