简体   繁体   中英

best practice for multidimensional arrays in python

Suppose I have a collection of objects which I wish to save in python, say, a list of numbers: [0.12, 0.85, 0.11, 0.12], [0.23, 0.52, 0.10, 0.19], etc. Suppose further that these objects are indexed by 3 attributes, say, "origin", "destination", and "month". I wish to store these objects in an array-like object which can be easily sliced, ideally using either numerical index or a name.

So, ie,

obj[2,1,7] # might return: [0.23, 0.52, 0.10, 0.19]

Or,

obj['chicago','new york','jan'] # might return: [0.12, 0.85, 0.11, 0.12]

And further,

obj[:,'new york','jan'] # would return data with first index = any.

I'm looking for the best practice to achieve this in python. I did find this post, which seems quite suitable, but it seemed to require some overhead and there was little discussion of alternatives. I also found something called the xarray package, though this doesn't seem as popular. I am transitioning form R, where I would do this the array() function, which adds a multi-dimensional index to any vector-like structure.

After some poking around, it appears that xarray is suitable for my needs. Unfortunately, given my lack of experience, I can't speak to compatibility with other packages or performance.

import numpy as np
import xarray as xr
cityOrig = ['chicago','new york', 'boston']
cityDest = ['chicago','new york', 'boston']
month = ['jan','feb','mar','apr']
data = np.random.rand(4,3,3,4)

myArray = xr.DataArray(data,
                       dims=['dat','orig','dest','month'],
                       coords = {'orig':cityOrig,'dest':cityDest,'month':month})

print(myArray[:,1,2,1].data)
[0.64  0.605 0.445 0.059]
print(myArray.loc[:,'chicago','new york','jan'].data)
[0.64  0.605 0.445 0.059]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM