Introduction to NumPy
Get acquainted with NumPy, a Python library used to store arrays of numbers, and learn basic syntax and functionality.
StartKey Concepts
Review core concepts you need to learn to master this subject
Indexing NumPy elements using conditionals
NumPy element-wise logical operations
Creating NumPy Arrays from files
NumPy Arrays
Accessing NumPy Array elements by index
NumPy element-wise arithmetic operations
Indexing NumPy elements using conditionals
Indexing NumPy elements using conditionals
numbers = np.array([-5, 4, 0, 2, 3])
positives = numbers[numbers > 0]
print(positives)
# array([4, 2, 3])
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
.
- 1Sarah 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…
- 2To use NumPy with Python, import it at the top of your file using the following line: import numpy as np Writing as np allows us to use np as a shorthand for NumPy, which saves us time when cal…
- 3NumPy 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, …
- 4Typically, 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…
- 5Generally, 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 …
- 6Arrays 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…
- 7In 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 …
- 8NumPy allows us to select elements from an array using their indices. Consider the one-dimensional array a = np.array([5, 2, 7, 0, 11]) If we wanted to select the first element in this array, we…
- 9Selecting 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 a[row,column] where a i…
- 10Another 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…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory