The L1 norm of a vector $\mathbf{x}=(x_1,x_2,\cdots,x_n)$ is defined as the sum of the absolute values of its components. Mathematically, for a vector $\mathbf{x}$, the L1 norm is given by:
$$||\mathbf{x}||1=\sum{i = 1}^{n}|x_i|$$
The name “taxicab norm” or “Manhattan norm” comes from the analogy of a taxicab moving in a city grid. The L1 norm calculates the distance a taxicab would travel in a city where movement is restricted to horizontal and vertical streets, rather than moving diagonally.
The L1 norm is widely used in machine learning and data science. For example, in linear regression, L1 regularization (also known as Lasso regression) adds the L1 norm of the coefficients to the cost function. This can lead to sparse solutions, where some coefficients are exactly zero, which is useful for feature selection.
First, make sure you have NumPy installed. If not, you can install it using pip install numpy
.
import numpy as np
# Create a 1-D array
x = np.array([1, -2, 3, -4])
# Calculate the L1 norm
l1_norm = np.linalg.norm(x, 1)
print(f"The L1 norm of the array {x} is: {l1_norm}")
In the code above, we first import the NumPy library. Then we create a 1 - D array x
. We use the np.linalg.norm
function with the second argument set to 1
to calculate the L1 norm of the array.
import numpy as np
# Create a 2-D array
y = np.array([[1, -2], [3, -4]])
# Calculate the L1 norm along axis 0 (column-wise)
l1_norm_axis0 = np.linalg.norm(y, 1, axis=0)
# Calculate the L1 norm along axis 1 (row-wise)
l1_norm_axis1 = np.linalg.norm(y, 1, axis=1)
print(f"L1 norm along axis 0: {l1_norm_axis0}")
print(f"L1 norm along axis 1: {l1_norm_axis1}")
In this example, we create a 2 - D array y
. By specifying the axis
parameter in the np.linalg.norm
function, we can calculate the L1 norm along different axes. When axis = 0
, we calculate the L1 norm for each column, and when axis = 1
, we calculate the L1 norm for each row.
As mentioned earlier, L1 regularization in linear regression can be used for feature selection. Here is a simple example using scikit - learn’s Lasso regression:
from sklearn.linear_model import Lasso
from sklearn.datasets import make_regression
import numpy as np
# Generate a regression dataset
X, y = make_regression(n_samples=100, n_features=10, random_state=42)
# Create a Lasso regression model
lasso = Lasso(alpha=0.1)
lasso.fit(X, y)
# The coefficients that are exactly zero indicate non - important features
zero_coef = np.where(lasso.coef_ == 0)[0]
print(f"Indices of non - important features: {zero_coef}")
In this example, we use the Lasso regression model which uses L1 regularization. Features with zero coefficients can be considered less important for the regression task.
The L1 norm can be used for outlier detection. Data points with a significantly higher L1 norm compared to others in a dataset can be considered outliers.
import numpy as np
data = np.array([1, 2, 3, 4, 100])
l1_norms = np.linalg.norm(data, 1, axis=0)
threshold = np.mean(l1_norms) + 2 * np.std(l1_norms)
outliers = data[np.abs(data) > threshold]
print(f"Outliers: {outliers}")
We calculate the L1 norm for each data point (in this 1 - D case) and then identify points that are far from the mean using a simple threshold based on the mean and standard deviation.
When using the np.linalg.norm
function, it’s important to handle potential errors. For example, if the input array contains NaN
or Inf
values, the result may be unexpected.
import numpy as np
arr = np.array([1, np.nan, 3])
try:
l1_norm = np.linalg.norm(arr, 1)
except Exception as e:
print(f"An error occurred: {e}")
This code attempts to calculate the L1 norm of an array that contains a NaN
value. By using a try - except
block, we can gracefully handle any potential errors.
For large arrays, calculating the L1 norm can be computationally expensive. It’s important to use vectorized operations as much as possible. Avoid using explicit loops in Python as they are generally slower compared to NumPy’s built - in functions.
When using the np.linalg.norm
function, it’s a good practice to add comments to clearly indicate what the code is doing, especially when dealing with multi - dimensional arrays and different axis settings. For example:
import numpy as np
# Create a 2-D array
matrix = np.array([[1, 2], [3, 4]])
# Calculate the L1 norm along the rows
l1_norm_rows = np.linalg.norm(matrix, 1, axis=1)
# Comment explaining the purpose of this calculation
print("L1 norm of each row in the matrix:", l1_norm_rows)
The NumPy L1 norm is a valuable tool in data science and numerical computing. It provides a straightforward way to measure the magnitude of vectors and matrices. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can effectively use the L1 norm in various applications such as feature selection, outlier detection, and more. Remember to handle errors properly and optimize performance for large datasets, and always keep your code readable for better maintainability.