As a first step, you’ll learn how NumPy arrays work differently than Python lists. You’ll then learn several ways to create NumPy arrays and perform basic operations on them. Let’s begin!

Basics of NumPy Arrays

NumPy is one of the most popular Python libraries for scientific computing and data analysis. The basic data structures in NumPy are N-dimensional arrays (N-D arrays). They have broadcasting capabilities and allow us to vectorize operations for speed and use built-in mathematical functions for performance improvement. To start working with NumPy, you should first install the library and import it into your working environment. It is available as a PyPI package that is installable through pip. To  install NumPy, open up your terminal and run the following command: After installing NumPy, you can import it into your working environment under an alias. The usual alias is np.

Python Lists vs. NumPy Arrays

Consider the following Python list of numbers: You can get a NumPy array from an existing list by calling the np.array() function with the list as the argument. To check the type of np_arr1, you call the built-in type() function, you’ll see that it’s ndarray, the fundamental data structure in NumPy. Though the Python list and the NumPy array may look similar, there are certain differences:

A Python list can hold objects of different data types, whereas a NumPy array contains elements of the same data type. The default data type is float with a precision of 64 bits (float64). The elements of a Python list are not necessarily stored in contiguous locations in memory. However, the elements of a NumPy array are stored in a contiguous block in memory. As a result, it is faster to look up and access elements.

Let’s go over a couple of other differences.

Broadcasting

A powerful feature of NumPy arrays is broadcasting. Suppose we’d like to add 2 to all the elements of np_arr1 and py_list. Let’s try adding 2 to py_list and see what happens: We see that we get a TypeError stating that we can only concatenate two lists, and adding py_list + 2 like this is not supported. Let’s try the same operation on the array, np_arr1. In the result, we see that 2 has been added to each element of the array. This is because NumPy implicitly broadcasted the scalar 2 to an array of compatible shape to yield this result.

Vectorization

NumPy arrays support vectorization for faster element-wise operations. Suppose we’d like to find the element-wise sum of the two arrays. Using a simple + operation on the list would return the concatenation of the two lists (which is not what we want!). But the same operation on the NumPy array, np_arr1, returns the element-wise sum of np_arr1 with itself. Similarly, nested lists may look similar in structure to an N-dimensional NumPy array. However, the differences discussed so far hold.

How to Create NumPy Arrays

You can always create NumPy arrays from existing Python lists using np.array(list-obj). However, this is not the most efficient way. Instead, you can use several built-in functions that let you create arrays of a specific shape. The shape of the array is a tuple that denotes the size of the array along each dimension. For example, the shape of a 2×2 array with two rows and two columns is (2,2). In this section, we’ll learn how to use some of these built-in functions.

Creating Arrays of Zeros and Ones

It’s often helpful to create an array of specific dimensions populated with all zeros or all ones. And then use them and modify them in subsequent steps in the program. We can use the zeros() function to create an array of zeros. Pass in the shape of the required array as a tuple: np.zeros(shape). Here’s the output, a 2D array of zeros: You can access the attributes of the NumPy array, call attributes such as dtype and shape, using the dot notation, as shown below: To get an array of ones, you can use the np.ones() function.

Creating an Identity Matrix

The identity matrix is widely used in several applications in linear algebra. And you can use the np.eye() function to create an identity matrix. The np.eye() function takes in only one argument: the order of the matrix (n).

Creating Arrays of Random Numbers

You can also create arrays of a specific shape populated with random numbers drawn from specific distributions. The commonly used probability distributions are the uniform distribution and the standard normal distribution. The randn() function, which is part of NumPy’s random module, can be used to generate arrays of numbers that are sampled from a standard normal distribution. Standard normal distribution is a Gaussian distribution with zero mean and unit variance. np.random.rand() returns an array of numbers sample from a uniform distribution over the interval [0,1). You can also create an array of random integers using the randint() function that is part of NumPy’s random module. np.random.randint(low, high, size) returns an array of integers. The shape of the array is inferred from the size argument and the integers take on values in the interval [low,high). Here’s an example:

