Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'tuple' object has no attribute 'shape'

So I have been writing a code to standardize the elements of a matrix and the function I used is as follows:

def preprocess(Data):
    if stdn ==True:
       st=np.empty((Data.shape[0],Data.shape[1]))
       for i in xrange(0,Data.shape[0]):
           st[i,0]=Data[i,0]
       for i in xrange(1,Data.shape[1]):
           st[:,i]=((Data[:,i]-np.min(Data[:,i]))/(np.ptp(Data[:,i])))       
           np.random.shuffle(st)
       return st
    else:
       return Data

It works very well outside the class but when used inside of it it gives me this error:

  AttributeError: 'tuple' object has no attribute 'shape'

Any idea on how I can fix it?? P.S. This is a KNN classification code

like image 283
Ferial Mohammed Avatar asked Dec 17 '16 11:12

Ferial Mohammed


People also ask

How do you fix an object that has no attribute?

The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.

How do you shape a tuple?

The shape of a simple Python tuple or list can be obtained with the built-in len() function. len() will return an integer that describes the number of objects in the tuple or list.

How do you solve a tuple object is not callable?

The Python "TypeError: 'tuple' object is not callable" occurs when we try to call a tuple as if it were a function. To solve the error, make sure to use square brackets when accessing a tuple at a specific index, e.g. my_tuple[0] .

What is attribute error in Python?

AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. For example, if we take a variable x we are assigned a value of 10. In this process suppose we want to append another value to that variable. It's not possible.


1 Answers

According to the error you posted, Data is of type tuple and there is no attribute shape defined for data. You could try casting Data when you call your preprocess function, e.g.:

preprocess(numpy.array(Data))
like image 155
José Sánchez Avatar answered Sep 20 '22 06:09

José Sánchez