pandas 如何在Python中标准化和规范化这些数据?

ehxuflar  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(120)
import pandas as pd
from sklearn import preprocessing

hmeq = pd.read_csv('hmeq_small.csv')

# Standardize the data
standardized = hmeq.StandardScaler()

# Output the standardized data as a data frame
hmeqStand = standardized(as_frame=True)

# Normalize the data
normalized = hmeq.normalize()

# Output the normalized data as a data frame
hmeqNorm = normalized(as_frame=True)

# Print the means and standard deviations of hmeqStand and hmeqNorm
print("The means of hmeqStand are ", hmeqStand.mean())
print("The standard deviations of hmeqStand are ", hmeqStand.stdev())
print("The means of hmeqNorm are ", hmeqNorm.mean())
print("The standard deviations of hmeqNorm are ", hmeqNorm.stdev())

我试着运行.StandardScaler().normalize()函数,但不断出现错误。

3xiyfsfu

3xiyfsfu1#

StandardScaler和Normalizer来自预处理包,在使用它们之前需要初始化它们。

from sklearn.preprocessing import StandardScaler, Normalizer

scaler = StandardScaler()
normalizer = Normalizer()

然后,您可以调整和转换数据

X = df.to_numpy() # I assume your df only has numerical values

hmeqStandardized = scaler.fit_transform(X)
hmeqNormalised   = normlizer.fit_transform(X)

相关问题