CSL 410 L08
CSL 410 L08
Unit No. 2
NumPy-Data Types
Lecture No. 08
float64 Double precision float: sign bit, 11 bits exponent, 52bits mantissa
complex_ Shorthand for complex128
complex64 Complex number, represented by two 32-bit floats (real and imaginary components)
complex128 Complex number, represented by two 64-bit floats (real and imaginary components)
Example 1
# using array-scalar type
import numpy as np
dt=np.dtype(np.int32)
print dt
Example 2
#int8, int16, int32, int64 can be replaced by equivalent string 'i1', 'i2','i4', etc.
import numpy as np
dt = np.dtype('i4')
print dt
Example 3
# using endian notation
import numpy as np
dt = np.dtype('>i4')
print dt
Example 4: The following examples show the use of structured data type.
Here, the field name and the corresponding scalar data type is to be
declared.
# first create structured data type
import numpy as np
dt = np.dtype([('age',np.int8)])
print dt
Example 5:
# now apply it to ndarray object
import numpy as np
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype=dt)
print a
Example 6:
# file name can be used to access content of age column
import numpy as np
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype=dt)
print a['age']
import numpy as np
student=np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
print student
a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype=student)
print a
The output is as follows:
[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')])
[('abc', 21, 50.0), ('xyz', 18, 75.0)]
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
NumPy-Data Types
• Each built-in data type has a character code that uniquely identifies it.
– 'b': boolean
– 'i': (signed) integer
– 'u': unsigned integer
– 'f': floating-point
– 'c': complex-floating point
– 'm': timedelta
– 'M': datetime
– 'O': (Python) objects
– 'S', 'a': (byte-)string
– 'U': Unicode
– 'V': raw data (void)
•NumPy-Data Types
References