Numpy array indexing

Numpy array indexing : The array indexing is used as the square bracket to the index array values. The Numpy will have the capability for arrays for indexing in Numpy and become popular. The Numpy array indexing is same as the tuples and list. The indexing is used to obtain the elements from array and used to obtain the entire columns and the rows from array. It is numeric python package on n dimensional arras called as the axes. The indexing and slicing are the common operations and works with numpy arrays.

Use of numpy:-

We can’t perform operations on the elements of the list directly.
From example we say that we cannot multiply the two lists directly.
So we multiply it by element wise carried out by numpy.
Basic use of the indexing and slicing are as follows:-
          Syntax is x [obj],
Here, x is array and object is index. The slice is index is of basic slicing.
Basic slicing is occurred when the object is:-
The slice of object is start: stop: step.
It is an integer
Tuple of slice has objects, integers.

 

One dimensional indexing:-

Example:-
import numpy as np
a=np.arange (0, 10)
a
Output:-
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Two dimensional indexing:-
import numpy as np
a=np.arange (8).reshape (2, 4)
a
Output:-                        
array([[0, 1, 2, 3],
           [4, 5, 6, 7]])
Access the value 7 selects the index 1 for row index 3 for column

1. Indexing by using the arrays:-

It is done by using an array as index. The numpy array is indexed with other array with exception of tuples.
The last element is indexed by the number -1, second last is -2 and so on.
Example:-
import numpy as np
x=np.arange ([1, 2, 3, 4, 5])
arr=x [np.array ([1, 2,-1])]
print(“\n elements are: \n”, arr)
Output:-
Elements are: - [2, 4, 5]

2. Slicing:-

To retrieve single value use the indexing method and to retrieve a collection of values you can use slicing.
It causes the problem for beginners to python and the numpy arrays.
The list, numpy can be sliced which means that the structure can be indexed and retrieved.
The slicing is important in ML to specify input and the output variables also splitting the training rows from testing rows.
The slicing is specified by the (:) operator it will extend from index and end one item before to the index.
One dimensional Example:-
int numpy as np
a=np.arange (10)
print(“\n Array is: \n”, a)
a[3:6]
a[:]
a[-3:2]
Output:-
array is:[0,1,2,3,4,5,6,7,8,9]
3, 4, 5
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[7, 6, 5, 4, 3]

Two dimensional Example:-
From numpy import array
Data=array ([[11, 22, 33], [44, 55, 66], [77, 88, 99]])
X,y=data[:,:-1],data[:,-1]
Print (x)
Print (y)
Output:-
[[11 22] [44 55] [77 88]][33 66 99]

3. Advance indexing:-

The ndarray of integer type or Boolean type has tuple with one sequence and non tuple sequence.
The advance indexing is the copy of the data and consists of two integer and Boolean type.

4. Purely integer indexing:-

The integer is used for the indexing and each element of first dimension is paired with second dimension.
The indexes are (0, 0), (1, 0), (2, 1) and then elements are selected.
Example:-
import numpy as np
a=np.array ([[1, 1], [3, 4], [5, 6]])
print(a [[0, 1, 2], [0, 0, 1])
Output:-
[1 3 6]

5. Combination of the advance and basic indexing:-

There are slice (:), eclipse as (…) and the new axis in index of the dimensions than advance indexes.
The concatenation of the indexing and result is the advanced element.
Program:-
import numpy as np
a=np.array([[0,1,2],[3,4,5],[6,7,8],[9,10,11])
print(a [1:2, 1:3])
print(a [1:2, 1, 2])
Output:-
[4, 5]
[4, 5]

6. Boolean Array indexing:-

This indexing is some like the Boolean expression as index and the elements are returned as they satisfy Boolean expression.
It is used for the filtering desired element values.
Example:-
import numpy as np
a=np.array ([10, 40, 80, 50,100])
a=print (a [a>40])
Output:-
                       [50 80 100]

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India