Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

15.2: NumPy

( \newcommand{\kernel}{\mathrm{null}\,}\)

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])
  • np.zeros(1, 2)
  • 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 4]
    [6 8]]
  • [[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


    This page titled 15.2: NumPy is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

    Support Center

    How can we help?