0% found this document useful (0 votes)
2 views15 pages

Module v Notes Numpy (1)

The document provides an introduction to Python libraries, focusing on NumPy, Pandas, and Matplotlib, which are essential for scientific and analytical tasks. It explains the functionalities of these libraries, including data manipulation, visualization, and the differences between NumPy arrays and Pandas DataFrames. The document also covers the creation, indexing, slicing, and operations on NumPy arrays, highlighting their efficiency and versatility compared to standard Python lists.

Uploaded by

pattankirti5m
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)
2 views15 pages

Module v Notes Numpy (1)

The document provides an introduction to Python libraries, focusing on NumPy, Pandas, and Matplotlib, which are essential for scientific and analytical tasks. It explains the functionalities of these libraries, including data manipulation, visualization, and the differences between NumPy arrays and Pandas DataFrames. The document also covers the creation, indexing, slicing, and operations on NumPy arrays, highlighting their efficiency and versatility compared to standard Python lists.

Uploaded by

pattankirti5m
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

Introduction to Python Libraries

Python libraries contain a collection of builtin modules that allow us to perform many
actions without writing detailed programs for it. Each library in Python contains a
large number of modules that one can import and use.

NumPy, Pandas and Matplotlib are three well-established Python libraries for
scientific and analytical use. These libraries allow us to manipulate, transform and
visualise data easily and efficiently.

NumPy, which stands for ‘Numerical Python’, it is a package that can be used for
numerical data analysis and scientific computing. NumPy uses a multidimensional
array object and has functions and tools for working with these arrays. Elements of an
array stay together in memory, hence, they can be quickly accessed.

PANDAS (PANel DAta) is a high-level data manipulation tool used for analysing
data. It is very easy to import and export data using Pandas library which has a very
rich set of functions. It is built on packages like NumPy and Matplotlib and gives us a
single, convenient place to do most of our data analysis and visualisation work.

Pandas has three important data structures, namely – Series, DataFrame and Panel to
make the process of analysing data organised, effective and efficient.

The Matplotlib library in Python is used for plotting graphs and visualisation. Using
Matplotlib, with just a few lines of code we can generate publication quality plots,
histograms, bar charts, scatterplots, etc. It is also built on Numpy, and is designed to
work well with Numpy and Pandas.

You may think what the need for Pandas is when NumPy can be used for data
analysis. Following are some of the differences between Pandas and Numpy:

1. A Numpy array requires homogeneous data, while a Pandas DataFrame


can have different data types (float, int, string, datetime, etc.).
2. Pandas have a simpler interface for operations like file loading, plotting,
selection, joining, GROUP BY, which come very handy in data-processing
applications.
3. Pandas DataFrames (with column names) make it very easy to keep track
of data.
4. Pandas is used when data is in Tabular Format, whereas Numpy is used for
numeric array based data manipulation.

INTRODUCTION TO NumPy

NumPy stands for ‘Numerical Python’. It is a package for data analysis and scientific
computing with Python. NumPy uses a multidimensional array object and has functions and
tools for working with these arrays. The powerful n-dimensional array in NumPy speeds-up
data processing. NumPy can be easily interfaced with other Python packages and provides

Page 1 of 15
tools for integrating with other programming languages like C, C++, etc.

ARRAY

An array is a data type used to store multiple values using a single identifier (variable name).
An array contains an ordered collection of data elements where each element is of the same
type and can be referenced by its index (position).

The important characteristics of an array are:

• Each element of the array is of the same data type, though the values stored in them
may be different.
• The entire array is stored contiguously in memory. This makes operations on an array
fast.
• Each element of the array is identified or referred to using the name of the Array along
with the index of that element, which is unique for each element. The index of an
element is an integral value associated with the element, based on the element’s
position in the array.

For example, consider an array with 5 numbers:


[ 10, 9, 99, 71, 90]

Here, the 1st value in the array is 10 and has the index value [0] associated with it; the 2nd
value in the array is 9 and has the index value [1] associated with it, and so on. The last value
(in this case the 5th value) in this array has an index [4].

This is called zero-based indexing. This is very similar to the indexing of lists in Python. The
idea of arrays is so important that almost all programming languages support it in one form
or another.

NUMPY ARRAY

NumPy arrays are used to store lists of numerical data, vectors, and matrices. The NumPy
library has a large set of routines (built-in functions) for creating, manipulating, and
transforming NumPy arrays.

