2

I am looking at the docs for the K-NN and found this piece of code:

X = [[0], [1], [2], [3]]
y = [0, 0, 1, 1]
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X, y)
KNeighborsClassifier(...)
print(neigh.predict([[3.1]]))

Looking at the class definition I see:

class sklearn.neighbors.KNeighborsClassifier(n_neighbors=5, *, weights='uniform', algorithm='auto', leaf_size=30, p=2, metric='minkowski', metric_params=None, n_jobs=None, **kwargs)

The second param is an * here. Does the ... in KNeighborsClassifier(...) and this * have something to do with each other and if so what?

sophros
  • 14,672
  • 11
  • 46
  • 75
SomeDutchGuy
  • 2,249
  • 4
  • 16
  • 42

1 Answers1

2

Asterisk

It is a new function arguments specification syntax introduced with PEP-3102 and Python 3.0 that indicates that the constructor of KNeighborsClassifier class does not take any other positional arguments after n_neighbors, and all of the remaining ones are keyword-only. The keyword-only arguments are provided with both name and value whereas positional arguments do not have to be. In this particular case you could legally use the following syntax (to provide value for n_neighbors):

KNeighborsClassifier(3)

but not:

KNeighborsClassifier(3, 'uniform')

weigths argument value have to be provided only in the following way:

KNeighborsClassifier(n_neighbors=5, weights='uniform')

But it would be allowed should the declaration had been:

sklearn.neighbors.KNeighborsClassifier(n_neighbors=5, weights='uniform', algorithm='auto', leaf_size=30, p=2, metric='minkowski', metric_params=None, n_jobs=None, **kwargs)

Three dots

Please see the SO thread What does the Ellipsis object do?

Usage in sklearn

It is not specific to either sklearn or this particular class.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
sophros
  • 14,672
  • 11
  • 46
  • 75