numpy dimensions

2024/10/5 7:17:22

Im a newbie to Numpy and trying to understand the basic question of what is dimension,

I tried the following commands and trying to understand why the ndim for last 2 arrays are same?

>>> a= array([1,2,3])
>>> a.ndim
1
>>> a= array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],[4, 5, 6]])
>>> a.ndim
2
>>> a=arange(15).reshape(3,5)
>>> a.ndim
2>>> a
array([[ 0,  1,  2,  3,  4],[ 5,  6,  7,  8,  9],[10, 11, 12, 13, 14]])

My understanding..

Case 1:array([[1, 2, 3],[4, 5, 6]])2 elements are present in main lists, so ndim is-2Case 2:array([[ 0,  1,  2,  3,  4],[ 5,  6,  7,  8,  9],[10, 11, 12, 13, 14]])

3 elements are present in the main lists, do ndim is-3

Answer

The shape of an array is a tuple of its dimensions. An array with one dimension has a shape of (n,). A two dimension array has a shape of (n,m) (like your case 2 and 3) and a three dimension array has a shape of (n,m,k) and so on.

Therefore, whilst the shape of your second and third example are different, the no. dimensions is two in both cases:

>>> a= np.array([[1,2,3],[4,5,6]])
>>> a.shape
(2, 3)>>> b=np.arange(15).reshape(3,5)
>>> b.shape
(3, 5)

If you wanted to add another dimension to your examples you would have to do something like this:

a= np.array([[[1,2,3]],[[4,5,6]]])

or

np.arange(15).reshape(3,5,1)

You can keep adding dimensions in this way:

One dimension:

>>> a = np.zeros((2))
array([ 0.,  0.])
>>> a.shape
(2,)
>>> a.ndim
1

Two dimensions:

>>> b = np.zeros((2,2))
array([[ 0.,  0.],[ 0.,  0.]])
>>> b.shape
(2,2)
>>> b.ndim
2

Three dimensions:

>>> c = np.zeros((2,2,2))
array([[[ 0.,  0.],[ 0.,  0.]],[[ 0.,  0.],[ 0.,  0.]]])
>>> c.shape
(2,2,2)
>>> c.ndim
3

Four dimensions:

>>> d = np.zeros((2,2,2,2))
array([[[[ 0.,  0.],[ 0.,  0.]],[[ 0.,  0.],[ 0.,  0.]]],[[[ 0.,  0.],[ 0.,  0.]],[[ 0.,  0.],[ 0.,  0.]]]])
>>> d.shape
(2,2,2,2)
>>> d.ndim
4
https://en.xdnf.cn/q/70507.html

Related Q&A

DataFrame Plot: how to sort X axis

I am plotting some counts from a field of dataframe (pandas) and I found that the X axis is sorted by the counts (descending order). Instead is it possible to sort by the alphabetical order of the fiel…

In-place custom object unpacking different behavior with __getitem__ python 3.5 vs python 3.6

a follow-up question on this question: i ran the code below on python 3.5 and python 3.6 - with very different results:class Container:KEYS = (a, b, c)def __init__(self, a=None, b=None, c=None):self.a …

Different ways of using __init__ for PyQt4

So... Im working on trying to move from basic Python to some GUI programming, using PyQt4. Im looking at a couple different books and tutorials, and they each seem to have a slightly different way of k…

Numpy: Reshape array along a specified axis

I have the following array:x = np.arange(24).reshape((2,3,2,2)) array([[[[ 0, 1],[ 2, 3]],[[ 4, 5],[ 6, 7]],[[ 8, 9],[10, 11]]],[[[12, 13],[14, 15]],[[16, 17],[18, 19]],[[20, 21],[22, 23]]]])I wou…

python suds wrong namespace prefix in SOAP request

I use python/suds to implement a client and I get wrong namespace prefixes in the sent SOAP header for a spefic type of parameters defined by element ref= in the wsdl. The .wsdl is referencing a data …

Allow help() to work on partial function object

Im trying to make sure running help() at the Python 2.7 REPL displays the __doc__ for a function that was wrapped with functools.partial. Currently running help() on a functools.partial function displ…

How To Fix Miscased Procfile in Heroku

Heroku will not reload my corrected ProcfileI have ran git status which shows me the Procfile and I realized that I spelled Procfile with a lower case p. I saw the error and updated the file name in my…

Using Pythons xml.etree to find element start and end character offsets

I have XML data that looks like:<xml> The captial of <place pid="1">South Africa</place> is <place>Pretoria</place>. </xml>I would like to be able to extra…

How to get public key using PyOpenSSL?

Im tring to create python script, that would take PKCS#12 package and print some information contained in x509 certificate and using for this purpouses PyOpenSSL module. So far i want to fetch from cer…

what is the best way to extract data from pdf

I have thousands of pdf file that I need to extract data from.This is an example pdf. I want to extract this information from the example pdf.I am open to nodejs, python or any other effective method. …