Numpy Arrays
Numpy Arrays
Numpy Arrays
1 Introduction
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 [3]: type(numbers)
Out[3]: numpy.ndarray
In [4]: numbers
Multidimensional Arguments
©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 [3]: integers
In [5]: floats
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)
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,)
/
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
1 2 3
4 5 6
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 [2]: np.zeros(5)
For a tuple of integers, these functions return a multidimensional array with the specified dimensions
©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.
In [3]: grades
Out[4]: 96
In [5]: grades[1]
In [6]: grades[0:2]
In [8]: grades[:, 0]
/
In [10]: grades[:, [0, 2]]
©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.