# Scaling Your Data with NumPy: The Power of Normalization

## Normalizing a NumPy Matrix: How to Scale Your Data for Better Analysis and Results

Data normalization is a critical step in data analysis that involves scaling the data to a common range. In simple terms, normalization ensures that all data points are in proportion to each other and not biased towards any specific variable.

One of the most commonly used tools for handling data in Python is the NumPy matrix, and in this article, we will explore how to normalize a NumPy matrix to improve your data analysis and results.

## Normalization Definition

Normalization is the process of scaling data to have a common range, usually between 0 and 1 or -1 and 1, to make it easier to compare and analyze. Normalization applies to quantitative data with varying absolute ranges or units.

For example, a dataset that contains age and income data would have varying units, such as years and dollars. To normalize data, you must transform it to a common scale, which removes any bias and makes it easier to compare and analyze.

## Normalizing Rows of NumPy Matrix

You can normalize rows of a NumPy matrix by dividing each row’s values by their maximum value. This method is known as L2 normalization, and it ensures that all rows of a matrix have a length of 1.

The result is a matrix where each row’s values are in proportion to each other. To normalize rows of a NumPy matrix, you can use the Normalize function from the sklearn.preprocessing package.

Here’s an example:

``````import numpy as np
from sklearn.preprocessing import normalize
matrix = np.array([
[1, 3, 5],
[2, 4, 6],
[7, 8, 9]
])
normalized_matrix = normalize(matrix, norm='l2', axis=1)
print(normalized_matrix)
``````

## Output:

``````array([[0.16903085, 0.50709255, 0.84515425],
[0.26726124, 0.53452248, 0.80178373],
[0.49613894, 0.56818706, 0.64023517]])
``````

The Normalize function takes two parameters; the matrix to normalize and the norm to use (‘l2’ for L2 normalization). The axis parameter indicates whether to normalize rows (axis=1) or columns (axis=0).

## Normalizing Columns of NumPy Matrix

You can also normalize columns of a NumPy matrix by dividing each column’s values by their maximum value. This method is known as column scaling, and it ensures that all columns of a matrix have a range of 0 to 1.

The result is a matrix where each column’s values are in proportion to each other. To normalize columns of a NumPy matrix, you can use the Normalize function from the sklearn.preprocessing package.

Here’s an example:

``````import numpy as np
from sklearn.preprocessing import normalize
matrix = np.array([
[1, 3, 5],
[2, 4, 6],
[7, 8, 9]
])
normalized_matrix = normalize(matrix, norm='l2', axis=0)
print(normalized_matrix)
``````

## Output:

``````array([[0.12309149, 0.31622777, 0.46366466],
[0.24618298, 0.63245553, 0.55708601],
[0.86182147, 0.63245553, 0.69513492]])
``````

The Normalize function takes two parameters; the matrix to normalize and the norm to use (‘l2’ for L2 normalization). The axis parameter indicates whether to normalize rows (axis=1) or columns (axis=0).

### Example 1: Normalizing Rows of NumPy Matrix

Let’s create a simple NumPy matrix and normalize its rows using the L2 normalization technique:

``````import numpy as np
from sklearn.preprocessing import normalize
matrix = np.array([
[1, 3, 5],
[2, 4, 6],
[7, 8, 9]
])
normalized_matrix = normalize(matrix, norm='l2', axis=1)
print(normalized_matrix)
``````

## Output:

``````array([[0.16903085, 0.50709255, 0.84515425],
[0.26726124, 0.53452248, 0.80178373],
[0.49613894, 0.56818706, 0.64023517]])
``````

In this example, we created a 3×3 NumPy matrix and called the Normalize function from the sklearn.preprocessing package, passing in the matrix to normalize and the normalization technique to use. The resulting normalized matrix has rows with a length of 1, as shown in the output above.

## View Normalized Matrix

To view the normalized matrix, you can simply print it to the console, as shown in the examples above. Alternatively, you can visualize the matrix using Matplotlib or any other visualization library.

## Conclusion

Normalizing a NumPy matrix is an essential step in data analysis that enables you to standardize the data and remove any bias. You can normalize rows or columns of a NumPy matrix using L2 normalization and the Normalize function from the sklearn.preprocessing package.

