I have a set of labels from 0to 9, for example:
2 7 5 3
I would like to convert this to one-time encoding, for example:
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0 0
So, I made this method:
def make_one_hot(m):
result = pd.DataFrame([])
for i in range(0, len(m)):
x = [0] * 10
x[m[i]] = 1
result = result.append(x)
print("result: " + result)
return result
When printing the result, I get this error:
Traceback (most recent call last):
File "../src/script.py", line 23, in <module>
train_labels = make_one_hot(train_data.ix[:,0])
File "../src/script.py", line 18, in make_one_hot
print("result: " + result)
File "/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py", line 1241, in f
8.8s
2
return self._combine_const(other, na_op)
File "/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py", line 3641, in _combine_const
raise_on_error=raise_on_error)
File "/opt/conda/lib/python3.6/site-packages/pandas/core/internals.py", line 3197, in eval
return self.apply('eval', **kwargs)
File "/opt/conda/lib/python3.6/site-packages/pandas/core/internals.py", line 3091, in apply
applied = getattr(b, f)(**kwargs)
File "/opt/conda/lib/python3.6/site-packages/pandas/core/internals.py", line 1205, in eval
8.8s
3
repr(other))
TypeError: Could not compare ['result: '] with block values
Since I am poorly versed in Python, I am not sure that only the print statement is incorrect, as well as how I compute the array.
So what is the simple and right way to do this?