Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'list' object has no attribute 'shape'

Tags:

python

list

numpy

how to create an array to numpy array?

def test(X, N):     [n,T] = X.shape     print "n : ", n     print "T : ", T    if __name__=="__main__":      X = [[[-9.035250067710876], [7.453250169754028], [33.34074878692627]], [[-6.63700008392334], [5.132999956607819], [31.66075038909912]], [[-5.1272499561309814], [8.251499891281128], [30.925999641418457]]]     N = 200     test(X, N) 

I am getting error as

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

So, I think I need to convert my X to numpy array?

like image 579
sam Avatar asked Jan 09 '14 09:01

sam


People also ask

How do I fix AttributeError list object has no attribute shape?

The Python "AttributeError: 'list' object has no attribute 'shape'" occurs when we try to access the shape attribute on a list. To solve the error, pass the list to the numpy. array() method to create a numpy array before accessing the shape attribute.

Which object has attribute shape in Python?

Use numpy. array to use shape attribute.

How do you show the shape of a list?

The shape of a list will be obtained using a built-in function len() and a module NumPy. The shape of a list normally returns the number of objects in a list. We can calculate a shape of a list using two methods, len() and NumPy array shape. Numpy has an attribute named np.


1 Answers

Use numpy.array to use shape attribute.

>>> import numpy as np >>> X = np.array([ ...     [[-9.035250067710876], [7.453250169754028], [33.34074878692627]], ...     [[-6.63700008392334], [5.132999956607819], [31.66075038909912]], ...     [[-5.1272499561309814], [8.251499891281128], [30.925999641418457]] ... ]) >>> X.shape (3L, 3L, 1L) 

NOTE X.shape returns 3-items tuple for the given array; [n, T] = X.shape raises ValueError.

like image 157
falsetru Avatar answered Sep 19 '22 21:09

falsetru