3

ESRI 允许访问将数据从表移动到数组并返回。我有一个脚本,它从 api 调用中获取人口普查数据并将其转换为数组,进行一些简单的数学运算,然后理想情况下将其放入表格中。要进行数学运算,数组不能是 rec 数组。vstack、hstack 或 concatenate 的组合似乎都没有给出好的结果。我求助于创建单独的一维数组作为recarrays,然后使用np.lib.recfunctions.merge_arrays 中的合并函数。肯定有更好的方法。

ESRI 从 TableToNumPyArray 的返回:

>>> testArray
array([ (41039000100.0, 2628.0, 100.0, 2339.0, 135.0, 18.0, 22.0, 16.0, 25.0, 0.0, 92.0, 0.0, 92.0, 0.0, 92.0, 0.0, 92.0, 6.0, 9.0, 249.0, 90.0, 0.0, 92.0, 1, u'41039000100'),
...
dtype=[('Geo_id', '<f8'), ('TotalUnits', '<f8'), ('MOE_Total', '<f8'), >('Total_1_detached', '<f8'), ('MOE_Total_1_detached', '<f8'), ('Total_1_attached', >'<f8'), ('MOE_Total_1_attached', '<f8'), ('Total_2', '<f8'), ('MOE_Total_2', '<f8'), >('Total_3_or_4', '<f8'), ('MOE_Total_3_or_4', '<f8'), ('Total_5_to_9', '<f8'), >('MOE_Total_5_to_9', '<f8'), ('Total_10_to_19', '<f8'), ('MOE_Total_10_to_19', '<f8'), >('Total_20_to_49', '<f8'), ('MOE_Total_20_to_49', '<f8'), ('Total_50_or_more', '<f8'), >('MOE_Total_50_or_more', '<f8'), ('Total_Mobile_home', '<f8'), ('MOE_Total_Mobile_home', '<f8'), ('Total_Boat_RV_van_etc', '<f8'), ('MOE_Total_Boat_RV_van_etc', '<f8'), >('ObjectID', '<i4'), ('geo_id_t', '<U50')])

我的代码片段看起来像

try:

    # Assign Geo_id array
    Geo_id_array = B25008_001E_array[...,0]
    Tpop_array = B25008_001E_array[...,1]
    Tunits_array = B25024_001E_array[...,1]
    # divide by sero is possible for real rowns and definite for the end-of-file
    # tract, so convert nan's in the HHsize_array to zero's with nan_to_num
    # HHsize_array = Tpop_array.view(np.float32)/Tunits_array.view(np.float32)
    HHsize_array = Tpop_array/Tunits_array
    HHsize_array = nan_to_num(HHsize_array)
    # Table_array = array(vstack((Geo_id_array, Tpop_array, Tunits_array, HHsize_array)), dtype = ([('Geo_id', '|S13'), ('Tpop', np.int32), ('Tunits_array', np.int32), ('HHsize', np.float32)]))
    # Table_array = np.hstack((Geo_id_array, Tpop_array, Tunits_array, HHsize_array))
    Geo_id_recarray = np.array(Geo_id_array, dtype = ([('Geo_id', '|S13')]))
    Tpop_recarray = np.array(Tpop_array, dtype = ([('Tpop', np.int32)]))
    Tunits_recarray = np.array(Tunits_array, dtype = ([('Tunits_array', np.int32)]))
    HHsize_recarray = np.array(HHsize_array, dtype = ([('HHsize', np.float32)]))
    arrays = [Geo_id_recarray, Tpop_recarray, Tunits_recarray, HHsize_recarray]
    MergedArray = np.lib.recfunctions.merge_arrays(arrays, usemask=False)
    print
    print



except Exception as e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "An error occured on line %i" % tb.tb_lineno
    print str(e)

我想,我更喜欢在构建数组之前合并/加入/堆叠数组。想法?

4

1 回答 1

1

您应该能够使用结构化数组(从技术上讲,您不使用recarrays)来进行“简单数学运算”。我不确定你是否展示了你想做的数学,但例如,如果你想做:

HHsize_array = Tpop_array/Tunits_array 

但是不想拥有所有这些单独的数组,您可以简单地对主(合并数组)的视图进行数学运算,我们称之为data

data['HHsize'] = data['Tpop']/data['Tunits']

其中HHsize,TpopTunits是一个名为 的结构化数组中的所有字段名称data,这样您就有

>>> data.dtype
dtype([('Geo_id', '|S13'), ('Tpop', np.int32), ('Tunits_array', np.int32), ('HHsize', np.float32)])
于 2013-02-18T03:54:41.560 回答