I'm not sure if my post question makes lots of sense; however, I'm building an input array for a class/function that takes in a lot of user inputed data and outputs a 开发者_JAVA技巧numpy array.
# 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, .... ,] ])
- What are/is my best option(s) in defining the input array(s)?
Numpy arrays are the wrong datatype here: they are designed for numeric manipulations of large amounts of similar data (e.g. large matrices). It looks like you could just use a dict
:
options = {
"zone_id": 10001,
"model_size": 1,
"analysis_type": 2,
"model_purposes": [ "ONE", ... ]
...
}
You could then pass this on to a function, either as the dictionary or by unpacking it into names arguments using **
:
def do_stuff(zone_id=10001, model_size=1, ...):
...
do_stuff(**options)
If you want a more complicated options datatype (e.g. if some of the options need to be calculated on the fly or depend on others), you could use a specialised Options
class (though be warned, this is almost certainly overkill);
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
and then
options = Options()
options.populate_values()
print(options.model_size)
精彩评论