Introduction to Libraries: NumPy

Learn basics about NumPy for numerical computing.


NumPy: Introduction to Numerical Computing in Python

What is NumPy?

NumPy (Numerical Python) is a fundamental library for numerical computations in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently. NumPy is the foundation upon which many other scientific computing libraries (like SciPy, Pandas, and Matplotlib) are built.

Purpose and Benefits

The primary purpose of NumPy is to enable efficient numerical computations, especially those involving large datasets. Here are some key benefits:

  • Efficient Array Operations: NumPy's arrays (ndarray objects) are much more memory-efficient and faster than Python lists for numerical operations. This is because NumPy arrays store data in a contiguous block of memory, allowing for optimized vectorized operations.
  • Broadcasting: NumPy's broadcasting feature allows for arithmetic operations between arrays of different shapes, making computations more concise and easier to write.
  • Mathematical Functions: NumPy provides a rich set of mathematical functions, including linear algebra routines, Fourier transforms, random number generation, and more.
  • Integration with Other Libraries: NumPy arrays are easily integrated with other scientific computing libraries in Python, such as SciPy, Pandas, and Matplotlib.

An Introductory Lesson to NumPy

Getting Started: Installation

If you don't have NumPy installed, you can install it using pip:

pip install numpy

Importing NumPy

To use NumPy, you typically import it with the alias np:

import numpy as np

Creating NumPy Arrays

The core of NumPy is the ndarray (n-dimensional array) object. Here are some ways to create arrays:

Creating an array from a Python list:

 import numpy as np

my_list = [1, 2, 3, 4, 5]
my_array = np.array(my_list)
print(my_array) # Output: [1 2 3 4 5]
print(type(my_array)) # Output: <class 'numpy.ndarray'> 

Creating an array of zeros:

 import numpy as np

zeros_array = np.zeros((2, 3))  # 2 rows, 3 columns
print(zeros_array)
# Output:
# [[0. 0. 0.]
#  [0. 0. 0.]] 

Creating an array of ones:

 import numpy as np

ones_array = np.ones((3, 2))  # 3 rows, 2 columns
print(ones_array)
# Output:
# [[1. 1.]
#  [1. 1.]
#  [1. 1.]] 

Creating an array with a range of values:

 import numpy as np

range_array = np.arange(0, 10, 2)  # Start, stop, step
print(range_array) # Output: [0 2 4 6 8] 

Creating an array with evenly spaced values:

 import numpy as np

linspace_array = np.linspace(0, 1, 5)  # Start, stop, number of values
print(linspace_array) # Output: [0.   0.25 0.5  0.75 1.  ] 

Array Attributes

NumPy arrays have several useful attributes:

 import numpy as np

my_array = np.array([[1, 2, 3], [4, 5, 6]])

print("Shape:", my_array.shape)  # Output: Shape: (2, 3) - (rows, columns)
print("Data type:", my_array.dtype)  # Output: Data type: int64 (or int32 depending on your system)
print("Number of dimensions:", my_array.ndim) # Output: Number of dimensions: 2
print("Size:", my_array.size)  # Output: Size: 6 (total number of elements) 

Array Operations

NumPy supports element-wise operations and broadcasting. Let's look at some examples:

Element-wise addition:

 import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

sum_array = a + b
print(sum_array)  # Output: [5 7 9] 

Element-wise multiplication:

 import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

product_array = a * b
print(product_array)  # Output: [ 4 10 18] 

Broadcasting (adding a scalar to an array):

 import numpy as np

a = np.array([1, 2, 3])
scalar = 2

result_array = a + scalar
print(result_array)  # Output: [3 4 5]