I am trying to create a new array, which will consist of (data value <=20000) from the first column, along with all other corresponding columns.
Mathematically I am doing the following: I am reading data from a text file. I am finding distance to all the points from the last point. Then I will take only those rows with distances less than 20000 and M1 less than 11.5. The data looks as follows:
# ID M1 M2 M3 M4 R4 M5 R5 x y z
10217 11.467 11.502 13.428 13.599 432.17 13.266 281.06 34972.8 42985.9 14906
7991 11.529 11.559 13.438 13.520 435.23 13.224 272.23 8538.05 33219.8 43375.1
2100 11.526 11.573 13.478 13.490 448.97 13.356 301.27 9371.75 13734.1 43398.6
9467 11.557 11.621 13.481 13.537 449.99 13.367 303.67 33200.3 36008.9 12735.8
my code looks the following:
import numpy as np
import matplotlib.pyplot as plt
halo = 'nntest.txt'
ID, m,r,x,y,z= np.loadtxt(halo, usecols=(0,6,7,8,9,10), unpack =True)
# selet the last point
m_mass = m[-1:]
ID_2 = ID[-1:]
r_2 = r[-1:]
x_2 = x[-1:]
y_2 = y[-1:]
z_2 = z[-1:]
#######################################
#Find distance to all points from our targeted point
nearest_neighbors = []
def neighbors(ID_2, cx,cy,cz, ID, m, r, x, y, z):
dist = np.sqrt((cx-x)**2 + (cy-y)**2 + (cz-z)**2)
return dist, ID, m, r, x, y, z
for i in range(len(ID_2)):
hist = neighbors(ID_2[i], m_2[i], r_2[i], x_2[i], y_2[i], z_2[i], ID, m , r, x, y, z)
#print all the IDs and all other data which are less than 20000 and M less than 11.5 of that targeted value
print ID[hist[0]<20000] and m[hist[1]<11.5]
But I am having problem with setting the 2 conditions. It returns me this error:
File "overlaping_halos_finder.py", line 53, in <module>
combined = zip(ID[hist[0]<r_2[i] and m[hist[1]>1.e12]])
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
if instead of
print ID[hist[0]<20000] and m[hist[1]<11.5]
I do only:
print ID[hist[0]<20000]
I have following sample output:
# ID M R X Y Z
6737.0 909913272632.0 103.06 1988.35 15894.6 40649.0
6617.0 997700063823.0 106.28 1523.55 15433.2 40363.2
6723.0 11 109.91 1993.05 15687.5 40557.2
But I want to get rid of those first two outputs and only want to print the 3 rd row where M column value <11.5
hope it clarifies a little
Your suggestion to fix this issue will be very helpful
setting the 2 conditionsdoesn't explain what you are trying to achieve. Ideally, you can show us an example of 2 small arrays and desired output.hist[0]<20000andhist[1]<11.5respectively? As far as I can seeIDandmare 2 separate arrays.ID[(hist[0]<20000) & (hist[1]<11.5)]?