You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SVC and OneVsOneClassifier decision_function inconsistent on sub-sample #9174
I'm seeing inconsistent numerical results with SVC's decision_function.
When estimated over an entire batch of samples ( (n_samples, n_features) matrix ) compared to analyzing sample-by-sample, the results are not the same.
This is true for both the individual numerical values per sample and the overall distribution of the results.
The model is SVC with RBF kernel, for a 3-class classification:
import numpy as np
from sklearn.datasets import load_iris
data = load_iris()
y=data.target
X = data.data
from sklearn.svm import SVC
from sklearn.multiclass import OneVsRestClassifier
clf = SVC(decision_function_shape='ovr').fit(X, y)
dfall = clf.decision_function(X)
same = list()
for i in range(X.shape[0]):
dfone = clf.decision_function(X[i, :].reshape(1, -1))
same.append(np.all(dfone == dfall[i, :]))
print(same)
On 21 Jun 2017 12:57 am, "DanaAverbuch" ***@***.***> wrote:
Here you go:
import numpy as np
from sklearn.datasets import load_iris
data = load_iris()
y=data.target
X = data.data
from sklearn.svm import SVC
from sklearn.multiclass import OneVsRestClassifier
clf = SVC(decision_function_shape='ovr').fit(X, y)
dfall = clf.decision_function(X)
same = list()
for i in range(X.shape[0]):
dfone = clf.decision_function(X[i, :].reshape(1, -1))
same.append(np.all(dfone == dfall[i, :]))
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#9174 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEz67GSyNB8MXIYj0lriIQOdS85qAihks5sF93cgaJpZM4N_Pqa>
.
Is someone working on this? I dont really know what the expected result is in this case. It is 'normal' that the results are not the same given that we use the max and the min of sum_of_confidences which are different for X[i] and X.
determine the scale used in _ovr_decision_function from the entire training set or a sample. But I'm not sure whether we then need to clip to ensure the score stays in (-.5, +.5) or whether doing so should be avoided as it would break ties differently, i.e. without regards to the outlying distance.
changed the title [-]SVC decision_function inconsistent numerical values[/-][+]SVC and OneVsOneClassifier decision_function inconsistent on sub-sample[/+]on Jan 8, 2018
I would like to propose a PR for this issue. I think the scale should be done sample by sample as this would ensure that the score always stays in (-.5, +.5) for every sample.
No, the point of the current implementation is that the rank across columns of the decision function should be meaningful as well as the rank across rows.
maybe we can apply a sigmoid to map the confidence from (-inf,+inf) to (-.5,+.5) which respect the order. (exp(x) - 1)/2(exp(x) + 1)
this will respect the rank across columns and rows of the decision function and can be computed sample by sample.
a sigmoid makes sense, I think, although it might break backwards
compatibility more. A PR is welcome. I don't know if the current logic is
reasonably well tested anywhere; certainly not in utils/tests. This should
change, and I'd appreciate if you start with testing the properties of the
old implementation (or verifying that such tests exist) before replacing it.
yes, I suppose it checks the main properties on a small sample. It assumes
you can't control the ovo output, while testing the helper directly would
allow us to do so and hence be more rigorous.
I wonder if anyone can comment if this is related to an issue I'm having. The decision_function in SVC is returning different values depending on the number of observations it is provided with. I've attempted to give a short example below (using only synthetic data). For me the results of the first line in the array is different in each case.
import numpy as np
from sklearn.svm import SVC
X_train = np.vstack((np.random.randn(10,2), np.random.randn(10,2)+np.array([2,2]) , np.random.randn(10,2)+np.array([2,0])))
y_train = np.hstack( (np.zeros(10),np.ones(10), np.ones(10)*2 ) )
svm = SVC()
svm.fit(X_train, y_train)
X_tt = np.array([[-1,-2],[-1,-3], [2,2], [1,3]])
print("Just as for the decision function of the first point:")
print(svm.decision_function(X_tt[0,:].reshape(1,-1)))
print("\n\nTry asking for all the points together")
print(svm.decision_function(X_tt))
Activity
DanaAverbuch commentedon Jun 20, 2017
Hi sklearn owners,
Is it posible the problam is in lines 422 and 423?
It seems the lack of axis argument is influencing the downstream calculations.
Should this function cope with a single sample array?
amueller commentedon Jun 20, 2017
Could you please provide a self-contained example that generates the model from built-in or synthetic data?
DanaAverbuch commentedon Jun 20, 2017
Here you go:
jnothman commentedon Jun 20, 2017
albertcthomas commentedon Jul 6, 2017
Is someone working on this? I dont really know what the expected result is in this case. It is 'normal' that the results are not the same given that we use the max and the min of
sum_of_confidenceswhich are different forX[i]andX.jnothman commentedon Jul 6, 2017
jnothman commentedon Jan 8, 2018
This messes with a lot. The current behaviour is clearly problematic. I think we should:
_ovr_decision_functionfrom the entire training set or a sample. But I'm not sure whether we then need to clip to ensure the score stays in (-.5, +.5) or whether doing so should be avoided as it would break ties differently, i.e. without regards to the outlying distance.[-]SVC decision_function inconsistent numerical values[/-][+]SVC and OneVsOneClassifier decision_function inconsistent on sub-sample[/+]Johayon commentedon Jan 9, 2018
I would like to propose a PR for this issue. I think the scale should be done sample by sample as this would ensure that the score always stays in (-.5, +.5) for every sample.
jnothman commentedon Jan 9, 2018
No, the point of the current implementation is that the rank across columns of the decision function should be meaningful as well as the rank across rows.
Johayon commentedon Jan 9, 2018
ok, I did not understand that.
Johayon commentedon Jan 9, 2018
maybe we can apply a sigmoid to map the confidence from
(-inf,+inf)to(-.5,+.5)which respect the order.(exp(x) - 1)/2(exp(x) + 1)this will respect the rank across columns and rows of the decision function and can be computed sample by sample.
jnothman commentedon Jan 9, 2018
Johayon commentedon Jan 9, 2018
the
test_ovr_decision_functionseems to test the current logic well enough.scikit-learn/sklearn/svm/tests/test_svm.py
Lines 938 to 977 in 6fdcb3b
jnothman commentedon Jan 9, 2018
harold-jeffreys commentedon Dec 4, 2018
I wonder if anyone can comment if this is related to an issue I'm having. The decision_function in SVC is returning different values depending on the number of observations it is provided with. I've attempted to give a short example below (using only synthetic data). For me the results of the first line in the array is different in each case.
jnothman commentedon Dec 9, 2018
Sounds like the same issue. Does #10440 get more consistent results?
glemaitre commentedon Jan 28, 2019
#10440 give consistent results.