1.
Introduction
2.
What is NumPy?
3.
NumPy Functions
3.1.
NumPy.linspace
3.2.
NumPy.repeat()
3.3.
NumPy.random()
3.4.
NumPy.shape
3.5.
NumPy.min
3.6.
NumPy.max
3.7.
NumPy.argmax
3.8.
NumPy.sort
3.9.
NumPy.isnan
3.10.
NumPy.mean, Numpy.median, Numpy.std
3.11.
NumPy.histogram
4.
5.
Key Takeaways
Last Updated: Mar 27, 2024

# Important Numpy Functions for ML

## Introduction

We’ll be learning about a popular array processing library in Python, ‘NumPy.’

'NumPy' stands for 'Numerical Python' as it mainly has to do with numbers.

## What is NumPy?

‘NumPy’ is a famous package for arrays in Python. It is beneficial in Linear Algebra, Fourier Transform, and Matrices.

This feature makes this library very useful in the field of Machine Learning.

'NumPy' arrays are very fast in comparison to lists. This is because they are stored at one continuous place in memory, unlike lists, so that processes can access and manipulate them very efficiently.

This significant feature of this library makes it very useful in Machine Learning and Data Science.

For more information, you can check out the source code of the NumPy library at the following linkhttps://github.com/numpy/numpy.

## NumPy Functions

NumPy consists of numerous mathematical functions. We would be going through the most important ones in the domain of Machine Learning.

We would be learning about the following NumPy functions:-

(i) Numpy.linspace()

(ii) Numpy.repeat()

(iii) Numpy.random()

(iv) Numpy.nan()

(v) Numpy.argmax()

(vi) Numpy.histogram()

(vii) Numpy.min(), Numpy.max()

(viii) NUmpy.mean()

(ix) Numpy.shape()

(x) Numpy.reshape()

(xi) Numpy.sort()

### NumPy.linspace

This function returns a list of numbers spaced evenly over the specified interval.

start - refers to the beginning of the interval range.

→ stop - refers to the end of the interval range.

→ Num - refers to the number of samples generated.

→ Endpoint - refers to the defined endpoint, if any.

→ dtype - refers to the datatype of the newly generated array.

→ Axis - refers to along rows if '1' and along columns if '0'.

NOTE: if 'num' is not defined, then automatically, 50 samples are generated.

Output

-> In the first example, we get a list of evenly spaced numbers in the specified range.

-> In the second example, we notice that since we have not defined the parameter 'num,' we get 50 samples automatically.

### NumPy.repeat()

This function repeats the elements of the array passed as its argument and returns a newly generated array.

→ Arr - refers to the input array.

→ Repetitions - refers to the no. of repetitions of each array element.

→ Axis - refers to the axis along which we wish to return values.

Output

-> In the first example, we notice that on passing repetition as 2, we get a newly generated array in which each element of the initial array is repeated.

-> In the second example, we notice that on passing the axis as 0, we get a newly generated 2-D array (matrix) in which each element of the initial array is repeated along the columns.

-> In the third example, we notice that on passing the axis as 1, we get a newly generated 2-D array (matrix) in which each element of the initial array is repeated along the rows.

### NumPy.random()

(i) Numpy.random.randint()

This function returns a list of random integers over a specified interval.

numpy.random.randint(low, high=None, size=None, dtype='l')

→ Low - refers to the start of the interval range.

→ High - refers to the end of the interval range.

→ Size - refers to the no of values to be generated.

→ dtype - refers to the data type of the values generated.

NOTE: if 'high' is not defined, then automatically, samples are generated in the range [0, low). Also, if 'size' is not defined, then only one value is generated.

Output

-> In the first example, we notice that we get five random integers in the range [low=1, high=10).

-> In the second example, we notice that we get five random integers in the range [0, low=5). The range is [0, low=5) since 'high' has not been defined.

(ii) Numpy.random.choice

This function returns a random sample of numbers from an array.

→ a - refers to the array.

→ Size - refers to no. of samples to be generated or the shape of the output array.

→ Replace - refers to whether the sample is with or without replacement.

→ p - refers to the probability of each sample/element present in the array.

Output

-> In the example, we notice that we get a list of 5 random samples from the array specified.

-> By printing the length of the list, we notice that we get five random samples only.

### NumPy.shape

This function returns the shape of the NumPy array specified.

