首頁  >  文章  >  後端開發  >  如何使用標準化將資料值保持在範圍內?

如何使用標準化將資料值保持在範圍內?

Linda Hamilton
Linda Hamilton原創
2024-10-18 17:02:03757瀏覽

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

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()

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

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())

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

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

以上是如何使用標準化將資料值保持在範圍內?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn