6
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 it won't print the a1 variable with the sigmoid function. It keeps kicking off an error saying that it requires 2 inputs instead of 1. But I thought that by calling the function within the class, I didn'tneed to supply self to it again?

Edit: I have the last two statements in there because I'm still testing things out to make sure that everything is doing what it's supposed to within the class.

8
  • 3
    Because sigmoid is a function within the class definition. Why did you indent a1 = .. and print a1 to be part of the class? Why not put def sigmoid outside of the class definition if it is not meant to be a method? Commented Apr 14, 2017 at 14:45
  • 1
    are you sure about the indentation of your two last line. Seems it should be out of the class. Commented Apr 14, 2017 at 14:46
  • If you were calling it from inside a class method, you would need to call it as self.sigmoid(7). If you wanted to call it from outside, then you would need to create an instance of Neuralnetwork to call obj.sigmoid(7) on. Commented Apr 14, 2017 at 14:46
  • You are right, you don't need self if you call from the class. But then, this call should occur from another method. You cannot have floating calls within a class as here. Commented Apr 14, 2017 at 14:47
  • 1
    see the two answers below, they should help you out Commented Apr 14, 2017 at 15:01

3 Answers 3

4

I notice that your sigmoid method does not use self at all, that is, it does not depend on the instance. You may put it outside the class as a normal function. But if it is closely related to the class, you may prefer to enclose it as a static method, removing completely the self from the sigmoid def:

#/usr/bin/env python3

import math

class NeuralNetwork(object):

    def __init__(self, data):    
        self.data = data

    def scan(self):
        print(self.data)

    @staticmethod
    def sigmoid(z):
        g = 1 / (1 + math.exp(-z))
        return (g)

a1 = NeuralNetwork('abc')
print(a1.sigmoid(7))
Sign up to request clarification or add additional context in comments.

Comments

1

sigmoid is a method of the Neuralnetwork class, so you need to create an instance of the Neuralnetwork class first, before you can utilize the sigmoid function, if you're calling it after the 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 and z with appropriate values
nn = Neuralnetwork(data)
a1 = nn.sigmoid(z)
print a1

If you need to use it within the class, put the block within a 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 and z with appropriate values
nn = Neuralnetwork(data)
nn.print_sigmoid(z)

I also recommend changing the class name to NeuralNetwork, as per the PEP 8 Style Guide: https://www.python.org/dev/peps/pep-0008/#class-names

2 Comments

Thank you! This makes a lot of sense.
You're welcome! @a1letterword Please accept/vote the answers if they solve your problem.
0

with the two last line outside your class (without indentation) you can modify them with:

a1 = Neuralnetwork(data).sigmoid(7)
print(a1)

but you have to give data to your class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.