归一法
import numpy as np
from sklearn.preprocessing import MinMaxScaler,StandardScaler
x=np.array([1,2,5,23,5,3,2,-7]).reshape(-1,1)
y=np.array(np.random.randint(1,100,12).reshape(3,4))
print(y)
结果:
[[18 8 70 7]
[44 69 7 7]
[33 84 34 29]]
最大-最小归一
l1=MinMaxScaler().fit_transform(y)
标准化归一
l2=StandardScaler().fit_transform(y)
print(l1,'\n','-'*20,'\n',l2)
结果:
[[0. 0. 1. 0. ]
[1. 0.80263158 0. 0. ]
[0.57692308 1. 0.42857143 1. ]]
--------------------
[[-1.28250306 -1.38944947 1.27872403 -0.70710678]
[ 1.15738081 0.46653048 -1.16247639 -0.70710678]
[ 0.12512225 0.92291899 -0.11624764 1.41421356]]
#数值化
#机器编码
from sklearn.preprocessing import LabelEncoder
x=['a','b','c','d']
l1=LabelEncoder().fit_transform(x)
print(l1)
结果:
[0 1 2 3]
#手动编码
d=dict([('a',0),('b',1),('c',2),('d',3)])
for i in x:
print(d.get(i))
结果:
0
1
2
3