from __future__ import unicode_literals
import sklearn.preprocession as sp
raw_samples = np.array([
[3,-1.5,2,-5.4],
[0.4.-0.3,2.1],
[1,3.3,-1.9,-4.3]
])
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([],dtype=int)
for i,key in enumerate(raw_sample):
ohe_sample = np.hstack((
ohe_samples,code_tables[i][key]
))
ohe_samples.append(ohe_sample)
ohe_samples = np.array(ohe_samples)
print(ohe_samples)
ohe = sp.OneHotEncoder(sparse = False,dtype=int)
ohe_samples = ohe.fit_transform(raw_samples)
print(ohe_samples)
ML6: Sklearn OneHotencoding
最新推荐文章于 2023-11-22 13:18:06 发布