从numpy数组中删除子数组

时间:2014-09-19 17:45:57

标签: python arrays numpy arcpy

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

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键。

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

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

2 个答案:

答案 0 :(得分:2)

您可以使用字段名称。

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

答案 1 :(得分:0)

这样的事情:

>>> 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不能保持不变,因为现在数组元素由三个字段组成,而不是原始字符中的两个字段。