I am trying to take the following R statement and convert it to Python using NumPy:
1 + apply(tmp,1,function(x) length(which(x[1:k] < x[k+1])))
Is there a Python equivalent to which()? Here, x is row in matrix tmp, and k corresponds to the number of columns in another matrix.
Previously, I tried the following Python code, and received a Value Error (operands could not be broadcast together with shapes):
for row in tmp:
print np.where(tmp[tmp[:,range(k)] < tmp[:,k]])
tmp
twice.. do you mean to userow
instead inside the loop? – Zubkoffk
come from? What shape is yourtmp
? – Visionallength(which(...))
, we only need to directly sum the Booleanssum(x[1:k] < x[k+1])
. – Linnwhich()
compares a vector against a condition and returns a vector of the indices which meet it. I think this post answers the literal question asked here. – Henhouse