{"id":1309,"date":"2023-03-25T10:12:45","date_gmt":"2023-03-25T02:12:45","guid":{"rendered":""},"modified":"2023-03-25T10:12:45","modified_gmt":"2023-03-25T02:12:45","slug":"\u4eba\u5de5\u667a\u80fd\u6570\u636e\u51c6\u5907","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1309.html","title":{"rendered":"\u4eba\u5de5\u667a\u80fd\u6570\u636e\u51c6\u5907"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

\u4eba\u5de5\u667a\u80fd\u6570\u636e\u51c6\u5907<\/h1>\n

\u4eba\u5de5\u667a\u80fd\u6570\u636e\u51c6\u5907\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u5728\u4e0a\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u76d1\u7763\u548c\u65e0\u76d1\u7763\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u3002 \u8fd9\u4e9b\u7b97\u6cd5\u9700\u8981\u683c\u5f0f\u5316\u6570\u636e\u624d\u80fd\u5f00\u59cb\u8bad\u7ec3\u8fc7\u7a0b\u3002\u5728\u8fd9\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u4ee5\u67d0\u79cd\u65b9\u5f0f\u51c6\u5907\u6216\u683c\u5f0f\u5316\u6570\u636e\uff0c\u4ee5\u4fbf\u5c06\u5176\u4f5c\u4e3aML\u7b97\u6cd5\u7684\u8f93\u5165\u63d0\u4f9b\u3002\n <\/div>\n
\n \u672c\u7ae0\u91cd\u70b9\u4ecb\u7ecd\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u7684\u6570\u636e\u51c6\u5907\u3002\n <\/div>\n

\u9884\u5904\u7406\u6570\u636e<\/h2>\n
\n \u5728\u6211\u4eec\u7684\u65e5\u5e38\u751f\u6d3b\u4e2d\uff0c\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u4f46\u8fd9\u4e9b\u6570\u636e\u662f\u539f\u59cb\u6570\u636e\u3002 \u4e3a\u4e86\u63d0\u4f9b\u6570\u636e\u4f5c\u4e3a\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u7684\u8f93\u5165\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u6709\u610f\u4e49\u7684\u6570\u636e\u3002 \u8fd9\u5c31\u662f\u6570\u636e\u9884\u5904\u7406\u8fdb\u5165\u56fe\u50cf\u7684\u5730\u65b9\u3002 \u6362\u8a00\u4e4b\uff0c\u53ef\u4ee5\u8bf4\u5728\u5c06\u6570\u636e\u63d0\u4f9b\u7ed9\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002\n <\/div>\n
\n \u6570\u636e\u9884\u5904\u7406\u6b65\u9aa4<\/strong>\n <\/div>\n
\n \u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5728Python\u4e2d\u9884\u5904\u7406\u6570\u636e -\n <\/div>\n
\n \u7b2c1\u6b65 <\/strong> - \u5bfc\u5165\u6709\u7528\u7684\u8f6f\u4ef6\u5305 - \u5982\u679c\u4f7f\u7528Python\uff0c\u90a3\u4e48\u8fd9\u5c06\u6210\u4e3a\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u7279\u5b9a\u683c\u5f0f(\u5373\u9884\u5904\u7406)\u7684\u7b2c\u4e00\u6b65\u3002\u5982\u4e0b\u4ee3\u7801 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
import <\/span>numpy as <\/span>np
sklearn import <\/span>preprocessing
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd9\u91cc\u4f7f\u7528\u4e86\u4ee5\u4e0b\u4e24\u4e2a\u8f6f\u4ef6\u5305 -\n <\/div>\n

NumPy<\/strong> - \u57fa\u672c\u4e0aNumPy\u662f\u4e00\u79cd\u901a\u7528\u7684\u6570\u7ec4\u5904\u7406\u8f6f\u4ef6\u5305\uff0c\u8bbe\u8ba1\u7528\u4e8e\u9ad8\u6548\u5904\u7406\u4efb\u610f\u8bb0\u5f55\u7684\u5927\u578b\u591a\u7ef4\u6570\u7ec4\u800c\u4e0d\u727a\u7272\u5c0f\u578b\u591a\u7ef4\u6570\u7ec4\u7684\u901f\u5ea6\u3002<\/span>
\n sklearn.preprocessing<\/strong> - \u6b64\u5305\u63d0\u4f9b\u4e86\u8bb8\u591a\u5e38\u7528\u7684\u5b9e\u7528\u51fd\u6570\u548c\u53d8\u6362\u5668\u7c7b\uff0c\u7528\u4e8e\u5c06\u539f\u59cb\u7279\u5f81\u5411\u91cf\u66f4\u6539\u4e3a\u66f4\u9002\u5408\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u7684\u8868\u793a\u5f62\u5f0f\u3002<\/span> <\/p>\n

\n \u7b2c2\u6b65<\/strong> - \u5b9a\u4e49\u6837\u672c\u6570\u636e - \u5bfc\u5165\u5305\u540e\uff0c\u9700\u8981\u5b9a\u4e49\u4e00\u4e9b\u6837\u672c\u6570\u636e\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u6570\u636e\u5e94\u7528\u9884\u5904\u7406\u6280\u672f\u3002\u73b0\u5728\u5c06\u5b9a\u4e49\u4ee5\u4e0b\u6837\u672c\u6570\u636e -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
input_data = np.array<\/span>([2.1, -1.9, 5.5],
                      [-1.5, 2.4, 3.5],
                      [0.5, -7.9, 5.6],
                      [5.9, 2.3, -5.8]])
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7b2c3\u6b65 <\/strong> - \u5e94\u7528\u9884\u5904\u7406\u6280\u672f - \u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5e94\u7528\u9884\u5904\u7406\u6280\u672f\u3002\n <\/div>\n
\n \u4ee5\u4e0b\u90e8\u5206\u63cf\u8ff0\u6570\u636e\u9884\u5904\u7406\u6280\u672f\u3002\n <\/div>\n

\u6570\u636e\u9884\u5904\u7406\u6280\u672f<\/h2>\n
\n \u4e0b\u9762\u4ecb\u7ecd\u6570\u636e\u9884\u5904\u7406\u6280\u672f -\n <\/div>\n
\n \u4e8c\u503c\u5316<\/strong>\n <\/div>\n
\n \u8fd9\u662f\u5f53\u9700\u8981\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u65f6\u4f7f\u7528\u7684\u9884\u5904\u7406\u6280\u672f\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u79cd\u5185\u7f6e\u7684\u65b9\u6cd5\u6765\u4e8c\u503c\u5316\u8f93\u5165\u6570\u636e\uff0c\u6bd4\u5982\u8bf4\u75280.5\u4f5c\u4e3a\u9608\u503c\uff0c\u65b9\u6cd5\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
data_binarized = preprocessing.Binarizer<\/span>(threshold = 0.5).transform<\/span>(input_data)
print(\"\\nBinarized data:\\n\"<\/span>, data_binarized)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\u540e\uff0c\u5c06\u5f97\u5230\u4ee5\u4e0b\u8f93\u51fa\uff0c\u6240\u6709\u9ad8\u4e8e0.5(\u9608\u503c)\u7684\u503c\u5c06\u88ab\u8f6c\u6362\u4e3a1\uff0c\u5e76\u4e14\u6240\u6709\u4f4e\u4e8e0.5\u7684\u503c\u5c06\u88ab\u8f6c\u6362\u4e3a0\u3002\n <\/div>\n
\n \u4e8c\u503c\u5316\u6570\u636e<\/strong>\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
[[ 1. 0. 1.]
[ 0. 1. 1.]
[ 0. 0. 1.]
[ 1. 1. 0.]]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5e73\u5747\u53bb\u9664<\/strong>\n <\/div>\n
\n \u8fd9\u662f\u673a\u5668\u5b66\u4e60\u4e2d\u4f7f\u7528\u7684\u53e6\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u7684\u9884\u5904\u7406\u6280\u672f\u3002 \u57fa\u672c\u4e0a\u5b83\u7528\u4e8e\u6d88\u9664\u7279\u5f81\u5411\u91cf\u7684\u5747\u503c\uff0c\u4ee5\u4fbf\u6bcf\u4e2a\u7279\u5f81\u90fd\u4ee5\u96f6\u4e3a\u4e2d\u5fc3\u3002 \u8fd8\u53ef\u4ee5\u6d88\u9664\u7279\u5f81\u5411\u91cf\u4e2d\u7684\u7279\u5f81\u504f\u5dee\u3002 \u4e3a\u4e86\u5bf9\u6837\u672c\u6570\u636e\u5e94\u7528\u5e73\u5747\u53bb\u9664\u9884\u5904\u7406\u6280\u672f\uff0c\u53ef\u4ee5\u7f16\u5199\u5982\u4e0bPython\u4ee3\u7801\u3002 \u4ee3\u7801\u5c06\u663e\u793a\u8f93\u5165\u6570\u636e\u7684\u5e73\u5747\u503c\u548c\u6807\u51c6\u504f\u5dee -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
print(\"Mean = \"<\/span>, input_data.mean<\/span>(axis <\/span>= 0))
print(\"Std deviation = \"<\/span>, input_data.std<\/span>(axis <\/span>= 0))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u884c\u540e\uff0c\u5c06\u5f97\u5230\u4ee5\u4e0b\u8f93\u51fa -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Mean = [ 1.75 -1.275 2.2]
Std deviation = [ 2.71431391 4.20022321 4.69414529]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c06\u5220\u9664\u8f93\u5165\u6570\u636e\u7684\u5e73\u5747\u503c\u548c\u6807\u51c6\u504f\u5dee -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
data_scaled = preprocessing.scale<\/span>(input_data)
print(\"Mean =\"<\/span>, data_scaled.mean<\/span>(axis=0))
print(\"Std deviation =\"<\/span>, data_scaled.std<\/span>(axis <\/span>= 0))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u884c\u540e\uff0c\u5c06\u5f97\u5230\u4ee5\u4e0b\u8f93\u51fa -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Mean = [ 1.11022302e-16 0.00000000e+00 0.00000000e+00]
Std deviation = [ 1. 1. 1.]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7f29\u653e<\/strong>\n <\/div>\n
\n \u8fd9\u662f\u53e6\u4e00\u79cd\u6570\u636e\u9884\u5904\u7406\u6280\u672f\uff0c\u7528\u4e8e\u7f29\u653e\u7279\u5f81\u5411\u91cf\u3002 \u7279\u5f81\u5411\u91cf\u7684\u7f29\u653e\u662f\u9700\u8981\u7684\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u7279\u5f81\u7684\u503c\u53ef\u4ee5\u5728\u8bb8\u591a\u968f\u673a\u503c\u4e4b\u95f4\u53d8\u5316\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u8bf4\u7f29\u653e\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u5e0c\u671b\u4efb\u4f55\u7279\u5f81\u5408\u6210\u4e3a\u5927\u6216\u5c0f\u3002 \u501f\u52a9\u4ee5\u4e0bPython\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u8f93\u5165\u6570\u636e\u8fdb\u884c\u7f29\u653e\uff0c\u5373\u7279\u5f81\u77e2\u91cf -\n <\/div>\n
\n \u6700\u5c0f\u6700\u5927\u7f29\u653e<\/strong>\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
data_scaler_minmax = preprocessing.MinMaxScaler<\/span>(feature_range=(0,1))
data_scaled_minmax = data_scaler_minmax.fit_transform<\/span>(input_data)
print <\/span>(\"\\nMin <\/span>max scaled data:\\n\"<\/span>, data_scaled_minmax)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u884c\u540e\uff0c\u5c06\u5f97\u5230\u4ee5\u4e0b\u8f93\u51fa -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
[ [ 0.48648649 0.58252427 0.99122807]
[ 0. 1. 0.81578947]
[ 0.27027027 0. 1. ]
[ 1. 0. 99029126 0. ]]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u6b63\u5e38\u5316<\/h2>\n
\n \u8fd9\u662f\u53e6\u4e00\u79cd\u6570\u636e\u9884\u5904\u7406\u6280\u672f\uff0c\u7528\u4e8e\u4fee\u6539\u7279\u5f81\u5411\u91cf\u3002 \u8fd9\u79cd\u4fee\u6539\u5bf9\u4e8e\u5728\u4e00\u4e2a\u666e\u901a\u7684\u5c3a\u5ea6\u4e0a\u6d4b\u91cf\u7279\u5f81\u5411\u91cf\u662f\u5fc5\u8981\u7684\u3002 \u4ee5\u4e0b\u662f\u53ef\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u7684\u4e24\u79cd\u6807\u51c6\u5316 -\n <\/div>\n
\n L1\u6807\u51c6\u5316<\/strong>\n <\/div>\n
\n \u5b83\u4e5f\u88ab\u79f0\u4e3a\u6700\u5c0f\u7edd\u5bf9\u504f\u5dee\u3002 \u8fd9\u79cd\u6807\u51c6\u5316\u4f1a\u4fee\u6539\u8fd9\u4e9b\u503c\uff0c\u4ee5\u4fbf\u7edd\u5bf9\u503c\u7684\u603b\u548c\u5728\u6bcf\u884c\u4e2d\u603b\u662f\u6700\u591a\u4e3a1\u3002 \u5b83\u53ef\u4ee5\u5728\u4ee5\u4e0bPython\u4ee3\u7801\uff0c\u4f7f\u7528\u4e0a\u9762\u7684\u8f93\u5165\u6570\u636e\u6765\u5b9e\u73b0 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
# Normalize data
<\/span> data_normalized_l1 = preprocessing.normalize<\/span>(input_data, norm = 'l1'<\/span>)
print(\"\\nL1 normalized data:\\n\"<\/span>, data_normalized_l1)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4e0a\u9762\u7684\u4ee3\u7801\u884c\u751f\u6210\u4ee5\u4e0b\u8f93\u51fa:\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
L1 normalized data:
[[ 0.22105263 -0.2 0.57894737]
[ -0.2027027 0.32432432 0.47297297]
[ 0.03571429 -0.56428571 0.4 ]
[ 0.42142857 0.16428571 -0.41428571]]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n L2\u6807\u51c6\u5316<\/strong>\n <\/div>\n
\n \u5b83\u4e5f\u88ab\u79f0\u4e3a\u6700\u5c0f\u4e8c\u4e58\u3002\u8fd9\u79cd\u5f52\u6b63\u5e38\u5316\u4fee\u6539\u4e86\u8fd9\u4e9b\u503c\uff0c\u4ee5\u4fbf\u6bcf\u4e00\u884c\u4e2d\u7684\u5e73\u65b9\u548c\u603b\u662f\u6700\u591a\u4e3a1\u3002\u5b83\u53ef\u4ee5\u5728\u4ee5\u4e0bPython\u4ee3\u7801\uff0c\u4f7f\u7528\u4e0a\u9762\u7684\u8f93\u5165\u6570\u636e\u6765\u5b9e\u73b0 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
# Normalize data
<\/span> data_normalized_l2 = preprocessing.normalize<\/span>(input_data, norm = 'l2'<\/span>)
print(\"\\nL2 normalized data:\\n\"<\/span>, data_normalized_l2)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\u884c\u5c06\u751f\u6210\u4ee5\u4e0b\u8f93\u51fa -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
L2 normalized data:
[[ 0.33946114 -0.30713151 0.88906489]
[ -0.33325106 0.53320169 0.7775858 ]
[ 0.05156558 -0.81473612 0.57753446]
[ 0.68706914 0.26784051 -0.6754239 ]]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u6807\u8bb0\u6570\u636e<\/h2>\n
\n \u6211\u4eec\u5df2\u7ecf\u77e5\u9053\uff0c\u67d0\u79cd\u683c\u5f0f\u7684\u6570\u636e\u5bf9\u4e8e\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u662f\u5fc5\u9700\u7684\u3002 \u53e6\u4e00\u4e2a\u91cd\u8981\u7684\u8981\u6c42\u662f\uff0c\u5728\u5c06\u6570\u636e\u4f5c\u4e3a\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u7684\u8f93\u5165\u53d1\u9001\u4e4b\u524d\uff0c\u5fc5\u987b\u6b63\u786e\u6807\u8bb0\u6570\u636e\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u6240\u8bf4\u7684\u5206\u7c7b\uff0c\u90a3\u4e48\u6570\u636e\u4e0a\u4f1a\u6709\u5f88\u591a\u6807\u8bb0\u3002 \u8fd9\u4e9b\u6807\u8bb0\u4ee5\u6587\u5b57\uff0c\u6570\u5b57\u7b49\u5f62\u5f0f\u5b58\u5728\u3002\u4e0esklearn\u4e2d\u7684\u673a\u5668\u5b66\u4e60\u76f8\u5173\u7684\u529f\u80fd\u671f\u671b\u6570\u636e\u5fc5\u987b\u5177\u6709\u6570\u5b57\u6807\u8bb0\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u6570\u636e\u662f\u5176\u4ed6\u5f62\u5f0f\uff0c\u90a3\u4e48\u5b83\u5fc5\u987b\u8f6c\u6362\u4e3a\u6570\u5b57\u3002 \u8fd9\u4e2a\u5c06\u5355\u8bcd\u6807\u7b7e\u8f6c\u6362\u4e3a\u6570\u5b57\u5f62\u5f0f\u7684\u8fc7\u7a0b\u79f0\u4e3a\u6807\u8bb0\u7f16\u7801\u3002\n <\/div>\n
\n \u6807\u8bb0\u7f16\u7801\u6b65\u9aa4<\/strong>\n <\/div>\n
\n \u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5728Python\u4e2d\u5bf9\u6570\u636e\u6807\u8bb0\u8fdb\u884c\u7f16\u7801 -\n <\/div>\n
\n \u7b2c1\u6b65<\/strong> - \u5bfc\u5165\u6709\u7528\u7684\u8f6f\u4ef6\u5305\n <\/div>\n
\n \u5982\u679c\u4f7f\u7528Python\uff0c\u90a3\u4e48\u8fd9\u5c06\u662f\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u7279\u5b9a\u683c\u5f0f(\u5373\u9884\u5904\u7406)\u7684\u7b2c\u4e00\u6b65\u3002 \u5b83\u53ef\u4ee5\u505a\u5230\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
import <\/span>numpy as <\/span>np
from <\/span>sklearn import <\/span>preprocessing
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7b2c2\u6b65<\/strong> - \u5b9a\u4e49\u6837\u672c\u6807\u7b7e\n <\/div>\n
\n \u5bfc\u5165\u5305\u540e\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e9b\u6837\u672c\u6807\u7b7e\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u521b\u5efa\u548c\u8bad\u7ec3\u6807\u7b7e\u7f16\u7801\u5668\u3002 \u73b0\u5728\u5c06\u5b9a\u4e49\u4ee5\u4e0b\u6837\u672c\u6807\u7b7e -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
# Sample input labels
<\/span> input_labels = ['red'<\/span><\/span>,'black'<\/span><\/span>,'red','green'<\/span>,'black','yellow'<\/span>,'white'<\/span>]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7b2c3\u6b65<\/strong> - \u521b\u5efa\u548c\u8bad\u7ec3\u6807\u7b7e\u7f16\u7801\u5668\u5bf9\u8c61\n <\/div>\n
\n \u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u6807\u7b7e\u7f16\u7801\u5668\u5e76\u5bf9\u5176\u8fdb\u884c\u8bad\u7ec3\u3002 \u4ee5\u4e0b\u662fPython\u4ee3\u7801\u7684\u5b9e\u73b0 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
# Creating the label encoder
<\/span> encoder = preprocessing.LabelEncoder<\/span>()
encoder.fit<\/span>(input_labels)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ee5\u4e0b\u662f\u8fd0\u884c\u4e0a\u9762\u7684Python\u4ee3\u7801\u540e\u7684\u8f93\u51fa -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
LabelEncoder()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7b2c4\u6b65<\/strong> - \u901a\u8fc7\u7f16\u7801\u968f\u673a\u6392\u5e8f\u5217\u8868\u6765\u68c0\u67e5\u6027\u80fd\n <\/div>\n
\n \u6b64\u6b65\u9aa4\u53ef\u7528\u4e8e\u901a\u8fc7\u7f16\u7801\u968f\u673a\u6392\u5e8f\u5217\u8868\u6765\u68c0\u67e5\u6027\u80fd\u3002 \u4e0b\u9762\u7684Python\u4ee3\u7801\u53ef\u4ee5\u505a\u540c\u6837\u7684\u4e8b\u60c5 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
# encoding a set of labels
<\/span> test_labels = ['green'<\/span>,'red'<\/span>,'black'<\/span>]
encoded_values = encoder.transform<\/span>(test_labels)
print(\"\\nLabels =\"<\/span>, test_labels)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6807\u7b7e\u5c06\u5982\u4e0b\u6253\u5370 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Labels = ['green'<\/span>, 'red'<\/span>, 'black'<\/span>]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u53ef\u4ee5\u5f97\u5230\u7f16\u7801\u503c\u5217\u8868\uff0c\u5373\u5c06\u6587\u5b57\u6807\u7b7e\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u5982\u4e0b\u6240\u793a -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
print(\"Encoded values =\"<\/span>, list(encoded_values))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa\u7ed3\u679c\u6253\u5370\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Encoded values = [1, 2, 0]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7b2c5\u6b65<\/strong> - \u901a\u8fc7\u89e3\u7801\u4e00\u7ec4\u968f\u673a\u6570\u6765\u68c0\u67e5\u6027\u80fd -\n <\/div>\n
\n \u901a\u8fc7\u5bf9\u968f\u673a\u6570\u5b57\u96c6\u8fdb\u884c\u89e3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b64\u6b65\u9aa4\u6765\u68c0\u67e5\u6027\u80fd\u3002 \u4e0b\u9762\u7684Python\u4ee3\u7801\u4e5f\u53ef\u4ee5\u505a\u540c\u6837\u7684\u4e8b\u60c5 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
# decoding a set of values
<\/span> encoded_values = [3,0,4,1]
decoded_list = encoder.inverse_transform<\/span>(encoded_values)
print(\"\\nEncoded values =\"<\/span>, encoded_values)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u5c06\u88ab\u6253\u5370\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Encoded values = [3, 0, 4, 1]
print(\"\\nDecoded labels =\"<\/span>, list(decoded_list))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u89e3\u7801\u503c\u5c06\u88ab\u6253\u5370\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Decoded labels = ['white'<\/span>, 'black'<\/span>, 'yellow'<\/span>, 'green'<\/span>]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6807\u8bb0\u4e0e\u672a\u6807\u8bb0\u6570\u636e<\/strong>\n <\/div>\n
\n \u672a\u6807\u8bb0\u7684\u6570\u636e\u4e3b\u8981\u7531\u81ea\u7136\u6216\u4eba\u9020\u7269\u4f53\u7684\u6837\u672c\u7ec4\u6210\uff0c\u8fd9\u4e9b\u6837\u672c\u53ef\u4ee5\u5f88\u5bb9\u6613\u4ece\u73b0\u5b9e\u4e16\u754c\u4e2d\u83b7\u5f97\u3002 \u5b83\u4eec\u5305\u62ec\u97f3\u9891\uff0c\u89c6\u9891\uff0c\u7167\u7247\uff0c\u65b0\u95fb\u6587\u7ae0\u7b49\u3002\n <\/div>\n
\n \u53e6\u4e00\u65b9\u9762\uff0c\u5e26\u6807\u7b7e\u7684\u6570\u636e\u91c7\u7528\u4e00\u7ec4\u672a\u6807\u8bb0\u7684\u6570\u636e\uff0c\u5e76\u7528\u4e00\u4e9b\u6709\u610f\u4e49\u7684\u6807\u7b7e\u6216\u6807\u7b7e\u6216\u7c7b\u6765\u6269\u5145\u6bcf\u7247\u672a\u6807\u8bb0\u7684\u6570\u636e\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u6709\u7167\u7247\uff0c\u90a3\u4e48\u6807\u7b7e\u53ef\u4ee5\u57fa\u4e8e\u7167\u7247\u7684\u5185\u5bb9\u653e\u7f6e\uff0c\u5373\u5b83\u662f\u7537\u5b69\u6216\u5973\u5b69\u6216\u52a8\u7269\u6216\u5176\u4ed6\u4efb\u4f55\u7167\u7247\u3002 \u6807\u8bb0\u6570\u636e\u9700\u8981\u4eba\u7c7b\u4e13\u4e1a\u77e5\u8bc6\u6216\u5224\u65ad\u4e00\u4e2a\u7ed9\u5b9a\u7684\u672a\u6807\u8bb0\u6570\u636e\u3002\n <\/div>\n
\n \u6709\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u65e0\u6807\u7b7e\u6570\u636e\u4e30\u5bcc\u4e14\u5bb9\u6613\u83b7\u5f97\uff0c\u4f46\u6807\u6ce8\u6570\u636e\u901a\u5e38\u9700\u8981\u4eba\u5de5\/\u4e13\u5bb6\u8fdb\u884c\u6ce8\u91ca\u3002 \u534a\u76d1\u7763\u5b66\u4e60\u5c1d\u8bd5\u5c06\u6807\u8bb0\u6570\u636e\u548c\u672a\u6807\u8bb0\u6570\u636e\u7ec4\u5408\u8d77\u6765\uff0c\u4ee5\u5efa\u7acb\u66f4\u597d\u7684\u6a21\u578b\u3002\n <\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"\u4eba\u5de5\u667a\u80fd\u6570\u636e\u51c6\u5907zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[],"class_list":["post-1309","post","type-post","status-publish","format-standard","hentry","category-rgznjc1"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1309"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1309"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1309\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}