文章目录
- 特征处理
- 标准化
- 归一化
- 正则化
特征处理
标准化
# 导入标准化库
from sklearn.preprocessing import StandardScalerfrom matplotlib import gridspec
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
# 随机生成0到100的整数,100行2列
data = np.random.randint(0, 100, (100, 2))
data
array([[ 7, 84],
[43, 81],
[22, 84],
[ 7, 61],
[51, 74],
[95, 94],
[80, 92],
[58, 35],
[88, 15],
[61, 42],
[75, 95],
[87, 64],
[18, 77],
[13, 60],
[18, 51],
[61, 12],
[32, 11],
[ 6, 77],
[85, 44],
[87, 10],
[84, 10],
[ 1, 28],
[76, 87],
[61, 0],
[ 9, 25],
[83, 75],
[ 7, 60],
[80, 73],
[62, 58],
[71, 84],
[78, 6],
[92, 54],
[92, 50],
[28, 57],
[73, 80],
[97, 86],
[84, 78],
[ 9, 29],
[90, 64],
[ 8, 32],
[98, 62],
[45, 93],
[73, 72],
[97, 11],
[21, 66],
[32, 9],
[65, 59],
[30, 36],
[19, 37],
[75, 43],
[90, 55],
[53, 8],
[73, 25],
[73, 82],
[84, 76],
[49, 97],
[29, 64],
[69, 37],
[72, 90],
[10, 87],
[19, 70],
[49, 53],
[56, 24],
[61, 16],
[58, 23],
[28, 31],
[37, 49],
[67, 25],
[31, 99],
[38, 84],
[55, 53],
[27, 89],
[83, 50],
[73, 86],
[67, 11],
[61, 72],
[17, 88],
[82, 67],
[56, 51],
[18, 59],
[73, 44],
[ 8, 86],
[ 6, 20],
[32, 12],
[15, 4],
[91, 17],
[21, 78],
[67, 63],
[12, 32],
[45, 76],
[41, 29],
[75, 64],
[75, 19],
[ 1, 76],
[17, 18],
[13, 47],
[80, 48],
[88, 76],
[29, 63],
[21, 95]])
# 标准化
ss = StandardScaler()
std_data = ss.fit_transform(data)
std_data
array([[-1.50234381, 1.09430096],
[-0.28120186, 0.98577525],
[-0.99353466, 1.09430096],
[-1.50234381, 0.26227048],
[-0.00983698, 0.73254858],
[ 1.48266985, 1.45605335],
[ 0.97386071, 1.38370287],
[ 0.22760729, -0.67828572],
[ 1.24522559, -1.40179049],
[ 0.32936912, -0.42505905],
[ 0.80425766, 1.49222858],
[ 1.21130498, 0.37079619],
[-1.1292171 , 0.84107429],
[-1.29882015, 0.22609524],
[-1.1292171 , -0.09948191],
[ 0.32936912, -1.5103162 ],
[-0.65432856, -1.54649144],
[-1.53626442, 0.84107429],
[ 1.14346376, -0.35270857],
[ 1.21130498, -1.58266668],
[ 1.10954315, -1.58266668],
[-1.70586747, -0.93151239],
[ 0.83817827, 1.20282668],
[ 0.32936912, -1.94441906],
[-1.43450259, -1.0400381 ],
[ 1.07562254, 0.76872382],
[-1.50234381, 0.22609524],
[ 0.97386071, 0.69637334],
[ 0.36328973, 0.15374476],
[ 0.66857522, 1.09430096],
[ 0.90601949, -1.72736763],
[ 1.38090802, 0.00904381],
[ 1.38090802, -0.13565714],
[-0.790011 , 0.11756952],
[ 0.73641644, 0.94960001],
[ 1.55051107, 1.16665144],
[ 1.10954315, 0.87724953],
[-1.43450259, -0.89533715],
[ 1.3130668 , 0.37079619],
[-1.4684232 , -0.78681143],
[ 1.58443168, 0.29844572],
[-0.21336064, 1.41987811],
[ 0.73641644, 0.6601981 ],
[ 1.55051107, -1.54649144],
[-1.02745527, 0.44314667],
[-0.65432856, -1.61884192],
[ 0.46505156, 0.18992 ],
[-0.72216978, -0.64211048],
[-1.09529649, -0.60593524],
[ 0.80425766, -0.38888381],
[ 1.3130668 , 0.04521905],
[ 0.05800424, -1.65501716],
[ 0.73641644, -1.0400381 ],
[ 0.73641644, 1.02195048],
[ 1.10954315, 0.80489905],
[-0.0776782 , 1.56457906],
[-0.75609039, 0.37079619],
[ 0.600734 , -0.60593524],
[ 0.70249583, 1.31135239],
[-1.40058198, 1.20282668],
[-1.09529649, 0.58784762],
[-0.0776782 , -0.02713143],
[ 0.15976607, -1.07621334],
[ 0.32936912, -1.36561525],
[ 0.22760729, -1.11238858],
[-0.790011 , -0.82298667],
[-0.48472551, -0.17183238],
[ 0.53289278, -1.0400381 ],
[-0.68824917, 1.63692954],
[-0.4508049 , 1.09430096],
[ 0.12584546, -0.02713143],
[-0.82393161, 1.27517715],
[ 1.07562254, -0.13565714],
[ 0.73641644, 1.16665144],
[ 0.53289278, -1.54649144],
[ 0.32936912, 0.6601981 ],
[-1.16313771, 1.23900191],
[ 1.04170193, 0.47932191],
[ 0.15976607, -0.09948191],
[-1.1292171 , 0.18992 ],
[ 0.73641644, -0.35270857],
[-1.4684232 , 1.16665144],
[-1.53626442, -1.2209143 ],
[-0.65432856, -1.5103162 ],
[-1.23097893, -1.79971811],
[ 1.34698741, -1.32944001],
[-1.02745527, 0.87724953],
[ 0.53289278, 0.33462096],
[-1.33274076, -0.78681143],
[-0.21336064, 0.80489905],
[-0.34904307, -0.89533715],
[ 0.80425766, 0.37079619],
[ 0.80425766, -1.25708953],
[-1.70586747, 0.80489905],
[-1.16313771, -1.29326477],
[-1.29882015, -0.24418286],
[ 0.97386071, -0.20800762],
[ 1.24522559, 0.80489905],
[-0.75609039, 0.33462096],
[-1.02745527, 1.49222858]])
# 作图
gs = gridspec.GridSpec(5,5)
fig = plt.figure()
ax1 = fig.add_subplot(gs[0:2, 1:4])
ax2 = fig.add_subplot(gs[3:5, 1:4])ax1.scatter(data[:, 0], data[:, 1])
ax2.scatter(std_data[:, 0], std_data[:, 1])plt.show()
归一化
# 导入归一化库
from sklearn.preprocessing import MinMaxScaler
import numpy as npdata = np.random.uniform(0, 100, 10)[:, np.newaxis]
mm = MinMaxScaler()
mm_data = mm.fit_transform(data)
origin_data = mm.inverse_transform(mm_data)
print('data is ',data)
print('after Min Max ',mm_data)
print('origin data is ',origin_data)
data is [[67.74476271]
[10.2077285 ]
[27.34037799]
[24.72236105]
[68.67245127]
[91.11026437]
[51.92345696]
[92.05191865]
[16.9495692 ]
[94.18851495]]
after Min Max [[0.6851214 ]
[0. ]
[0.20400678]
[0.17283278]
[0.69616784]
[0.96334578]
[0.49672943]
[0.97455851]
[0.08027837]
[1. ]]
origin data is [[67.74476271]
[10.2077285 ]
[27.34037799]
[24.72236105]
[68.67245127]
[91.11026437]
[51.92345696]
[92.05191865]
[16.9495692 ]
[94.18851495]]
正则化
# 导入L1正则化库
from sklearn.preprocessing import Normalizer# 导入L2正则化库
from sklearn.preprocessing import normalizeX = [[1, -1, 2],[2, 0, 0],[0, 1, -1]]normalizerl1 = Normalizer(norm='l1')
l1 = normalizerl1.fit_transform(X)
print('l1:', l1)l2 = normalize(X, norm='l2')
print('l2:', l2)
l1: [[ 0.25 -0.25 0.5 ][ 1. 0. 0. ][ 0. 0.5 -0.5 ]]
l2: [[ 0.40824829 -0.40824829 0.81649658][ 1. 0. 0. ][ 0. 0.70710678 -0.70710678]]