`# -- coding: utf-8 --
from future import unicode_literals
import numpy as np
import sklearn.preprocessing as sp
raw_samples = np.array([
[1, 3, 2],
[7, 5, 4],
[1, 8, 6],
[7, 3, 9]])
print(raw_samples)
底层原理
code_tables = []
先创建一个字典,来保存原始数据,编译的结果数据,用键值对来表示最好
一个列一个字典,那么就是一个字典列表了
for col in raw_samples.T:
code_table = {}
for val in col:
code_table[val] = None
code_tables.append(code_table)
给字典取值
for code_table in code_tables:
size = len(code_table)
for one, key in enumerate(sorted(code_table.keys())):
code_table[key] = np.zeros(shape=size, dtype=int)
code_table[key][one] = 1
ohe_samples = []
下面用到了列表里面嵌套列表
开始编码
for raw_sample in raw_samples:
ohe_sample = np.array([], dtyp