3.11 Basics of NumPy arrays

 v  Basics of NumPy arrays: -

Ø The basics of NumPy arrays consist of manipulating arrays to access the data and sub arrays, to split, reshape and join arrays.

Ø The basic array manipulations are

1.      Attributes of arrays.

2.      Slicing and indexing of arrays.

3.      Reshaping of arrays.

4.      Joining and splitting of arrays.

 

1)    Attributes of arrays: -

Ø The attributes of an array determine the size, shape, memory consumption and data types of an array.

Ø Each array has attributes like ndim (no of dimensions), shape (size of each dimension), and size (total size of array).

Ø N-dimensional array (ndarray) is most important object defined in NumPy.

Ø ndarray is a fixed size multi-dimensional container of elements of same type and size.

Ø Ndarray attributes are:

a.      ndarray.ndim

b.      ndarray.shape

c.      ndarray.size

d.      ndarray.itemsize

a)     ndarray.ndim: -

Ø  This array attribute returns the number of array dimensions.

Ø  In NumPy, the number of dimensions is referred to as Rank.

Ø  We get the dimension of array using ndim attribute.

Example: -

Output: -


b)     ndarray.shape: -

This array attribute returns tuple describing the length of each dimension.

We use shape attribute to get the number of elements in the row.

Example: -

Output: -


c)     ndarray.size: -

Ø  The array size attribute returns the total number of elements in the array.

 

Example: -

Output: -


d)     ndarray.itemsize: -

Ø  The attribute itemsize returns the memory size (in bytes) of elements in the array.

 

Example: -

Output: -


2)    Slicing and indexing of arrays: -

Ø Slicing means accessing a range of elements from an array.

Ø The syntax is similar to Python lists.



 

 


·        Start: - The starting index. If omitted, defaults to 0th position.

·        End: - The ending index. (not including value at this index).

·        Step size: - The spacing between index values, default is 1.

Ø Indexing means accessing individual elements of an array using their position (index).

Ø Indexing starts from 0 (like normal Python lists).

Ø In multi-dimensional arrays, you use comma-separated indices.

 

Example: -


Output: -


3)    Reshaping of arrays: -

Ø The reshape( ) function gives a new shape to an array without changing the data.

Ø This function takes a single argument that specifies the new shape of the array.

 

Example: -

Output: -


4)    Joining and splitting of arrays: -

Ø Array joining means combining two or more arrays into a single array.

Ø The concatenate( ) function can join two or more arrays into a single array.

 

Example: -

Output: -


Ø  Array splitting means to split a single array into multiple arrays.

Ø  We use array_split( ) for splitting an array.

Example: -


Output: -



Popular posts from this blog

operators in c programming

Variables in c

2.4 Arrays in c programming