Num Py

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 20

Knowledge Discovery Process

Knowledge Discovery Process extracts meaningful insights from rawdata. It involves


the following series of steps.

Problem Definition

Data Collection

Data Preprocessing

Data Transformation

Data Mining

Data Analysis

Data Visualization

Python provides many powerful libraries that can be used to perform various tasks
described above.

NumPy

An essential library used for scientific computing in Python.

Holds data in N-dimensional array (ndarray) objects, which can store data in
multiple dimensions.

Supports performing efficient array operations through Broadcasting feature.

pandas

Provides functionality to deal with structured data.

Stores Data in different Primary data structures: Series, DataFrame and Panel.

matplotlib

Widely used for Data Visualization.

Used to generate various types of plots.

SciPy

A collection of efficient numerical algorithms used in Numerical integration,


Signal processing and Optimization.
NLTK

Performs different tasks related to Natural Language Processing.


scikit-learn

Python library used for Machine learning

Jupyter

Provides web based interactive computational environment.

Combines code, rich text, plots, media and mathematical equations together.
Bokeh

Offers interactive Web visualization features.


PyMongo

PyMongo distribution comprises tools for working with MongoDB.

MongoDB is a highly scalable and robust NoSQL database.

Scientific Distributions
Data scientist has to manually install all the python libraries required for
performing various tasks involved in Knowledge Discovery Process.

Drawbacks of Manual Installation

Installing few libraries may require an installation of other dependencies.

Time-consuming task.

Installation of few libraries may be unsuccessful.

Prone to manual errors.

All draw backs of manual installation could be overcome using any one of the
available Scientific Distributions.

A Scientific distribution is a collection of Python libraries, which provide a


ready to use Python environment.

A Scientific distribution is easy to download, install and use.

Few popular distributions include Anaconda, Enthought Python, PythonXY, WinPython.

In this course, you will learn about Anaconda.

Anaconda is a popular high-performance platform used for data science.

The base version is open source and contains over 100+ packages from Python, R, and
Scala.

Additionally, provides access to over 700+ packages that could be installed and
managed using conda.

Anaconda is available for 32-bit and 64-bit Operating systems: Windows, Linux, and
Mac OSX.

Installing Anaconda
Steps for installing Anaconda

Identify your system's OS and its architecture, i.e., 32-bit or 64-bit.

Go to Anaconda's downloads page.

Select the download section of your OS.

Choose the Python Version, i.e 3.x or 2.x, based on your interest.

Download the installer based on your system architecture.


Optional: Verify data integrity with MD5 or SHA-256.

Install the downloaded file.

Anaconda Navigator

Provides access to various components of Anaconda Distribution.

The following windows appear at the left side of Anaconda Navigator.

Home

Environment

Projects

Learning

Community

Home and Environment Windows


Home Window

Opened by default with root environment.

Enables launching working environment through various modes like Jupyter Notebooks,
Jupyter qt-console, and Sypder IDE.

Environment Window

Shows information about various available environments.

Details of packages installed for each available environment is viewable.

Projects, Learning, and Community Windows


Project Window

Provides tools for managing Anaconda projects.


Learning Window

Provides access to popular Data Science Resources.


Community Window

Provides links to popular Data Science Events, Forums, Blogs, etc.

Anaconda Prompt

Anaconda Prompt is the command line tool provided by Anaconda Distribution.

You can access anaconda's default Python interactive interpreter, using command
'python'.

You can also work with Conda, anaconda's package manager.

Command for checking Conda's version.

conda --version
Command for viewing available environments.
conda info --envs
Creating New Environment
By default anaconda comes with root environment.

A new environment testenv, with Python 2.7, can be created using the below command.

conda create --name testenv python=2.7

Command for activating testenv

activate testenv

Command for viewing available packages in testenv.

conda list

Installing a new package

Accessing numpy package from current testenv results in ImportError.

You can install the package using conda install.

conda install numpy

Now you can verify the numpy availability with conda list command.

After successful installation, you can access numpy from testenv, without any
errors.

IPython
IPython provides interactive working environment, which is highly convenient and
efficient.

Its major components are:

An interactive Python shell.

A Jupyter kernel that allows working with Python code in various interactive front
ends.

Features of IPython
Python statements and System commands can be executed in IPython.

IPython supports Tab completion feature.

With Magic Methods, IPython enables performing many tasks easily.

