python - how do I make a numpy array from a list of pint quantities -
import numpy np pint import unitregistry unit = unitregistry() q_ = unit.quantity = 1.0*unit.meter b = 2.0*unit.meter # calculations change , b x=np.array([a.magnitude,b.magnitude])*q_(1.0,a.units) will make numpy array variables , b pint quantities. crude there no guarantee , b have same units. there cleaner way it? need write function?
you should convert base unit to_base_units() before creating numpy array it, no need write function this, simple list comprehension nicely.
if you're doing numerically heavy calculations on x want keep in reference unit , use raw array (without attached unit), , limit unit conversions input/output phase of program.
import numpy np pint import unitregistry unit = unitregistry() q_ = unit.quantity = 1.0 * unit.meter b = 2.0 * unit.meter c = 39.37 * unit.inch # list values in different units (meters , inches) measures = [a, b, c] # use comprehension list magnitudes in base unit x = np.array([measure.to_base_units().magnitude measure in measures]) print x * q_(1.0, unit.meter) >> [ 1. 2. 0.999998] meter
Comments
Post a Comment