Create a hot panda data framework

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?

+4
source share
2 answers

Approach # 1: Here is one approach with NumPy broadcasting-

In [143]: a = [2 ,7 ,5 ,3]

In [144]: pd.DataFrame((np.asarray(a)[:,None] == np.arange(10)).astype(int))
Out[144]: 
   0  1  2  3  4  5  6  7  8  9
0  0  0  1  0  0  0  0  0  0  0
1  0  0  0  0  0  0  0  1  0  0
2  0  0  0  0  0  1  0  0  0  0
3  0  0  0  1  0  0  0  0  0  0

Approach # 2: Another with zeros-initialization-

In [145]: out = np.zeros((len(a), 10),dtype=int)

In [146]: out[np.arange(len(a)), a] = 1

In [147]: pd.DataFrame(out)
Out[147]: 
   0  1  2  3  4  5  6  7  8  9
0  0  0  1  0  0  0  0  0  0  0
1  0  0  0  0  0  0  0  1  0  0
2  0  0  0  0  0  1  0  0  0  0
3  0  0  0  1  0  0  0  0  0  0

Approach # 3: Using Scipy Sparse Matrices -

In [166]: from scipy.sparse import csr_matrix

In [167]: n = len(a)

In [169]: pd.DataFrame(csr_matrix(([1]*n, (range(n), a)), shape=(n, 10)).toarray())
Out[169]: 
   0  1  2  3  4  5  6  7  8  9
0  0  0  1  0  0  0  0  0  0  0
1  0  0  0  0  0  0  0  1  0  0
2  0  0  0  0  0  1  0  0  0  0
3  0  0  0  1  0  0  0  0  0  0
+4
source

pandas, pd.get_dummies?

a = [2, 7, 5, 3]

pd.get_dummies(a)
Out: 
   | 2 | 3 | 5 | 7
---|---|---|---|---
 0 | 1 | 0 | 0 | 0
 1 | 0 | 0 | 0 | 1
 2 | 0 | 0 | 1 | 0
 3 | 0 | 1 | 0 | 0
0

Source: https://habr.com/ru/post/1680318/


All Articles