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

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

Linda Hamilton
發布: 2024-10-18 17:02:03
原創
885 人瀏覽過

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中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板