Vector Norms and Magnitude
Swipe um das Menü anzuzeigen
Understanding the size or "length" of a vector is essential in linear algebra and data science. The most common way to measure a vector's magnitude is the Euclidean norm, also called the L2 norm. For a vector v=[v1,v2,...,vn], the L2 norm calculates the straight-line distance from the origin to the point represented by the vector in n-dimensional space. The formula for the L2 norm is:
∣∣v∣∣2=v12+v22+...+vn2Another important norm is the L1 norm, also known as the Manhattan norm. This norm sums the absolute values of the vector's components:
∣∣v∣∣1=∣v1∣+∣v2∣+...+∣vn∣These norms are widely used in data science. The L2 norm is often used for measuring Euclidean distance and in regularization (like Ridge regression), while the L1 norm is used in scenarios where sparsity is important (like Lasso regression).
123456789101112import numpy as np # Define a vector v = np.array([3, -4, 1]) # Compute the L2 (Euclidean) norm l2_norm = np.sqrt(np.sum(v ** 2)) print("L2 norm:", l2_norm) # Compute the L1 norm l1_norm = np.sum(np.abs(v)) print("L1 norm:", l1_norm)
In the code above, you define a vector v with three components. To find the L2 norm, you square each component, sum them, and then take the square root. This gives you the Euclidean distance from the origin to the point [3, -4, 1]. The L1 norm is calculated by summing the absolute values of each component, which measures the total "taxicab" or "Manhattan" distance.
The L2 norm is sensitive to large values in any component, as squaring amplifies their effect. This is suitable when you want to penalize large deviations strongly or measure straight-line distances. The L1 norm treats each component equally, making it robust to outliers and useful when you want to encourage sparsity in solutions, such as in feature selection for machine learning models. Choosing between these norms depends on your specific use case and the properties you want to emphasize in your data analysis or model.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen