# Tutorial – Numpy Indexing, Numpy Slicing, Numpy Where in Python ## Introduction

In your machine learning or data science projects you would often be required to fetch specific elements of the numpy array or slice its content. There are functions of numpy indexing, numpy slicing, and numpy where to carry out such operations. Throughout this tutorial, we will try to learn about these functions with the help of examples where we will also understand the syntax of these numpy functions.

### Importing Numpy Library

Before we begin, let’s just import numpy library.

In :
```import numpy as np
```

## Numpy Indexing

Array indexing refers to any use of the square brackets ([]) to index array values. There are many options to perform indexing.

### Example 1: Single element indexing

This example shows how we can access specific values from an array.

In :
```x = np.arange(10)
x
```
Out:
`array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])`
In :
```x
```
Out:
`5`

### Example 2: Using negative indexing

The negative indexing starts from last value and ends at first value. The negative indexing starts from 9 which -1th index. This is the reason -3rd index results in 7.

In :
```x[-3]
```
Out:
`7`

### Example 3: Multidimensional Indexing

Numpy arrays support multidimensional indexing for multidimensional arrays. Here using shape attribute, we have converted the flattened 1-D array into an array of 5 rows and 2 columns.

In :
```x.shape = (5,2)
x
```
Out:
```array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])```

While indexing we can fetch different values, here in this example we have accessed value at 3rd row and 1st column.

NOTE – The indexing in array starts from 0th index.

In :
```x[3,1]
```
Out:
`7`

The next operation is slicing.

## Numpy Slicing

It is possible to slice arrays to extract arrays of the same number of dimensions, but of different sizes than the original.

### Example 1: Using single dimension array for slicing

In :
```x = np.arange(10)
x
```
Out:
`array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])`

Here for slicing, we specify the starting value which is 1 in this case and ending value is 6. The result of slicing is again an array where starting value is included and ending value is excluded from the result.

In :
```x[1:6]
```
Out:
`array([1, 2, 3, 4, 5])`

### Example 2: Using multidimensional array for performing slicing

In :
```y = np.arange(25)
y
```
Out:
```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, 24])```
In :
```We have used reshape method to convert the shape of array to 5 x 5.
```
```  File "<ipython-input-10-6b7309d62191>", line 1
We have used reshape method to convert the shape of array to 5 x 5.
^
SyntaxError: invalid syntax
```
In :
```y = np.arange(25).reshape(5,5)
y
```
Out:
```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, 24]])```

Using the slicing method, we have fetched the result as 16 by specifying row index as 3rd and column index as 1st

In :
```y[3:4,1:2]
```
Out:
`array([])`

### Example 3: Using Negative indexing for slicing

Here we have used multidimensional arrays for slicing through negative indexing

In :
```x = np.array([[0, 1, 2],[3, 4, 5], [6, 7, 8],[5,2,6]])
x
```
Out:
```array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[5, 2, 6]])```

Here -2 index fetches 2nd row and its elements are printed.

In :
```x[-2]
```
Out:
`array([6, 7, 8])`

Lastly, we have numpy where operation.

## Numpy Where: np.where()

Numpy where function is used for executing an operation on the fulfillment of a condition.

### Syntax

numpy.where(condition[x,y])

condition : array_like,bool – This results either x if true is obtained otherwise y is yielded if false is obtained.

x,y : array_like – These are the values from which to choose. x, y and condition need to be broadcastable to some shape.

Output of the numpy where() is an array with elements from x where condition is True, and elements from y elsewhere.

### Example 1 : example of np.where()

In :
```a = np.arange(10)
a
```
Out:
`array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])`

Here the condition states that all the values which are lesser than 6 will be printed without any changes and values greater than 6 will be multiplied with 9. This is the reason we can see that values greater than 5 are changed.

In :
```np.where(a < 6, a, 9*a)
```
Out:
`array([ 0,  1,  2,  3,  4,  5, 54, 63, 72, 81])`

## Conclusion

Here in this tutorial we learned about the most fundamental operations performed on arrays. We explored numpy indexing, numpy slicing, and numpy where operations. It is highly recommended to be proficient with these functions of array.

Reference-  https://numpy.org/doc/