{"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
\u4eba\u5de5\u667a\u80fd\u6570\u636e\u51c6\u5907\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\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>\u9884\u5904\u7406\u6570\u636e<\/h2>\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 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 # 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\u6570\u636e\u9884\u5904\u7406\u6280\u672f<\/h2>\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 # 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 # 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 # 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 # 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 # 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 # 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 # 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
# 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 # 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 # 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 # 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
# 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 # 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 # 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