Page 1
6.1 Introduct Ion 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 tools for
integrating with other programming languages
like C, C++ etc.
Introduct Ion to n um Py
Chapter
6
“The goal is to turn data into information,
and information into insight.”
— Carly Fiorina
In this chapter
» Introduction
» Array
» NumPy Array
» Indexing and Slicing
» Operations on Arrays
» Concatenating Arrays
» Reshaping Arrays
» Splitting Arrays
» Statistical Operations
on Arrays
» Loading Arrays from
Files
» Saving NumPy Arrays
in Files on Disk
Chap 6.indd 95 19-Jul-19 3:43:32 PM
2024-25
Page 2
6.1 Introduct Ion 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 tools for
integrating with other programming languages
like C, C++ etc.
Introduct Ion to n um Py
Chapter
6
“The goal is to turn data into information,
and information into insight.”
— Carly Fiorina
In this chapter
» Introduction
» Array
» NumPy Array
» Indexing and Slicing
» Operations on Arrays
» Concatenating Arrays
» Reshaping Arrays
» Splitting Arrays
» Statistical Operations
on Arrays
» Loading Arrays from
Files
» Saving NumPy Arrays
in Files on Disk
Chap 6.indd 95 19-Jul-19 3:43:32 PM
2024-25
96
Informat Ics Pract Ices – c lass XI
Installing NumPy
NumPy can be installed by typing following command:
pip install NumPy
6.2 Arr Ay We have learnt about various data types like list, tuple,
and dictionary. In this chapter we will discuss another
datatype ‘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 same data
type, though the values stored in them may be
different.
• The entire array is stored contiguously in
memory. This makes operations on array fast.
• Each element of the array is identified or
referred 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 2
nd
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 5
th
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.
6.3 n um Py Arr Ay 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
Contiguous memory
allocation:
The memory space
must be divided
into the fined sized
position and each
position is allocated
to a single data only.
Now Contiguous
Memory Allocation:
Divide the data into
several blocks and
place in different
parts of the memory
according to the
availability of memory
space.
Chap 6.indd 96 19-Jul-19 3:43:32 PM
2024-25
Page 3
6.1 Introduct Ion 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 tools for
integrating with other programming languages
like C, C++ etc.
Introduct Ion to n um Py
Chapter
6
“The goal is to turn data into information,
and information into insight.”
— Carly Fiorina
In this chapter
» Introduction
» Array
» NumPy Array
» Indexing and Slicing
» Operations on Arrays
» Concatenating Arrays
» Reshaping Arrays
» Splitting Arrays
» Statistical Operations
on Arrays
» Loading Arrays from
Files
» Saving NumPy Arrays
in Files on Disk
Chap 6.indd 95 19-Jul-19 3:43:32 PM
2024-25
96
Informat Ics Pract Ices – c lass XI
Installing NumPy
NumPy can be installed by typing following command:
pip install NumPy
6.2 Arr Ay We have learnt about various data types like list, tuple,
and dictionary. In this chapter we will discuss another
datatype ‘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 same data
type, though the values stored in them may be
different.
• The entire array is stored contiguously in
memory. This makes operations on array fast.
• Each element of the array is identified or
referred 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 2
nd
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 5
th
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.
6.3 n um Py Arr Ay 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
Contiguous memory
allocation:
The memory space
must be divided
into the fined sized
position and each
position is allocated
to a single data only.
Now Contiguous
Memory Allocation:
Divide the data into
several blocks and
place in different
parts of the memory
according to the
availability of memory
space.
Chap 6.indd 96 19-Jul-19 3:43:32 PM
2024-25
Introduct Ion to n um Py
97
array is officially called ndarray but commonly known
as array. In rest of the chapter, we will be referring to
NumPy array whenever we use “array”. following are few
differences between list and Array.
6.3.1 Difference Between List and Array
List Array
List can have elements of different data
types for example, [1,3.4, ‘hello’, ‘a@’]
All elements of an array are of same data type for
example, an array of floats may be: [1.2, 5.4, 2.7]
Elements of a list are not stored
contiguously in memory.
Array elements are stored in contiguous memory
locations. This makes operations on arrays faster than
lists.
Lists do not support element wise operations,
for example, addition, multiplication, etc.
because elements may not be of same type.
Arrays support element wise operations. For example,
if A1 is an array, it is possible to say A1/3 to divide
each element of the array by 3.
Lists can contain objects of different
datatype that Python must store the type
information for every element along with its
element value. Thus lists take more space
in memory and are less efficient.
NumPy array takes up less space in memory as
compared to a list because arrays do not require to
store datatype of each element separately.
List is a part of core Python. Array (ndarray) is a part of NumPy library.
6.3.2 Creation of NumPy Arrays from List
There are several ways to create arrays. To create an
array and to 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 = np.array([10,20,30])
#Display the contents of the array
>>> array1
array([10, 20, 30])
• Creating a 1-D Array
An array with only single row of elements is called
1-D array. Let us try to create a 1-D array from
a list which contains numbers as well as strings.
>>> array2 = np.array([5,-7.4,'a',7.2])
>>> array2
Chap 6.indd 97 19-Jul-19 3:43:32 PM
2024-25
Page 4
6.1 Introduct Ion 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 tools for
integrating with other programming languages
like C, C++ etc.
Introduct Ion to n um Py
Chapter
6
“The goal is to turn data into information,
and information into insight.”
— Carly Fiorina
In this chapter
» Introduction
» Array
» NumPy Array
» Indexing and Slicing
» Operations on Arrays
» Concatenating Arrays
» Reshaping Arrays
» Splitting Arrays
» Statistical Operations
on Arrays
» Loading Arrays from
Files
» Saving NumPy Arrays
in Files on Disk
Chap 6.indd 95 19-Jul-19 3:43:32 PM
2024-25
96
Informat Ics Pract Ices – c lass XI
Installing NumPy
NumPy can be installed by typing following command:
pip install NumPy
6.2 Arr Ay We have learnt about various data types like list, tuple,
and dictionary. In this chapter we will discuss another
datatype ‘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 same data
type, though the values stored in them may be
different.
• The entire array is stored contiguously in
memory. This makes operations on array fast.
• Each element of the array is identified or
referred 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 2
nd
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 5
th
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.
6.3 n um Py Arr Ay 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
Contiguous memory
allocation:
The memory space
must be divided
into the fined sized
position and each
position is allocated
to a single data only.
Now Contiguous
Memory Allocation:
Divide the data into
several blocks and
place in different
parts of the memory
according to the
availability of memory
space.
Chap 6.indd 96 19-Jul-19 3:43:32 PM
2024-25
Introduct Ion to n um Py
97
array is officially called ndarray but commonly known
as array. In rest of the chapter, we will be referring to
NumPy array whenever we use “array”. following are few
differences between list and Array.
6.3.1 Difference Between List and Array
List Array
List can have elements of different data
types for example, [1,3.4, ‘hello’, ‘a@’]
All elements of an array are of same data type for
example, an array of floats may be: [1.2, 5.4, 2.7]
Elements of a list are not stored
contiguously in memory.
Array elements are stored in contiguous memory
locations. This makes operations on arrays faster than
lists.
Lists do not support element wise operations,
for example, addition, multiplication, etc.
because elements may not be of same type.
Arrays support element wise operations. For example,
if A1 is an array, it is possible to say A1/3 to divide
each element of the array by 3.
Lists can contain objects of different
datatype that Python must store the type
information for every element along with its
element value. Thus lists take more space
in memory and are less efficient.
NumPy array takes up less space in memory as
compared to a list because arrays do not require to
store datatype of each element separately.
List is a part of core Python. Array (ndarray) is a part of NumPy library.
6.3.2 Creation of NumPy Arrays from List
There are several ways to create arrays. To create an
array and to 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 = np.array([10,20,30])
#Display the contents of the array
>>> array1
array([10, 20, 30])
• Creating a 1-D Array
An array with only single row of elements is called
1-D array. Let us try to create a 1-D array from
a list which contains numbers as well as strings.
>>> array2 = np.array([5,-7.4,'a',7.2])
>>> array2
Chap 6.indd 97 19-Jul-19 3:43:32 PM
2024-25
98
Informat Ics Pract Ices – c lass XI
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 array.
Note: U32 means Unicode-32 data type.
• Creating a 2-D Array
We can create a two dimensional (2-D) arrays by
passing nested lists to the array() function.
Example 6.1
>>> array3 = np.array([[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.
6.3.3 Attributes of NumPy Array
Some important attributes of a NumPy ndarray object are:
i) ndarray.ndim: 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 6.2
>>> array1.ndim
1
>>> array3.ndim
2
ii) ndarray.shape: It gives the sequence of integers
indicating the size of the array for each dimension.
Example 6.3
# array1 is 1D-array, there is nothing
# after , in sequence
>>> array1.shape
(3,)
>>> array2.shape
(4,)
>>> array3.shape
(3, 2)
A common mistake
occurs while passing
argument to array() if
we forget to put square
brackets. Make sure
only a single argument
containing list of
values is passed.
#incorrect way
>>> a =
np.array(1,2,3,4)
#correct way
>>> a =
np.array([1,2,3,4])
A list is called nested
list when each
element is a list itself.
Chap 6.indd 98 19-Jul-19 3:43:32 PM
2024-25
Page 5
6.1 Introduct Ion 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 tools for
integrating with other programming languages
like C, C++ etc.
Introduct Ion to n um Py
Chapter
6
“The goal is to turn data into information,
and information into insight.”
— Carly Fiorina
In this chapter
» Introduction
» Array
» NumPy Array
» Indexing and Slicing
» Operations on Arrays
» Concatenating Arrays
» Reshaping Arrays
» Splitting Arrays
» Statistical Operations
on Arrays
» Loading Arrays from
Files
» Saving NumPy Arrays
in Files on Disk
Chap 6.indd 95 19-Jul-19 3:43:32 PM
2024-25
96
Informat Ics Pract Ices – c lass XI
Installing NumPy
NumPy can be installed by typing following command:
pip install NumPy
6.2 Arr Ay We have learnt about various data types like list, tuple,
and dictionary. In this chapter we will discuss another
datatype ‘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 same data
type, though the values stored in them may be
different.
• The entire array is stored contiguously in
memory. This makes operations on array fast.
• Each element of the array is identified or
referred 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 2
nd
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 5
th
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.
6.3 n um Py Arr Ay 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
Contiguous memory
allocation:
The memory space
must be divided
into the fined sized
position and each
position is allocated
to a single data only.
Now Contiguous
Memory Allocation:
Divide the data into
several blocks and
place in different
parts of the memory
according to the
availability of memory
space.
Chap 6.indd 96 19-Jul-19 3:43:32 PM
2024-25
Introduct Ion to n um Py
97
array is officially called ndarray but commonly known
as array. In rest of the chapter, we will be referring to
NumPy array whenever we use “array”. following are few
differences between list and Array.
6.3.1 Difference Between List and Array
List Array
List can have elements of different data
types for example, [1,3.4, ‘hello’, ‘a@’]
All elements of an array are of same data type for
example, an array of floats may be: [1.2, 5.4, 2.7]
Elements of a list are not stored
contiguously in memory.
Array elements are stored in contiguous memory
locations. This makes operations on arrays faster than
lists.
Lists do not support element wise operations,
for example, addition, multiplication, etc.
because elements may not be of same type.
Arrays support element wise operations. For example,
if A1 is an array, it is possible to say A1/3 to divide
each element of the array by 3.
Lists can contain objects of different
datatype that Python must store the type
information for every element along with its
element value. Thus lists take more space
in memory and are less efficient.
NumPy array takes up less space in memory as
compared to a list because arrays do not require to
store datatype of each element separately.
List is a part of core Python. Array (ndarray) is a part of NumPy library.
6.3.2 Creation of NumPy Arrays from List
There are several ways to create arrays. To create an
array and to 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 = np.array([10,20,30])
#Display the contents of the array
>>> array1
array([10, 20, 30])
• Creating a 1-D Array
An array with only single row of elements is called
1-D array. Let us try to create a 1-D array from
a list which contains numbers as well as strings.
>>> array2 = np.array([5,-7.4,'a',7.2])
>>> array2
Chap 6.indd 97 19-Jul-19 3:43:32 PM
2024-25
98
Informat Ics Pract Ices – c lass XI
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 array.
Note: U32 means Unicode-32 data type.
• Creating a 2-D Array
We can create a two dimensional (2-D) arrays by
passing nested lists to the array() function.
Example 6.1
>>> array3 = np.array([[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.
6.3.3 Attributes of NumPy Array
Some important attributes of a NumPy ndarray object are:
i) ndarray.ndim: 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 6.2
>>> array1.ndim
1
>>> array3.ndim
2
ii) ndarray.shape: It gives the sequence of integers
indicating the size of the array for each dimension.
Example 6.3
# array1 is 1D-array, there is nothing
# after , in sequence
>>> array1.shape
(3,)
>>> array2.shape
(4,)
>>> array3.shape
(3, 2)
A common mistake
occurs while passing
argument to array() if
we forget to put square
brackets. Make sure
only a single argument
containing list of
values is passed.
#incorrect way
>>> a =
np.array(1,2,3,4)
#correct way
>>> a =
np.array([1,2,3,4])
A list is called nested
list when each
element is a list itself.
Chap 6.indd 98 19-Jul-19 3:43:32 PM
2024-25
Introduct Ion to n um Py
99
The output (3, 2) means array3 has 3 rows and 2
columns.
iii) ndarray.size: It gives the total number of
elements of the array. This is equal to the product
of the elements of shape.
Example 6.4
>>> array1.size
3
>>> array3.size
6
iv) ndarray.dtype: 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 6.5
>>> array1.dtype
dtype('int32')
>>> array2.dtype
dtype('<U32>')
>>> array3.dtype
dtype('float64')
v) ndarray.itemsize: 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 6.6
>>> array1.itemsize
4 # memory allocated to integer
>>> array2.itemsize
128 # memory allocated to string
>>> array3.itemsize
8 #memory allocated to float type
6.3.4 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.
n otes Chap 6.indd 99 19-Jul-19 3:43:32 PM
2024-25
Read More