What is the identity of "ndim, shape, size, .. etc" ndarray in numpy
I am new to Python.
After using Matlab for years, lately, I started learning numpy / scipy
It seems that the main element of numpy is ndarray
. Ndarray has the following attributes:
-
ndarray.ndim
-
ndarray.shape
-
ndarray.size
- ... etc.
I'm pretty familiar with C ++ / JAVA classes, but I'm new to OOP Python.
Q1: My first question is what is the identity of the above attributes?
I first assumed that the above attribute might be public member variables. But I soon found it a.ndim = 10
didn't work (assuming it a
is an object ndarray
). So it looks like it is not a public member variable.
I further assumed that they might be public methods, similar to getter methods in C ++. However, when I tried a.nidm()
with the parenthesis it didn't work. So it seems that this is not a public method.
Another possibility might be that they are private member variables, but printing a.ndim
works, so they cannot be private data members.
So, I cannot figure out what is the true identity of the above attributes.
Q2. Where can I find the implementation of Python code ndarray
? Since I have installed numpy / scipy on my local machine, I guess there might be some ways to look at the source code, then I think things might be clear.
Could you give some advice on this?
source to share
numpy
implemented as a combination of code C
and code Python
. The source is viewable at github
and can be downloaded as a repository git
. But paving the way to the source C
takes some work. Many files are marked as .c.src
, which means they go through one or more levels of processing before being compiled.
And Python is written in combination with C and Python. So don't try to force things into C ++ terms.
It is probably best to use MATLAB expertise, with settings to allow Python. And it numpy
has a number of quirks outside of Python. It uses Python syntax, but since it has its own C code, it is not just a Python class.
I use it Ipython
as a normal work environment. With this I can use foo?
to see the documentation for foo
(same as Python help(foo)
and foo??
to see the code - if written in Python (like MATLAB / Octave type(foo)
))
Python objects have attributes and methods. Also properties
, which look like attributes but actually use get / set methods. You usually don't need to know the difference between attributes and properties.
x.ndim # as noted, has a get, but no set; see also np.ndim(x)
x.shape # has a get, but can also be set; see also np.shape(x)
x.<tab>
in Ipython shows me all the completion for a ndarray
. There are 4 * 18. Some of them are methods, some are attributes. x._<tab>
also shows the bundle that starts with __
. These are 'private' - not intended for public consumption, just semantics. You can look at them and use them as needed.
Off hand x.shape
is the only property ndarray
I have set and even with this I usually use reshape(...)
. Read their docs to see the difference. ndim
is the number of dimensions, and there is no point in changing it directly. It is len(x.shape)
; change the form to change ndim
. Likewise, x.size
it shouldn't be what you change directly.
Some of these properties are available through functions. np.shape(x) == x.shape
, similar to MATLAB size(x)
. (MATLAB has no attribute syntax .
).
x.__array_interface__
- a convenient property that gives a dictionary with a number of attributes
In [391]: x.__array_interface__
Out[391]:
{'descr': [('', '<f8')],
'version': 3,
'shape': (50,),
'typestr': '<f8',
'strides': None,
'data': (165646680, False)}
The docs for ndarray(shape, dtype=float, buffer=None, offset=0,
strides=None, order=None)
, method __new__
lists these attributes:
`Attributes
----------
T : ndarray
Transpose of the array.
data : buffer
The array elements, in memory.
dtype : dtype object
Describes the format of the elements in the array.
flags : dict
Dictionary containing information related to memory use, e.g.,
'C_CONTIGUOUS', 'OWNDATA', 'WRITEABLE', etc.
flat : numpy.flatiter object
Flattened version of the array as an iterator. The iterator
allows assignments, e.g., ``x.flat = 3`` (See `ndarray.flat` for
assignment examples; TODO).
imag : ndarray
Imaginary part of the array.
real : ndarray
Real part of the array.
size : int
Number of elements in the array.
itemsize : int
The memory use of each array element in bytes.
nbytes : int
The total number of bytes required to store the array data,
i.e., ``itemsize * size``.
ndim : int
The array number of dimensions.
shape : tuple of ints
Shape of the array.
strides : tuple of ints
The step-size required to move from one element to the next in
memory. For example, a contiguous ``(3, 4)`` array of type
``int16`` in C-order has strides ``(8, 2)``. This implies that
to move from element to element in memory requires jumps of 2 bytes.
To move from row-to-row, one needs to jump 8 bytes at a time
(``2 * 4``).
ctypes : ctypes object
Class containing properties of the array needed for interaction
with ctypes.
base : ndarray
If the array is a view into another array, that array is its `base`
(unless that array is also a view). The `base` array is where the
array data is actually stored.
They should all be considered properties, although I don't think they numpy
actually use the mechanism property
. In general, they should be considered read-only. Besides shape
, I only remember changing data
(pointer to data buffer) and strides
.
source to share
As for your first question, Python has syntactic sugar for properties , including fine-grained control on get, set, delete, and also limit any of the above.
So, for example, if you have
class Foo(object):
@property
def shmip(self):
return 3
then you can write Foo().shmip
to get 3
, but if this is a class definition, you have disabled the setting Foo().shmip = 4
.
In other words, these are read-only properties.
source to share
Question 1
The list you mention is the one that contains the attributes for the Numpy array.
For example:
a = np.array([1, 2, 3])
print(type(a))
>><class 'numpy.ndarray'>
Since a
nump.ndarray is, you can use these attributes to read more about it. (i.e. a.size
will result in 3). For information on what everyone will visit SciPy's Attributes Documentation .
Question 2
You can start here to familiarize yourself with some of the basic Numpy tools, as well as the Reference Manual , assuming you are using v1.9. For information related to Numpy array, you can go to Array Objects .
Their documentation is very extensive and very helpful. Examples are provided on the website with several examples.
source to share