0% found this document useful (0 votes)
9 views17 pages

CSL 410 L08

Uploaded by

rpschauhan2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views17 pages

CSL 410 L08

Uploaded by

rpschauhan2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Program:B.

Tech(CSE) IV Semester II Year

CSL-410: Data Science using Python

Unit No. 2
NumPy-Data Types

Lecture No. 08

Dr. Sanjay Jain


Associate Professor, CSA/SOET
Outlines
• Introduction of NumPy
• NUMPY − ENVIRONMENT
• NumPy Arrays
• NUMPY − NDARRAY OBJECT
• Create Array
• NumPy-Data Types
• Examples
• References
Student Effective Learning Outcomes(SELO)
01: Ability to understand subject related concepts clearly along with
contemporary issues.
02: Ability to use updated tools, techniques and skills for effective domain
specific practices.
03: Understanding available tools and products and ability to use it
effectively.
NumPy-Data Types
• NumPy supports a much greater variety of numerical types than Python does.
The following table shows different scalar data types defined in NumPy.
Data Types Description
bool_ Boolean (True or False) stored as a byte
int_ Default integer type (same as C long;normally either int64 or int32)
intc Identical to C int (normally int32 or int64)
intp Integer used for indexing (same as C ssize_t;normally either int32 or int64)
int8 Byte (-128 to 127)
int16 Integer (-32768 to 32767)
int32 Integer (-2147483648 to 2147483647)
int64 Integer (-9223372036854775808 to 9223372036854775807)
uint8 Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
float_ Shorthand for float64
float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa

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)

<SELO: 1,2,3> <Reference No.: R1,R2,R3>


NumPy-Data Types
• Data Type Objects (dtype): A data type object describes interpretation of
fixed block of memory corresponding to an array, depending on the
following aspects:
– Type of data (integer, float or Python object)
– Size of data
– Byte order (little-endian or big-endian)
– In case of structured type, the names of fields, data type of each field
and part of the memory block taken by each field
– If data type is a subarray, its shape and data type

<SELO: 1,2,3> <Reference No.: R1,R2,R3>


NumPy-Data Types

A dtype object is constructed using the following syntax:


numpy.dtype(object, align, copy)
The parameters are:
– Object: To be converted to data type object
– Align: If true, adds padding to the field to make it similar to C-struct
– Copy: Makes a new copy of dtype object. If false, the result is reference
to builtin data type object

<SELO: 1,2,3> <Reference No.: R1,R2,R3>


NumPy-Data Types

Example 1
# using array-scalar type
import numpy as np
dt=np.dtype(np.int32)
print dt

The output is as follows:


int32

<SELO: 1,2,3> <Reference No.: R1,R2,R3>


NumPy-Data Types

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

The output is as follows:


int32

<SELO: 1,2,3> <Reference No.: R1,R2,R3>


NumPy-Data Types

Example 3
# using endian notation
import numpy as np
dt = np.dtype('>i4')
print dt

The output is as follows:


>i4

<SELO: 1,2,3> <Reference No.: R1,R2,R3>


NumPy-Data Types

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

The output is as follows:


[('age', 'i1')]

<SELO: 1,2,3> <Reference No.: R1,R2,R3>


NumPy-Data Types

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

The output is as follows:


[(10,) (20,) (30,)]

<SELO: 1,2,3> <Reference No.: R1,R2,R3>


NumPy-Data Types

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']

The output is as follows:


[10 20 30]

<SELO: 1,2,3> <Reference No.: R1,R2,R3>


NumPy-Data Types

Example 7: The following examples define a structured data type called


student with a string field 'name', an integer field 'age' and a float field
'marks'. This dtype is applied to ndarray object.

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)

<SELO: 1,2,3> <Reference No.: R1,R2,R3>


Learning Outcomes

The students have learn and understand the followings:

•NumPy-Data Types
References

1. Data Science with Python by by Aaron England, Mohamed Noordeen


Alaudeen, and Rohan Chopra. Packt Publishing; July 2019
2. https://intellipaat.com/blog/what-is-data-science/
3. https://onlinecourses.nptel.ac.in/noc20_cs36/
Thank you

You might also like