0

I'm studiyng Adaline GSD algorithm and I have an error that says TypeError: activation () takes 1 positional argument but 2 were given. How I interpretate that? There are two variables in that line?

I know my code looks long, but most are methods

My code is:

    def __init__(self, eta=0.01, n_iter=50, shuffle=True, random_state =None) :
        self.eta = eta
        self.n_iter=n_iter
        self.w_initialized=False
        self.shuffle=True
        self.random_state = random_state 

    def fit(self, X, y):
        self._initialize_weights(X.shape[1])
        self.cost_=[]
        for i in range (self.n_iter):
            if self.shuffle:
                X, y= self._shuffle(X,y)
            cost =[]
            for xi, target in zip(X,y):
                cost.append(self._update_weights(xi,target))
            avg_cost=sum(cost)/len(y)
            self.cost._append(avg_cost)
        return self

    def partial_fit(self,X,y):
        if not self.w_initialized:
            self._initialize_weights(X.shape[1])
        if y.ravel().shape[0]>1:
            for xi, target in zip(X,y):
                self._update_weights(xi, target)

        else:
            self._update_weights(X,y)
        return self

    def _shuffle(self,X,y):
        r=self.rgen.permutation(len(y))
        return X[r], y[r]

    def _initialize_weights(self,m):
        self.rgen= np.random.RandomState(self.random_state)
        self.w_=self.rgen.normal(loc= 0.0, scale= 0.01, size= 1+m)
        self.w_initialized=True

    def _update_weights(self, xi, target):
        output= self.activation(self.net_input(xi))
        error=(target- output)
        self.w_[1:] += self.eta * xi.dot(error)
        self.w_[0] += self.eta * error
        cost= 0.5*error**2
        return cost

    def net_input(self,X):
        return np.dot(X, self.w_[1:])+ self.w_[0]

    def activation(X):
        return X

    def predict(self,X):
        return np.where(self.activation(self.net_input(X)) >= 0.0, 1, -1)

It is not all my code, but is enaugh. How can I write output= self.activation(self.net_input(xi)) without getting an error like that?

1
  • 1
    "activation" needs a "self" parameter or should be declared as "@staticmethod". Commented Jun 4, 2020 at 2:20

1 Answer 1

3

You forgot to add self to your function. This should fix the issue:

def activation(self, X): 
    return X

Anytime you're calling a class method with self. prepended to the variable self is automatically passed as the first parameter and any values provided within the parentheses are passed as additional parameters.

Sign up to request clarification or add additional context in comments.

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.