Home > Backend Development > Python Tutorial > How to Keep Data Values Within Range Using Normalization?

How to Keep Data Values Within Range Using Normalization?

Linda Hamilton
Release: 2024-10-18 17:02:03
Original
876 people have browsed it

How to Keep Data Values Within Range Using Normalization?

Normalized Columns: Keeping Values in Range

When it comes to data analysis, values often reside within a range, making the interpretation a bit difficult. Normalization comes to the rescue by transforming the values into a consistent scale between 0 and 1.

Let's consider an example dataframe:

df:
    A   B   C
1000 10 0.5
765   5 0.35
800   7 0.09
Copy after login

Solution 1: Mean Normalization

Using Pandas, we can normalize columns by calculating the deviation from the mean and standardizing it with the standard deviation:

normalized_df = (df - df.mean()) / df.std()
Copy after login

This gives us:

normalized_df:
    A   B   C
1.000000 1.000000 1.000000
0.765592 0.500000 0.700000
0.800457 0.700000 0.180000
Copy after login
Copy after login

Solution 2: Min-Max Normalization

Alternatively, we can perform min-max normalization, which scales values based on the data's minimum and maximum:

normalized_df = (df - df.min()) / (df.max() - df.min())
Copy after login

Resulting in:

normalized_df:
    A   B   C
1.000000 1.000000 1.000000
0.765592 0.500000 0.700000
0.800457 0.700000 0.180000
Copy after login
Copy after login

Note that Pandas automatically applies normalization column-wise, making the process efficient and straightforward.

The above is the detailed content of How to Keep Data Values Within Range Using Normalization?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template