从numpy数组中移除子数组

2024-10-03 06:26:49 发布

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

我正在尝试从一个使用numpy和arcpy的要素类中获取一个2D数组。。。在

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

结果是:

^{pr2}$

我想要的:

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的平均值,并提取值高于或低于平均值的所有质数键。在

编辑-现在可以使数组“看起来”正确

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列的平均值。。。(索引器错误:索引过多)

解决方案(请参阅下面的“解决方案”中的注释,这是一个摘要):

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']))

Tags: importnumpynp数组arraypointsda平均值
2条回答

你可以用field的名字玩。在

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')])

像这样:

>>> numpy.array([(x[0][0], x[0][1], x[1]) for x in locxyarray], dtype=np.dtype([('X', np.float), ('Y', np.float), ('PrimeKey', np.int32)]))
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=[('X', '<f8'), ('Y', '<f8'), ('PrimeKey', '<i4')])

注意,dtype不能保持不变,因为现在数组元素由三个字段组成,而不是原来的两个字段。在

相关问题 更多 >