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/ 20
Program:B.
Tech(CSE) IV Semester II Year
CSL-410: Data Science using Python
Unit No. 2 Introduction of NumPy Library
Lecture No. 07
Dr. Sanjay Jain
Associate Professor, CSA/SOET Outlines • Introduction of NumPy • NUMPY − ENVIRONMENT • NumPy Arrays • NUMPY − NDARRAY OBJECT • Create Array • 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. Introduction of NumPy
• NumPy is a Python package. It stands for 'Numerical Python'. It is a library
consisting of multidimensional array objects and a collection of routines for processing of array. • Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another package Numarray was also developed, having some additional functionalities. • In 2005, Travis Oliphant created NumPy package by incorporating the features of Numarray into Numeric package. There are many contributors to this open source project.
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
Operations using NumPy
• Using NumPy, a developer can perform the following operations:
– Mathematical and logical operations on arrays. – Fourier transforms and routines for shape manipulation. – Operations related to linear algebra. – NumPy has in-built functions for linear algebra and random number generation.
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
NUMPY − ENVIRONMENT
• Standard Python distribution doesn't come bundled with
NumPy module. • A lightweight alternative is to install NumPy using popular Python package installer, pip. pip install numpy • The best way to enable NumPy is to use an installable binary package specific to your operating system. • These binaries contain full SciPy stack (inclusive of NumPy, SciPy, matplotlib, IPython, SymPy and nose packages along with core Python).
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
NUMPY − ENVIRONMENT • To test whether NumPy module is properly installed, try to import it from Python prompt. import numpy • If it is not installed, the following error message will be displayed. • Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> import numpy ImportError: No module named 'numpy‘ • Alternatively, NumPy package is imported using the following syntax: import numpy as np
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
NumPy Arrays • The central feature of NumPy is the array object class. • Arrays are similar to lists in Python, except that every element of an array must be of the same type, typically a numeric type like float or int. • Arrays make operations with large amounts of numeric data very fast and are generally much more efficient than lists. • A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. • The number of dimensions is the rank of the array; • The shape of an array is a tuple of integers giving the size of the array along each dimension.
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
NUMPY − NDARRAY OBJECT • The most important object defined in NumPy is an N-dimensional array type called ndarray. It describes the collection of items of the same type. Items in the collection can be accessed using a zero-based index. • Every item in an ndarray takes the same size of block in the memory. Each element in ndarray is an object of data-type object (called dtype). • Any item extracted from ndarray object (by slicing) is represented by a Python object of one of array scalar types. The following diagram shows a relationship between ndarray, data type object (dtype) and array scalar type:
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
NUMPY − NDARRAY OBJECT • The following diagram shows a relationship between ndarray, data type object (dtype) and array scalar type:
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
Create Array • An instance of ndarray class can be constructed by different array creation routines. The basic ndarray is created using an array function in NumPy as follows: numpy.array • It creates an ndarray from any object exposing array interface, or from any method that returns an array. numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0) Create Array numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0) • The above constructor takes the following parameters:
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
Create Array Example 1: import numpy as np a=np.array([1,2,3]) print a The output is as follows: [1, 2, 3]
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
Create Array Example 2 # more than one dimensions import numpy as np a = np.array([[1, 2], [3, 4]]) print a The output is as follows: [[1, 2] [3, 4]]
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
Create Array Example 3 # minimum dimensions import numpy as np a=np.array([1, 2, 3,4,5], ndmin=2) print a The output is as follows: [[1, 2, 3, 4, 5]]
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
Create Array Example 4 # dtype parameter import numpy as np a = np.array([1, 2, 3], dtype=complex) print a The output is as follows: [ 1.+0.j, 2.+0.j, 3.+0.j]
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
Create Array • The ndarray object consists of contiguous one-dimensional segment of computer memory, combined with an indexing scheme that maps each item to a location in the memory block. • The memory block holds the elements in a row-major order (C style) or a column-major order (FORTRAN or MatLab style).
<SELO: 1,2,3> <Reference No.: R1,R2,R3>
Learning Outcomes
The students have learn and understand the followings:
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