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:

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:

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:

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:

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.

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal