How to change flags of ndarray in numpy in python - Python for Data Analytics

Python Programs | Python Tricks | Solution for problems | Data Cleaning | Data Science

How to change flags of ndarray in numpy in python

Changing flag of ndarray in numpy

numpy.ndarray.setflags(write=None, align=None, uic=None

write : bool, optional
Describes whether or not a can be written to.
align : bool, optional
Describes whether or not a is aligned properly for its type.
uic : bool, optional
Describes whether or not a is a copy of another “base” array.
Array flags provide information about how the memory area used for the array is to be interpreted. There are 6 Boolean flags in use, only three of which can be changed by the user: UPDATEIFCOPY, WRITEABLE, and ALIGNED.

WRITEABLE (W) the data area can be written to;

ALIGNED (A) the data and strides are aligned appropriately for the hardware (as determined by the compiler);

UPDATEIFCOPY (U) this array is a copy of some other array (referenced by .base). When this array is deallocated, the base array will be updated with the contents of this array.

**All flags can be accessed using their first (upper case) letter as well as the full name.**

Example:

>>> y
array([[3, 1, 7],
       [2, 0, 0],
       [8, 5, 9]])
>>> y.flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False
>>> y.setflags(write=0, align=0)
>>> y.flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : False
  ALIGNED : False
  UPDATEIFCOPY : False
>>> y.setflags(uic=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot set UPDATEIFCOPY flag to True 
 
source: scipy.org 

2 comments: