Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Describe the NumPy library.
  • Create a NumPy array object.
  • Choose appropriate NumPy functions to process arrays of numerical data.

NumPy library

NumPy (Numerical Python) is a Python library that provides support for efficient numerical operations on large, multi-dimensional arrays and serves as a fundamental building block for data analysis in Python. The conventional alias for importing NumPy is np. In other words, NumPy is imported as import numpy as np. NumPy implements the ndarray object, which allows the creation of a multi-dimensional array of homogeneous data types (columns with the same data type) and efficient data processing. An ndarray object can have any number of dimensions and can store elements of various numeric data types. To create a NumPy ndarray object, one of the following options can be used:

  • Creating an ndarray by converting a Python list or tuple using the np.array() function.
  • Using built-in functions like np.zeros() and np.ones() for creating an array of all 0's or all 1's, respectively.
  • Generating an array with random numbers using np.random.rand(n, m), where n and m are the number of rows and columns, respectively.
  • Loading data from a file. Ex: np.genfromtxt('data.csv', delimiter=',').

Checkpoint

Creating an ndarray object

Concepts in Practice

NumPy library

1.
Which of the following creates an ndarray object with one row and two columns?
  1. np.array([2, 3])
  2. np.zeros(1, 2)
  3. np.zeros((1, 2))
2.
Which of the following is a NumPy data type?
  1. ndarray
  2. list
  3. array
3.
What is the benefit of using anndarray object compared to a list?
  1. computational efficiency
  2. array-oriented computing
  3. memory efficiency
  4. all the above

NumPy operations

In addition to the ndarray data type, NumPy's operations provide optimized performance for large-scale computation. The key features of NumPy include:

  • Mathematical operations: NumPy provides a range of mathematical functions and operations that can be applied to entire arrays or specific elements. These operations include arithmetic, trigonometric, exponential, and logarithmic functions.
  • Array manipulation: NumPy provides various functions to manipulate the shape, size, and structure of arrays. These include reshaping, transposing, concatenating, splitting, and adding or removing elements from arrays.
  • Linear algebra operations: NumPy offers a set of linear algebra functions for matrix operations, like matrix multiplication, matrix inversion, eigenvalues, and eigenvectors.

Checkpoint

NumPy array operations

Concepts in Practice

NumPy operations

4.
What is the output of the following code?
import numpy as np

arr = np.array([[1, 2], [3, 4]])
out = 2 * arr
print(out)
  1. [[2 4]
    [8 16]]
  2. [[2 4]
    [6 8]]
  3. [[1 2]
    [3 4]]
5.
Which of the following results in a 2 by 3 ndarray?
  1. import numpy as np
    arr = np.array(2, 3)
    
  2. import numpy as np
    arr = np.array([[1, 2], [1, 2], [1, 2]])
    
  3. import numpy as np
    arr = np.array([[1, 2], [1, 2], [1, 2]]).T
    
6.
The function np.multiply(arr1, arr2) receives two ndarray objects arr1 and arr2 with the same dimensions, and performs element-wise multiplication. What is the output of the following code?
import numpy as np

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[1, 0], [0, 1]])
out = np.multiply(arr1, arr2)
print(out)
  1. [[1 0]
    [0 4]]
    
  2. [[1 2]
    [3 4]]
    
  3. [[2 2]
    [3 5]]

Exploring further

Please refer to the NumPy user guide for more information about the NumPy library.

Programming practice with Google

Use the Google Colaboratory document below to practice NumPy functionalities to extract statistical insights from a dataset.

Google Colaboratory document

Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Mar 15, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.