Python:如何比较numpy数组中的整数数据和另一个numpy数组中的整数数据,并将结果读入一个.txt文件?

2024-10-01 22:31:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有动物位置(X,Y,Z)数据和树数据(X,Y,Z)。我需要提取所有出现在动物位置周围的XYZ树输入-所以我需要将包含XYZ动物位置的numpy数组与包含同一区域中树的XYZ点位置的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)

Tags: toinnumpynpuserspointsprint动物
1条回答
网友
1楼 · 发布于 2024-10-01 22:31:52

实际上,你需要两个不同的函数,这取决于你是在一个集合内还是在两个集合之间进行测量。这两个功能的解释是:

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"

它的作用是:

  1. ^{}查找集合元素之间的距离,但只找到值的右上三角(因为a->bb->a之间的距离相同,a->总是0)。这是一个一维数组,它对应于^{}给出的位置。找到小于阈值的距离(pdist < dist),将其映射到索引(k = i[...]),并将与这些索引对应的坐标写入磁盘(^{}

  2. ^{}查找两个集合之间的距离,如二维矩阵。找到小于阈值(cdist(ind, dep, . . . ) < dist)的元素,找到其中包含True的任何列(^{}),然后再次将所有坐标写入磁盘(np.savetxt(ind[k] . . .)

如果有很多值,可能需要使用^{}

^{pr2}$

相关问题 更多 >

    热门问题