# Python NumPy Normalize + Examples

In this Python tutorial, we will learn how to normalize the NumPy array in Python. Also, we will cover these topics.

• Python numpy normalize between 0 and 1
• Python numpy normalize vector
• Python Numpy normalize array
• Python NumPy normalize 2d array
• Python NumPy normalize each row
• Python NumPy normalize angle
• Python Numpy normalized cross correlation
• Python NumPy normalized histogram
• Python normalize vector without NumPy
• Python NumPy normalize list
• Python NumPy normalize data
• Python NumPy normalize matrix

## Python NumPy normalize

• In this Program, we will discuss how to normalize a NumPy array in Python.
• In Python, Normalize means the normal value of the array has a vector magnitude and we have to convert the array to the desired range. To do this task we are going to use numpy.linalg.norm() method.
• This method is basically used to calculate different vector norms or we can say different matrix norms and this function has three important parameters.

Syntax:

Here is the Syntax of numpy.linalg.norm() method

``````linalg.norm
(
x,
ord=None,
axis=None,
keepdims=False``````
• It consists of few parameters
• x: This parameter indicates the input array of n-dimensional
• ord: It specifies that how we want to get the order of the norm and by default, it takes none value.
• axis: This parameter checks the condition if the axis is an integer value then the vector value is generated for the axis of x and if the axis is none then the vector norm is returned.
• keepdims: it will check the condition if this argument is True then the axes are normed over the left in the output.

Example:

Let’s take an example and understand how to find the normal form of an array

Source Code:

``````import numpy as np

arr = np.array([89,78,14,16,19,20])
result = np.linalg.norm(arr)

new_output=arr/result
print(new_output)``````

In the above code, we have used the numpy array and then create a variable ‘result’ in which we assigned a function np.linalg.norm to calculate the normal value and each term divided into an array.

Here is the execution of the following given code

### Another approach to check the normalize in NumPy array

By using the list comprehension method, we can easily create a new list from the given list. In this example, we have to normalize our given list ‘my_lis’. In the list, we have to divide each item by the sum of all items.

Source Code:

``````my_lis = [[14,27,34,24]]

result = [m / sum(n) for n in my_lis for m in n]
print(result)``````

Here is the Screenshot of the following given code

This is how to normalize a numpy array in Python.

## Python numpy normalize between 0 and 1

• In this section, we will discuss how to normalize a numpy array between 0 and 1 by using Python.
• Here you can normalize data between 0 and 1 by subtracting it from the smallest value, In this program, we use the concept of np.random.rand() function and this method generate from given sampling and it returns an array of specified shapes.
• While creating a numpy array we have applied the concept of np.min and np.ptp. In Python, ptp stands for the peak to peak and this method is beneficial for users to return a range of values. In this example, the range is (5,4).

Syntax:

Here is the Syntax of np.ptp() method

``````numpy.ptp
(
a,
axis=None,
out=None,
keepdims=<no value>
)``````

Example:

Let’s take an example and check how to normalize a numpy array between 0 and 1

Source Code:

``````import numpy as np

arr1 = np.random.rand(5,4)
result = (arr1 - np.min(arr1))/np.ptp(arr1)
print(result)``````

In the above code, once you will print ‘result’ then the output display the normalized array, and the minimum value in the numpy array will always be normalized as 0 and the maximum will be 1.

Here is the implementation of the following given code

## Python numpy normalize vector

Here we can see how to normalize a numpy array to a unit vector. In this example, we have created a vector norm by using the np.square root, and this method will square the sum of the elements in the array.

Source Code:

``````import numpy as np

arr1 = np.random.random((3, 2))

new_output = arr1 / np.sqrt(np.sum(arr1**2))
print(new_output)``````

In the above code, we have divided the data with the np.sqrt() function along with that we have assigned the np.sum() function as an argument. Once you will print ‘new_output’ then the output will display the unit vector of the numpy array.

Here is the Output of the following given code

## Python Numpy normalize array

• In this section, we will discuss how to normalize a NumPy array by using Python.
• By using sklearn normalize, we can perform this particular task and this method will help the user to convert samples individually to the unit norm and this method takes only one parameter others are optional.
• In this example we have set axis =0 that represents each feature will be normalized and if the axis=1 then the data will normalize individually along with that we are going to apply the np.axis to return all rows from the numpy array.

Syntax:

Here is the Syntax of the sklearn normalize module

``````sklearn.preprocessing.normalize
(
X,
norm=l2,
*,
axis=1,
copy=True,
return_norm=False
)``````

Source Code:

``````import numpy as np
from sklearn.preprocessing import normalize

new_arr = np.array([12,45,23,45,10,13,12])*5
new_output = normalize(new_arr[:,np.newaxis], axis=0)

print(new_output)``````

You can refer to the below Screenshot

## Python NumPy normalize 2d array

• In this Program, we will discuss how to normalize a numpy two-dimensional array by using Python.
• In this we have to normalize a 2-dimensional array that has random values generated by the np.array function. Now to do this task we have to use numpy.linalg.norm() method. This method is basically used to calculate different vector norms.

Example:

``````import numpy as np

new_arr = np.array([[89,78,14,16,19,20],
[53,12,3,4,6,17]])
new_output = np.linalg.norm(new_arr)

b=new_arr/new_output
print("Normalize 2-d array:",b)``````

In the above code first, we have imported the numpy library and then create an array ‘new_arr’. Now use the np.linalg.norm() function for normalizing the numpy 2-d array.

Here is the Screenshot of the following given code

## Python NumPy normalize each row

• Here we can see how to normalize each row in the Numpy array by using Python.
• In this program, we have applied the numpy ndarray sum to calculate each row that is available in the array. This method is another way to normalize the numpy array with a vector.
• In this example we have set the axis=1 that represents the data will normalize individually along with that we have assigned ‘new_arr’ as an argument.

Example:

``````import numpy as np
new_arr = np.array([[6,19],
[15,12]])

arr2 = np.ndarray.sum(new_arr,axis=1)

new_result=new_arr/arr2
print(new_result)``````

Here is the implementation of the following given code

This is how to normalize a numpy array with each row in Python.

## Python NumPy normalize angle

• In this section, we will discuss how to normalize a numpy array by using the np.angle() function in Python.
• In this example, we want to find out the angle of floating and complex values. To do this task we are going to use Numpy.angle() function and this method will also convert the numpy array values to normalize.

Syntax:

Here is the Syntax of np.angle() function

``````numpy.angle
(
z,
deg=false
)``````
• It consists of few parameters
• z: This parameter indicates the complex number which we have to calculate.
• deg: By default its a optional parameter and it takes ‘false’ value that returns the angle in radians.

Source Code:

``````import numpy as np

new_arr = np.array([[6.0,19.0j],
[15+1j,12.0]])
new_result= np.angle(new_arr)
print(new_result)``````

Here is the Screenshot of the following given code

## Python Numpy normalized cross correlation

• In this Program, we will discuss how to normalize a normal array by using the correlation method in Python.
• In Python the correlation method represent the cross-correlation between one-dimensional vectors and also define the single processing text c_{av}[k] = sum_n a[n+k] * conj(v[n]).

Syntax:

Here is the Syntax of numpy.correlation() method

``````numpy.correlate
(
a,
v,
mode='valid'
)``````

Source Code:

``````import numpy as np

arr1 =np.array([6,3,4])
arr2 =np.array([2,6,7])
m = (arr1 - np.mean(arr1)) / (np.std(arr1) * len(arr1))
n = (arr2 - np.mean(arr2)) / (np.std(arr2))
new_result = np.correlate(m, n, 'full')
print(new_result)``````

In the above code we have taken two numpy arrays by using np.array() function named ‘arr1’ and ‘arr2’. After that, we have displayed the output by using cross-correlation values on each mode.

You can refer to the below Screenshot

## Python NumPy normalized histogram

• In this section, we will discuss how to normalize a numpy array by using a histogram in Python.
• Here we can use the concept of pyplot.hist() method and this function display the shape of sample data. In this example we have loaded the data into a numpy array then we use the pyplot instance and call the hist() method for plotting a histogram.
• In this Program, we also use pyplot.xtricks() method for setting the current tick location and labels of the x-axis.

Syntax:

Here is the Syntax of matplotlib.pyplot.hist() method

``````matplotlib.pyplot.hist
(
x,
bins=None,
range=None,
density=False,
weights=None,
cumulative=False,
bottom=None,
histtype='bar'
)``````

Example:

``````import matplotlib.pyplot as plt
import numpy as np

m=(4,4,4,4)
v, bins, o=plt.hist(m, density=True)

plt.xticks( np.arange(8) )
plt.show() ``````

Here is the Screenshot of the following given code

Code Screenshot

## Python normalize vector without NumPy

• Let us see how to normalize a vector without using Python NumPy.
• In Python, we cannot normalize vector without using the Numpy module because we have to measure the input vector to an individual unit norm.

## Python NumPy normalize list

• In this section, we will discuss how to normalize list by using Python Numpy.
• In this example first we have created a list ‘my_new_lis’ and then we declare a variable ‘new_output’ and use list comprehension method for dividing each item by the sum of all items.

Example:

``````my_new_lis = [[21,12,4,5]]

new_output = [s / sum(t) for t in my_new_lis for s in t]
print(new_output)``````

This is how to normalize a list by using Python NumPy.

## Python NumPy normalize data

• In this program, we will discuss how to normalize a data by using Python NumPy.
• We have already covered this method in different example and you can refer this method on previous topic.

Source Code:

``````import numpy as np

new_arr = np.random.random((4, 5))

new_result = new_arr / np.sqrt(np.sum(new_arr**2))
print(new_result)``````

Here is the Screenshot of the following given code

## Python NumPy normalize matrix

• Here we can see how to normalize matrix by using NumPy Python.
• To do this task we are going to use numpy.linalg.norm() method and this function is basically used to calculate different vector norms.

Example:

``````import numpy as np

arr = np.array([21,2,5,8,4,2])
result = np.linalg.norm(arr)

new_output=arr/result
print(new_output)``````

In the above code, we have used the numpy array ‘arr’ and then declare a variable ‘result’ in which we assigned a function np.linalg.norm to calculate the normal value and each term divided into an array. Once you will print ‘new_output’ then the output will display the normalized unit vector.

You can refer to the below Screenshot

You may also like read the following Numpy tutorials.

In this Python tutorial, we have learned how to normalize the NumPy array in Python. Also, we have covered these topics.

• Python numpy normalize between 0 and 1
• Python numpy normalize vector
• Python Numpy normalize array
• Python NumPy normalize 2d array
• Python NumPy normalize each row
• Python NumPy normalize angle
• Python Numpy normalized cross correlation
• Python NumPy normalized histogram
• Python normalize vector without NumPy
• Python NumPy normalize list
• Python NumPy normalize data
• Python NumPy normalize matrix