You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

is a powerhouse for scientific computing in Python. It provides support for large, multi-dimensional arrays and matrices, enabling fast and efficient operations on datasets. This makes it essential for data science tasks and the foundation for other libraries.

NumPy offers tools for creating and manipulating arrays, from basic indexing to reshaping and joining. It also provides a wide range of mathematical and statistical functions, allowing for efficient data processing and analysis. Understanding these features is crucial for effective data manipulation in Python.

Introduction to NumPy

Purpose and features of NumPy

Top images from around the web for Purpose and features of NumPy
Top images from around the web for Purpose and features of NumPy
  • Fundamental library for scientific computing in Python
    • Provides support for large, multi-dimensional arrays and matrices
    • Offers a wide range of mathematical functions to efficiently operate on these arrays
  • Key features enable fast and efficient operations on large datasets
    • Supports allows arrays with different shapes to work together
    • Provides tools for integrating C/C++ and Fortran code
    • Enables the use of sophisticated mathematical and statistical functions (
      [np.sin()](https://www.fiveableKeyTerm:np.sin())
      ,
      [np.exp()](https://www.fiveableKeyTerm:np.exp())
      )
  • Essential for data science tasks
    • Allows for efficient storage and manipulation of data (
      np.array()
      ,
      [np.zeros()](https://www.fiveableKeyTerm:np.zeros())
      )
    • Serves as the foundation for other data science libraries (Pandas, SciPy)

Creation and manipulation of arrays

  • Create arrays using
    np.array()
    from a list or tuple
    • Create arrays with specific values using functions (
      np.zeros()
      ,
      [np.ones()](https://www.fiveableKeyTerm:np.ones())
      ,
      [np.arange()](https://www.fiveableKeyTerm:np.arange())
      )
    • Specify the data type of an array using the
      [dtype](https://www.fiveableKeyTerm:dtype)
      parameter (
      np.array([1, 2, 3], dtype=np.float64)
      )
  • Array attributes provide information about the array
    • shape
      returns the dimensions of the array as a tuple (
      (3, 4)
      )
    • size
      returns the total number of elements in the array (
      12
      )
    • ndim
      returns the number of dimensions (axes) of the array (
      2
      )
  • Access elements using indexing and slicing
    • Use square brackets
      []
      with index or slice notation (
      arr[0]
      ,
      arr[1:5]
      )
    • Use comma-separated indices to access elements in multi-dimensional arrays (
      arr[1, 2]
      )
    • Slice arrays using the
      start:stop:step
      syntax (
      arr[0:6:2]
      )
    • Use to select multiple non-contiguous elements (
      arr[[0, 2, 4]]
      )
  • Reshape arrays to change their shape without altering data
    • Use
      reshape()
      to change the shape of an array (
      arr.reshape(2, 6)
      )
    • Flatten multi-dimensional arrays into 1D arrays using
      [flatten()](https://www.fiveableKeyTerm:flatten())
      or
      [ravel()](https://www.fiveableKeyTerm:ravel())
      (
      arr.flatten()
      )
  • Join and split arrays
    • Concatenate arrays using
      [np.concatenate()](https://www.fiveableKeyTerm:np.concatenate())
      ,
      [np.vstack()](https://www.fiveableKeyTerm:np.vstack())
      , and
      [np.hstack()](https://www.fiveableKeyTerm:np.hstack())
      (
      np.concatenate((arr1, arr2))
      )
    • Split arrays into smaller arrays using
      [np.split()](https://www.fiveableKeyTerm:np.split())
      ,
      [np.vsplit()](https://www.fiveableKeyTerm:np.vsplit())
      , and
      [np.hsplit()](https://www.fiveableKeyTerm:np.hsplit())
      (
      np.split(arr, 3)
      )

NumPy Functions and Data Processing

NumPy functions for data processing

  • Perform mathematical operations on arrays
    • Element-wise arithmetic operations using
      +
      ,
      -
      ,
      *
      ,
      /
      , and
      **
      (
      arr1 + arr2
      )
    • Apply functions like
      [np.sqrt()](https://www.fiveableKeyTerm:np.sqrt())
      ,
      np.exp()
      , and
      [np.log()](https://www.fiveableKeyTerm:np.log())
      for element-wise computations (
      np.sqrt(arr)
      )
    • Use trigonometric functions like
      np.sin()
      ,
      [np.cos()](https://www.fiveableKeyTerm:np.cos())
      , and
      [np.tan()](https://www.fiveableKeyTerm:np.tan())
      on arrays (
      np.sin(arr)
      )
    • Utilize for efficient element-wise operations
  • Calculate statistical measures
    • Summary statistics using
      [np.mean()](https://www.fiveableKeyTerm:np.mean())
      ,
      [np.median()](https://www.fiveableKeyTerm:np.median())
      ,
      [np.std()](https://www.fiveableKeyTerm:np.std())
      , and
      [np.var()](https://www.fiveableKeyTerm:np.var())
      (
      np.mean(arr)
      )
    • Find the minimum and maximum values using
      [np.min()](https://www.fiveableKeyTerm:np.min())
      and
      [np.max()](https://www.fiveableKeyTerm:np.max())
      (
      np.max(arr)
      )
    • Compute the sum and product of array elements using
      [np.sum()](https://www.fiveableKeyTerm:np.sum())
      and
      [np.prod()](https://www.fiveableKeyTerm:np.prod())
      (
      np.sum(arr)
      )
    • Specify the for operations to work along different dimensions (
      np.mean(arr, axis=0)
      )
  • Utilize broadcasting to work with arrays of different shapes
    • NumPy automatically broadcasts arrays to make their shapes compatible (
      arr1 + arr2
      )
  • Mask and filter arrays
    • Create boolean masks using comparison operators (
      >
      ,
      <
      ,
      ==
      ,
      !=
      )
    • Use boolean masks to filter arrays and select specific elements (
      arr[arr > 5]
      )
    • Combine boolean masks using logical operators (
      &
      ,
      |
      ,
      ~
      )
  • Generate random numbers
    • Use the
      np.random
      module to generate random numbers (
      np.random.rand(3, 4)
      )
    • Create arrays with random values using functions like
      [np.random.rand()](https://www.fiveableKeyTerm:np.random.rand())
      and
      [np.random.randn()](https://www.fiveableKeyTerm:np.random.randn())
      (
      np.random.randn(5)
      )
    • Generate random integers using
      [np.random.randint()](https://www.fiveableKeyTerm:np.random.randint())
      (
      np.random.randint(0, 10, size=(2, 3))
      )

Advanced NumPy Concepts

  • for heterogeneous data types
    • Create arrays with named fields and different data types
    • Access fields using dot notation or string indexing
  • and performance optimization
    • Understand row-major (C-style) vs. column-major (Fortran-style) order
    • Use to efficiently access array elements in memory
  • Array views vs. copies
    • Create views of arrays without copying data
    • Understand when operations create new arrays or modify existing ones
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary