emax emax - 3 days ago 5
Python Question

Python: how to map ID to matrix indices?

I have to convert an a number

ID
to index
i
and
j
of a matrix
M
of which I know the dimension. The expression is the following:

s = shape(M)
j = (ID - 1) % s[0]
i = np.int((ID - 1 - j) / s[0])


I have to save values coming from a dataframe
df
and each type I repeat the following:

M = np.zeros((m, n))
s = shape(M)
for k in df.index:
ID = df['Id'][k]
y = (ID - 1) % s[0]
x = np.int((ID - 1 - y) / s[0])
M[x,y] = M[x,y] + df['R'][k]


I am wondering there is a way to avoid the loop each time and save computational time.

Answer

Essentially, you can directly obtain the matrix M from a Series of R with MultiIndex i, j using .unstack():

import numpy as np
import pandas as pd

M = pd.DataFrame(index=range(4), columns=range(4))
s = M.shape

df = pd.DataFrame({
    'ID': np.arange(4*4),
    'R':  np.random.rand(4*4),
    })


df['j'] = df.ID % s[0]
df['i'] = np.floor(df.ID / s[0]).astype(np.int)

#In [26]: q.df
Out[26]:
#     ID         R  j  i
# 0    0  0.847475  0  0
# 1    1  0.343867  1  0
# 2    2  0.806913  2  0
# 3    3  0.105036  3  0
# 4    4  0.170887  0  1
# 5    5  0.782607  1  1
# 6    6  0.377907  2  1
# 7    7  0.586738  3  1
# 8    8  0.895262  0  2
# 9    9  0.290358  1  2
# 10  10  0.109228  2  2
# 11  11  0.423267  3  2
# 12  12  0.454464  0  3
# 13  13  0.604296  1  3
# 14  14  0.603850  2  3
# 15  15  0.974695  3  3

df.set_index(['i', 'j'], inplace=True)

M = df['R'].unstack()

# In [30]: q.M
# Out[30]:
# j         0         1         2         3
# i
# 0  0.975895  0.506286  0.055459  0.551988
# 1  0.846078  0.114153  0.981231  0.681130
# 2  0.142864  0.050597  0.323655  0.918745
# 3  0.734922  0.951795  0.209542  0.547859
Comments