You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
565 B
25 lines
565 B
"""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
|