Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dtype work in FROM but not IMPORT

I swear I read almost all the "FROM vs IMPORT" questions before asking this.

While going through the NumPy tutorial I was using:

import numpy as np

but ran into trouble when declaring dtype of a matrix like:

a = np.ones((2,3),dtype=int32)

I kept getting "NameError: name 'int32' is not defined." I am using Python v3.2, and am following the tentative tutorial that goes along with it. I used:

from numpy import *
a = ones((2,3),dtype=int32)

Which works. Any insight as to why this is would be much appreciated. Thank you in advance!

like image 319
Andreas GS Avatar asked Oct 19 '12 18:10

Andreas GS


1 Answers

import numpy as np

#this will work because int32 is defined inside the numpy module
a = np.ones((2,3), dtype=np.int32)
#this also works
b = np.ones((2,3), dtype = 'int32') 

#python doesn't know what int32 is because you loaded numpy as np
c = np.ones((2,3), dtype=int32) 

back to your example:

from numpy import *
#this will now work because python knows what int32 is because it is loaded with numpy.
d = np.ones((2,3), dtype=int32) 

I tend to define the type using strings as in array b

like image 193
abdulhaq-e Avatar answered Sep 22 '22 10:09

abdulhaq-e