One way to compensate for the fact that the algorithm may be using neighbors that are too far away is to weight them according to their distance. This is similar to the method used in Chapter 2, where people's preferences were weighted according to how similar they were to the preferences of a person seeking a recommendation.
The more similar the items are, the smaller the distance between them, so you'll need a way of converting distances to weights. There are a few different ways of doing this, each with advantages and drawbacks. This section will look at three functions that you can use.
Figure 8-3. Inverse weight function
The simplest form of this function returns 1 divided by the distance. However, in some cases, items are exactly the same or very close, leading to a very high or infinite weight. For this reason, it's necessary to add a small number to the distance before inverting it.
function to numpredict.py:
def inverseweight(dist,num=1.0,const=0.1): return num/(dist+const)
This function is fast and easy to implement, and you can
experiment with different values of
num to see what produces good results. Its main potential drawback is that it ...