特征变换 归一化、标准化、数值化

归一法

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值