0

I don't understand why this code doesn't work:

import numpy as np 
class Normalizer:
 def __init__(self,x):
 self.x = x 
 def mean(self):
 return np.sum(self.x)/np.size(self.x)
 def mean_zero(self):
 return self.x - self.x.mean()
 def new_calc(self):
 return self.x.mean_zero()
 a = np.random.randint(150,200,(5,8))
 heights = Normalizer(a)
 print(a)
 print(heights.mean()) 
 print(heights.mean_zero())
 print(heights.mean_zero().mean())
 print(heights.new_calc())

It executes heghts.mean_zero() correctly but in the method def new_calc(self) it doesn't execute it. It would be great if someone could explain that to me. Thanks!

sophros
17.3k12 gold badges52 silver badges84 bronze badges
asked Feb 1, 2019 at 7:15
2
  • mean_zero is class method. You can't call it with your class's variable. Commented Feb 1, 2019 at 7:19
  • Use self.mean_zero() instead of self.x.mean_zero() Commented Feb 1, 2019 at 7:20

5 Answers 5

1

I don't understand why this code doesn't work:

if you run the following code it will throw an error:

AttributeError: 'numpy.ndarray' object has no attribute 'mean_zero'
  • locate the problem, the only place where mean_zero has been called is new_calc method. So, first step done.

  • analyze, if you look at Normalize class it has one attribute x which is of the type numpy.ndarray. If you carefully read the error message it says that ndarray type doesn't have the attribute mean_zero. On the other hand you have mean_zero method defined in your class and that is the one you should call.

These two steps leads to conclusion that the problem is in new_calc method:

def new_calc(self):
 return self.mean_zero() #(wrong)return self.x.mean_zero()
answered Feb 1, 2019 at 7:31
Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure what x from the __init__ is but it is very likely that you actually want to call mean_zero in new_calc in the context of self variable (the same object):

def new_calc(self):
 return self.mean_zero()
answered Feb 1, 2019 at 7:20

Comments

0

Insted of self.x.mean_zero() write self.mean_zero()

import numpy as np 
class Normalizer:
 def __init__(self,x):
 self.x = x 
 def mean(self):
 return np.sum(self.x)/np.size(self.x)
 def mean_zero(self):
 return self.x - self.mean()
 def new_calc(self):
 return self.mean_zero()
a = np.random.randint(150,200,(5,8))
heights = Normalizer(a)
print(a)
print(heights.mean()) 
print(heights.mean_zero())
print(heights.mean_zero().mean())
print(heights.new_calc())
answered Feb 1, 2019 at 7:20

Comments

0

You're initializing Normalizer with 'a', which is the output of np.random.randint, which returns a numpy.ndarray object.

In the new_calc method you are attempting to call the mean_zero method of the ndarray object, but ndarray has no such method. mean_zero is a method on Normalizer, but self.x is not of type Normalizer.

I'm not sure what this code new_calc is supposed to do. If you can make that clearer, I may be able to provide more assistance.

answered Feb 1, 2019 at 7:23

1 Comment

new_calc was just a simplification to debug what I am actually trying to accomplish. The actual code will look like this: def variance(self): return (np.sum((self.x.mean_zero()**2)))/ np.size(self.x)
0

The culprit:

def new_calc(self):
 return self.x.mean_zero()

The reason:

self.x is an attribute of the Normalizer class. So if heights is an instance of the Normalizer class, then heights.x is the self.x.

The answer:

def new_calc(self):
 return self.mean_zero()

The justification:

AttributeError: 'numpy.ndarray' object has no attribute 'mean_zero'

ndarray has no such method. mean_zero is a method of Normalizer

answered Feb 1, 2019 at 7:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.