Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today we're going to learn about creating arrays in NumPy! NumPy arrays are more efficient than regular lists. Can someone give me a reason why we would want to use arrays in machine learning?
Because they are faster and use less memory?
Exactly! Arrays allow us to perform operations on large datasets more efficiently. Now, let's create a simple 1D array. Who can tell me how to do that?
We use np.array([1, 2, 3, 4]) right?
Spot on! When we run this code, we will have a 1D array. Let’s print it out and see the result.
Now that we understand 1D arrays, let's talk about 2D arrays, also known as matrices. What does a 2D array look like?
It's like a grid with rows and columns!
Exactly! To create a 2D array, we use nested lists. For example, arr2 = np.array([[1, 2], [3, 4]]). Can anyone tell me what the output looks like?
"It should print as:
So why do we favor NumPy arrays over regular lists? They are faster and have many built-in functions that simplify mathematical operations.
Like what kind of operations?
Great question! Operations like addition, multiplication, or applying functions across all elements. Can anyone give me an example?
If we add two arrays together, we’d get a new array with the elements summed up!
Exactly! That's a powerful aspect of NumPy. Let’s see this in a practical example.
To make arrays quick and neat, NumPy is hard to beat!
Imagine building a Lego structure where every block (element) fits perfectly in either a line (1D) or a grid (2D) just like how arrays can be built.
Recall the acronym "N.E.A.T" for NumPy: 'N' for Numbers, 'E' for Efficient, 'A' for Arrays, 'T' for Technology!
M.A.T: 'M' for Math, 'A' for Arrays, 'T' for Technology in NumPy!
Term
What is a 1D array?
Click to flip
Term
What is a 2D array?
Click to flip
This section explains how to create and understand arrays using the NumPy library in Python, highlighting their benefits over traditional Python lists.
In this section, learners will discover the process of creating 1D and 2D arrays using NumPy. The advantages of using NumPy arrays over regular Python lists, such as speed and efficiency, are discussed. Practical examples are provided to illustrate these concepts.
NumPy is essential for efficient mathematical operations in Python. In this section, we delve into creating arrays, which are pivotal in machine learning for handling data effectively. Arrays are similar to Python lists but offer better performance. To create arrays in NumPy, you can use the np.array() function. For instance, a one-dimensional array can be created using arr1 = np.array([1, 2, 3, 4]), while a two-dimensional array can represent matrices, like arr2 = np.array([[1, 2], [3, 4]]).
The output of these array creations demonstrates their structure and ease of use, and it underscores why NumPy is favored in machine learning scenarios.