Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
code--

C#
import numpy as np

def __init__(self, k = 3, eps = 0.0001):
        self.k = k ## number of clusters
        self.eps = eps ## threshold to stop `epsilon`
        


X=np.genfromtxt('D:\\M Tech\\ctech\\my_courses\\ELL784\\Assignment-3\\train_data.csv',delimiter=' ') #loading test data
print(X)
n, d = X.shape
print("n=",n  ,"d=",d)
clus=3
mu = X[np.random.choice(n, self.k, False), :]
print(mu)



output--

C#
[[ 1.09222171  1.89465052]
 [ 1.25575788 -1.59750016]
 [ 1.2305203   3.6342979 ]
 [ 0.69251878 -0.12405407]]
n= 4 d= 2
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-20-7bd596b4f068> in <module>()
     12 print("n=",n  ,"d=",d)
     13 clus=3
---> 14 mu = X[np.random.choice(n, self.k, False), :]
     15 print(mu)

NameError: name 'self' is not defined


What I have tried:

not able to find error,
I think something need to be corrected in _init_ function, but what is that ??
plz help.
Posted
Updated 9-Nov-16 4:20am
Comments
Richard MacCutchan 9-Nov-16 12:11pm    
See my updated solution.

The self object only exists within the scope of the __init__ method. Within the remaining code it is not defined. You need to create a different variable to store that value.

[edit]
See 9. Classes — Python 3.4.5 documentation[^].
[/edit]
 
Share this answer
 
v2
In Python, the __init__ method is used to initialize an object instance (semi-equivalent to a constructor in C++). By "self.k", you're saying that the variable "k" exists within some object. If you'd be trying to access that variable externally to the object, you'd use the ObjectName.k convention.

In the case of the code you posted though, you have an init method with no associated class, so not sure that code will even be called at all. I'm surprised that's not an automatic Python error actually, because it doesn't seem like it should be valid (probably just being ignored by the parser).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900