Recap and Today’s Theme
Hello! In the previous episode, we explored the basic operations of Jupyter Notebook. By using Jupyter Notebook, you can run Python code interactively and immediately check the results, making data analysis and AI development much more efficient.
Today, we will focus on NumPy, a numerical computation library for Python. NumPy enables fast numerical computations and is an essential tool for AI and data analysis. Mastering its basic operations will help you develop your skills for future projects. Let’s dive into NumPy!
What Is NumPy?
NumPy (short for Numerical Python) is a library that facilitates efficient numerical computations in Python. It offers several key features:
- High-Performance Multidimensional Arrays (ndarray): More efficient than Python lists, these arrays are used for storing numerical data.
- Extensive Numerical Functions: Includes capabilities for matrix operations, statistical calculations, linear algebra, random number generation, and more.
- Performance Optimization: Implemented in C, NumPy is significantly faster than Python’s native list operations.
Using these features effectively can greatly enhance AI development and data analysis efficiency.
Installing NumPy
If you are using Anaconda, NumPy is installed by default. However, if you want to install it manually, you can use the following command:
pip install numpy
Now, let’s explore the basic operations of NumPy.
Basic Operations in NumPy
1. Importing NumPy
To use NumPy, you first need to import it. By convention, NumPy is imported using the alias np
.
import numpy as np
2. Creating ndarrays
The core data structure in NumPy is the ndarray (N-dimensional array). Let’s look at how to create basic arrays.
Creating a 1-Dimensional Array (Vector)
# Creating a 1D array
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Creating a 2-Dimensional Array (Matrix)
# Creating a 2D array
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix)
3. Array Attributes
Let’s check some attributes of NumPy arrays.
- ndim: Number of dimensions of the array.
- shape: Shape of the array (size of each dimension).
- size: Total number of elements in the array.
- dtype: Data type of the elements in the array.
print(matrix.ndim) # Output: 2 (2D)
print(matrix.shape) # Output: (2, 3) (2 rows, 3 columns)
print(matrix.size) # Output: 6 (total elements)
print(matrix.dtype) # Output: int64 (default data type)
4. Creating Special Arrays
NumPy can create arrays based on specific conditions easily.
- zeros: Array with all elements set to 0.
- ones: Array with all elements set to 1.
- arange: Array with a range of consecutive numbers.
- linspace: Array with evenly spaced numbers over a specified interval.
zeros = np.zeros((2, 3))
ones = np.ones((3, 3))
arange = np.arange(0, 10, 2) # From 0 to 10 with a step of 2
linspace = np.linspace(0, 1, 5) # 5 evenly spaced numbers between 0 and 1
print(zeros)
print(ones)
print(arange)
print(linspace)
5. Array Manipulation
Accessing and Modifying Elements
NumPy arrays allow element access using indices, and elements can be easily modified.
print(matrix[0, 2]) # Output: 3 (1st row, 3rd column)
matrix[1, 2] = 9 # Changing the value in the 2nd row, 3rd column to 9
print(matrix)
Slicing
Like Python lists, NumPy arrays support slicing.
sub_array = matrix[0, 1:3] # Extract elements from the 1st row, columns 2 to 3
print(sub_array)
6. Array Operations
NumPy supports basic operations (addition, subtraction, multiplication, division) between arrays, applied element-wise.
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # Output: [5 7 9]
print(a * b) # Output: [4 10 18]
For matrix operations, use the dot
function.
# Dot product of matrices
c = np.array([[1, 2], [3, 4]])
d = np.array([[5, 6], [7, 8]])
result = np.dot(c, d)
print(result)
7. Statistical Functions
NumPy offers statistical functions such as mean, median, and standard deviation.
arr = np.array([1, 2, 3, 4, 5])
print(np.mean(arr)) # Mean
print(np.median(arr)) # Median
print(np.std(arr)) # Standard deviation
8. Generating Random Numbers
Random number generation is frequently used in AI development. NumPy provides a range of functions for generating random numbers.
random_array = np.random.rand(3, 3) # 3x3 array of random numbers between 0 and 1
normal_array = np.random.randn(3, 3) # 3x3 array of random numbers following a standard normal distribution
print(random_array)
print(normal_array)
Practical Example Using NumPy
NumPy is not just for numerical data manipulation; it can also be used for plotting graphs and simulations. Below is a simple example of creating a sine wave.
1. Plotting a Sine Wave
Using NumPy, you can plot mathematical functions like a sine wave.
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi, 100) # 100 points between 0 and 2π
y = np.sin(x)
plt.plot(x, y)
plt.title("Sine Wave")
plt.xlabel("x")
plt.ylabel("sin(x)")
plt.show()
Summary
In this episode, we covered the basics of NumPy. Mastering NumPy allows for fast and efficient numerical computations in Python, which is crucial for AI development and data analysis. Learning the fundamentals of NumPy is an important step for every developer.
Next Episode Preview
Next time, we will introduce Pandas, a data manipulation library. Pandas, along with NumPy, is widely used in data analysis and simplifies data loading, manipulation, and processing. Stay tuned!
Annotations
- ndarray: A NumPy object for N-dimensional arrays, allowing for fast numerical computations.
- Dot Product: An operation that calculates the product between matrices, frequently used in machine learning models.
Comments