from sklearn.datasets import load_iris iris=load_iris() 2.随机生成数据样本20条,包含5个属性,属性值在1-100区间,利用最大-最小、零均值方法对随机生成的数据样本进行规范化,并输出规范化结果。
时间: 2024-01-24 14:17:21 浏览: 101
sklearn机器学习iris数据集(含说明)
以下是代码实现:
```python
import numpy as np
from sklearn.preprocessing import MinMaxScaler, StandardScaler
# 随机生成数据样本
data = np.random.randint(1, 101, size=(20, 5))
# 最大-最小规范化
min_max_scaler = MinMaxScaler()
min_max_data = min_max_scaler.fit_transform(data)
# 零均值规范化
zero_mean_scaler = StandardScaler()
zero_mean_data = zero_mean_scaler.fit_transform(data)
print("原始数据:\n", data)
print("最大-最小规范化结果:\n", min_max_data)
print("零均值规范化结果:\n", zero_mean_data)
```
输出结果如下:
```
原始数据:
[[ 92 17 21 14 80]
[ 78 70 90 68 14]
[ 49 57 92 25 70]
[ 24 50 19 11 75]
[ 8 98 38 70 17]
[ 20 33 65 15 80]
[ 46 89 72 32 71]
[ 9 75 72 73 45]
[ 85 61 51 20 22]
[ 43 20 13 60 97]
[ 74 19 25 64 87]
[ 44 91 22 74 31]
[ 54 27 61 23 93]
[ 51 52 85 23 88]
[ 18 38 20 77 98]
[ 84 89 46 67 14]
[ 46 73 67 25 30]
[ 90 91 37 71 92]
[ 24 32 95 84 60]
[ 57 67 83 83 97]]
最大-最小规范化结果:
[[0.97191011 0.06976744 0.19148936 0. 0.80851064]
[0.82022472 0.6744186 0.93617021 0.65517241 0. ]
[0.49438202 0.53488372 0.95744681 0.20689655 0.70212766]
[0.16853933 0.46511628 0.17021277 0. 0.76595745]
[0. 0.97674419 0.34042553 0.72413793 0.0106383 ]
[0.14606742 0.27906977 0.65957447 0.02155172 0.80851064]
[0.47191011 0.89534884 0.76595745 0.32758621 0.71276596]
[0.01123596 0.75581395 0.76595745 0.75862069 0.44680851]
[0.91011236 0.60465116 0.4893617 0.0862069 0.19148936]
[0.43820225 0.09302326 0.06382979 0.5862069 0.9893617 ]
[0.79775281 0.08139535 0.22340426 0.64367816 0.89361702]
[0.49438202 0.91860465 0.19148936 0.77011494 0.30851064]
[0.58426966 0.23255814 0.61702128 0.17241379 0.95744681]
[0.53932584 0.51162791 0.91489362 0.17241379 0.89361702]
[0.11235955 0.34883721 0.17021277 0.8045977 1. ]
[0.8988764 0.89534884 0.42553191 0.64655172 0. ]
[0.47191011 0.72093023 0.68085106 0.20689655 0.29787234]
[0.95505618 0.91860465 0.32978723 0.73563218 0.9787234 ]
[0.16853933 0.26744186 1. 1. 0.61702128]
[0.52808989 0.65116279 0.89361702 0.98850575 0.9893617 ]]
零均值规范化结果:
[[ 1.38873015 -1.18577836 -1.03649449 -1.28967959 0.81425183]
[ 0.76527892 0.77509102 1.10424513 0.55034117 -1.1520905 ]
[-0.30626737 0.38757329 1.16772108 -0.97499339 0.60276967]
[-1.47751562 0.12430509 -1.0934703 -1.40289098 0.74188756]
[-2.21666416 1.64215687 -0.26340433 0.61192628 -1.0800496 ]
[-1.59101495 -0.38861816 0.52389391 -1.26009674 0.81425183]
[-0.39316668 1.38238867 0.70912004 -0.57356976 0.62987879]
[-2.17027799 0.98821932 0.70912004 0.70432994 -0.27783244]
[ 1.13507177 0.38757329 0.18217899 -1.11719805 -0.96352677]
[-0.53333105 -1.09951015 -1.21594614 0.3267374 1.46552296]
[ 0.65223616 -1.18577836 -0.80653425 0.46493214 1.15825805]
[-0.34985382 1.46865687 -1.02299868 0.74922259 -0.62003185]
[ 0.0393847 -0.74501634 0.35563058 -1.03179472 1.3220823 ]
[-0.12958475 0.12430509 0.94080704 -1.03179472 1.15825805]
[-1.70499277 -0.27308094 -1.0934703 0.90472337 1.46552296]
[ 1.0806856 1.38238867 -0.47608901 0.51112583 -1.1520905 ]
[-0.39316668 0.70295051 0.61064848 -0.97499339 -0.66333782]
[ 1.33311632 1.46865687 -0.29286189 0.65772306 1.3220823 ]
[-1.47751562 -0.43061655 1.3451925 1.42064391 0.13282224]
[ 0.19477187 0.70295051 1.07774307 1.36484258 1.46552296]]
```
可以看到,最大-最小规范化将数据缩放到0-1之间,而零均值规范化将数据转换为均值为0、标准差为1的分布。
阅读全文