I am a beginner user of Python. I used to work with matlab intensively. Now I am shifting to python. I have a question about the dimension of an array.
I import Numpy
I first create an array X, then I use some embedded function, like, sum, to play with my array. Eventually, when I try to check the dimension of my array X, it becomes: X.shape, outputs (81,). The number 81 is what I expected, but I also expect the 2nd dimension is 1, rather than just omitted. This makes me feel very uncomfortable even though when I directly type X, it output correctly, i.e., one column and the figures in X are all as expected.
Then when I use another array Y, which literally has Y.shape, outputs (81,1), then if I type X*Y, which I expected to see one array of dimension (81,1) but instead, I saw an array of dimension (81,81).
I don't know what is the underlying mechanism to produce this results.
The way I solve this problem is very stupid. I first create a new array C = zeros((81,1)), so C literally has dimension (81,1), then I assign my X to C by typing C[:,0]=X, then C.shape = (81,1). Note that if I type C=X, then C.shape=(81,), which goes back to my problem. So I can solve my problem, but I am sure there is better method to solve my problem and I also don't understand why python would produce something like (81,), with the 2nd dimension omitted.
len() is the Python built-in function that returns the number of elements in a list or the number of characters in a string. For numpy. ndarray , len() returns the size of the first dimension. Equivalent to shape[0] and also equal to size only for one-dimensional arrays.
Python Numpy – Create One Dimensional Array One dimensional array contains elements only in one dimension. In other words, the shape of the numpy array should contain only one value in the tuple. To create a one dimensional array in Numpy, you can use either of the array(), arange() or linspace() numpy functions.
Creating arrays with more than one dimension In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.
In Mathematics/Physics, dimension or dimensionality is informally defined as the minimum number of coordinates needed to specify any point within a space. But in Numpy, according to the numpy doc, it's the same as axis/axes: In Numpy dimensions are called axes. The number of axes is rank.
Numpy does not omit the dimension of an array. It is a library built for multidimensional arrays (not just 1d or 2d arrays), and so it makes very clear distinctions between arrays of different dimensions (it cannot assume that any array is just a degenerate form of a higher dimension array, because the number of dimensions is infinite, conceptually).
An array with dimension (81, 1)
is a 2d array with the value of the 2nd dimension equal to 1.
An array with dimension (81, )
is just a 1d array.
When you write C[:,0]
, you are referring to a column. Therefore
If you write C[: 0] = X
, you are assigning a column to one of the columns of C
(which happens to be the only one), and therefore are not changing the dimension of C
.
If you write C = X
, then you are saying that C
is now a column as well, and therefore are changing the dimension of C
.
A fundamental difference between MATLAB and numpy is that in MATLAB a matrix has at least 2 dimensions. Back in v3.0, it had only 2. numpy
arrays can have 0, 1, 2 or more dimensions.
C.shape
returns a tuple of the dimensions; it may be of 0 length, ()
, 1 value, (81,)
, or 2 (81,1)
. c.ndim
is just len(C.shape)
.
numpy
has a matrix
subclass that behaves like the old MATLAB matrices with only 2 dimensions. For some MATLAB programmers it is easier to use, but otherwise is limited in usefulness.
Actions like np.sum(C)
reduce the number of dimensions (unless the keepdims
parameter is used).
Your actions:
# X.shape == (81,)
C = zeros((81,1))
# C.shape == (81,1)
C[:,0] = X # no change in shape
# X has 81 elements, C[:,0] does too - so the assignment works
C = X # reassign the variable
C=X
does nothing with the original C
. You could just as well have written Foobar = X
.
To make X
have 2 dimensions, modify its shape - there are various ways (with MATLAB parallels)
X = np.reshape(X, (81,1))
X.shape = (81,1)
X = X[:,None]
Broadcasting works by expanding dimensions as follows:
(81,1)*(81,) =>
(81,1)*(1,81) =>
(81,81)*(81,81) =>
(81,81).
Note that numpy automatically adds dimensions at the beginning as needed - MATLAB does the same, but at the end.
Check out this post: "Difference between numpy.array shape (R, 1) and (R,)"
Specifically Gareth Rees's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With