def asarray(a, dtype=None, order=None):
return array(a, dtype, copy=False, order=order)
The main difference is that array
(by default) will make a copy of the object, while asarray
will not unless necessary.
The difference can be demonstrated by this example:
- generate a matrix
>>> A = numpy.matrix(np.ones((3,3))) >>> A matrix([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]])
- use
numpy.array
to modifyA
. Doesn't work because you are modifying a copy>>> numpy.array(A)[2]=2 >>> A matrix([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]])
- use
numpy.asarray
to modifyA
. It worked because you are modifyingA
itself>>> numpy.asarray(A)[2]=2 >>> A matrix([[ 1., 1., 1.], [ 1., 1., 1.], [ 2., 2., 2.]])
No comments:
Post a Comment