Pregunta

In R this code works fine:

denom <- matrix(c(0.1125588, 2.1722226, 0.5424582, 1.1727604,3.0524269 ,0.0524625 ,0.1752363 ,0.7198743,0.7282291 ,0.1646349 ,0.7574503, 2.3496857),3,4,byrow=TRUE)
indexM <- apply(denom,1,function(x) which(x==max(x)))
indexM
# 2 1 4

Is this close to the Python equivalent? It doesn't work (AttributeError: 'tuple' object has no attribute 'ndim')

denom = np.array([[0.1125588, 2.1722226, 0.5424582], [1.1727604,3.0524269 ,0.0524625] ,[0.1752363 ,0.7198743,0.7282291] ,[0.1646349 ,0.7574503, 2.3496857]]);
indexM = np.apply_over_axes(lambda x,y: np.where(x == x.max(axis=1)) ,denom, axes=(0) );
¿Fue útil?

Solución

You are creating different data in R and Python. In R you create data with 3 rows and 4 columns, but in Python you create data with 4 rows and 3 columns.

Once that is fixed, to get what you want you can just do denom.argmax(axis=1):

denom = np.array([
   [0.1125588, 2.1722226, 0.5424582, 1.1727604],
   [3.0524269 ,0.0524625,0.1752363 ,0.7198743],
   [0.7282291,0.1646349 ,0.7574503, 2.3496857]
])

>>> denom.argmax(axis=1)
array([1, 0, 3], dtype=int64)

(The numbers in the result are different because Python uses 0-based indexing and R uses 1-based indexing, but they refer to the same positions.)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top