Python language also has an array data structure, but it is not as versatile, efficient, and useful
as the NumPy array. The NumPy array is officially called ndarray but is commonly known as
an array. In the rest of the chapter, we will be referring to the NumPy array whenever we use
“array”. following are a few differences between the list and Array.

List Array
The list can have elements of different All elements of an array are of t h e same data type,
data types, for example, [1,3.4, ‘hello’, ‘a@’] for example, an array of floats may be: [1.2, 5.4, 2.7]
Elements of a list are not Array elements are stored in contiguous memory
stored contiguously in locations. This makes operations on arrays faster
memory. than lists.

Page 2 of 15
Lists do not support element-wise Arrays support element-wise operations. For
operations, for example, addition, example, if A1 is an array, it is possible to say A1/3
multiplication, etc. because elements may to divide each element of the array by 3.
not be of the same type.
Lists can contain objects of the different NumPy array takes up less space in memory as
datatype that Python must store the type compared to a list because arrays do not require
information for every element along with its storing the datatype of each element separately.
element value. Thus lists take more space in
memory and are less efficient.
List is a part of core Python. Array (ndarray) is a part of NumPy library.

Creation of NumPy Arrays from List

There are several ways to create arrays. To create an array and use its methods, first,
we need to import the NumPy library.

#NumPy is loaded as np (we can assign any #name), numpy must be written in
lowercase

>>> import numpy as np

The NumPy’s array () function converts a given list into an array. For example,

#Create an array called array1 from the #given list.

>>> array1 = [Link]([10,20,30])

#Display the contents of the array

>>> array1 array ([10, 20, 30])

• Creating a 1-D Array

An array with only a single row of elements is called a 1-D array. Let us try to create
a 1-D array from a list that contains numbers as well as strings.

>>> array2 = [Link]([5,-7.4,'a',7.2])


>>> array2

array(['5', '-7.4', 'a', '7.2'],dtype='<U32')

Observe that since there is a string value in the list, all integer and float values have
been promoted to string, while converting the list to an array.

Note: U32 means Unicode-32 data type.

• Creating a 2-D Array

Page 3 of 15
We can create two-dimensional (2-D) arrays by passing nested lists to the array ()
function.

Example :

>>> array3 = [Link]([[2.4,3],


[4.91,7],[0,-1]])

>>> array3
array([[ 2.4 , 3. ],
[ 4.91, 7. ],
[ 0. , -1. ]])

Observe that the integers 3, 7, 0, and -1 have been promoted to floats.

• Attributes of NumPy Array

Some important attributes of a NumPy ndarray object are:

[1]. [Link]: gives the number of dimensions of the array as an integer value.
Arrays can be 1-D, 2-D or n-D. In this chapter, we shall focus on 1-D and 2-D
arrays only. NumPy calls the dimensions as axes (plural of axis). Thus, a 2-D
array has two axes. The row-axis is called axis-0 and the column-axis is called
axis-1. The number of axes is also called the array’s rank.

Example :

>>> [Link]
1
>>> [Link]
2

[2]. [Link]: It gives the sequence of integers indicating the size of the array
for each dimension.

Example :

# array1 is 1D-array, there is nothing # after , in sequence


>>> [Link]
(3,)
>>> [Link]
(4,)
>>> [Link]
(3, 2)

Page 4 of 15
The output (3, 2) means array3 has 3 rows and 2 columns.
[3]. [Link]: It gives the total number of elements of the array. This is equal to
the product of the elements of shape.

Example :
>>> [Link]
3
>>> [Link]
6

[4]. [Link]: is the data type of the elements of the array. All the elements of
an array are of same data type. Common data types are int32, int64, float32,
float64, U32, etc.

Example :
>>> [Link]
dtype('int32')
>>> [Link]
- dtype('<U32>')
>>> [Link]
dtype('float64')

[5]. [Link]: It specifies the size in bytes of each element of the array. Data
type int32 and float32 means each element of the array occupies 32 bits in
memory. 8 bits form a byte. Thus, an array of elements of type int32 has
itemsize 32/8=4 bytes. Likewise, int64/float64 means each item has itemsize
64/8=8 bytes.

Example :
>>> [Link]
4 # memory allocated to integer
>>> [Link]
128 # memory allocated to string
>>> [Link]
8 #memory allocated to float type