IPython caches Input and Output history.

IPython supports Parallel Computing.

Launching Jupyter qt-console

How to open IPython in Jupyter qt-console from Anaconda Navigator.

How to execute Python statements in IPython?


How to run System commands in IPython?

Knowing about an object or a method.

Using Tab completion feature.

Understanding Magic Methods


Magic Methods begin with a single % or double %% symbols.

Line Magic Method: Magic method starting with one % symbol.

Line Magic Method is applicable only on a single line of code.

Cell Magic Method: Magic method starting with two %% symbols.

Cell Magic Method is applicable on multiple lines of code, written in a single


cell.

Q)

Open a IPython Console and perform the following tasks.

Define the list fruits = ['apple', 'mango', 'kiwi', 'watermelon', 'pear'] in first
cell.

In second cell type fru and press tab.

Simply display content of list fruits.

Q)Determine the length of each defined fruit names and save it in list fruits_len.

Hint: Use list Comprehension


Display contents of fruits_len .

Find the fruit names that start with 'm' or 'p' and save it in list fruits_mp.

Hint: Use list Comprehension


Display contents of fruits_mp.

In [1]: fruits = ['apple', 'mango', 'kiwi', 'watermelon', 'pear']


In [2]: print(fruits)
['apple', 'mango', 'kiwi', 'watermelon', 'pear']
In [3]: fruits_len = [len(j) for j in fruits]
In [4]: print(fruits_len)
[5, 5, 4, 10, 4]
In [5]: fruits_mp = [j for j in fruits if j.startswith('m') or j.startswith('p')]
In [6]: print(fruits_mp)
['mango', 'pear']

Q)Make use of magic method %save to save the previous six commands in a file
sample_script.py. - Hint : Use the expression %save sample_script.py 1-6- View the
contents of file sample_script.py using magic method %more. - Try the command %more
sample_script.py

Starting Jupyter Notebook Server


Starting Jupyter Notebook Server
Jupyter Notebook server can be launched from Anaconda Navigator Home Window. The
Notebook server opens in a browser and displays contents of starting folder.
The displayed page contains the following three tabs.

Files displays folders and files present in starting folder.

Running holds information of notebooks that are running.

Clusters contain information of notebooks running in parallel mode.

Creating a Folder
Creating a Folder
A folder can be created using Folder option present under New section.

The GIF illustrates the following.

Creating an Untitled folder.

Renaming it to MyJupyterNoteBooks, and

Changing working directory to MyJupyterNoteBooks folder.

Starting a Jupyter Notebook


Starting a Jupyter Notebook
A Jupyter Notebook can be created by Choosing an available Kernel.

The Kernel enables the environment required for executing the code snippets.

The GIF illustrates

Creation of Untitled Notebook.

Renaming it to MyFirstNoteBook.

Checking it's running status in Files / Running tabs.

Shutting down the notebook MyFirstNoteBook.

About a Notebook Cell


The basic element of a Notebook is Cell.

A user is allowed to write either code snippets or markdown text, inside a cell.

A Markdown Text can be used to embed Normal text, Header Text, Unordered, Ordered
Lists, Hyperlinks, Tables, Images, Videos, HTML content, and other useful elements
inside the Notebook.

Markdown Basics
Markdown Basics
In this section, you will be writing the following elements in Markdown.

Headers : Continuous 1 to 6 Hash Symbols are used to create Headers.

Emphasizing Text : Asterix *, or underscores _ are used to emphasize the text in


bold or italic.

Markdown Basics
Unordered Lists : Either of the symbols - Asterix *, hypen -, plus + are used.

Ordered Lists : Numbers followed with a dot . and


a space are used.

Nested Unordered Lists : The nested lists are indexed with a minimum of four spaces
and followed with symbols.

Justifying Text of a list element : Two spaces, at the end of each line, are used
to justify multiple lines of text.

Markdown Basics
Code snippets: Pair of three back quotes are used.

Hyperlinks: Text, written in a pair of square brackets, is linked to a Hyperlink,


specified in a pair of parenthesis.

Reference Links: Text and Reference both are written in two different pairs of
square brackets.

HTML Content : HTML tags can be directly used in Markdown.


Writing Your First Notebook
Writing Your First Notebook
The above-shown GIF performs the following tasks in the notebook - MyFirstNoteBook.

Defines the string 's' with value Welcome to Jupyter Notebooks!!!.

