大师兄的数据分析学习笔记(十二):特征预处理(三)

2022-06-10  本文已影响0人  superkmi

大师兄的数据分析学习笔记(十一):特征预处理(二)
大师兄的数据分析学习笔记(十三):特征预处理(四)

四、特征变换

1. 对指化
1.1 指数化
>>>import numpy as np

>>>nums = np.arange(0.1,1, 0.1)
>>>print(nums)
[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
>>>print(np.exp(nums))
[1.10517092 1.22140276 1.34985881 1.4918247  1.64872127 1.8221188
 2.01375271 2.22554093 2.45960311]
1.2 对数化
>>>import numpy as np

>>>nums = np.arange(100000, 1000000, 100000)
>>>print(nums)
[100000 200000 300000 400000 500000 600000 700000 800000 900000]
>>>print(np.log(nums))
[11.51292546 12.20607265 12.61153775 12.89921983 13.12236338 13.30468493
 13.45883561 13.59236701 13.71015004]
2. 离散化
  1. 为了克服数据本身的缺陷,比如派出连续数据中的噪声,可以离散后用一部分去做比较。
  2. 某些算法的属性必须是离散值,比如朴素贝叶斯算法。
  3. 数据的非线性映射需求,比如连续数据中心明显的拐点或不同的数值区间,可能代表不同的意义。
2.1 等身分箱
>>>import numpy as np
>>>import pandas as pd

>>>data = np.random.randint(1,100,20)
>>>print(data)
[60 61 74 29 21 88 39 95 45 61 15 85 21 52 30 21 97 69 48 40]
>>>pprint.pprint(list(pd.qcut(data,q=3)))
[Interval(39.333, 61.0, closed='right'),
 Interval(39.333, 61.0, closed='right'),
 Interval(61.0, 97.0, closed='right'),
 Interval(14.999, 39.333, closed='right'),
 Interval(14.999, 39.333, closed='right'),
 Interval(61.0, 97.0, closed='right'),
 Interval(14.999, 39.333, closed='right'),
 Interval(61.0, 97.0, closed='right'),
 Interval(39.333, 61.0, closed='right'),
 Interval(39.333, 61.0, closed='right'),
 Interval(14.999, 39.333, closed='right'),
 Interval(61.0, 97.0, closed='right'),
 Interval(14.999, 39.333, closed='right'),
 Interval(39.333, 61.0, closed='right'),
 Interval(14.999, 39.333, closed='right'),
 Interval(14.999, 39.333, closed='right'),
 Interval(61.0, 97.0, closed='right'),
 Interval(61.0, 97.0, closed='right'),
 Interval(39.333, 61.0, closed='right'),
 Interval(39.333, 61.0, closed='right')]
>>>print(list(pd.qcut(data,q=3,labels=["l","m","h"])))
['m', 'm', 'h', 'l', 'l', 'h', 'l', 'h', 'm', 'm', 'l', 'h', 'l', 'm', 'l', 'l', 'h', 'h', 'm', 'm']
2.2 等距分箱
>>>import numpy as np
>>>import pandas as pd

>>>data = np.random.randint(1,100,20)
>>>print(data)
[93 83 60 87 93 46 69 84 97 60 54 57 18 48 79 97 73 64 17 76]
>>>pprint.pprint(list(pd.cut(data,bins=3)))
[Interval(70.333, 97.0, closed='right'),
 Interval(70.333, 97.0, closed='right'),
 Interval(43.667, 70.333, closed='right'),
 Interval(70.333, 97.0, closed='right'),
 Interval(70.333, 97.0, closed='right'),
 Interval(43.667, 70.333, closed='right'),
 Interval(43.667, 70.333, closed='right'),
 Interval(70.333, 97.0, closed='right'),
 Interval(70.333, 97.0, closed='right'),
 Interval(43.667, 70.333, closed='right'),
 Interval(43.667, 70.333, closed='right'),
 Interval(43.667, 70.333, closed='right'),
 Interval(16.92, 43.667, closed='right'),
 Interval(43.667, 70.333, closed='right'),
 Interval(70.333, 97.0, closed='right'),
 Interval(70.333, 97.0, closed='right'),
 Interval(70.333, 97.0, closed='right'),
 Interval(43.667, 70.333, closed='right'),
 Interval(16.92, 43.667, closed='right'),
 Interval(70.333, 97.0, closed='right')]
>>>print(list(pd.cut(data,bins=3,labels=["l","m","h"])))
['h', 'h', 'm', 'h', 'h', 'm', 'm', 'h', 'h', 'm', 'm', 'm', 'l', 'm', 'h', 'h', 'h', 'm', 'l', 'h']
3. 归一化
  1. 可以直接观察单个数据仙姑低于整体情况的比例。
  2. 可以在不同量纲的数据特征之间建立合适的距离度量方法。
>>>import os
>>>import numpy as np
>>>import pandas as pd
>>>from sklearn.preprocessing import MinMaxScaler

>>>df = pd.read_csv(os.path.join(".", "data", "WA_Fn-UseC_-HR-Employee-Attrition.csv"))
>>>age = df.Age
>>>print(MinMaxScaler().fit_transform(np.array(age).reshape(-1,1)))
[[0.54761905]
 [0.73809524]
 [0.45238095]
 ...
 [0.21428571]
 [0.73809524]
 [0.38095238]]
4. 标准化
>>>import os
>>>import numpy as np
>>>import pandas as pd
>>>from sklearn.preprocessing import MinMaxScaler,StandardScaler

>>>df = pd.read_csv(os.path.join(".", "data", "WA_Fn-UseC_-HR-Employee-Attrition.csv"))
>>>age = df.Age
>>>print(StandardScaler().fit_transform(np.array(age).reshape(-1,1)))
[[ 0.4463504 ]
 [ 1.32236521]
 [ 0.008343  ]
 ...
 [-1.08667552]
 [ 1.32236521]
 [-0.32016256]]
5. 数值化
定类数据 定序数据 定距数据 定比数据
无大小关系 大小关系不好衡量 大小关系可以衡量,但无零点 大小关系可以衡量,有零点
无法进行加减法运算 无法进行加减法运算 有加减法运算,但无乘除法运算 有加减乘除法运算
>>>import os
>>>import pandas as pd
>>>from sklearn.preprocessing import LabelEncoder

>>>df = pd.read_csv(os.path.join(".", "data", "WA_Fn-UseC_-HR-Employee-Attrition.csv"))
>>>department = df.Department
>>>print(list(department))
['Sales', 'Research & Development', 'Research & Development', ...,'Research & Development', 'Research & Development']
>>>label = LabelEncoder().fit_transform(department)
>>>print(label)
[2 1 1 ... 1 2 1]
>>>import os
>>>import pandas as pd
>>>import numpy as np
>>>from sklearn.preprocessing import LabelEncoder, OneHotEncoder

>>>df = pd.read_csv(os.path.join(".", "data", "WA_Fn-UseC_-HR-Employee-Attrition.csv"))
>>>ef = np.array(df.EducationField).reshape(-1, 1)
>>>encoder = LabelEncoder()
>>>lb_trans = encoder.fit_transform(ef)
>>>oht_encoder = OneHotEncoder().fit(lb_trans.reshape(-1,1))
>>>oht = oht_encoder.transform(encoder.transform(np.array(ef)).reshape(-1, 1))
>>>print(oht.toarray())
[[0. 1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0.]
 ...
 [0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0. 0.]]
6. 正规化
  1. 直接用在特征上。
  2. 用在每个对象的各个特征的表示(特征矩阵的行)。
  3. 模型的参数。
>>>import os
>>>import pandas as pd
>>>import numpy as np
>>>from sklearn.preprocessing import Normalizer

>>>df = pd.read_csv(os.path.join(".", "data", "WA_Fn-UseC_-HR-Employee-Attrition.csv"))
>>>es = np.array([df.EnvironmentSatisfaction])
>>>l1 = Normalizer(norm="l1").fit_transform(es)
>>>print(l1)
[[0.00049988 0.00074981 0.00099975 ... 0.00049988 0.00099975 0.00049988]]
>>>l2 = Normalizer(norm="l2").fit_transform(es)
>>>print(l2)
[[0.01778568 0.02667853 0.03557137 ... 0.01778568 0.03557137 0.01778568]]
上一篇下一篇

猜你喜欢

热点阅读