Here we would be learning about the attributes of the array.
This array attribute returns a tuple consisting of array dimensions. It can also be used to resize the array.
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print (a.shape)
# this resizes the ndarray
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
a.shape = (3,2)
print (a)
NumPy also provides a reshape function to resize an array.
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
print (b)
# an array of evenly spaced numbers
import numpy as np
a = np.arange(24)
print (a)
# this is one dimensional array
import numpy as np
a = np.arange(24)
a.ndim
# now reshape it
b = a.reshape(2,4,3)
print (b)
# b is having three dimensions
This array attribute returns the length of each element of array in bytes.
# dtype of array is int8 (1 byte)
import numpy as np
x = np.array([1,2,3,4,5], dtype = np.int8)
print (x.itemsize)
# dtype of array is now float32 (4 bytes)
import numpy as np
x = np.array([1,2,3,4,5], dtype = np.float32)
print (x.itemsize)
The ndarray object has the following attributes. Its current values are returned by this function.
import numpy as np
x = np.array([1,2,3,4,5])
print (x.flags)
Attribute | Description | |||
---|---|---|---|---|
C_CONTIGUOUS (C) | The data is in a single, C-style contiguous segment | |||
F_CONTIGUOUS (F) | The data is in a single, Fortran-style contiguous segment | |||
OWNDATA (O) | The array owns the memory it uses or borrows it from another object | |||
WRITEABLE (W) | The data area can be written to. Setting this to False locks the data, making it read-only | |||
ALIGNED (A) | The data and all elements are aligned appropriately for the hardware | |||
UPDATEIFCOPY (U) | This array is a copy of some other array. When this array is deallocated, the base array will be updated with the contents of this array |
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
When going through coding examples, it's quite common to have doubts and errors.
If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.
You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.
If you want to