Python - 使用 Pandas 逐列缩放数字
机器学习中的缩放数字是一种常见的预处理技术,用于在固定范围内标准化数据中存在的独立特征。当应用于Python序列(例如 Pandas 系列)时,缩放会产生一个新序列,以便列中的所有值都在一个范围内。例如,如果范围是 ( 0 ,1 ),则该列中的整个数据将仅在 0,1 范围内。
例子:
if the sequence is [1, 2, 3]
then the scaled sequence is [0, 0.5, 1]
应用:
- 在机器学习中,缩放可以提高各种算法的收敛速度。
- 通常在机器学习中,您会遇到变化很大的数据集,许多机器学习模型很难很好地处理这些数据,因此在这种情况下,缩放有助于将数据保持在一个范围内。
注意:我们将在本文中使用 Scikit-learn 来缩放 Pandas 数据框。
脚步:
- 在Python导入 pandas 和 sklearn 库。
- 调用 DataFrame 构造函数以返回一个新的 DataFrame。
- 创建 sklearn.preprocessing.MinMaxScaler 的实例。
- 调用 sklearn.preprocessing.MinMaxScaler.fit_transform(df[[column_name]]) 以从第一步返回 Pandas DataFrame df,其中指定的列 min-max 已缩放。
示例 1:
MinMax 的一个非常基本的例子
Python3
# importing the required libraries
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# creating a dataframe for example
pd_data = pd.DataFrame({
"Item": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"Price": [100, 300, 250, 120, 910, 345, 124, 1000, 289, 500]
})
# Creating an instance of the sklearn.preprocessing.MinMaxScaler()
scaler = MinMaxScaler()
# Scaling the Price column of the created dataFrame and storing
# the result in ScaledPrice Column
pd_data[["ScaledPrice"]] = scaler.fit_transform(pd_data[["Price"]])
print(pd_data)
Python3
# importing the required libraries
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# creating a dataframe for example
pd_data = pd.DataFrame({
"Item": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"Price": [100, 300, 250, 120, 910, 345, 124, 1000, 289, 500],
"Weight": [200, 203, 350, 100, 560, 456, 700, 250, 800, 389]
})
# Creating an instance of the sklearn.preprocessing.MinMaxScaler()
scaler = MinMaxScaler()
# Scaling the Price column of the created dataFrame and storing
# the result in ScaledPrice Column
pd_data[["ScaledPrice", "ScaledWeight"]] = scaler.fit_transform(
pd_data[["Price", "Weight"]])
print(pd_data)
Python3
# importing the required libraries
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# creating a dataframe for example
pd_data = pd.DataFrame({
"Item": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"Price": [100, 300, 250, 120, 910, 345, 124, 1000, 289, 500]
})
# Creating an instance of the sklearn.preprocessing.MinMaxScaler()
# specifying the min and max value of the scale
scaler = MinMaxScaler(feature_range=(20, 500))
# Scaling the Price column of the created dataFrame
# and storing the result in ScaledPrice Column
pd_data[["ScaledPrice"]] = scaler.fit_transform(pd_data[["Price"]])
print(pd_data)
输出 :
示例 2:您还可以一次缩放多个 Pandas,DataFrame 的列,您只需在 MinMaxScaler.fit_transform()函数传递列名。
蟒蛇3
# importing the required libraries
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# creating a dataframe for example
pd_data = pd.DataFrame({
"Item": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"Price": [100, 300, 250, 120, 910, 345, 124, 1000, 289, 500],
"Weight": [200, 203, 350, 100, 560, 456, 700, 250, 800, 389]
})
# Creating an instance of the sklearn.preprocessing.MinMaxScaler()
scaler = MinMaxScaler()
# Scaling the Price column of the created dataFrame and storing
# the result in ScaledPrice Column
pd_data[["ScaledPrice", "ScaledWeight"]] = scaler.fit_transform(
pd_data[["Price", "Weight"]])
print(pd_data)
输出 :
示例 3:默认情况下,MinMaxScaler() 类使用的比例值为 (0,1),但您可以根据需要将其更改为您想要的任何值。
蟒蛇3
# importing the required libraries
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
# creating a dataframe for example
pd_data = pd.DataFrame({
"Item": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"Price": [100, 300, 250, 120, 910, 345, 124, 1000, 289, 500]
})
# Creating an instance of the sklearn.preprocessing.MinMaxScaler()
# specifying the min and max value of the scale
scaler = MinMaxScaler(feature_range=(20, 500))
# Scaling the Price column of the created dataFrame
# and storing the result in ScaledPrice Column
pd_data[["ScaledPrice"]] = scaler.fit_transform(pd_data[["Price"]])
print(pd_data)
输出 :