NumPy Array Selection and Updating
NumPy Array Basics
The NumPy Array
At the heart of the NumPy library is its core data structure: the n-dimensional array, or ndarray. Think of it as a powerful grid that can hold data of the same type. While it might sound similar to a standard Python list, a NumPy array is much faster and more memory-efficient for numerical calculations.
NumPy arrays are the building blocks for a huge amount of data science and machine learning work in Python. They allow for fast, vectorized operations on large datasets.
Let's start by creating one. The most direct way is to convert a Python list into an array. To do this, we first need to import the NumPy library, which is conventionally imported with the alias np.
import numpy as np
# Create a 1D array from a list
my_list = [1, 2, 3, 4, 5]
arr1d = np.array(my_list)
print(arr1d)
You can also create multi-dimensional arrays by passing in a list of lists. Each inner list becomes a row in the array.
# Create a 2D array (a matrix)
my_2d_list = [[1, 2, 3], [4, 5, 6]]
arr2d = np.array(my_2d_list)
print(arr2d)
Creating Placeholder Arrays
Often, you'll need an array of a specific size but don't have the final data yet. NumPy provides handy functions for this. For example, you can create an array filled entirely with zeros or ones.
# Create a 1D array of five zeros
zeros_arr = np.zeros(5)
print(zeros_arr)
# Create a 2x3 array of ones
ones_arr = np.ones((2, 3))
print(ones_arr)
Notice that for the 2D array, we passed a tuple (2, 3) to specify the shape: 2 rows and 3 columns.
Another useful function is np.arange, which works much like Python's built-in range function to create an array with a sequence of numbers.
# Create an array with numbers 0 through 9
seq_arr = np.arange(10)
print(seq_arr)
Understanding Array Attributes
Every NumPy array has attributes that describe its properties. These are not functions, so you don't use parentheses to access them. Three of the most important are shape, size, and dtype.
The
shapeattribute tells you the dimensions of the array as a tuple. For a 1D array, it's a tuple with one number. For a 2D array, it's (rows, columns).
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Get the shape of the array
print(arr.shape)
The size attribute gives you the total number of elements in the array. This is simply the product of the numbers in the shape tuple. For our arr above, the size is .
# Get the total number of elements
print(arr.size)
Finally, dtype tells you the data type of the elements inside the array. Unlike Python lists, all elements in a NumPy array must be of the same type. This uniformity is key to NumPy's speed. Common types include int64 for integers and float64 for floating-point numbers.
# Check the data type of the elements
print(arr.dtype)
# An array with floating-point numbers
float_arr = np.array([1.5, 2.0, 3.7])
print(float_arr.dtype)
Basic Operations
NumPy arrays make performing mathematical operations on entire sets of data simple and fast. Operations are applied element by element. If you add two arrays of the same shape, NumPy adds the corresponding elements from each array.
a = np.array([10, 20, 30])
b = np.array([1, 2, 3])
# Element-wise addition
print(a + b)
# Element-wise multiplication
print(a * b)
This also works with a scalar (a single number). If you multiply an array by a number, every element in the array is multiplied by that number.
arr = np.array([1, 2, 3])
# Multiply every element by 10
print(arr * 10)
This ability to perform batch operations without writing explicit loops is called vectorization. It's a core concept that makes NumPy so powerful for data analysis.