Quesion Description
While reading scikit source code, got confused by following codes in line 149, 150 in classification.py under scikit-learn-0.15.2\sklearn\neighbors in scikit-learn-0.15.2.
classes_ = self.classes_
_y = self._y
Where classes_ is the classes of iris and _y is the sample’s targets.
Why create new fields instead of using them directly?
Explain
The reason is about that scope.
Here is an example to show this.
class A:
'test fields in Python'
def __init__(self, a=3):
self.a = a
print self.a
def f(self, b):
a = self.a + b
print a
return a, self.a, b
def p(self):
print self.a
aa = A(10)
c, _, d = aa.f(3)
aa.p()
print c, d
The output is
10
13
10
13, 3
Unlike java, in python, fields in a class are not available in methonds in the same class. So if you want to use them, use self and create new fields in methods.
By the way, we can use _ to ignore return values that we don’t want. This idea is from pridict function:
mode, _ = stats.mode(_y[neigh_ind, k], axis=1)