Other Useful Built-In Functions

Next, let’s go over a few other helpful functions to create NumPy arrays. The arange() function returns an array of numbers between a start and stop value in steps of a step value: start, start + step,  start + 2*step up to but not including stop. The start and the step values are optional. The default step size is 1 and the default start value is 0. In this example, array_a is an array of numbers starting at 1 going up to but not including 10 in steps of 0.5. You can also create arrays of evenly spaced numbers using np.linspace(). Use np.linspace(start, stop, num) to get an array of num evenly spaced numbers between the start and stop values. Here, arr_lin is an array of 5 evenly spaced numbers in the interval [1,10]. Similarly, arr_lin2 is an array of 10 evenly spaced numbers in the interval [1,20]. 💡 Unlike the arange() function, the linspace() function includes the endpoint by default.

Basic Operations on NumPy Arrays

Next, let’s go over some of the basic operations on NumPy arrays.

Finding the Minimum and Maximum Elements

Whenever we use functions from NumPy’s random module to create arrays, we’ll get a different result each time the code is run. To get reproducible results, we should set a seed: np.random.seed(seed_value). In the following example, I have set the seed for reproducibility, int_arr1 is an array of seven random integers in the interval [1,100).

To find the maximum element in the array, you can call the max() method on the array object, int_arr1, and To find the minimum element in the array, you can call the min() method on the array object, int_arr1.

Finding the Index of the Maximum and Minimum Elements

Sometimes, you may need to find the index of the maximum and the minimum elements. To do this, you can call the argmax() and the argmin() methods on the array object. Here, the maximum element 73 occurs at index 2. And the minimum element 20 occurs at index 0.

How to Concatenate NumPy Arrays

Another common operation that you may want to do with NumPy arrays is concatenation.

Vertical Concatenation Using vstack

You can concatenate arrays vertically using the vstack() function.  Here is an example. arr1 is an array of ones with two rows and three columns and arr2 is an array of zeros two rows and three columns. We can concatenate these two arrays vertically using the vstack() function as shown: As the stacking happens vertically, the two arrays should have the same number of columns. Let’s change arr2 to be of shape (2,2). It now has two rows and two columns. Therefore, vertical concatenation is not possible, and we get a ValueError.

Horizontal Concatenation Using hstack

You can concatenate NumPy arrays horizontally using the hstack() function, as shown below. Because the stacking happens horizontally, the input arrays should have the same number of rows. Here, both arr1 and arr2 have three rows.

Using concatenate

You can also use concatenate NumPy arrays along a specific axis using the concatenate() function. Set the optional axis argument to the axis you’d like to concatenate along; the default value of the axis is zero. Here are a few examples: When we don’t specify the axis to concatenate along, the arrays are concatenated along axis 0. In the resultant array, the second array arr2 is added (as rows) below the first array. When we specify axis = 1, we get the following result. arr2 is concatenated (as columns) beside the first array, arr1. As with the hstack() and vstack() functions, the dimensions of the arrays along the concatenation axis should match.

Conclusion

In this tutorial, you’ve learned the differences between NumPy arrays and Python lists, with a focus on the advantages of N-dimensional arrays in terms of speed and efficiency. You’ve also learned several useful functions to create arrays of a particular dimension and perform common operations, such as finding the minimum and the maximum elements, concatenating arrays, and more. Next, learn how to reshape NumPy arrays.

NumPy Arrays  An Introduction  With Examples  - 1NumPy Arrays  An Introduction  With Examples  - 82NumPy Arrays  An Introduction  With Examples  - 78NumPy Arrays  An Introduction  With Examples  - 58NumPy Arrays  An Introduction  With Examples  - 16NumPy Arrays  An Introduction  With Examples  - 98NumPy Arrays  An Introduction  With Examples  - 9