?bBiufcmMOS, aUVimport numpy as np
npr1=np.array(['a','b','c'])
npr2=np.array([1,2,5])
npr3=np.array([True,True,False,True])
print(npr1.dtype) # <U1
print(npr2.dtype) # int64
print(npr3.dtype) # bool
Specifying data type while creating the array
npr4 = np.array([1, 2, 3, 4], dtype='?')
print(npr4.dtype) # bool
npr5 = np.array([1, 2, 3, 4], dtype='U')
print(npr5.dtype) # <U1
npr4 = np.array([1, 2, 3, 4], dtype='?')
print(npr4.dtype) # bool
npr4= npr4.astype(np.int32)
print(npr4.dtype) # int32
While changing the data may change to match the new requirment. Here float data type is changed to integer type.
npr1=np.array([1.4,3,4.9,9])
print(npr1.dtype) #float64
npr1=npr1.astype(np.int8) # changing to int8 data type
print(npr1.dtype) # int8
print(npr1) # [1 3 4 9] # Output after changing the data type
When we increase the size there will not be any change of data.
npr2=np.array(['a','b','c'])
print(npr2.dtype) #<U1
npr2=npr2.astype('<U2') # changed data type to U2
print(npr2.dtype) # <U2
print(npr2) # ['a' 'b' 'c']
When we decrease the size there will be data loss.
npr2=np.array(['a12','b123','c1234'])
print(npr2.dtype) # <U5
npr2=npr2.astype('<U2') # change data type to U2
print(npr2) # ['a1' 'b1' 'c1']
Note in above lines we are specifying the size while defining the data type.
import numpy as np
student=np.dtype([('id','i'),('name','S20')])
print(type(student)) # <class 'numpy.dtype[void]'>
data=np.array([(1,'Alex'),(2,'Rabi')]) # adding data
print(data)
Output
<class 'numpy.dtype[void]'>
[['1' 'Alex']
['2' 'Rabi']]
More Example on using structured data.
import numpy as np
student=np.dtype([('id','i'),('name','S20')])
data=np.array([(1,'Alex'),(2,'Rabi')]) # adding data
print(data[1]) # ['2' 'Rabi']
print(data[0][1]) # Alex
Example of getting dtype from the structured data array.
import numpy as np
student=np.dtype([('id','i'),('name','S20')])
data=np.array([(1,'Alex'),(2,'Rabi')]) # adding data
name=data[:,1] # Name column
id=data[:,0] #id column
print(id.dtype) # <U21
id=id.astype(np.int32) # converting the data type to integer
print(id.dtype) # int32
Create a Numpy array using Student table. Numpy type |
C type |
Description |
|---|---|---|
|
|
Boolean (True or False) stored as a byte |
|
|
Platform-defined |
|
|
Platform-defined |
|
|
Platform-defined |
|
|
Platform-defined |
|
|
Platform-defined |
|
|
Platform-defined |
|
|
Platform-defined |
|
|
Platform-defined |
|
|
Platform-defined |
|
|
Platform-defined |
|
Half precision float: sign bit, 5 bits exponent, 10 bits mantissa |
|
|
|
Platform-defined single precision float: typically sign bit, 8 bits exponent, 23 bits mantissa |
|
|
Platform-defined double precision float: typically sign bit, 11 bits exponent, 52 bits mantissa. |
|
|
Platform-defined extended-precision float |
|
|
Complex number, represented by two single-precision floats (real and imaginary components) |
|
|
Complex number, represented by two double-precision floats (real and imaginary components). |
|
|
Complex number, represented by two extended-precision floats (real and imaginary components). |
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.