Lesson 1

Demystifying Numpy Arrays: Understanding Creation and Manipulation

Introduction to Numpy Array

Welcome to this lesson on understanding Numpy arrays! Today, we will be focusing on numerical computations with Numpy, specifically around the creation, manipulation, and operations of Numpy arrays.

Numpy, short for Numerical Python, is an essential library for performing numerical computations in Python. It has support for arrays (like lists in Python, but can store items of the same type), multidimensional arrays, matrices, and a large collection of high-level mathematical functions.

In the world of data manipulation using Python, understanding and being able to use Numpy arrays allows us to efficiently manage numerical data.

Creating a Numpy Array

The most common way to create a Numpy array is by using the numpy.array() function. You can pass any sequence-like object into this function, and it will be converted into an array. Let's convert a regular Python list into a Numpy array:

Python
1import numpy as np 2 3# Create a Python list 4py_list = [1, 2, 3, 4, 5] 5 6# Convert list to a Numpy array 7np_array = np.array(py_list) 8 9print(np_array) # Output: [1 2 3 4 5]

Executing these lines creates a one-dimensional Numpy array np_array with the same elements as our regular Python list py_list.

We can also create two-dimensional arrays (similar to matrices). Let's convert a list of lists into a 2D Numpy array:

Python
1import numpy as np 2 3# Create a 2D Python list 4py_list_2d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 5 6# Convert list to a Numpy array 7np_array_2d = np.array(py_list_2d) 8 9print(np_array_2d) 10# Output: 11# [[1 2 3] 12# [4 5 6] 13# [7 8 9]]

We now have a two-dimensional Numpy array np_array_2d, with each sub-list of py_list_2d as a row in np_array_2d.

Attributes of Numpy Arrays

Numpy arrays come equipped with several attributes for gaining more insights:

  • ndim tells us the number of dimensions of the array.
  • shape gives us the size of each dimension.
  • size tells us the total number of elements in the array.
  • dtype tells us the data type of the elements in the array.

Let's create a 2D array and use these attributes:

Python
1np_array = np.array([[1, 2, 3], [4, 5, 6]]) 2 3print("Dimensions: ", np_array.ndim) # Dimensions: 2 4print("Shape: ", np_array.shape) # Shape: (2, 3) 5print("Size: ", np_array.size) # Size: 6 6print("Data Type: ", np_array.dtype) # Data Type: int64

After running these lines, we see that np_array is a 2D array (since ndim returns the value 2), has 2 rows and 3 columns (shape returns the tuple (2, 3)), contains 6 elements (size returns 6), and is of integer type (dtype returns 'int64').

Array Manipulations: Indexing, Slicing and Reshaping Numpy Arrays

Numpy arrays can be manipulated in various ways. For instance, we can access specific positions (indexing) and even slices of the array (slicing), or change the shape of the array (reshaping).

Let's play around with these manipulations:

Python
1# Indexing: access the element at the first row, third column 2print("Indexed Value: ", np_array[0, 2]) # Indexed Value: 3 3 4# Slicing: access the first row 5print("Sliced Value: ", np_array[0,:]) # Sliced Value: [1 2 3] 6 7# Reshape the array to 3 rows and 2 columns (only applicable if the reshaped total size equals the original size) 8reshaped_array = np_array.reshape(3, 2) 9print("Reshaped Array:\n", reshaped_array) 10# Reshaped Array: 11# [[1 2] 12# [3 4] 13# [5 6]]

In the code snippet shown above, indexing is used to access a specific element in the array, slicing is used to access a range of elements, and reshaping is used to reconfigure the layout of the array while keeping the data intact.

Array Operations

We can perform arithmetic operations such as addition, subtraction, multiplication, and division on Numpy arrays, which calculates the outcome element-wise.

Python
1import numpy as np 2 3np_array1 = np.array([1, 2, 3]) 4np_array2 = np.array([4, 5, 6]) 5 6# Addition 7print(np_array1 + np_array2) # Output: [5 7 9] 8 9# Subtraction 10print(np_array1 - np_array2) # Output: [-3 -3 -3] 11 12# Multiplication 13print(np_array1 * np_array2) # Output: [4 10 18] 14 15# Division 16print(np_array1 / np_array2) # Output: [0.25 0.4 0.5]

Here, every operation is applied to each corresponding pair of elements in np_array1 and np_array2, resulting in a new array consisting of these outcomes.

Summary

Well done! In this lesson, you've started your journey in numerical computation with Python by learning the basics of Numpy arrays - including their creation, accessing their attributes, performing manipulations like indexing, slicing and reshaping, and executing basic arithmetic operations.

Ready for Practice?

Next, you can apply the concepts you've learned to engage in practice exercises that cover every aspect of this lesson. Through this hands-on experience, you'll gain a better feel for manipulating Numpy arrays, a foundational skill in handling numerical data in Python. Happy practicing!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.