skip to Main Content

Everything You Need to Know About NumPy for Machine Learning

Photo by Jorge Salvador on Unsplash

NumPy (Numerical Python) is a linear algebra package for Python. It is a highly significant in that it is used by practically every data science or machine learning Python package, including SciPy, Matplotlib, Scikit-learn, and many others. NumPy can perform mathematical and logical operations on arrays and has a variety of useful capabilities for matrices as well.

Why NumPy?

Python lists take a long time to execute and are much less efficient than arrays.

The power of NumPy rests in its array objects, which are up to 50 times quicker than conventional Python lists. The NumPy array object is referred to as an ndarray, which stands for n-dimensional array. They have a number of supporting methods that make them very convenient.

In this article, we will walk through the fundamentals of NumPy as a novice in machine learning. This involves learning how to make NumPy arrays and how to implement different operations on NumPy arrays.

Creating a NumPy Array

Basic ndarray:

A NumPy array can be created in a variety of ways, but one of the most common ways is to create one from a list (or list-like object) by passing it to the np.array function.

Let us suppose we have a Python list called listtt. Simply build a NumPy array named arr as shown below, and display the result.

Here, we’ve simply converted a Python list into a one-dimensional array. In order to create a two-dimensional list, we will need to pass a list of lists to the np.array function, as seen below.

The main difference between an array and a list is that arrays are built to accommodate vectorized operations, but Python lists are not. That is, when you apply a function, it is applied to each item in the array rather than the entire array object.

Using the np.arange( ) function:

We can use np.arange() to generate a NumPy array, in a similar way to how the built-in range() method builds a list. Below we generate ten digits in an array.

Array of zeros:

The np.zeros() function allows you to build an array of all zeros. All you have to do is supply the array’s required shape:

The above array is one dimensional, but we can also create a two-dimensional array of zeros, as shown below:

Creating an array of random numbers:

Using the rand(), randn(), or randint() methods, we can produce an array of random numbers.

Avoid growing pains early by implementing MLOps best practices today. Learn what to look out for in our notes from the field.

We may create an array of random integers with the shape we want from a uniform distribution across 0 to 1 by using the random.rand() function.

For instance, if we want a one-dimensional array of four items dispersed equally from 0 to 1, we may use the code as follows:

A two-dimensional array with two rows and three columns is also possible:

Creating an identity matrix:

When working with linear algebras, identity matrices are quite helpful. Typically, is a square matrix with two dimensions. This indicates that the number of rows and columns is equal. Identity matrices have the peculiarity that only the diagonals are 1s while the rest are 0. Identity matrices often only require one parameter.

Transposing a NumPy array:

The transpose() method (also shortened as .T()) is another useful NumPy reshaping tool. It takes the input array and swaps the row values for the column values:

Shape of NumPy:

The shape attribute of a NumPy array indicates the number of rows and columns there are along each dimension.

Size of NumPy array:

The size attribute can be used to identify how many values are in the array. It simply multiplies the number of rows in the ndarray by the number of columns:

Reshaping a NumPy array:

The np.reshape() function can be used to reshape an ndarray:

Arithmetic operations of NumPy:

The following is a list of all the operations that can be performed on NumPy arrays.

For more reference , you can have a look at the official NumPy documentation here.

NumPy Applications

  • Mathematical operations: Working with NumPy also offers simple routines for doing mathematical operations on an array data collection. In NumPy, we have several modules for performing fundamental and sophisticated mathematical functions. Linear Algebra, bitwise operations, Fourier transform, arithmetic operations, string operations, and so on are all supported.
  • For multi-dimensional arrays: NumPy allows us to generate multidimensional arrays. The usage of matrices can make the code more memory efficient. To execute various operations on these matrices, we use a matrix module.
  • Maintains minimal memory: Memory allocation is much lower for arrays as compared with Python lists, partially because NumPy has features to prevent memory waste in the data buffer. It also has functionality like copying, viewing, and indexing that can save memory. Indexing aids in returning the perspective of the original array, allowing for data reuse. It also identifies the element’s data type, which helps code optimization.

Conclusion:

In this article, we learned how to create a NumPy array and how to perform some simple operations on NumPy arrays. If you like this post, please like and share it.

Anoop Painuly headshot

Anoop Painuly

Back To Top