What is the difference between Numpy's array() and asarray() functions?

前端 未结 6 803
孤城傲影
孤城傲影 2020-11-28 00:52

What is the difference between Numpy\'s array() and asarray() functions? When should you use one rather than the other? They seem to generate identical output for all the in

6条回答
  •  伪装坚强ぢ
    2020-11-28 01:17

    asarray(x) is like array(x, copy=False)

    Use asarray(x) when you want to ensure that x will be an array before any other operations are done. If x is already an array then no copy would be done. It would not cause a redundant performance hit.

    Here is an example of a function that ensure x is converted into an array first.

    def mysum(x):
        return np.asarray(x).sum()
    

提交回复
热议问题