gpt4 book ai didi

python - 从 numpy 数组中删除子数组

转载 作者:太空宇宙 更新时间:2023-11-04 03:41:58 25 4
gpt4 key购买 nike

我正在尝试使用 numpy 和 arcpy 从要素类中获取二维数组...

import arcpy
import numpy
locxyarray = arcpy.da.FeatureClassToNumPyArray("Points", ['SHAPE@XY', 'PrimeKey'])

结果:

array([([506173.7478, 5455684.263900001], 1),
([506175.22869999986, 5455648.723099999], 2),
([506229.03359999973, 5455661.5572999995], 3),
([506250.25939999986, 5455614.169500001], 4),
([506305.54509999976, 5455579.122300001], 5),
([506331.70710000023, 5455688.2129], 6)],
dtype=[('SHAPE@X', '<f8', (2,)), ('PrimeKey', '<i4')])

我想要的:

array([(506173.7478, 5455684.263900001, 1),
(506175.22869999986, 5455648.723099999, 2),
(506229.03359999973, 5455661.5572999995, 3),
(506250.25939999986, 5455614.169500001, 4),
(506305.54509999976, 5455579.122300001, 5),
(506331.70710000023, 5455688.2129, 6)],
dtype=[('SHAPE@XY', '<f8', (2,)), ('PrimeKey', '<i4')])

我想要上面的内容,所以我可以按 0 或 1 列排序,并按该顺序返回 2 列...另外,我需要能够计算 X 的平均值和 Y 的平均值,并拉取所有值高于或低于平均值的 Prime Keys。

编辑 - 现在能够使数组“看起来”正确;

locxyarray = arcpy.da.FeatureClassToNumPyArray("Points", ['SHAPE@X', 'SHAPE@Y', 'PrimeKey'])

>>array([(506173.7478, 5455684.263900001, 1),
(506175.22869999986, 5455648.723099999, 2),
(506229.03359999973, 5455661.5572999995, 3),
(506250.25939999986, 5455614.169500001, 4),
(506305.54509999976, 5455579.122300001, 5),
(506331.70710000023, 5455688.2129, 6)],
dtype=[('SHAPE@X', '<f8'), ('SHAPE@Y', '<f8'), ('PrimeKey', '<i4')])

但我无法计算 X 或 Y 列的平均值...(IndexError:索引太多)

解决方案(请参阅解决方案下面的评论,这是一个摘要):

import arcpy
import numpy as np
locxyarray = arcpy.da.FeatureClassToNumPyArray("Points", ['SHAPE@X','SHAPE@Y','PrimeKey'])
LSideLocs = np.where(locxyarray['SHAPE@X']<=np.mean(locxyarray['SHAPE@X']))
RSideLocs = np.where(locxyarray['SHAPE@X']>=np.mean(locxyarray['SHAPE@X']))

最佳答案

您可以随意使用字段名称。

r=[(x['SHAPE@XY'][i][0],x['SHAPE@XY'][i][1],x['PrimeKey'][i]) for i in range(x.shape[0])]
x=np.rec.fromrecords(r, formats = 'f8,f8,i4', names = 'SHAPE@X,SHAPE@Y,PrimeKey')
rec.array([(506173.7478, 5455684.263900001, 1),
(506175.22869999986, 5455648.723099999, 2),
(506229.03359999973, 5455661.5572999995, 3),
(506250.25939999986, 5455614.169500001, 4),
(506305.54509999976, 5455579.122300001, 5),
(506331.70710000023, 5455688.2129, 6)],
dtype=[('SHAPE@X', '<f8'), ('SHAPE@Y', '<f8'), ('PrimeKey', '<i4')])

关于python - 从 numpy 数组中删除子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25939600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com