Data types Numpy


There are more variety of data types in Numpy than in Python. We can assign chars to each data types and here is the list.
?
Boolean
b
(Signed) byte
B
(UnSigned) byte
i
(Signed) Integer
u
(UnSigned) Integer
f
Floating point
c
Complex floating point
m
TimeDelta
M
Datetime
O
(Python) Objects
S, a
Zero terminated Bytes
U
Unicode String
V
raw data (void)
Using dtype to get the Data type of the Numpy array.
import 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

astype()

We can change the data type of an array by using astype(), here one Boolean data type array ( originally ) changed to integer or int32 data type.
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.

Creating structured data

By using our defined data type we can create structured data to store columns with data.
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.

Types of Data supported by Numpy

Source : Numpy Manual

Numpy type

C type

Description

numpy.bool_

bool

Boolean (True or False) stored as a byte

numpy.byte

signed char

Platform-defined

numpy.ubyte

unsigned char

Platform-defined

numpy.short

short

Platform-defined

numpy.ushort

unsigned short

Platform-defined

numpy.intc

int

Platform-defined

numpy.uintc

unsigned int

Platform-defined

numpy.int_

long

Platform-defined

numpy.uint

unsigned long

Platform-defined

numpy.longlong

long long

Platform-defined

numpy.ulonglong

unsigned long long

Platform-defined

numpy.half numpy.float16

Half precision float: sign bit, 5 bits exponent, 10 bits mantissa

numpy.single

float

Platform-defined single precision float: typically sign bit, 8 bits exponent, 23 bits mantissa

numpy.double

double

Platform-defined double precision float: typically sign bit, 11 bits exponent, 52 bits mantissa.

numpy.longdouble

long double

Platform-defined extended-precision float

numpy.csingle

float complex

Complex number, represented by two single-precision floats (real and imaginary components)

numpy.cdouble

double complex

Complex number, represented by two double-precision floats (real and imaginary components).

numpy.clongdouble

long double complex

Complex number, represented by two extended-precision floats (real and imaginary components).

Numpy random.randint , Random Integers
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer