21
loading...
This website collects cookies to deliver better user experience
#Numpy array of different dimensions
import numpy as np
print(np.array([[[1, 2], [3, 4], [5, 6]], [[1],[2]]], dtype=int))
# Output
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 2, in <module>
print(np.array([[[1, 2], [3, 4], [5, 6]], [[1],[2]]], dtype=int))
ValueError: setting an array element with a sequence. The requested array has an
inhomogeneous shape after 1 dimensions. The detected shape
was (2,) + inhomogeneous part.
#Numpy array of same dimensions
import numpy as np
print(np.array([[[1, 2], [3, 4], [5, 6]]], dtype=int))
# Output
[[[1 2]
[3 4]
[5 6]]]
# Mutliple data type and dtype as float
import numpy as np
print(np.array([55.55, 12.5, "Hello World"], dtype=float))
# Output
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 2, in <module>
print(np.array([55.55, 12.5, "Hello World"], dtype=float))
ValueError: could not convert string to float: 'Hello World'
# Changing the dtype as object and having multiple data type
import numpy as np
print(np.array([55.55, 12.5, "Hello World"], dtype=object))
# Output
[55.55 12.5 'Hello World']
import numpy
numpy.array([1,2,3]) #good
numpy.array([1, (2,3)]) #Fail, can't convert a tuple into a numpy
#array element
numpy.mean([5,(6+7)]) #good
numpy.mean([5,tuple(range(2))]) #Fail, can't convert a tuple into a numpy
#array element
def foo():
return 3
numpy.array([2, foo()]) #good
def foo():
return [3,4]
numpy.array([2, foo()]) #Fail, can't convert a list into a numpy
#array element