Introduction

NumPy (short for Numerical Python) is a powerful Python library for numerical computing. It provides support for large, multi-dimensional arrays and matrices, as well as a wide range of mathematical functions to operate on these arrays. NumPy is widely used in scientific computing, data analysis, and machine learning applications.

In this blog post, we will provide a practical introduction to NumPy. We will cover the basics of creating and manipulating NumPy arrays, and we will explore some of the most common operations and functions that you can perform with them. By the end of the post, you should have a solid understanding of how to use NumPy to perform numerical computations in Python.

NumPy arrays

NumPy arrays are the foundation of the library. An array is a collection of elements, all of the same type, arranged in a rectangular shape of one or more dimensions. While Python arrays are a basic built-in data type that can store homogeneous elements, NumPy arrays are a more powerful data structure that can handle multidimensional arrays of homogeneous or heterogeneous data types with built-in mathematical functions optimized for numerical operations.

In order to initialize an array, the most useful functions are:

Python
import numpy as np

# Creates a NumPy array from a Python array
a = np.array([1, 2, 3, 4, 5])

# Creates an array of 0s with the given dimensions
b = np.zeros((3, 4))

# Creates an array of 1s with the given dimensions
c = np.ones((2, 2))

# Creates an array of evenly spaced values
# within a specified interval
d = np.arange(10)

# Creates an array of evenly spaced values
# within a specified interval, but spiecifying
# the interval
e = np.linspace(0, 1, 5)

print(f"a = {a}")
print(f"b = {b}")
print(f"c = {c}")
print(f"d = {d}")
print(f"e = {e}")
a = [1 2 3 4 5]
b = [[0. 0. 0. 0.]
     [0. 0. 0. 0.]
     [0. 0. 0. 0.]]
c = [[1. 1.]
     [1. 1.]]
d = [0 1 2 3 4 5 6 7 8 9]
e = [0.   0.25 0.5  0.75 1.  ]

Furthermore, you can change the shape of an array or concatenate arrays:

Python
import numpy as np

# Reshape array a into an array of dimensions (2,3)
a = np.array([1, 2, 3, 4, 5, 6])
b = a.reshape(2, 3)
print(f"Original array: {a}")
print(f"Reshaped array: {b}")

# Concatenate 2 arrays by rows
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
c = np.concatenate((a, b), axis=0)
print(f"Original array: {a}")
print(f"Second array: {b}")
print(f"Concatenated array: {c}")

# Concatenate 2 arrays by columns
a = np.array([[1, 2], [3, 4]])
b = np.array([[5], [6]])
c = np.concatenate((a, b), axis=1)
print(f"Original array: {a}")
print(f"Second array: {b}")
print(f"Concatenated array: {c}")
Original array: [1 2 3 4 5 6]
Reshaped array: [[1 2 3]
                 [4 5 6]]

Original array: [[1 2]
                 [3 4]]
Second array: [[5 6]]
Concatenated array: [[1 2]
                     [3 4]
                     [5 6]]


Original array: [[1 2]
                 [3 4]]
Second array: [[5]
               [6]]
Concatenated array: [[1 2 5]
                     [3 4 6]]

Array operations

Once you have created a NumPy array, you can access its elements using indexing and slicing. Indexing allows you to retrieve a specific element or a set of elements from an array, while slicing allows you to retrieve a subset of elements from an array.

Python
import numpy as np

# 1 dimensional array
a = np.array([1, 2, 3, 4, 5])
# 2 dimensional array
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Indexing
print(f"Element at a[1] is {a[1]}")
print(f"Element at b[1, 2] is {b[1, 2]}")

# Slicing
print(f"Elements in a[1:4] are {a[1:4]}")
print(f"Elements in b[1:3, 0:2] are {b[1:3, 0:2]}")
Element at a[1] is 2
Element at b[1, 2] is 6

Elements in a[1:4] are [2 3 4]
Elements in b[1:3, 0:2] are [[4 5]
                             [7 8]]

Functions over arrays

NumPy provides a wide range of mathematical functions that you can use to perform complex calculations on NumPy arrays. These functions are optimized for efficient computation on large arrays and can often be applied to entire arrays at once.

Python
import numpy as np

angles = np.array([0, np.pi/4, np.pi/2, 3*np.pi/4, np.pi])
sines = np.sin(angles)

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

# Calculate the exponential of all elements
exponents = np.exp(values)
mean = np.mean(values)
std = np.std(values)

print(f"Sines: {sines}")
print(f"Exponents: {exponents}")
print(f"Mean: {mean}")
print(f"STD: {std}")
Sines: [0.00000000e+00 7.07106781e-01 1.00000000e+00 7.07106781e-01- 1.22464680e-16]
Exponents: [ 1.          2.71828183  7.3890561  20.08553692 54.59815003]
Mean: 2.0
STD: 1.4142135623730951

Conclusions

In conclusion, NumPy is a powerful library for scientific computing in Python. It provides a high-performance multidimensional array object, along with tools for working with these arrays. NumPy allows for the efficient computation of large arrays and provides a vast array of mathematical and array manipulation functions. By understanding the basics of NumPy, you can unleash the full potential of scientific computing with Python.

Categories: Practical

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *