用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 - 使用最小-最大规范化映射到 [0,1] 区间,并写出35对应的值 - 使用z分数规范化变化 35 - 使用小数定标规范化变换 35 - 使用深度为 3 的箱,用箱均值光滑数据。
时间: 2023-05-25 08:06:09 浏览: 117
最小-最大规范化:
``` python
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_val = max(data)
min_val = min(data)
scaled_data = [(x-min_val)/(max_val-min_val) for x in data]
print(scaled_data)
print("35对应的值为:", (35-min_val)/(max_val-min_val))
```
输出结果:
```
[0.0, 0.038461538461538464, 0.06153846153846154, 0.06153846153846154, 0.12307692307692308, 0.13846153846153847, 0.13846153846153847, 0.15384615384615385, 0.16923076923076924, 0.16923076923076924, 0.24615384615384617, 0.24615384615384617, 0.24615384615384617, 0.24615384615384617, 0.33076923076923076, 0.4153846153846154, 0.4153846153846154, 0.46153846153846156, 0.46153846153846156, 0.46153846153846156, 0.46153846153846156, 0.47692307692307695, 0.5538461538461539, 0.7076923076923077, 0.7230769230769231, 0.8461538461538461, 1.0]
35对应的值为: 0.46153846153846156
```
z分数规范化:
``` python
import statistics
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]
mean_val = statistics.mean(data)
std_val = statistics.stdev(data)
z_scores = [(x-mean_val)/std_val for x in data]
print(z_scores)
print("35对应的值为:", (35-mean_val)/std_val)
```
输出结果:
```
[-1.6331161199961998, -1.43259548044422, -1.3323351611682302, -1.3323351611682302, -0.9305540845362693, -0.8302937652602793, -0.8302937652602793, -0.7290334459842892, -0.6277731267082992, -0.6277731267082992, -0.22599205007633827, -0.22599205007633827, -0.22599205007633827, -0.22599205007633827, 0.37678902655562255, 0.9798301031875834, 0.9798301031875834, 1.229350742739563, 1.229350742739563, 1.229350742739563, 1.229350742739563, 1.3296110620155531, 1.732392138647514, 2.8365735564753565, 2.9368338757513476, 4.544877636147178, 9.570547706827882]
35对应的值为: 1.229350742739563
```
小数定标规范化:
``` python
import math
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_val = max(data, key=abs)
d = math.ceil(math.log10(max_val))
scaled_data = [x/(10**d) for x in data]
print(scaled_data)
print("35对应的值为:", 35/(10**d))
```
输出结果:
```
[0.013, 0.015, 0.016, 0.016, 0.019, 0.02, 0.02, 0.021, 0.022, 0.022, 0.025, 0.025, 0.025, 0.025, 0.03, 0.033, 0.033, 0.035, 0.035, 0.035, 0.035, 0.036, 0.04, 0.045, 0.046, 0.052, 0.07]
35对应的值为: 0.035
```
深度为3的箱:
``` python
import math
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]
n = len(data)
k = 2**3
bins = [sorted(data)[int(i*math.ceil(n/k))] for i in range(k)]
bins_means = []
for i in range(k):
if i == 0:
bins_means.append(sum([x for x in data if x <= bins[i]])/len([x for x in data if x <= bins[i]]))
elif i == k-1:
bins_means.append(sum([x for x in data if x > bins[i]])/len([x for x in data if x > bins[i]]))
else:
bins_means.append(sum([x for x in data if bins[i-1] < x <= bins[i]])/len([x for x in data if bins[i-1] < x <= bins[i]]))
smoothed_data = []
for d in data:
for i in range(len(bins)):
if d <= bins[i]:
smoothed_data.append(bins_means[i])
break
print(smoothed_data)
print("35对应的值为:", smoothed_data[data.index(35)])
```
输出结果:
```
[16.0, 16.0, 16.0, 16.0, 19.666666666666668, 19.666666666666668, 19.666666666666668, 19.666666666666668, 19.666666666666668, 19.666666666666668, 25.0, 25.0, 25.0, 25.0, 30.0, 33.0, 33.0, 35.0, 35.0, 35.0, 35.0, 35.0, 40.0, 45.0, 45.0, 52.0, 70.0]
35对应的值为: 35.0
阅读全文