### Shape and size of array

Posted under » Python Data Analysis on 15 June 2023

From Numpy array, there are several types of array but how do you know the shape and size of an array?

ndarray.ndim will tell you the number of axes, or dimensions, of the array.

ndarray.size will tell you the total number of elements of the array. This is the product of the elements of the array’s shape.

ndarray.shape will display a tuple of integers that indicate the number of elements stored along each dimension of the array. If, for example, you have a 2-D array with 2 rows and 3 columns, the shape of your array is (2, 3).

For example, if you create this array:

```array_example = np.array([[[0, 1, 2, 3],
[4, 5, 6, 7]],

[[0, 1, 2, 3],
[4, 5, 6, 7]],

[[0 ,1 ,2, 3],
[4, 5, 6, 7]]])

array_example.ndim
3

array_example.size
24

array_example.shape
(3, 2, 4)
```

Using arr.reshape() will give a new shape to an array without changing the data. Just remember that when you use the reshape method, the array you want to produce needs to have the same number of elements as the original array. If you start with an array with 12 elements, you’ll need to make sure that your new array also has a total of 12 elements. If you start with this array:

```a = np.arange(6)
print(a)
[0 1 2 3 4 5]
```

You can use reshape() to reshape your array. For example, you can reshape this array to an array with three rows and two columns:

```b = a.reshape(3, 2)

print(b)
[[0 1]
[2 3]
[4 5]]
```

With np.reshape, you can specify a few optional parameters:

```np.reshape(a, newshape=(1, 6), order='C')
array([[0, 1, 2, 3, 4, 5]])
```

a is the array to be reshaped.

newshape is the new shape you want. You can specify an integer or a tuple of integers. If you specify an integer, the result will be an array of that length. The shape should be compatible with the original shape.

order: C means to read/write the elements using C-like index order, F means to read/write the elements using Fortran-like index order (This is an optional parameter and doesn’t need to be specified.)

Indexing and slicing