• Other Ways of Creating NumPy Arrays

1. We can specify data type (integer, float, etc.) while creating array using dtype
as an argument to array(). This will convert the data automatically to the
mentioned type. In the following example, nested list of integers are passed to
the array function. Since data type has been declared as float, the integers
are converted to floating point numbers.
2.
Example:

Page 5 of 15
>>> array4
array([[1., 2.],[3., 4.]])

3. We can create an array with all elements initialized to 0 using the function
zeros(). By default, the data type of the array created by zeros() is float. The
following code will create an array with 3 rows and 4 columns with each
element set to 0.
>>> array5 = [Link]((3,4))
>>> array5
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])

4. We can create an array with all elements initialized to 1 using the function
ones(). By default, the data type of the array created by ones() is float. The
following code will create an array with 3 rows and 2 columns.

>>> array6 = [Link]((3,2))


>>> array6 array([[1., 1.],
[1., 1.],
[1., 1.]]-)
5. We can create an array with numbers in a given range and sequence
using the arange() function. This function is analogous to the range() function
of Python.

>>> array7 = [Link](6)

# an array of 6 elements is created with start value 5 and step size


1

>>> array7
array([0, 1, 2, 3, 4, 5])

# Creating an array with start value -2, end # value 24, and step
size 4

>>> array8 = [Link]( -2, 24, 4 )


>>> array8

array([-2, 2, 6, 10, 14, 18, 22])

INDEXING AND SLICING

NumPy arrays can be indexed, sliced and iterated over.

Page 6 of 15
• Indexing

We have learnt about indexing single-dimensional array in above section. For 2-D
arrays indexing for both dimensions starts from 0, and each element is referenced
through two indexes i and j, where i represents the row number and j represents the
column number.

Table 2 : Marks of students in different Sujects


Name Python OS ADA
Suman 78 67 56

Sonia 76 75 47

Ramya 84 59 60

Shree 67 72 54

Consider Table 2 showing marks obtained by students in three


different subjects. Let us create an array called marks to store marks given
in three subjects for four students given in this table. As there are 4
students (i.e. 4 rows) and 3 subjects (i.e. 3 columns), the array will be
called marks[4][3]. This array can store 4*3 = 12 elements.
Here, marks[i,j] refers to the element at (i+1)th row and (j+1)th column
because the index values start at 0. Thus marks[3,1] is the element in 4th row
and second column which is 72 (marks of Shree in OS).

# accesses the element in the 1st row in# the 3rd column
>>> marks[0,2]
56
>>> marks [0,4]
index Out of Bound "Index Error". Index 4is out of bounds for axis with size 3

• Slicing
Sometimes we need to extract part of an array. This is done through slicing.
We can define which part of the array to be sliced by specifying the start and
end index values using [start : end] along with the array name.
Example :
>>> array8
array([-2, 2, 6, 10, 14, 18, 22])

# excludes the value at the end index


>>> array8[3:5]
array([10, 14])

# reverse the array

Page 7 of 15
>>> array8[ : : -1]
array([22, 18, 14, 10, 6, 2, -2])

let us see how slicing is done for 2-D arrays. For this, let us create a 2-D
array called array9 having 3 rows and 4 columns.

>>> array9 = [Link]([[ -7, 0, 10, 20],


[ -5, 1, 40, 200],
[ -1, 1, 4, 30]])

# access all the elements in the 3rd column


>>> array9[0:3,2]
array([10, 40, 4])

Note that we are specifying rows in the range 0:3 because the end value of
the range is excluded.

# access elements of 2nd and 3rd row from 1st# and 2nd column
>>> array9[1:3,0:2]
array([[-5, 1],
[-1, 1]])
If row indices are not specified, it means all the rows are to be considered.
Likewise, if column indices are not specified, all the columns are to be
considered. Thus, the statement to access all the elements in the 3 rd
column can also be written as:

>>>array9[:,2]

array([10, 40, 4])

• OPERAtIONs ON ARRAYs
Once arrays are declared, we can access it's element or perform certain
operations the last section, we learnt about accessing elements. This section
describes multiple operations that can be applied on arrays.
• Arithmetic Operations
Arithmetic operations on NumPy arrays are fast and simple. When we
perform a basic arithmetic operation like addition, subtraction,
multiplication, division etc. on two arrays, the operation is done on each
corresponding pair of elements. For instance, adding two arrays will result in
the first element in the first array being added to the first element in the
second array, and so on.
Consider the following element-wise operations on two arrays:

>>> array1 = [Link]([[3,6],[4,2]])


>>> array2 = [Link]([[10,20],[15,12]])

Page 8 of 15
#Element-wise addition of two matrices.
>>> array1 + array2
array([[13, 26],
[19, 14]])

#Subtraction
>>> array1 - array2
array([[ -7, 14],
[-11, -10]])

#Multiplication
>>> array1 * array2
array([[ 30,120],
[ 60, 24]])

#Matrix Multiplication
>>> array1 @ array2
array([[120, 132],
[ 70, 104]])

#Exponentiation
>>> array1 ** 3
array([[ 27, 216],
[ 64, 8]], dtype=int32)

#Division
>>> array2 / array1
array([[3.33333333, 3.33333333],
[3.75 , 6. ]])

#Element wise Remainder of Division#(Modulo)


>>> array2 % array1
array([[1, 2],
[3, 0]], dtype=int32)

It is important to note that for element-wise operations, size of both


arrays must be the same. That is, [Link] must be equal to
[Link].

• Transpose

Transposing an array turns its rows into columns and columns into rows just like
matrices in mathematics.

#Transpose
>>> array3 = [Link]([[10,-7,0, 20],
[-5,1,200,40],[30,1,-1,4]])

>>> array3

Page 9 of 15
array([[ 10, -7, 0, 20],
[ -5, 1, 200, 40],
[ 30, 1, -1, 4]])

The original array does not change


>>> [Link]()
array([[ 10, -5, 30],
[ -7, 1, 1],
[ 0, 200, -1],
[ 20, 40, 4]])

• Sorting

Sorting is arranging the elements of an array in hierarchical order either


ascending or descending. By default, numpy does sorting in ascending
order.
>>> array4 = [Link]([1,0,2,-3,6,8,4,7])
>>> [Link]()
>>> array4
array([-3, 0, 1, 2, 4, 6, 7, 8])

In 2-D array, sorting can be done along either of the axes i.e., row-wise
or column-wise. By default, sorting is done row-wise (i.e., on axis = 1). It
means to arrange elements in each row in ascending order. When axis=0,
sorting is done column-wise, which means each column is sorted in
ascending order.

>>> array4 = [Link]([[10,-7,0, 20],


[-5,1,200,40],[30,1,-1,4]])

>>> array4
array([[ 10, -7, 0, 20],
[ -5, 1, 200, 40],
[ 30, 1, -1, 4]])

#default is row-wise sorting


>>> [Link]()
>>> array4
array([[ -7, 0, 10, 20],
[ -5, 1, 40, 200],
[ -1, 1, 4, 30]])
>>> array5 = [Link]([[10,-7,0, 20],
[-5,1,200,40],[30,1,-1,4]])

#axis =0 means column-wise sorting


>>> [Link](axis=0)
>>> array5

Page 10 of 15
array([[ -5, -7, -1, 4],
[ 10, 1, 0, 20],
[ 30, 1, 200, 40]])

• cONcAtENAtING ARRAYs
Concatenation means joining two or more arrays. Concatenating 1-D arrays means
appending the sequences one after another. [Link]() function can
be used to concatenate two or more 2-D arrays either row-wise or
column-wise. All the dimensions of the arrays to be concatenated must
match exactly except for the dimension or axis along which they need to
be joined. Any mismatch in the dimensions results in an error. By default,
the concatenation of the arrays happens along axis=0.
Example 6.8
>>> array1 = [Link]([[10, 20], [-30,40]])
>>> array2 = [Link]((2, 3), dtype=array1.
dtype)

>>> array1 array([[ 10, 20],


[-30, 40]])

>>> array2 array([[0, 0, 0],


[0, 0, 0]])

>>> [Link] (2, 2)


>>> [Link] (2, 3)

>>> [Link]((array1,array2), axis=1) array([[ 10, 20, 0, 0, 0],


[-30, 40, 0, 0, 0]])

>>> [Link]((array1,array2), axis=0) Traceback (most recent call last):


File "<pyshell#3>", line 1, in <module> [Link]((array1,array2))
ValueError: all the input array dimensions except for the concatenation axis must
match exactly

