How To Make Numpy Treat Each Row/tensor As A Value
Solution 1:
Try to post what I have learned. The method Joe used is called structured arrays. It will allow users to define what is contained in a single cell/element.
We take a look at the description of the first example the documentation provided.
x = np.array([(1,2.,'Hello'), (2,3.,"World")], ... dtype=[('foo', 'i4'),('bar', 'f4'), ('baz', 'S10')])
Here we have created a one-dimensional array of length 2. Each element of this array is a structure that contains three items, a 32-bit integer, a 32-bit float, and a string of length 10 or less.
Without passing in dtype
, however, we will get a 2 by 3 matrix.
With this method, we would be able to let numpy
treat a higher dimensional array as an single element with properly set dtype
.
Another trick Joe showed is that we don't need to really form a new numpy array to achieve the purpose. We can use the view
function (See ndarray.view
) to change the way numpy
view data. There is a section of Note
section in ndarray.view
that I think you should take a look before utilizing the method. I have no guarantee that there would not be side effects. The paragraph below is from the note section and seems to call for caution.
For a.view(some_dtype), if some_dtype has a different number of bytes per entry than the previous dtype (for example, converting a regular array to a structured array), then the behavior of the view cannot be predicted just from the superficial appearance of a (shown by print(a)). It also depends on exactly how a is stored in memory. Therefore if a is C-ordered versus fortran-ordered, versus defined as a slice or transpose, etc., the view may give different results.
Other reference
https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.dtypes.htmlhttps://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.dtype.html
Post a Comment for "How To Make Numpy Treat Each Row/tensor As A Value"