Once normalized, you can analyze and compare the data more effectively, enabling you to make better data-driven decisions. Example 2:

### Normalizing Columns of NumPy Matrix

Let’s create another simple NumPy matrix and normalize its columns using the L2 normalization technique:

``````import numpy as np
from sklearn.preprocessing import normalize
matrix = np.array([
[1, 3, 5],
[2, 4, 6],
[7, 8, 9]
])
normalized_matrix = normalize(matrix, norm='l2', axis=0)
print(normalized_matrix)
``````

## Output:

``````array([[0.12309149, 0.31622777, 0.46366466],
[0.24618298, 0.63245553, 0.55708601],
[0.86182147, 0.63245553, 0.69513492]])
``````

In this example, we created a 3×3 NumPy matrix and called the Normalize function from the sklearn.preprocessing package, passing in the matrix to normalize and the normalization technique to use. The resulting normalized matrix has columns with a range of 0 to 1, as shown in the output above.

## View Normalized Matrix

To view the normalized matrix, you can simply print it to the console, as shown in the example above. Alternatively, you can visualize the matrix using Matplotlib or any other visualization library.

## NumPy Matrix Creation

Before we can normalize a NumPy matrix, we must first create one. You can create a NumPy matrix using the np.array() function, passing in a list or tuple of values.

For example, here’s how to create a 3×3 NumPy matrix using a list of values:

``````import numpy as np
matrix = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
print(matrix)
``````

## Output:

``````array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
``````

You can also create a NumPy matrix using other NumPy functions, such as np.zeros(), np.ones(), np.random(), and np.arange().

## Normalizing Columns of NumPy Matrix

Normalizing columns of a NumPy matrix involves dividing each value in a column by the maximum value of that column. The result is a column where each value is between 0 and 1.

To normalize columns of a NumPy matrix, you can use the Normalize function from the sklearn.preprocessing package, as shown in the example above. The Normalize function takes two parameters; the matrix to normalize and the normalization technique to use (‘l2’ for L2 normalization).

The axis parameter indicates whether to normalize rows (axis=1) or columns (axis=0). Alternatively, you can perform column normalization using NumPy operations.

Here’s an example:

``````import numpy as np
matrix = np.array([
[1, 3, 5],
[2, 4, 6],
[7, 8, 9]
])
max_values = matrix.max(axis=0)
normalized_matrix = matrix / max_values
print(normalized_matrix)
``````

## Output:

``````array([[0.14285714, 0.375     , 0.55555556],
[0.28571429, 0.5       , 0.66666667],
[1.        , 1.        , 1.        ]])
``````

In this example, we used the max() function to compute the maximum value of each column.

We then divided each column’s values by their maximum value to normalize the columns. The resulting normalized matrix has each column’s values between 0 and 1.

If you want to learn more about NumPy and data normalization, here are some excellent resources to explore:

1. NumPy User Guide: https://numpy.org/doc/stable/user/index.html
2. Scikit-learn User Guide: https://scikit-learn.org/stable/user_guide.html
3. Data Normalization in Python: A Comprehensive Guide: https://towardsdatascience.com/data-normalization-in-python-a-comprehensive-guide-aa8b924f47e5
4. Normalization and Standardization in Python: Learn Data Preprocessing: https://www.analyticsvidhya.com/blog/2020/04/feature-scaling-machine-learning-normalization-standardization/
5. A Gentle Introduction to Normalization in Machine Learning: https://machinelearningmastery.com/normalize-standardize-machine-learning-data-weka/

These resources provide a range of tutorials, code examples, and best practices for applying data normalization techniques using NumPy, Scikit-learn, and other Python tools.

By exploring these resources, you can deepen your knowledge of NumPy matrices and improve your data analysis skills. In conclusion, normalizing a NumPy matrix is crucial for standardizing data and removing any bias, making it easier to analyze and compare variables.

You can normalize rows or columns of a NumPy matrix using L2 normalization and the Normalize function from the sklearn.preprocessing package. Alternatively, you can normalize columns of a NumPy matrix using NumPy operations.

Understanding how to normalize a NumPy matrix enables you to make more data-driven decisions and improve your data analysis skills. By exploring available resources, you can deepen your knowledge of this important data preparation technique.

Remember that normalization is a crucial step in data analysis and can significantly impact the results of your work.