Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy np.where multiple condition

I need to work with multiple condition using numpy.

I'm trying this code that seem to work.

My question is: There is another alternative that can do the same job?

Mur=np.array([200,246,372])*pq.kN*pq.m
Mumax=np.array([1400,600,700])*pq.kN*pq.m
Mu=np.array([100,500,2000])*pq.kN*pq.m
Acreq=np.where(Mu<Mur,0,"zero")
Acreq=np.where(((Mur<Mu)&(Mu<Mumax)),45,Acreq)
Acreq=np.where(Mu>Mumax,60,Acreq)
Print(Acreq)
['0' '45' '60']
like image 227
Eduardo Avatar asked Mar 12 '23 05:03

Eduardo


1 Answers

Starting with this:

Mur    = np.array([200,246,372])*3*5
Mumax  = np.array([1400,600,700])*3*5
Mu     = np.array([100,500,2000])*3*5
Acreq  = np.where(Mu<Mur,0,"zero")
Acreq  = np.where((Mur<Mu)&(Mu<Mumax),45,Acreq)
Acreq  = np.where(Mu>Mumax,60,Acreq)

print(Acreq)

['0' '45' '60']

Try this:

conditions  = [Mu<Mur, (Mur<Mu)&(Mu<Mumax), Mu>Mumax ]
choices     = [ 0, 45, 60 ]
Acreq       = np.select(conditions, choices, default='zero')
print(Acreq)


['0' '45' '60']

This also works:

np.where((Mur<Mu)&(Mu<Mumax),45,np.where(Mu>Mumax,60,np.where(Mu<Mur,0,"zero")))
like image 108
Merlin Avatar answered Mar 19 '23 18:03

Merlin