in Education by
import numpy as np from pint import UnitRegistry unit = UnitRegistry() Q_ = unit.Quantity a = 1.0*unit.meter b = 2.0*unit.meter # some calculations that change a and b x=np.array([a.magnitude,b.magnitude])*Q_(1.0,a.units) will make a numpy array from variables a and b which are Pint quantities. It is somewhat crude as there is no guarantee that a and b have the same units. Is there a cleaner way to do it? Do I need to write a function? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You should convert to a base unit with to_base_units() before creating a numpy array from it, no need to write a function for this, a simple list comprehension will do it nicely. If you're doing numerically heavy calculations on x you probably will want to keep it in a reference unit and use it as a raw array (without attached unit), and limit the unit conversions to the input/output phase of the program. import numpy as np from pint import UnitRegistry unit = UnitRegistry() Q_ = unit.Quantity a = 1.0 * unit.meter b = 2.0 * unit.meter c = 39.37 * unit.inch # A list with values in different units (meters and inches) measures = [a, b, c] # We use a comprehension list to get the magnitudes in a base unit x = np.array([measure.to_base_units().magnitude for measure in measures]) print x * Q_(1.0, unit.meter) >> [ 1. 2. 0.999998] meter

Related questions

0 votes
    I have a numpy array (dtype bool) representing an array of bits. For example, the array np.array( ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Concatenate two NumPy arrays vertically (4 answers) Closed 3 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Concatenate two NumPy arrays vertically (4 answers) Closed 3 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Concatenate two NumPy arrays vertically (4 answers) Closed 3 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    How Do I Make 3d Plots/visualizations Using Numpy/scipy?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    How Do I Make Plots Using Numpy/scipy?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    List the steps to create a 1D array and 2D array in NumPy?...
asked Apr 24, 2021 by JackTerrance
0 votes
    How do you create a 3D array in NumPy?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    I know how to use the checkboxlist in ASP.NET to display options retrieved from a database. What I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    Point out the wrong statement. (a) Each universal function takes array inputs and produces array outputs (b) Broadcasting ... input arguments are ndarrays (d) All of the mentioned...
asked Oct 7, 2021 in Technology by JackTerrance
0 votes
    How can you identify the datatype of a given NumPy array?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    I have a dataset that only contains y-values in one column. I want to insert a column of x- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I need to iterate through the elements in a numpy array so I can treat any zero elements separately. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I need to iterate through the elements in a numpy array so I can treat any zero elements separately. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I want to encode a 1-D numpy array: x = array([1,0,3]) As a 2-D 1-hot array y = array([ ... some faster technique other than looping. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
...