NumPy Basics

 Your First Steps with Powerful Arrays

If you've spent any time with Python for data science, you've likely heard of NumPy. It's the foundational library for numerical computing, but what makes it so special? The short answer is speed and simplicity. NumPy gives us the ndarray—a powerful and fast N-dimensional array object that is far more efficient than a standard Python list for numerical operations.
Why not just use lists?
Let's look at a simple example. Say you want to add 2 to every number in a list. With Python lists, you'd need a loop.
python
# Python lists require a loop
my_list = [1, 2, 3, 4]
new_list = [x + 2 for x in my_list]
print(new_list) # Output: [3, 4, 5, 6]

With NumPy, it's as simple as writing it out. NumPy knows you want to perform the operation on every element.
python
import numpy as np

# NumPy allows this "vectorized" operation
my_array = np.array([1, 2, 3, 4])
new_array = my_array + 2
print(new_array) # Output: [3 4 5 6]
This is called vectorization, and it's what makes NumPy so fast.
Creating Your First NumPy Arrays
Creating an array is straightforward. You can create a 1D array from a list or a 2D array from a list of lists.
From a Python List:
python
import numpy as np

one_d_array = np.array([10, 20, 30])
print(one_d_array)
# Output: [10 20 30]

two_d_array = np.array([[1, 2, 3], [4, 5, 6]])
print(two_d_array)
# Output:
# [[1 2 3]
#  [4 5 6]]

Using Built-in Functions:
NumPy also has functions for generating arrays quickly.
  • np.zeros(): Creates an array filled with zeros.
  • np.ones(): Creates an array filled with ones.
  • np.arange(): Creates a sequence of numbers (similar to Python's range).
python
zeros_array = np.zeros((2, 3)) # A 2x3 array of zeros
print(zeros_array)
# Output:
# [[0. 0. 0.]
#  [0. 0. 0.]]

sequence = np.arange(5) # An array from 0 to 4
print(sequence)
# Output: [0 1 2 3 4]

Indexing and Slicing
Accessing elements in a NumPy array works just like with Python lists, but with extra power for multi-dimensional arrays.
python
# Create a 2D array
data = np.array([[10, 20, 30], [40, 50, 60]])

# Get the element at the first row, second column
print(data[0, 1]) # Output: 20

# Get the second row
print(data[1, :]) # Output: [40 50 60]

# Slice to get the first column
print(data[:, 0]) # Output: [10 40]

Notice how we use a comma to separate the row and column indices.

Comments

Popular Posts