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

前端 未结 5 1285
灰色年华
灰色年华 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条回答
  •  青春惊慌失措
    2021-01-03 21:16

    You can use np.take -

    np.take(matrix,0)
    

    Sample run -

    In [15]: matrix = np.array([[67]])
    
    In [16]: np.take(matrix,0)
    Out[16]: 67
    
    In [17]: type(np.take(matrix,0))
    Out[17]: numpy.int64
    

提交回复
热议问题