AttributeError: 'tuple' object has no attribute 'shape'
Asked Answered
L

2

15

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.

Looker answered 17/12, 2016 at 11:34 Comment(2)
Maybe try converting your Data into a numpy.array?Chinquapin
What is Data?Lowestoft
S
15

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))
Summerlin answered 17/12, 2016 at 12:12 Comment(0)
A
0

.shape is an attribute of numpy ndarrays and tuples don't have such attributes but it's possible to call numpy.shape on a tuple to get its "shape".

import numpy as np
sh = np.shape(Data)

In general (not for OP), it's more useful to get the length of tuples:

len(Data)
Anthrax answered 18/3, 2023 at 1:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.