Python:如何将numpy数组中的整数数据与另一个numpy数组中的整数数据进行比较,并将结果读入.txt文件?
我有动物位置(X,Y,Z)数据和树数据(X,Y,Z)。我需要提取动物位置周围发生的所有XYZ树输入-因此我需要将包含XYZ动物位置的numpy数组与包含同一区域中树的x y z点位置的numpy数组进行比较。我想把所有的xyz树拉到半径为4个单位的点上,并编写了一个函数来实现这一点。但事实上,它并不仅仅是拉着动物周围的树木。它只是打印所有可能的树。我怎样才能只拉动物点周围的树,然后把它们放进一个.txt文件,我可以在另一个程序中使用?我是编程新手,非常感谢您对我的帮助 以下是我的代码和#说明:Python:如何将numpy数组中的整数数据与另一个numpy数组中的整数数据进行比较,并将结果读入.txt文件?,python,arrays,python-3.x,numpy,compare,Python,Arrays,Python 3.x,Numpy,Compare,我有动物位置(X,Y,Z)数据和树数据(X,Y,Z)。我需要提取动物位置周围发生的所有XYZ树输入-因此我需要将包含XYZ动物位置的numpy数组与包含同一区域中树的x y z点位置的numpy数组进行比较。我想把所有的xyz树拉到半径为4个单位的点上,并编写了一个函数来实现这一点。但事实上,它并不仅仅是拉着动物周围的树木。它只是打印所有可能的树。我怎样才能只拉动物点周围的树,然后把它们放进一个.txt文件,我可以在另一个程序中使用?我是编程新手,非常感谢您对我的帮助 以下是我的代码和#说明:
#using array instead of dataframe
import numpy as np
from laspy.file import File
#load and consolidate Veg Point Coordinates into one array
VegList = sorted(glob.glob('/Users/sophiathompson/Desktop/copys/Clips/*.las'))
VegListCoords = []
for f in VegList:
print(f)
Veg= File(filename = f, mode = "r") # Open the file # Eventually, this will need to be the actual .laz files
VegListCoords.append(np.vstack((Veg.x, Veg.y, Veg.z)).transpose())
print (VegListCoords)
XYZVegComplete = np.concatenate((VegListCoords), axis = 0)
#Load animal point locations (x, y, z .csv file) from clip 240967 into array
Animal240967 = np.loadtxt(fname='/Users/ST/Desktop/copys/CatTXTfiles/240967_CatsFt.csv', delimiter =',')
#Use to find all vegetation heights in a 4 unit diameter of each animal point (animalx, animaly). #'d out lines of code are my attempts to make something work
def near_Animal(animalx, animaly):
for x, y, z in XYZVegComplete:
r=2 #xy coordinates in ft
PointsNearAnml = []
if (x-animalx)**2 + (y-animaly)**2 >= r**2:
PracticeTxt=open("/Users/ST/Desktop/practicefilecreate.txt", "w")
print (x, y, z)
#print (x, y, z) >> PracticeTxt, x, y, z
#PracticeTxt.write('%d %d %d \n' % Points)
#PracticeTxt.write('%d %d %d \n' % x y z)
#Points= (x, y, z)
#with open("/Users/sophiathompson/Desktop/practicefilecreate.txt", "w") as PracticeTxt:
#print >> PracticeTxt, Points
#PracticeTxt.close
#Use to call near_Animal: gather Veg Points based on proximity to animal points (using arrays)-
for animalx, animaly in Animal240967:
near_Animal(animalx, animaly)
实际上,您需要两个不同的函数,这取决于您是在一个集合内测量还是在集合之间测量。这里有一个函数同时执行这两个功能,并对它所执行的操作进行了一些解释:
from scipy.spatial.distance import pdist, cdist
def near_fn(*args, dist = 2., outfile = "practicefilecreate.txt"):
if len(args) == 1: #within a set, i.e. `Animal240967`
coords = args[0]
i, j = np.triu_indices(coords.size, 1)
mask = pdist(coords, 'euclidean') < dist
k = np.unique(np.r_[i[mask], j[mask]])
np.savetxt(coords[k], outfile, delimiter = ',') #or whatever you want
elif len(args) == 2: # between sets i.e. `XYZVegComplete` and `Animal240967`
coords_ind, coords_dep = args
k = np.any(cdist(coords_ind, coords_dep, 'euclidean') < dist, axis = 1)
np.savetxt(coords_ind[k], outfile, delimiter = ',') #or whatever you want
else:
assert False, "Too many inputs"
实际上,您需要两个不同的函数,这取决于您是在一个集合内测量还是在集合之间测量。这里有一个函数同时执行这两个功能,并对它所执行的操作进行了一些解释:
from scipy.spatial.distance import pdist, cdist
def near_fn(*args, dist = 2., outfile = "practicefilecreate.txt"):
if len(args) == 1: #within a set, i.e. `Animal240967`
coords = args[0]
i, j = np.triu_indices(coords.size, 1)
mask = pdist(coords, 'euclidean') < dist
k = np.unique(np.r_[i[mask], j[mask]])
np.savetxt(coords[k], outfile, delimiter = ',') #or whatever you want
elif len(args) == 2: # between sets i.e. `XYZVegComplete` and `Animal240967`
coords_ind, coords_dep = args
k = np.any(cdist(coords_ind, coords_dep, 'euclidean') < dist, axis = 1)
np.savetxt(coords_ind[k], outfile, delimiter = ',') #or whatever you want
else:
assert False, "Too many inputs"
使用您提供的代码并不容易。考虑创建具有代表性示例数据和预期输出的A。这样你就更有可能更快地得到更好的答案。使用你提供的代码并不容易。考虑创建具有代表性示例数据和预期输出的A。这样你就更有可能更快地得到更好的答案。丹尼尔F非常感谢你的回答。我非常感谢您对每个步骤所做的详细说明。作为一名计算机编程新手,这对我来说是一次令人惊讶的教育。请随意投票并检查答案,因为这是最有帮助的:)我一直在尝试使用KDTree,但未能使其发挥作用。我不确定我把动物点数放在哪里了。我是否将它们视为KDTree args中的参数?例如。在定义def_kd_near_fn(*args…..我会在这里定义animalx,animaly吗?),然后在树中引用它们=[KDTree(arg animalx,animaly]然后i,j对将与我试图从中提取的基于AnimalXY对的植被文件的X,Y对相关联?您将放入一个或两个点数组,这取决于您是否要查找一个数组内的距离或两个数组之间的距离。
*args
可以是任意数量的参数,在这种情况下,我将其限制为only 1或2.Daniel F非常感谢您的回答。我非常感谢您对每个步骤的详细说明和说明。作为一名计算机编程新手,这对我来说是一次令人惊讶的教育。请随意投票并检查答案,因为这是最有帮助的:)我一直在尝试使用KDTree,但未能使其正常工作。我不确定我把动物点数放在哪里了。我是否将它们视为KDTree args中的参数?例如。在定义def_kd_near_fn(*args…..我会在这里定义animalx,animaly吗?),然后在树中引用它们=[KDTree(arg animalx,animaly]然后i,j对将与我试图从中提取的基于AnimalXY对的植被文件的X,Y对相关联?您将放入一个或两个点数组,这取决于您是否要查找一个数组内的距离或两个数组之间的距离。*args
可以是任意数量的参数,在这种情况下,我将其限制为only 1或2。