How to convert singleton array to a scalar value in Python?

前端 未结 5 1269
灰色年华
灰色年华 2021-01-03 20:51

Suppose I have 1x1x1x1x... array and wish to convert it to scalar?

How do I do it?

squeeze does not help.

import numpy as np

ma         


        
5条回答
  •  旧时难觅i
    2021-01-03 21:11

    Numpy has a function explicitly for this purpose: asscalar

    >>> np.asscalar(np.array([24]))
    24
    

    This uses item() in the implementation.

    I guess asscalar was added to more explicit about what's going on.

提交回复
热议问题