Introduction to NumPy

Get acquainted with NumPy, a Python library used to store arrays of numbers, and learn basic syntax and functionality.

Start[missing "en.views.course_landing_page.intro-statistics-numpy.course_illustration" translation]

Key Concepts

Review core concepts you need to learn to master this subject

NumPy Arrays

# Import the NumPy module, aliased as 'np' import numpy as np # NumPy Arrays can be created with a list my_array = np.array([1, 2, 3, 4])

NumPy (short for "Numerical Python") is a Python module used for numerical computing, creating arrays and matrices, and performing very fast operations on those data structures. The core of NumPy is a multidimensional Array object.

The NumPy .array() method is used to create new NumPy Arrays.

Creating NumPy Arrays from files

# Import the NumPy module, aliased as 'np' import numpy as np # NumPy Arrays can be created with a list my_array = np.array([1, 2, 3, 4])

NumPy Arrays can be created from data in CSV files or other delimited text by using the np.genfromtxt() method.

The named parameter delimiter is used to determine the delimiting character between values.

NumPy element-wise arithmetic operations

# Import the NumPy module, aliased as 'np' import numpy as np # NumPy Arrays can be created with a list my_array = np.array([1, 2, 3, 4])

NumPy Arrays support element-wise operations, meaning that arithmetic operations on arrays are applied to each value in the array.

Multiple arrays can also be used in arithmetic operations, provided that they have the same lengths.

Accessing NumPy Array elements by index

# Import the NumPy module, aliased as 'np' import numpy as np # NumPy Arrays can be created with a list my_array = np.array([1, 2, 3, 4])

Individual NumPy Array elements can be accessed by index, using syntax identical to Python lists: array[index] for a single element, or array[start:end] for a slice, where start and end are the starting and ending indexes for the slice.

Nested Arrays or elements can be accessed by adding additional comma-separated parameters.

Indexing NumPy elements using conditionals

# Import the NumPy module, aliased as 'np' import numpy as np # NumPy Arrays can be created with a list my_array = np.array([1, 2, 3, 4])

NumPy elements can be indexed using conditionals. The syntax to filter an array using a conditional is array_name[conditional].

The returned array will contain only the elements for which the conditional evaluates to True.

NumPy element-wise logical operations

# Import the NumPy module, aliased as 'np' import numpy as np # NumPy Arrays can be created with a list my_array = np.array([1, 2, 3, 4])

NumPy Arrays support element-wise logical operations, returning new Arrays populated with False or True based on their evaluation.

Introduction to NumPy
Lesson 1 of 1
  1. 1

    Sarah records her second-grade class's grades in an online spreadsheet. Her web browser records that she visited that spreadsheet, in addition to every other site she's visited. Those sites record...

  2. 2

    To use NumPy with Python, import it at the top of your file using the following line: [...] Writing [...] allows us to use [...] as a shorthand for NumPy, which saves us time when calling ...

  3. 3

    NumPy includes a powerful data structure known as an array. A NumPy array is a special type of list. It’s a data structure that organizes multiple items. Each item can be of any type (strings, ...

  4. 4

    Typically, you won't be entering data directly into an array. Instead, you'll be importing the data from somewhere else. We're able to transform CSV (comma-separated values) files into arrays usin...

  5. 5

    Generally, NumPy arrays are more efficient than lists. One reason is that they allow you to do element-wise operations. An element-wise operation allows you to quickly perform an operation, such ...

  6. 6

    Arrays can also be added to or subtracted from each other in NumPy, assuming the arrays have the same number of elements. When adding or subtracting arrays in NumPy, each element will be added/sub...

  7. 7

    In Python, we can create lists that are made up of other lists. Similarly, in NumPy we can create an array of arrays. If the arrays that make up our bigger array are all the same size, then it has ...

  8. 8

    NumPy allows us to select elements from an array using their indices. Consider the one-dimensional array [...] If we wanted to select the first element in this array, we would call: [...] I...

  9. 9

    Selecting elements from a 2-d array is very similar to selecting them from a 1-d array, we just have two indices to select from. The syntax for selecting from a 2-d array is [...] where [...] i...

  10. 10

    Another useful thing that arrays can do is perform element-wise logical operations. For instance, suppose we want to know how many elements in an array are greater than 5. We can easily write som...

  11. 11

    Let's take a second and review. In this lesson, you learned the basics of the NumPy package. Here are some key points: - Arrays are a special type of list that allows us to store values in an orga...

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Introduction to NumPy

Start[missing "en.views.course_landing_page.intro-statistics-numpy.course_illustration" translation]