Sample weight normalisation using Perceptron

Viewed 102

I was doing classification with Perceptron from Sklearn. When implementing uniform sample weights as an array of ones divided with a normalisation factor, I noticed the classification accuracy change even though uniform weights should have no effect on the results. I started experimenting and noticed that depending on what factor I use to divide the uniform weights, the results will change dramatically. If with the example code below I change the normalisation factor from 600 to 500, classification accuracy changes from 86 percent to 53 percent. Is this some floating point weirdness or some other subtelty going on behind the scenes, or have I stumbled on a bug? And if not a bug, what is a good practice to avoid such inconsistencies?

import numpy as np
from sklearn import datasets
from sklearn.linear_model import Perceptron

iris = datasets.load_iris()
X = iris.data[:, [2, 3]]
y = iris.target

ml = Perceptron()    
normalisation_factor = 500
weights = np.ones(len(y))/normalisation_factor
ml.fit(X, y, sample_weight = weights)
print("accuracy:", sum(ml.predict(X) == y)/len(y))
0 Answers
Related