August 30, 2024
A guest post from Fabrício Ceolin, DevOps Engineer at Comet. Inspired by the growing demand…
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.
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.
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.
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.
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:
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:
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.
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:
The shape attribute of a NumPy array indicates the number of rows and columns there are along each dimension.
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:
The np.reshape()
function can be used to reshape an ndarray:
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.
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.