# Best Ways to Normalize Numpy Array

Hello geeks and welcome in this article, we will cover Normalize NumPy array. You can divide this article into 2 sections. In the 1st section, we will cover the NumPy array. Whereas in the second one, we will cover how to normalize it. To achieve a complete understanding of this topic, we cover its syntax and parameter. Then we will see the application of all the theory part through a couple of examples. But before moving that far ahead, let us get a brief understanding of the 2 things.

Numpy is a powerful mathematical library of python. Here the function Numpy array helps us create an array of different dimensions and sizes. Now coming to normalization, we can define it as a procedure of adjusting values measured on a different scale to a common scale. Now moving ahead, let us cover them in detail.

Contents

## NumPy array

As discussed earlier, a Numpy array helps us in creating arrays. In this section, we will look at the syntax and different parameters associated with it. Along with that, we will also look at some examples.

### Syntax of NumPy array

``numpy.array`(object)`

This is the general syntax for the function. It has several parameters associated with which we will be covering in the next section.

### Parameters

1. object:array_like

This parameter represents the input array that we want as output.

2. dtype:data-type

This parameter represents the data type which the array elements will have. It is an optional parameter. By default it is not specified it will take the minimum type required to hold the elements.

3. order:

This is another optional parameter and specifies the memory layout of an array. The newly created array will be in c-order (row-major) if the object is not an array type. Also if F is specified that is (column-major) then it will take its shape.

4. ndmin:int

This optional parameter specifies the maximum number of dimension resulting array will have.

Returns

``out:ndarray``

On completion of program it returns an array of specified condition.

### Examples

As of now, we are done covering all the theories associated with the NumPy array. Let us now see some examples and understand how it is executed. After this, we will quickly jump to Normalize Numpy Array

```import numpy as ppool
a=ppool.array([[1,2,3,],
[4,5,6]],dtype="float")
print(a)
```

Output:

``````[[1. 2. 3.]
[4. 5. 6.]]``````

Above we can see a simple example of NumPy array. Here we have first imported the NumPy library. After which we have used the proper syntax and also specified the dtype to be a float. In the end, our result justifies our input and hence it is verified. Similarly, you can also do it for the complex data type.

## Normalize Numpy Array

As if now we have covered Numpy Array. Now we can generate arrays as per our liking and need. In this section, we will focus on normalizing those arrays. As mentioned earlier that normalization is a procedure of adjusting values measured on a different scale to a common scale. To normalize an array 1st, we need to find the normal value of the array. After which we need to divide the array by its normal value to get the Normalized array.

In order to calculate the normal value of the array we use this particular syntax.

`numpy.linalg.norm()`

Now as we are done with all the theory section. Let us see it’s application through an example.

```import numpy as ppool
a=ppool.array([[1,2,3,],
[4,5,6]],dtype="float")
print(a)
b=ppool.linalg.norm(a)
print(b)
norm=a/b
print(norm)
```

Output:

``````[[1. 2. 3.]
[4. 5. 6.]]
9.539392014169456
[[0.10482848 0.20965697 0.31448545]
[0.41931393 0.52414242 0.6289709 ]]``````

In the above example, we have used the same array as generated above. Now first we have calculated the normal value of the array. After calculating the normal value we have divided each term of the array by the normal value. Hence we obtain a normalized NumPy array.

## Different methods of normalization of NumPy array

### 1. Normalizing using NumPy Sum

In this method, we use the NumPy ndarray sum to calculate the sum of each individual row of the array. After which we divide the elements if array by sum. Let us see this through an example.

```import numpy as ppool
a=ppool.array([[1,2],
[4,5]],dtype="float")
print(a)
b=ppool.ndarray.sum(a,axis=1)
print(b)
c=a/b
print(c)
```

Output:

``````[[1. 2.]
[4. 5.]]
[3. 9.]
[[0.33333333 0.22222222]
[1.33333333 0.55555556]]``````

This is another you can use for normalizing the array. This method is really effective for row-wise normalization.

### 2. Normalization using sklearn

Sklearn is a module of python used highly for data science and mining. Using this method also we can normalize the array. It follows a really simple procedure and let us understand it using an example.

```from sklearn import preprocessing
print(preprocessing.normalize([[5.0, 48.0], [14.0, 32.0]]))
```

Output:

``````[[0.10360608 0.99461841]
[0.40081883 0.91615733]]``````

### 3. Normalization using list comprehension

You can also normalize a list in python. List comprehension, in general, offers a shorter syntax, which helps in creating the new list from the existing list. Let us look at it through an example.

```list = [[7.0, 5.0, 9.0, 7.0]]
norm_list = [i / sum(j) for j in list for i in j]
print(norm_list)
```

Look how we were able to normalize our existing list. Here we can see that we have divided each element in the list by the sum of all elements. This also a good option for normalizing.

### 4. Normalization using For loop

We also carry forward the normalization process using for loop. Using for loop, we can calculate the sum of all elements. Then divide each element by that sum. Here I advise you to use the NumPy array. While carrying on the division, you may get the error as “list/int” not a suitable data-type.

```import numpy as ppool
def _sum(arr):

sum=0

for i in arr:
sum = sum + i

return(sum)

arr = ppool.array([11, 32, 45, 18] )
n = len(arr)
ans = _sum(arr)
print (ans)
b= arr/ans
print(b)
```

Output:

``````106
[0.10377358 0.30188679 0.4245283  0.16981132]``````

## Conclusion

In this article, we have covered the Normalize NumPy array. To so at first, we covered NumPy array along with its syntax, parameters and example. Then in the next section, we covered how to normalize the array.

I  hope this article was able to clear all doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this why not read about Syslog next.

Subscribe
Notify of 