Using a function within a class in python (to use self or not) -


class neuralnetwork(object):       def __init__(self, data):              self.data = data       def scan(self):          print(self.data)       def sigmoid(self, z):          g = 1 / (1 + math.exp(-z))          return (g)       a1 = sigmoid(7)      print a1 

i'm not sure why won't print a1 variable sigmoid function. keeps kicking off error saying requires 2 inputs instead of 1. thought calling function within class, didn'tneed supply self again?

edit: have last 2 statements in there because i'm still testing things out make sure doing it's supposed within class.

sigmoid method of neuralnetwork class, need create instance of neuralnetwork class first, before can utilize sigmoid function, if you're calling after class definition:

class neuralnetwork(object):      def __init__(self, data):              self.data = data       def scan(self):          print(self.data)       def sigmoid(self, z):          g = 1 / (1 + math.exp(-z))          return (g)  # replace data , z appropriate values nn = neuralnetwork(data) a1 = nn.sigmoid(z) print a1 

if need use within class, put block within method:

class neuralnetwork(object):      def __init__(self, data):              self.data = data       def scan(self):          print(self.data)       def sigmoid(self, z):          g = 1 / (1 + math.exp(-z))          return (g)       def print_sigmoid(self, z):          a1 = self.sigmoid(z)          print a1  # replace data , z appropriate values nn = neuralnetwork(data) nn.print_sigmoid(z) 

i recommend changing class name neuralnetwork, per pep 8 style guide: https://www.python.org/dev/peps/pep-0008/#class-names


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -