Normalizing a vector in Python
A common concept in the field of machine learning is to normalize a vector or dataset before passing it to the algorithm.
When we talk about normalizing a vector, we say that its vector magnitude is 1, being a unit vector.
In this tutorial, we will convert a numpy array to a unit vector.
Normalize a vector in Python using mathematical formulas
In this method, we will use a mathematical formula to calculate the vector norm of an array. When we divide the array with the norm vector, we get the normalized vector. The following code achieves this.
import numpy as np
v = np.random.rand(10)
normalized_v = v / np.sqrt(np.sum(v ** 2))
print(normalized_v)
Output:
[0.10366807 0.05821296 0.11852538 0.42957961 0.27653372 0.36389277
0.47575824 0.32059888 0.2721495 0.41856126]
Note that if the length of the vector is 0, this method will return some error.
numpy.linalg.norm()
Normalize vectors in Python using function
The module in Python NumPy
has norm()
a function that returns the vector norm of an array. The array is then divided by this norm vector to get a normalized vector. For example, in the following code, we will create a random array and use this method to find its normalized form.
import numpy as np
v = np.random.rand(10)
normalized_v = v / np.linalg.norm(v)
print(normalized_v)
Output:
[0.10881785 0.32038649 0.51652046 0.05670539 0.12873248 0.52460815
0.32929967 0.32699446 0.0753471 0.32043046]
sklearn.preprocessing.normalize()
Normalize vectors in Python using function
sklearn
The module has efficient methods that can be used for data preprocessing and other machine learning tools. The function in this library normalize()
is usually used with 2-D matrices and provides options for L1 and L2 normalization. The following code uses this function with a 1D array and finds its normalized form.
import numpy as np
from sklearn.preprocessing import normalize
v = np.random.rand(10)
normalized_v = normalize(v[:, np.newaxis], axis=0).ravel()
print(normalized_v)
Output:
[0.19361438 0.36752554 0.26904722 0.10672546 0.32089067 0.48359538
0.01824837 0.47591181 0.26439268 0.33180998]
The method used in the above method ravel()
is used to flatten multidimensional arrays in Python.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Tensor to NumPy array in Python
Publish Date:2025/05/03 Views:85 Category:Python
-
This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra
Saving NumPy arrays as images in Python
Publish Date:2025/05/03 Views:193 Category:Python
-
In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond
Transposing a 1D array in NumPy
Publish Date:2025/05/03 Views:98 Category:Python
-
Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra
Find the first index of an element in a NumPy array
Publish Date:2025/05/03 Views:58 Category:Python
-
In this tutorial, we will discuss how to find the first index of an element in a numpy array. Use where() the function to find the first index of an element in a NumPy array The function in the numpy module where() is used to return an arra
Remove Nan values from NumPy array
Publish Date:2025/05/03 Views:118 Category:Python
-
This article discusses some built-in NumPy functions that you can use to remove nan values. Remove Nan values using logical_not() and methods in NumPy isnan() logical_not() is used to apply logical NOT to the elements of an array. isn
Converting a PIL image to a NumPy array
Publish Date:2025/05/03 Views:148 Category:Python
-
This tutorial will discuss methods to convert a PIL image to a 3-dimensional NumPy array in Python. numpy.array() Convert a PIL image to a NumPy array in Python using the PIL is used to perform various operations on images in Python. The Pi
How to Convert DataFrame Column to String in Pandas
Publish Date:2025/05/02 Views:162 Category:Python
-
We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a
How to count the frequency of values in a Pandas DataFrame
Publish Date:2025/05/02 Views:84 Category:Python
-
Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().
How to get value from Pandas DataFrame cell
Publish Date:2025/05/02 Views:148 Category:Python
-
We'll look at using to get values from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .