I've been trying to hyper tuning my KNNBasic algorithm by the help of grid search for recommendation system for movie review data. The problem is that both of my KNNBasicTuned and KNNBasicUntuned shows the same result. Here is my code for KNNTuning.. I have tried the SVD algo tuning and it worked perfectly so all my libraries are working perfectly. However my all libraries are in my github linke : https://github.com/iSarcastic99/KNNBasicTuning

Code of KNNBasicTuning :

enter image description here

# -*- coding: utf-8 -*-
Created on Sat Apr  4 01:25:40 2020

@author: rahulss
#My libraries

from MovieLens import MovieLens    
from surprise import KNNBasic
from surprise import NormalPredictor
from Evaluator import Evaluator
from surprise.model_selection import GridSearchCV

import random
import numpy as np

#loading my working data
def LoadMovieLensData():
    ml = MovieLens()
    print("Loading movie ratings...")
    data = ml.loadMovieLensLatestSmall()
    print("\nComputing movie popularity ranks so we can measure novelty later...")
    rankings = ml.getPopularityRanks()
    return (ml, data, rankings)


# Load up common data set for the recommender algorithms
(ml, evaluationData, rankings) = LoadMovieLensData()

print("Searching for best parameters...")
param_grid = {'n_epochs': [10, 30], 'lr_all': [0.005, 0.010],
          'n_factors': [50, 90]}
gs = GridSearchCV(KNNBasic, param_grid, measures=['rmse', 'mae'], cv=3)


# best RMSE score
print("Best RMSE score attained: ", gs.best_score['rmse'])

# combination of parameters that gave the best RMSE score

# Construct an Evaluator to, you know, evaluate them
evaluator = Evaluator(evaluationData, rankings)

params = gs.best_params['rmse']
KNNBasictuned = KNNBasic(n_epochs = params['n_epochs'], lr_all =  params['lr_all'], n_factors = params['n_factors'])
evaluator.AddAlgorithm(KNNBasictuned, "KNN - Tuned")

KNNBasicUntuned = KNNBasic()
evaluator.AddAlgorithm(KNNBasicUntuned, "KNN - Untuned")

# Evaluating all algorithms

evaluator.SampleTopNRecs(ml, testSubject=85, k=10)
  • $\begingroup$ Hi and welcome to AI SE! You say "The problem is that both of my KNNBasicTuned and KNNBasicUntuned shows the same result.", but what do you mean by "result"? $\endgroup$
    – nbro
    Apr 7, 2020 at 19:22
  • $\begingroup$ @nbro, Thanks for welcoming me. By result, I meant my RMSE and MAE score of both KNNBasicTuned and KNNBasicUntuned are same. $\endgroup$ Apr 7, 2020 at 20:58
  • $\begingroup$ All the parameters you set on the grid search are completely irrelevant for KNN. You probably get the same results cause you're always training the model with default parameters. The only parameters you can fine tune are k, min_k and eventually extra options (like the metric to use to compute the distances). Check the documentation link $\endgroup$ Apr 10, 2020 at 1:20
  • $\begingroup$ @EdoardoGuerriero, First thanks for trying to help. However, as you can see that i've used GridSearchCV and it requires the given parameters above. For more infor pls check the link here GridSearchCV $\endgroup$ Apr 10, 2020 at 21:04
  • $\begingroup$ @techPirate99 In a grid search you need to specify the parameters you want to tune, but a KNN is not trained using epochs, and it doesn't require a learning rate, it is not a neural network. I'll write an answer in an hour to show you what you can tune. $\endgroup$ Apr 10, 2020 at 21:17


You must log in to answer this question.