Saturday, August 17, 2013

Creating a price model using k-Nearest Neighbours + Genetic Algorithm

Chapter 8 of Programming Collective Intelligence (PCI) explains the usage and implementation of the k-Nearest Neighbours algorithm. (k-NN).

Simply put:
k-NN is a classification algorithm that uses (k) for the number of neighbours to determine what class an item will belong to.  To determine the neighbours to be used the algorithm uses a distance / similarity score function, in this example (Euclidian Distance).

PCI takes it a little further to help with accuracy in some scenarios. This includes the usage of a weighted average of the neighbours, as well as then using either simulated annealing or genetic algorithms to determine the best weights, building on Optimization techniques - Simulated Annealing & Genetic Algorithms

As with all the previous chapters the code is in my github repository.

So the similarity score function looked like (slightly different to the one used earlier, which was inverted to return 1 if equals):

The simulated annealing and genetic algorithm code I updated as I originally implemented them using Ints... (lesson learnt when doing anything it do with ML or AI, stick to doubles).

Then finally putting it all together my Java implementation of the PCI example

While reading up some more on k-NN I also stumbled upon the following blog posts
First one describing some of the difficulties around using k-NN.
k-Nearest Neighbors - dangerously simple

And then one giving a great overview of k-NN
A detailed introduction to k-NN algorithm

6 comments:

  1. Thanks for telling this story.

    ReplyDelete
  2. You create price model using algorithms and different languages. This post is very helpful for students. Thank you for sharing with us. Moreover, If you are worried locksmith work, Then you should hire Automotive Locksmith Services for quality work as your desire requirements.

    ReplyDelete
  3. This is a really interesting approach to price modeling! Combining k-NN with a genetic algorithm for weight optimization sounds incredibly powerful. I've messed around with k-NN for some classification tasks before, but never thought to integrate something like this for fine-tuning. It's a bit like playing Block Blast , trying to find the perfect arrangement for the best outcome. Definitely going to check out your GitHub for the code!

    ReplyDelete
  4. I love how this ties k-NN with optimization to squeeze better accuracy out of feature weights. I’ve been playing Infinite Craft lately, just experimenting with wild combinations like random elements turning into something totally unexpected, and it weirdly reminds me of tuning models through trial and surprise. When you switched from ints to doubles, did you notice a big improvement in the GA results?

    ReplyDelete

Building KubeSkippy: Learnings from a thought experiment

So, I got Claude Code Max and I thought of what would be the most ambitious thing I could try "vibe"? As my team looks after Kuber...