• Reshaping ARRAYs

We can modify the shape of an array using the reshape() function. Reshaping an

Page 11 of 15
array cannot be used to change the total number of elements in the array.
Attempting to change the number of elements in the array using reshape() results in
an error.

Example 6.9
>>> array3 = [Link](10,22)
>>> array3
array([10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21])

>>> [Link](3,4)
array([[10, 11, 12, 13],
[14, 15, 16, 17],
[18, 19, 20, 21]])

>>> [Link](2,6) array([[10, 11, 12, 13, 14, 15],


[16, 17, 18, 19, 20, 21]])

• sPLIttING ARRAYs
We can split an array into two or more subarrays. [Link]() splits an
array along the specified axis. We can either specify sequence of index values
where an array is to be split; or we can specify an integer N, that indicates
the number of equal parts in which the array is to be split, as parameter(s)
to the [Link]() function. By default, [Link]()splits along axis
=0.
Consider the array given below:
>>> array4
array([[ 10, -7, 0, 20],
[ -5, 1, 200, 40],
[ 30, 1, -1, 4],
[ 1, 2, 0, 4],
[ 0, 1, 0, 2]])

# [1,3] indicate the row indices on which# to split the array


>>> first, second, third = numpy split(array4,
[1, 3])

# array4 is split on the first row and# stored on the sub-array first
>>> first
array([[10, -7, 0, 20]])

# array4 is split after the first row and# upto the third row and
stored on the # sub-array second
>>> second
array([[ -5, 1, 200, 40],
[ 30, 1, -1, 4]])

Page 12 of 15
# the remaining rows of array4 are stored# on the sub-array third
>>> third
array([[1, 2, 0, 4],
[0, 1, 0, 2]])

#[1, 2], axis=1 give the columns indices #along which to split

>>> firstc, secondc, thirdc =numpy split(array4,[1, 2], axis=1)


>>> firstc
array([[10],
[-5],
[30],
[ 1],
[ 0]])

>>> secondc array([[-7],


[ 1],
[ 1],
[ 2],
[ 1]])

>>> thirdc array([[ 0, 20],


[200, 40],
[ -1, 4],
[ 0, 4],
[ 0, 2]])

# 2nd parameter 2 implies array is to be # split in 2 equal parts axis=1 along the
# column axis
>>> firsthalf, secondhalf =[Link](array4,2,
axis=1)
>>> firsthalf
array([[10, -7],
[-5, 1],
[30, 1],
[ 1, 2],
[ 0, 1]])

>>> secondhalf array([[ 0, 20],


[200, 40],
[ -1, 4],
[ 0, 4],
[ 0, 2]])

• stAtIstIcAL OPERAtIONs ON ARRAYs


NumPy provides functions to perform many useful statistical operations on
arrays.
Let us consider two arrays:

>>>arrayA = [Link]([1,0,2,-3,6,8,4,7])

Page 13 of 15
>>> arrayB = [Link]([[3,6],[4,2]])

1. The max() function finds the maximum element from an array.


# max element form the whole 1-D array
>>> [Link]()

8
# max element form the whole 2-D array
>>> [Link]()
6
# if axis=1, it gives column wise maximum
>>> [Link](axis=1)
array([6, 4])
# if axis=0, it gives row wise maximum
>>> [Link](axis=0)
array([4, 6])
2. The min() function finds the minimum element from an array.

>>> [Link]()
-3
>>> [Link]()
2
>>> [Link](axis=0)
array([3, 2])
3. The sum()function finds the sum of all elements of an array.
>>> [Link]()
25
>>> [Link]()
15
#axis is used to specify the dimension
#on which sum is to be made. Here axis = 1
#means the sum of elements on the first row

>>> [Link](axis=1)
array([9, 6])
4. The mean() function finds the average of elements of the array.
>>> [Link]()

3.125
>>> [Link]()
3.75
>>> [Link](axis=0)
array([3.5, 4. ])
>>> [Link](axis=1)
array([4.5, 3. ])
5. The std() function is used to find standard deviation of an array
of elements.
>>> [Link]()
3.550968177835448
>>> [Link]()

Page 14 of 15
1.479019945774904

>>> [Link](axis=0)

array([0.5, 2. ])

>>> [Link](axis=1)
array([1.5, 1. ])

Page 15 of 15

You might also like