用python将数据规范化并写出代码: 数据 13,15,16,16,19,20,20,21,22,22,25,25,25,25,30,33,33,35,35,35,35,36,40,45,46,52,70 - 使用最小-最大规范化将 35 映射到 [0,1] 区间 - 使用z分数规范化变化 35 - 使用小数定标规范化变换 35 - 使用深度为 3 的箱,用箱均值光滑数据。
时间: 2023-05-22 20:03:46 浏览: 110
下面是python代码:
# 最小-最大规范化
def min_max_normalization(x, min_value, max_value):
return (x - min_value) / (max_value - min_value)
data = [13,15,16,16,19,20,20,21,22,22,25,25,25,25,30,33,33,35,35,35,35,36,40,45,46,52,70]
max_value = max(data)
min_value = min(data)
normalized_min_max = []
for x in data:
normalized_min_max.append(min_max_normalization(x, min_value, max_value))
print('最小-最大规范化:', normalized_min_max)
# z分数规范化
import numpy as np
mean = np.mean(data)
std = np.std(data)
z_score = []
for x in data:
z_score.append((x - mean) / std)
print('z分数规范化:', z_score)
# 小数定标规范化
def decimal_scaling(x, q):
return x / 10**q
max_abs = max(abs(max_value), abs(min_value))
q = len(str(max_abs)) - 1
decimal_scaled = []
for x in data:
decimal_scaled.append(decimal_scaling(x, q))
print('小数定标规范化:', decimal_scaled)
# 箱均值光滑
bin_count = 2 ** 3
bin_size = (max_value - min_value) / bin_count
binned_data = [[] for _ in range(bin_count)]
for x in data:
bin_no = int((x - min_value) // bin_size)
binned_data[bin_no].append(x)
bin_means = [np.mean(bin_data) for bin_data in binned_data]
smoothed_data = []
for x in data:
bin_no = int((x - min_value) // bin_size)
smoothed_data.append(bin_means[bin_no])
print('箱均值光滑:', smoothed_data)
阅读全文