Python Library Optimized for Numerical Computation, NumPy
We will begin coding exercises using Python. If you are new to Python coding, these exercises might be challenging. We recommend you first take the Gentle Introduction to Python Coding course.
If you are more interested in understanding the principles behind how AI functions rather than coding exercises, feel free to experience the coding activities briefly on the right side of the screen. Focus on the theory explanations provided on the left side.
In AI and data analysis, it is crucial to process large amounts of numerical data quickly and efficiently.
NumPy
is one of the most widely used packages in Python for such tasks.
With NumPy, you can efficiently perform array operations, and it offers high-speed computations for vector and matrix operations.
This makes data processing much faster compared to using normal Python lists.
Installing NumPy
You can install NumPy in Python using the following command. The practice environment already has NumPy installed, so there's no need for separate installation.
pip install numpy
Why NumPy is Widely Used
NumPy is essential in many data science and AI projects for several reasons:
-
Fast Computation Speed: Implemented in C language internally, which allows much faster computations than Python lists.
-
Support for Multidimensional Arrays: Handles diverse data structures from 1D vectors to 2D matrices and higher dimensions.
-
Compatibility with Other Libraries: Integrally used in various data analysis and AI libraries such as Pandas, Scikit-learn, and TensorFlow.
Creating NumPy Arrays (numpy.ndarray)
The fundamental data structure in NumPy is ndarray
(N-dimensional array). Let's learn how to create basic arrays.
import numpy as np
# Create a 1D array
arr = np.array([1, 2, 3, 4, 5])
print(arr)
# Output: [1 2 3 4 5]
In the code above, you can convert a typical list into a NumPy array using the np.array()
function.
Creating a 2D Array (Matrix)
A 2D array consists of rows
and columns
.
To create a 2D array using NumPy, you can pass a nested list as follows.
import numpy as np
# Create a 2D array (matrix)
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix)
In the next lesson, we will learn basic operations using NumPy.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.