pyqt_data_analysis/libdataanalysis/8-Kmeans代码实现/utils/features/normalize.py

25 lines
544 B
Python
Raw Normal View History

2024-06-08 19:26:36 +08:00
"""Normalize features"""
import numpy as np
def normalize(features):
features_normalized = np.copy(features).astype(float)
# 计算均值
features_mean = np.mean(features, 0)
# 计算标准差
features_deviation = np.std(features, 0)
# 标准化操作
if features.shape[0] > 1:
features_normalized -= features_mean
# 防止除以0
features_deviation[features_deviation == 0] = 1
features_normalized /= features_deviation
return features_normalized, features_mean, features_deviation