Displays the string 's'.

Provides the required description.


Writing Your First Notebook...
Writing Your First Notebook...
The above GIF illustrates performing the following, additional tasks in
MyFirstNoteBook.

Determines the length of 's'.

Obtains the slice Jupyter Notebooks from 's'.

Find the number of vowels in 's'.

Filter the words starting with either 'J' or 'N'.

Provides titles as required.

NumPy
NumPy
NumPy is a Python library, which supports efficient handling of various numerical
operations on arrays holding numeric data.

These arrays are known as N-dimensional arrays or ndarrays.

Ndarrays are capable of holding data elements in multiple dimensions.

Each data element of a ndarray is of fixed size.

All elements of a ndarray are of same data type.

N-dimensional array (ndarray)


N-dimensional array is an object, capable of holding data elements of same type and
of a fixed size in multiple dimensions.
Creation of a 1-D array of five elements, from a list is shown in Example 1.

Example 1

import numpy as np
x = np.array([5, 8,
9, 10,
11]) # using 'array' method

type(x) # Displays type of array 'x'


Output

numpy.ndarray

N-dimensional array (ndarray)...


Creation of a 2-D array from a list of lists is shown in Example 2.
Example 2

y = np.array([[6, 9, 5],
[10, 82, 34]])
print(y)
Output

array([[ 6, 9, 5],
[10, 82, 34]])

ndarray Attributes
Some of the important attributes of a ndarray are

ndim : Returns number of dimensions.

shape: Returns Shape in tuple.

size : Total number of elements.

dtype : Type of each element.

itemsize : Size of each element in Bytes.

nbytes : Total bytes consumed by all elements.

Example 3

print(y.ndim, y.shape, y.size, y.dtype, y.itemsize, y.nbytes)


Output

2 (2, 3) 6 int32 4 24

Numpy dtypes
Numpy supports various data types based on number of bytes required by the data
elements.

Data type can be explicitly specified with dtype argument.

A ndarray, holding float values is defined in Example 4.

Example 4

y = np.array([[6, 9, 5],
[10, 82, 34]],
dtype='float64')
print(y)
print(y.dtype)
Output

array([[ 6., 9., 5.],


[ 10., 82., 34.]])
float64

Numpy Array creation


N-dimensional arrays or ndarray can be created in multiple ways in numpy.

Now let us focus on creating ndarray,

From Python built-in datatypes : lists or tuples

Using Numpy array creation methods like ones, ones_like, zeros, zeros_like

Using Numpy numeric sequence generators.

Using Numpy random module.

By reading data from a file.

import numpy as np
import numpy as np
n = [[-1, -2, -3, -4], [-2,-4, -6, -8]]
y
y = np.array(n)
print(y.dtype)
print(y.dtype)
int64
ndim
print(y.ndim)
2
shape
print(y.shape)
(2, 4)
size
print(y.size)
8
print(y.dtype)
int64
nbytes
print(y.nbytes)
64

ndarrays from Lists


Data available in lists, or tuples can be converted into numpy arrays using array
method.

Creating a 3-Dimensional array from a list of list of lists is shown in Example 1.

import numpy as np
a = [[[4.1, 2.5], [1.1, 2.3], [9.1, 2.5]],
[[8.6, 9.9],[3.6, 4.3], [6.6, 0.3]]]
x = np.array(a, dtype='float64')

type(x), x.ndim, x.shape

Output

numpy.ndarray, 3, (2, 3, 2)

Array Creation Methods


Numpy allows creation of arrays with default values like 0, 1, or another value.
Example 1: Using zeros method

x = np.zeros(shape=(2,4))
print(x)
Output of Example 1

