7.
1 Introduction
NumPy (Numerical Python) Library
First appeared in 2006 and is the preferred Python array implementation.
High-performance, richly functional n-dimensional array type called ndarray .
Written in C and up to 100 times faster than lists.
Critical in big-data processing, AI applications and much more.
According to libraries.io , over 450 Python libraries depend on NumPy.
Many popular data science libraries such as Pandas, SciPy (Scientific Python) and Keras (for deep learning) are built on or depend on NumPy.
Array-Oriented Programming
Functional-style programming with internal iteration makes array-oriented manipulations concise and straightforward, and reduces the possibility of error.
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science:
Learning to Program with AI, Big Data and the Cloud (https://amzn.to/2VvdnxE).
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the
theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to
the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising
out of, the furnishing, performance, or use of these programs.
/
7.2 Creating array s from Existing Data
Creating an array with the array function
Argument is an array or other iterable
Returns a new array containing the argument’s elements
In [1]: import numpy as np
In [2]: numbers = np.array([2, 3, 5, 7, 11])
In [3]: type(numbers)
Out[3]: numpy.ndarray
In [4]: numbers
Out[4]: array([ 2, 3, 5, 7, 11])
Multidimensional Arguments
In [5]: np.array([[1, 2, 3], [4, 5, 6]])
Out[5]: array([[1, 2, 3],
[4, 5, 6]])
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science:
Learning to Program with AI, Big Data and the Cloud (https://amzn.to/2VvdnxE).
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the
theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to
the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising
out of, the furnishing, performance, or use of these programs.
/
7.3 array Attributes
attributes enable you to discover information about its structure and contents
In [1]: import numpy as np
In [2]: integers = np.array([[1, 2, 3], [4, 5, 6]])
In [3]: integers
Out[3]: array([[1, 2, 3],
[4, 5, 6]])
In [4]: floats = np.array([0.0, 0.1, 0.2, 0.3, 0.4])
In [5]: floats
Out[5]: array([0. , 0.1, 0.2, 0.3, 0.4])
NumPy does not display trailing 0s
Determining an array ’s Element Type
In [6]: integers.dtype
Out[6]: dtype('int64')
In [7]: floats.dtype
Out[7]: dtype('float64')
For performance reasons, NumPy is written in the C programming language and uses C’s data types
Other NumPy types (https://docs.scipy.org/doc/numpy/user/basics.types.html)
Determining an array ’s Dimensions
ndim contains an array ’s number of dimensions
shape contains a tuple specifying an array ’s dimensions
In [8]: integers.ndim
Out[8]: 2
In [9]: floats.ndim
Out[9]: 1
In [10]: integers.shape
Out[10]: (2, 3)
In [11]: floats.shape
Out[11]: (5,)
Determining an array ’s Number of Elements and Element Size
view an array ’s total number of elements with size
view number of bytes required to store each element with itemsize
/
In [12]: integers.size
Out[12]: 6
In [13]: integers.itemsize
Out[13]: 8
In [14]: floats.size
Out[14]: 5
In [15]: floats.itemsize
Out[15]: 8
Iterating through a Multidimensional array ’s Elements
In [16]: for row in integers:
for column in row:
print(column, end=' ')
print()
1 2 3
4 5 6
Iterate through a multidimensional array as if it were one-dimensional by using flat
In [17]: for i in integers.flat:
print(i, end=' ')
1 2 3 4 5 6
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science:
Learning to Program with AI, Big Data and the Cloud (https://amzn.to/2VvdnxE).
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the
theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to
the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising
out of, the furnishing, performance, or use of these programs.
/
7.4 Filling array s with Specific Values
Functions zeros , ones and full create array s containing 0 s, 1 s or a specified value, respectively
In [1]: import numpy as np
In [2]: np.zeros(5)
Out[2]: array([0., 0., 0., 0., 0.])
For a tuple of integers, these functions return a multidimensional array with the specified dimensions
In [3]: np.ones((2, 4), dtype=int)
Out[3]: array([[1, 1, 1, 1],
[1, 1, 1, 1]])
In [4]: np.full((3, 5), 13)
Out[4]: array([[13, 13, 13, 13, 13],
[13, 13, 13, 13, 13],
[13, 13, 13, 13, 13]])
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science:
Learning to Program with AI, Big Data and the Cloud (https://amzn.to/2VvdnxE).
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the
theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to
the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising
out of, the furnishing, performance, or use of these programs.
/
7.10 Indexing and Slicing
One-dimensional array s can be indexed and sliced like lists.
Indexing with Two-Dimensional array s
To select an element in a two-dimensional array , specify a tuple containing the element’s row and column indices in square brackets
In [1]: import numpy as np
In [2]: grades = np.array([[87, 96, 70], [100, 87, 90],
[94, 77, 90], [100, 81, 82]])
In [3]: grades
Out[3]: array([[ 87, 96, 70],
[100, 87, 90],
[ 94, 77, 90],
[100, 81, 82]])
In [4]: grades[0, 1] # row 0, column 1
Out[4]: 96
Selecting a Subset of a Two-Dimensional array ’s Rows
To select a single row, specify only one index in square brackets
In [5]: grades[1]
Out[5]: array([100, 87, 90])
Select multiple sequential rows with slice notation
In [6]: grades[0:2]
Out[6]: array([[ 87, 96, 70],
[100, 87, 90]])
Select multiple non-sequential rows with a list of row indices
In [7]: grades[[1, 3]]
Out[7]: array([[100, 87, 90],
[100, 81, 82]])
Selecting a Subset of a Two-Dimensional array ’s Columns
The column index also can be a specific index, a slice or a list
In [8]: grades[:, 0]
Out[8]: array([ 87, 100, 94, 100])
In [9]: grades[:, 1:3]
Out[9]: array([[96, 70],
[87, 90],
[77, 90],
[81, 82]])
/
In [10]: grades[:, [0, 2]]
Out[10]: array([[ 87, 70],
[100, 90],
[ 94, 90],
[100, 82]])
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science:
Learning to Program with AI, Big Data and the Cloud (https://amzn.to/2VvdnxE).
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the
theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to
the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising
out of, the furnishing, performance, or use of these programs.