Diving into NumPy arrays

1. Diving into NumPy arrays

In this lesson, we're going to dive deeper into NumPy arrays. First, we're going to explore how to use mathematical operators to manipulate data stored in NumPy arrays, then how to use functions to summarize this data. Let's get started.

2. Calculations on NumPy arrays

Like in MATLAB, you can use the same operators that we use to perform calculations on floats and integers on arrays. This can be a very efficient way of applying the same calculation across an entire array. For example, let's say we wanted to compute the areas of three circles with radii of 1, 2, and 3. These values are stored in the NumPy array `radius`. We can then compute the area of each circle by using the double-star operator to square each value in the array, then multiplying this by pi using the `pi` constant from the NumPy package.

3. Math between NumPy arrays

Similarly, NumPy operators make it very easy to perform element-wise operations across NumPy arrays. In this example, we have two NumPy arrays: revenue, which contains the revenue for a business venture over a number of weeks, and costs, which contains the costs over the same time period. If we want to compute the week-by-week profit, we simply subtract the cost array from the revenue array. Note, that, unlike MATLAB, multiplication with NumPy can also be performed element-wise. NumPy supports matrix multiplication through functions like dot(), but that functionality is out of scope for this course.

4. Summarizing data in NumPy arrays

In addition to arrays, NumPy also has many functions to understand the data that arrays contain. For example, NumPy's mean() function can be used to calculate the average value of the elements in an array. Other useful functions include min(), which returns the smallest value in an array; max(), which returns the largest; and sum(), which returns the total of the values in the array.

5. Let's practice!

Now let's try some examples.