→ array_name - refers to the array specified.

Output

-> In the example, we notice that we get the shape as (2,5). This implies that the 2-D array(matrix) specified has two rows and five columns.

### NumPy.min

This function returns the minimum value from the specified array.

→ array_name - refers to the array specified.

Output

-> In the example, we observe that the output is 1. The number ‘1’ is the minimum element in the array specified.

### NumPy.max

This function returns the maximum value from the specified array.

→ array_name - refers to the array specified.

Output

-> In the example, we observe that the output is 10. The number ‘10’ is the maximum element in the array specified.

### NumPy.argmax

This function returns the index of the maximum element of the array specified in a particular axis.

→ Array - refers to the array specified.

→ Axis - refers to the axis specified. Axis '0' implies along the columns, and axis '1' means along the rows.

→ Out - provides a feature to insert output to the array.

Output

-> In the above example, we have first taken the axis as '0,' i.e., along the columns. We get the result as [1 0 0 0 ]. This is because of the following facts:-

• First column: 1<4, so the max index is 1.
• Second column : 2>1 so max index is 0.
• Third column : 3>1 so max index is 0.
• Fourth column : 4>3 s0 max index is 0.

So, the output is [1 0 0 0].

-> Secondly, we have taken the axis as '1,' i.e., along the rows. We get the result as [3 0]. This is because of the following facts:-

• First row: 1<2<3<4, so the max index is 3.
• Second row : 4>1 ; 4>2 ; 4>3 so max index is 0.

So, the output is [3 0].

### NumPy.sort

This function returns a sorted version of the array specified.

→ a - refers to the array specified.

→ Axis - refers to the axis specified.

→ Kind – refers to the type of sorting algorithm used.

→ Order - refers to specifying which fields to compare first.

Output

-> In the above example, we firstly sort along the columns with axis=’0’. We get the result as displayed above. This is because of the following facts:-

• First column: 10> 5, so we get 5.
• Second column: 9> 4, so we get 4.
• Third column: 8>3, so we get 3.
• Fourth column: 7>2, so we get 2.
• Fifth column: 6>1, so we get 1.

-> Secondly, we sort along the rows with axis=’1’. We get the result as displayed above. This is because of the following facts:-

• First row: 6<7<8<9<10.
• Second row: 1<2<3<4<5.

### NumPy.isnan

This function returns a boolean array indicating the presence/absence of NaN values.

→ Array - refers to the array specified.

Output

-> In the example above, we notice that there is only one NaN value in the boolean array.

-> 'True' indicates the presence of the 'NaN' value in the boolean array.

### NumPy.mean, Numpy.median, Numpy.std

These functions return the specified list's mean, median, and standard deviation.

Output

-> In the example above, we notice that we get the mean as 3.0, the median as 3.0, and the standard deviation as 1.41.

### NumPy.histogram

This function returns a histogram representing the frequency of data distribution in graphical form.

→ Data - refers to the array to be plotted.

→ Bins - refers to the number of equal width bars.

→ Range - refers to the lower and upper range of bins.

→ Normed - refers to the density attribute.

→ Weights - refers to the array of weights.

→ Density - refers to an optional parameter relating to probability density function.

Output

-> In the example above, we notice that two values are returned.

-> The first value refers to the array of values of the histogram.

-> The second value refers to the array of bin edges.

Check out this array problem - Merge 2 Sorted Arrays

1. Which Python libraries are essential for Machine Learning apart from NumPy?
Some of the other vital libraries are:-
i) Scikit-learn
ii) Scipy
iii) Theano
iv) TensorFlow
v) Keras
vi) PyTorch
vii) Pandas
viii) Matplotlib

2. What is the advantage of NumPy?
The advantage of NumPy is that it is computationally very fast. Also, it takes much less memory in comparison to lists and can handle various data types.

3. What is the full form for NumPy?
NumPy stands for 'Numerical Python' as it deals with Linear Algebra.

## Key Takeaways

Congratulations on making it this far. This blog discussed significant NumPy functions for Machine Learning!!

We learned about various numpy function, namely, numpy.linspacenumpy.repeatnumpy.randomnumpy.shapenumpy.minnumpy.maxnumpy.argmax
numpy.sortnumpy.isnannumpy.meannumpy.mediannumpy.std and numpy.histogram

You can also consider our Machine Learning Course to give your career an edge over others.

Live masterclass