代码之家  ›  专栏  ›  技术社区  ›  dassouki

为动态方程输入组织和构建numpy数组

  •  1
  • dassouki  · 技术社区  · 14 年前

    我不确定我的post问题是否有意义;但是,我正在为一个类/函数构建一个输入数组,该类/函数接收大量用户输入的数据并输出一个numpy数组。

    # I'm trying to build an input array that should include following information:
    '''
    * zone_id - id from db - int  
    * model size - int
      * type of analysis - one of the following:
        * type 1 - int or string
        * type 2 - int or string
        * type 3 - int or string
      * model purposes:
        * default: ONE, TWO, THREE #this is just a title of the purpose
        * Custom: default + others (anywhere from 0 to 15 purposes)
      * Modeling step 1: some socio economic factors #produces results 1
      * Modeling step 2: 
        * Default: equation coefficients for retail/non retail
        * Custom: equation coefficients for each extra activities as defined by
          the user
        * produces results 2
    
    Example array:
      def_array = (zone_id, model_size, analysis_type,
                   model_purpose[],
                   socio_economics[],
                   socio_coefficients[] )
    '''
    # Numerical example:
      my_arr = [np.array([ 10001, 1, 2,
                         [ 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE' ],
                         [ {'retail':500, 'non_retail':300, 'school':300', 'other':900} ],
                         [ {'retail':500, 'non_retail':300, 'school':300', 'other':900} ],
                         [ {'ONE':{'retail':.5, 'non_retail':1.7, 'school':.4', 'other':4.7},
                           {'TWO':{'retail':.2, 'non_retail':2.5, 'school':.5', 'other':4.3},
                           {'THREE':{'retail':.3, 'non_retail':2.3, 'school':.6', 'other':2.2},
                           {'FOUR':{'retail':.4, 'non_retail':1.1, 'school':.7', 'other':1.0},
                           {'FIVE':{'retail':7, 'non_retail':2, 'school':3', 'other':1} ] ])
    
    # this array will be inserted into 3 functions and together should return the following array:
    arr_results = [np.array([ 10001, one_1, TWO_1, THREE_1, FOUR_1, FIVE_1, ONE_2, TWO_2, THREE_2, FOUR_2, FIVE_2],
                            [10002, .... ,] ])
    
    1 回复  |  直到 14 年前
        1
  •  3
  •   Katriel    14 年前

    Numpy数组在这里是错误的数据类型:它们是为大量相似数据(例如大型矩阵)的数字操作而设计的。看起来你可以用一个 dict :

    options = {
        "zone_id": 10001,
        "model_size": 1,
        "analysis_type": 2,
        "model_purposes": [ "ONE", ... ]
        ...
    }
    

    然后可以将其作为字典传递给函数,或者使用参数将其解包为names ** :

    def do_stuff(zone_id=10001, model_size=1, ...):
        ...
    
    do_stuff(**options)
    

    如果您想要一个更复杂的options数据类型(例如,如果某些选项需要动态计算或依赖于其他选项),您可以使用专门的 Options 阶级(尽管要警告,这几乎肯定是杀伤力过大);

    class Options:
        def __init__(self):
            # set some default values
            self.zone_id = 10001
    
        def populate_values(self):
            # maybe handle some user input?
            self.name = input("name: ")
    
        # use a property to calculate model_size on the fly
        @property
        def model_size(self):
            return 2-1
    

    然后

    options = Options()
    options.populate_values()
    print(options.model_size)