[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
Example 2 : Using full method

y = np.full(shape=(2,3), fill_value=10.5)
print(y)
Output of Example 2

[[ 10.5 10.5 10.5]


[ 10.5 10.5 10.5]]

Numeric Sequence Generators


Two major methods used in numpy for generating number sequences are,
np.fill_diagonal(array with all 0s of specific axb, fill value)
arange : Numbers created based on step value.

Syntax - numpy.arange([start, ]stop, [step, ]dtype=None)

linspace : Numbers created based on size value.

Syntax - numpy.linspace(start, stop, #num inbetween, endpoint=True, retstep=False,


dtype=None)

Example 1

x = np.arange(3, 15, 2.5) # 2.5 is step


print(x)
Output of Example 1

[ 3. 5.5 8. 10.5 13. ]


Example 2

y = np.linspace(3, 15, 5) # 5 is size of array 'y'


print(y)
Output of Example 2

[ 3. 6. 9. 12. 15.]

Random Numbers Generator


random module of numpy is used to generate various random sequences.
Example
np.random.seed(100) # setting seed
x = np.random.rand(2) # 2 random numbers between 0 and 1

print(x)
Output of Example1

[ 0.54340494 0.27836939]
Example 2

np.random.seed(100) # setting seed


y = np.random.randint(10, 50, 3) # 3 random integers between 10 and 50

print(y)
Output of Example 2

[18 34 13]

Simulating Normal Distribution


randn is used to simulate standard normal distribution.
Example 1

np.random.seed(100)
x = np.random.randn(3) # Standard normal distribution

print(x)
Output of Example1

Try it Out - 3D array


Define a random 3-D array x4 of shape (3, 4, 2) and of numbers between 0 and 1.

Simulate a random normal distribution of 20 elements, whose mean is 5 and standard


deviation 2.5 . Capture the result in x5.

[-1.74976547 0.3426804 1.1530358 ]


Example 2

np.random.seed(100)
x = 10 + 2*np.random.randn(3) # normal distribution with mean 10 and sd 2

print(x)
Output of Example 2

[ 5.62558632 10.85670101 12.88258951]

Reading Data from a file


loadtxt is used to read data from a text file or any input data stream.
Example 1

from io import StringIO


import numpy as np

x = StringIO('''88.25 93.45 72.60 90.90


72.3 78.85 92.15 65.75
90.5 92.45 89.25 94.50
''')

d = np.loadtxt(x,delimiter=' ')

print(d)
print(d.ndim, d.shape)
Output of Example 1

[[ 88.25 93.45 72.6 90.9 ]


[ 72.3 78.85 92.15 65.75]
[ 90.5 92.45 89.25 94.5 ]]
2 (3, 4)

Import numpy package as np

Define a ndarray x1 from list, `[[[-1,1],[-2,2]],[[-3 3], [-4, 4]]]

Determine the following attributes of x1

Number of dimensions

Shape

Size

Try it Out - ndarray with Shapes


Import numpy package as np

Define a ndarray x2, whose shape is (3, 2, 2) and contains all 1's.

Define a ndarray x3, whose shape is (4,4) and contains 1's on diagonal and 0's
elsewhere.

Try it Out - More on ndarray


Define a ndarray x6, having first 20 even numbers.

Hint : Make use of arange.


Define a ndarray x7, having equally spaced 30 numbers between 10 and 20.

Hint : Make use of linspace.


16996 Learners successfully completed this handson.
Last successful completion happened on Sep 16, 2021

Reshaping ndarrays
Shape of an array can be changed using reshape.
Example

import numpy as np
np.random.seed(100)
x = np.random.randint(10, 100, 8)
print(x, end='\n\n')
y = x.reshape(2,4)
print(y, end='\n\n')
z = x.reshape(2,2,2)
print(z, '\n\n')
Output

[18 34 77 97 89 58 20 62]
[[18 34 77 97]
[89 58 20 62]]
[[[18 34]
[77 97]]
[[89 58]
[20 62]]]

Stacking arrays vertically


Two or more arrays can be joined vertically using the generic vstack method.
Example

import numpy as np
x = np.array([[-1, 1], [-3, 3]])
y = np.array([[-2, 2], [-4, 4]])
np.vstack((x,y))
Output

array([[-1, 1],
[-3, 3],
[-2, 2],
[-4, 4]])

Stacking arrays horizontally


Two or more arrays can be joined horizontally using the generic hstack method.
Example

import numpy as np
x = np.array([[-1, 1], [-3, 3]])
y = np.array([[-2, 2], [-4, 4]])
z = np.array([[-5, 5], [-6, 6]])
np.hstack((x,y,z))
Output

array([[-1, 1, -2, 2, -5, 5],


[-3, 3, -4, 4, -6, 6]])

Splitting arrays vertically


Arrays can be split vertically using the generic vsplit method.
Example : Spliting Vertically into two arrays

import numpy as np
x = np.arange(30).reshape(6, 5)
res = np.vsplit(x, 2)
print(res[0], end='\n\n')
print(res[1])
Output

[[ 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]
[25 26 27 28 29]]

Splitting arrays vertically


It is also possible to split at specific row numbers using vsplit.
Example : Spliting Vertically into three arrays

import numpy as np
x = np.arange(30).reshape(6, 5)
res = np.vsplit(x, (2, 5))
print(res[0], end='\n\n')
print(res[1], end='\n\n')
print(res[2])
Output

[[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]]
[[25 26 27 28 29]]

Splitting arrays Horizontally


Arrays can be split horizontally using the generic hsplit method.
Example

import numpy as np
x = np.arange(10).reshape(2, 5)
res = np.hsplit(x, (2,4))
print(res[0], end='\n\n')
print(res[1], end='\n\n')
print(res[2])
Output

[[0 1]
[5 6]]
[[2 3]
[7 8]]
[[4]
[9]]

Try it Out - Array Manipulation 1


Create a ndarray x having first 20 natural numbers, using arange method.

Determine the shape of x.

Change the shape of x to (2, 10) and assign it to new array y.

Split the array y horizontally in to two arrays.

Try it Out - Array Manipulation 2


Now, Change the shape of x to (4, 5) and assign it to new array z.

Split the array z vertically in to two arrays.

Hint: Use vsplit

Try it Out - Join Arrays


Create a 2-D array p, of shape (2, 2) with elements 3, 6, 9, 12.

Create a 2-D array q, of shape (2, 3) with elements 15, 18, 21, 24, 27, 30.

Join the two arrays p and q horizontally. use hstack

Basic Operations with Scalars


Operations in Numpy are carried out element wise.

Hence the expression x + 10, increases every element of array x by 10.


Example

import numpy as np
x = np.arange(6).reshape(2,3)
print(x + 10, end='\n\n')
print(x * 3, end='\n\n')
print(x % 2)
Output

[[10 11 12]
[13 14 15]]
[[ 0 3 6]
[ 9 12 15]]
[[0 1 0]
[1 0 1]]

Basic Operations between Arrays


Operations between arrays also happen element wise.
Example

import numpy as np
x = np.array([[-1, 1], [-2, 2]])
y = np.array([[4, -4], [5, -5]])
print(x + y, end='\n\n')
print(x * y)
Output

[[ 3 -3]
[ 3 -3]]
[[ -4 -4]
[-10 -10]]

Basic Operations between Arrays


It is also possible to perform operations on arrays with varying size and shape.
Example

import numpy as np
x = np.array([[-1, 1], [-2, 2]])
y = np.array([-10, 10])
print(x * y)
Output

[[10 10]
[20 20]]
This is due Broadcasting feature exhibited by numpy arrays.

Broadcasting in NumPy
Element wise operations between arrays are possible only when they have the same
shape or compatible for Broadcasting.

Steps followed to verify the feasibility of Broadcasting between arrays are:

Initially, compare the dimensions of all arrays.

If dimensions do not match, prepend 1's to shape of a smaller array so that it


matches dimensions of a larger array.
Start comparing array shapes from the last dimension and move backward.

If the shape of both arrays are equal or either of it has a shape of 1, continue
the comparison.

Else at any dimension, if step 4 fails, broadcasting between arrays is not


feasible.

Finally, the resulted broadcasting array shape would be maximum of two compared
shapes in each dimension.

Feasibility of Broadcasting
Below examples show feasibility of broadcasting between two arrays, having shape s1
and s2 respectively.
Examples

Given: s1 = (4, 3); s2 = (3,)


Step 1 and 2: s1 = (4, 3); s2 = (1, 3)
Step 3 and 4: pass in 2 dimensions
Result : Broadcasting feasible;
resulted array shape - (4,3)
Given: s1 = (5,); s2 = (5,4,3)
Step 1 and 2: s1 = (1, 1, 5); s2 = (5, 4, 3)
Step 3 and 4: fail in last dimension. ( 5 != 3)
Result : Broadcasting not feasible.

NumPy Universal Functions


Numpy provides a lot of mathematical functions, in the form of Universal functions.
Example

import numpy as np
x = np.array([[0,1], [2,3]])
print(np.square(x), end='\n\n')
print(np.sin(x))
Output

[[0 1]
[4 9]]
[[ 0. 0.84147098]
[ 0.90929743 0.14112001]]
To know more on Universal functions, refer this link.

https://docs.scipy.org/doc/numpy/reference/ufuncs.html

NumPy Array Methods


Many of the universal functions are available as methods of ndarray class.

By default sum method adds all array elements.

It is also possible to apply sum method on elements of a specific dimension, using


axis argument.

Example

import numpy as np
x = np.array([[0,1], [2, 3]])
print(x.sum(), end='\n\n')
print(x.sum(axis=0), end='\n\n')
print(x.sum(axis=1))
Output

6
[2 4]
[1 5]
8 of

Try it Out - Operations on Arrays 1


Create a 2-D array y of shape (2, 3), having numbers from 1 to 6.

Square each element of y.

Add 5 to each element of resulted array.

16711 Learners successfully completed this handson.


Last successful completion happened on Sep 16, 2021

Try it Out - Operation on Arrays 2


Create a array x of shape (5, 6), having random integers between -30 and 30.

Determine the cumulative sum of x along axis 0.

Determine the cumulative sum of x along axis 1.

16669 Learners successfully completed this handson.


Last successful completion happened on Sep 16, 2021

Try it Out - Operation on Arrays 3


Create a array x of shape (50, ), having 50 random numbers from a normal
distribution of mean 10 and standard deviation 2.

Calculate mean of x

Calculate Standard deviation of x

Calculate variance of x.

16655 Learners successfully completed this handson.

Indexing, Slicing a 1-D ndarray


Slicing refers to extracting a portion of existing array.

This can be achieved with a slice object.

A slice object is of the form start:end:step. All three are optional.

Having only a single number inside square brackets refer to start index.

Example: Slicing a 1-D array

x = np.array([5, 10, 15, 20, 25, 30, 35])


print(x[1]) # Indexing
print(x[1:6]) # Slicing
print(x[1:6:3]) # Slicing
Output of Example

10
[10 15 20 25 30]
[10 25]
Indexing, Slicing a 2-D ndarray
Two slice objects, one for each dimension, are required to slice a 2-D array.

They are separated by a comma (,) and having only a single slice object inside
square brackets refers to first dimension.

All elements of a single dimension can be referred with a colon (:).

Example

import numpy as np
y = np.array([[0, 1, 2],
[3, 4, 5]])
print(y[1:2, 1:3])
print(y[1])
print(y[:, 1])
Output

[[4 5]]
[3 4 5]
[1 4]
3 of 12

Slicing Higher Dimensions ndarrays


For slicing an n dimensional ndarray, n slice objects are required.

Having only a single slice object refers to first dimension.

Example

z = np.array([[[-1, 1], [-2, 2]],


[[-4, 4], [-5, 5]],
[[-7, 7], [-9, 9]]])
print(z[1,:,1]) # index 1 element in row of index 1
print(z[1:,1,:]) # From all outer rows except the first, select 1st index element
(which itself is an array) completely.
print(z[2]) # print 2nd index element
Output

[4 5]
[[-5 5]
[-9 9]]
[[-7 7]
[-9 9]]

Iterating using 'for'


for loop can be used to iterate over every dimensional element.
Example

x = np.array([[-1, 1], [-2, 2]])


for row in x:
print('Row :',row)
Output

Row : [-1 1]
Row : [-2 2]

Iterating using 'nditer'


nditer method of numpy creates an iterator, which enable accessing each element one
after the other.
Example

import numpy as np
x = np.array([[0,1], [2, 3]])
for a in np.nditer(x):
print(a)
Output

0
1
2
3

Boolean Indexing
Checking if every element of an array satisfies a condition, results in a Boolean
array.

This Boolean array can be used as index to filter elements that satisfy the
condition.

Example

import numpy as np
x = np.arange(10).reshape(2,5)
condition = x % 2 == 0
print(condition)
print(x[condition])
Output

[[ True False True False True]


[False True False True False]]
[0 2 4 6 8]

Try it Out - Array Indexing


Create a array x of shape (6, 5), having first 30 natural numbers.

Obtain elements of last row.

Obtain elements of middle column.

Obtain elements, overlapping first two rows and last three columns.

16652 Learners successfully completed this handson.


Last successful completion happened on Sep 17, 2021

Try it Out - Slicing


Create a array x of shape (2, 3, 5), having first 30 natural numbers.

Create a boolean array b of shape (2,), having elements True, False.

Try the following expressions

x[b]

x[b,:,1:3]

16644 Learners successfully completed this handson.


Last successful completion happened on Sep 17, 2021

You might also like