0% found this document useful (0 votes)
15 views

Unit 4 Final

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Unit 4 Final

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

Unit-IV- Data Analysis using Python libraries

Unit 4 Data Analysis using Python libraries

NumPy: Introduction, NdArray object, Data Types, Array Attributes, Indexing and
Slicing,Array manipulation, mathematical functions, Matplotlib; Pandas: Introduction
to pandas data structures-series-Data Frame-Panel-basic functions-descriptive statistics
function-iterating data frames-statistical functions-aggregations-visualization- plotting
graphs using plotly Library.

Case Study: Sales Forecasting.

NumPy:

What is NumPy?

NumPy is a Python library used for working with arrays.

It also has functions for working in domain of linear algebra, fourier transform, and
matrices.

NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can
use it freely.

NumPy stands for Numerical Python.

Why Use NumPy?

In Python we have lists that serve the purpose of arrays, but they are slow to process.

NumPy aims to provide an array object that is up to 50x faster than traditional Python lists.

The array object in NumPy is called ndarray, it provides a lot of supporting functions that make
working with ndarray very easy.

Arrays are very frequently used in data science, where speed and resources are very important.

Data Science: is a branch of computer science where we study how to store, use and analyze
data for deriving information from it.

Why is NumPy Faster Than Lists?

NumPy arrays are stored at one continuous place in memory unlike lists, so processes can
access and manipulate them very efficiently.

This behavior is called locality of reference in computer science.

This is the main reason why NumPy is faster than lists. Also it is optimized to work with latest
CPU architectures.

Which Language is NumPy written in?


NumPy is a Python library and is written partially in Python, but most of the parts that require
fast computation are written in C or C++.

Installation of NumPy

If you have Python and PIP already installed on a system, then installation of NumPy is very
easy.

Install it using this command:

C:\Users\Your Name>pip install numpy

If this command fails, then use a python distribution that already has NumPy installed like,
Anaconda, Spyder etc.

Import NumPy

Once NumPy is installed, import it in your applications by adding the import keyword:

import numpy

Now NumPy is imported and ready to use.

import numpy

arr = numpy.array([1, 2, 3, 4, 5])

print(arr)

NumPy as np

NumPy is usually imported under the np alias.

alias: In Python alias are an alternate name for referring to the same thing.

Create an alias with the as keyword while importing:

import numpy as np

Now the NumPy package can be referred to as np instead of numpy.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)

Checking NumPy Version

The version string is stored under __version__ attribute.

import numpy as np
print(np.__version__)

will discuss the various array attributes of NumPy.

ndarray.shape

This array attribute returns a tuple consisting of array dimensions. It can also be used to resize
the array.

import numpy as np

a = np.array([[1,2,3],[4,5,6]])

print a.shape

The output is as follows −

(2, 3)

# this resizes the ndarray

import numpy as np

a = np.array([[1,2,3],[4,5,6]])

a.shape = (3,2)

print a

The output is as follows −

[[1, 2]

[3, 4]

[5, 6]]

Example 3

NumPy also provides a reshape function to resize an array.

Live Demo

import numpy as np

a = np.array([[1,2,3],[4,5,6]])

b = a.reshape(3,2)

print b

The output is as follows −


[[1, 2]

[3, 4]

[5, 6]]

ndarray.ndim

This array attribute returns the number of array dimensions.

Example 1

Live Demo

# an array of evenly spaced numbers

import numpy as np

a = np.arange(24)

print a

The output is as follows −

[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

Example 2

Live Demo

# this is one dimensional array

import numpy as np

a = np.arange(24)

a.ndim

# now reshape it

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

print b

# b is having three dimensions


The output is as follows −

[[[ 0, 1, 2]

[ 3, 4, 5]

[ 6, 7, 8]

[ 9, 10, 11]]

[[12, 13, 14]

[15, 16, 17]

[18, 19, 20]

[21, 22, 23]]]

numpy.itemsize

This array attribute returns the length of each element of array in bytes.

Example 1

Live Demo

# dtype of array is int8 (1 byte)

import numpy as np

x = np.array([1,2,3,4,5], dtype = np.int8)

print x.itemsize

The output is as follows −

Example 2

Live Demo

# dtype of array is now float32 (4 bytes)

import numpy as np

x = np.array([1,2,3,4,5], dtype = np.float32)


print x.itemsize

The output is as follows −

numpy.flags

The ndarray object has the following attributes. Its current values are returned by this function.

Sr.No. Attribute & Description

C_CONTIGUOUS (C)

The data is in a single, C-style contiguous segment

F_CONTIGUOUS (F)

The data is in a single, Fortran-style contiguous segment

OWNDATA (O)

The array owns the memory it uses or borrows it from another object

WRITEABLE (W)

The data area can be written to. Setting this to False locks the data, making it read-only
5

ALIGNED (A)

The data and all elements are aligned appropriately for the hardware

UPDATEIFCOPY (U)

This array is a copy of some other array. When this array is deallocated, the base array will be
updated with the contents of this array

Example

The following example shows the current values of flags.

Live Demo

import numpy as np

x = np.array([1,2,3,4,5])

print x.flags

The output is as follows −

C_CONTIGUOUS : True

F_CONTIGUOUS : True

OWNDATA : True

WRITEABLE : True

ALIGNED : True

UPDATEIFCOPY : False

Create a NumPy ndarray Object


NumPy is used to work with arrays. The array object in NumPy is called ndarray.

We can create a NumPy ndarray object by using the array() function.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)

print(type(arr))

type(): This built-in Python function tells us the type of the object passed to it. Like in above
code it shows that arr is numpy.ndarray type.

To create an ndarray, we can pass a list, tuple or any array-like object into the array() method,
and it will be converted into an ndarray:

Use a tuple to create a NumPy array:

import numpy as np

arr = np.array((1, 2, 3, 4, 5))

print(arr)

Dimensions in Arrays

A dimension in arrays is one level of array depth (nested arrays).

nested array: are arrays that have arrays as their elements.

0-D Arrays

0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.

Create a 0-D array with value 42

import numpy as np

arr = np.array(42)

print(arr)

1-D Arrays

An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.

These are the most common and basic arrays.

Create a 1-D array containing the values 1,2,3,4,5:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])

print(arr)

2-D Arrays

An array that has 1-D arrays as its elements is called a 2-D array.

These are often used to represent matrix or 2nd order tensors.

NumPy has a whole sub module dedicated towards matrix operations called numpy.mat

Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr)

3-D arrays

An array that has 2-D arrays (matrices) as its elements is called 3-D array.

These are often used to represent a 3rd order tensor.

Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and
4,5,6:

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(arr)

Check Number of Dimensions?

NumPy Arrays provides the ndim attribute that returns an integer that tells us how many
dimensions the array have.

Check how many dimensions the arrays have:

import numpy as np

a = np.array(42)

b = np.array([1, 2, 3, 4, 5])

c = np.array([[1, 2, 3], [4, 5, 6]])

d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(a.ndim)
print(b.ndim)

print(c.ndim)

print(d.ndim)

Higher Dimensional Arrays

An array can have any number of dimensions.

When the array is created, you can define the number of dimensions by using the ndmin
argument.

Create an array with 5 dimensions and verify that it has 5 dimensions:

import numpy as np

arr = np.array([1, 2, 3, 4], ndmin=5)

print(arr)

print('number of dimensions :', arr.ndim)

Access Array Elements

Array indexing is the same as accessing an array element.

You can access an array element by referring to its index number.

The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the
second has index 1 etc.

ExampleGet your own Python Server

Get the first element from the following array:

import numpy as np

arr = np.array([1, 2, 3, 4])

print(arr[0])
ExampleGet your own Python Server

Get the second element from the following array.

import numpy as np

arr = np.array([1, 2, 3, 4])

print(arr[1])

ExampleGet your own Python Server

Get third and fourth elements from the following array and add them.

import numpy as np

arr = np.array([1, 2, 3, 4])

print(arr[2] + arr[3])

Access 2-D Arrays

To access elements from 2-D arrays we can use comma separated integers representing the
dimension and the index of the element.

Think of 2-D arrays like a table with rows and columns, where the dimension represents the
row and the index represents the column.

Access the element on the first row, second column:

import numpy as np

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('2nd element on 1st row: ', arr[0, 1])

ExampleGet your own Python Server

Access the element on the 2nd row, 5th column:

import numpy as np

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])


print('5th element on 2nd row: ', arr[1, 4])

Access 3-D Arrays

To access elements from 3-D arrays we can use comma separated integers representing the
dimensions and the index of the element.

Access the third element of the second array of the first array:

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

print(arr[0, 1, 2])

Example Explained

arr[0, 1, 2] prints the value 6.

And this is why:

The first number represents the first dimension, which contains two arrays:

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

and:

[[7, 8, 9], [10, 11, 12]]

Since we selected 0, we are left with the first array:

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

The second number represents the second dimension, which also contains two arrays:

[1, 2, 3]

and:

[4, 5, 6]

Since we selected 1, we are left with the second array:

[4, 5, 6]

The third number represents the third dimension, which contains three values:

6
Since we selected 2, we end up with the third value:

Negative Indexing

Use negative indexing to access an array from the end.

Print the last element from the 2nd dim:

import numpy as np

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('Last element from 2nd dim: ', arr[1, -1])

NumPy Array Slicing

Slicing arrays

Slicing in python means taking elements from one given index to another given index.

We pass slice instead of index like this: [start:end].

We can also define the step, like this: [start:end:step].

If we don't pass start its considered 0

If we don't pass end its considered length of array in that dimension

If we don't pass step its considered 1

Slice elements from index 1 to index 5 from the following array:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5])

Note: The result includes the start index, but excludes the end index.

Slice elements from index 4 to the end of the array:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[4:])

Slice elements from the beginning to index 4 (not included):

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[:4])

Negative Slicing

Use the minus operator to refer to an index from the end:

Slice from the index 3 from the end to index 1 from the end:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[-3:-1])

STEP

Use the step value to determine the step of the slicing:

Return every other element from index 1 to index 5:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[1:5:2])

Return every other element from the entire array:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[::2])

Slicing 2-D Arrays

From the second element, slice elements from index 1 to index 4 (not included):

import numpy as np

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

print(arr[1, 1:4])

Note: Remember that second element has index 1.

From both elements, return index 2:

import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

print(arr[0:2, 2])

From both elements, slice index 1 to index 4 (not included), this will return a 2-D array:

import numpy as np

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

print(arr[0:2, 1:4])

Data Types in Python

By default Python have these data types:

strings - used to represent text data, the text is given under quote marks. e.g. "ABCD"

integer - used to represent integer numbers. e.g. -1, -2, -3

float - used to represent real numbers. e.g. 1.2, 42.42

boolean - used to represent True or False.

complex - used to represent complex numbers. e.g. 1.0 + 2.0j, 1.5 + 2.5j

Data Types in NumPy

NumPy has some extra data types, and refer to data types with one character, like i for integers,
u for unsigned integers etc.

Below is a list of all data types in NumPy and the characters used to represent them.

i - integer

b - boolean

u - unsigned integer

f - float

c - complex float

m - timedelta

M - datetime
O - object

S - string

U - unicode string

V - fixed chunk of memory for other type ( void )

Checking the Data Type of an Array

The NumPy array object has a property called dtype that returns the data type of the array:

Get the data type of an array object:

import numpy as np

arr = np.array([1, 2, 3, 4])

print(arr.dtype)

Get the data type of an array containing strings:

import numpy as np

arr = np.array(['apple', 'banana', 'cherry'])

print(arr.dtype)

Creating Arrays With a Defined Data Type

We use the array() function to create arrays, this function can take an optional argument: dtype
that allows us to define the expected data type of the array elements:

Create an array with data type string:

import numpy as np

arr = np.array([1, 2, 3, 4], dtype='S')

print(arr)

print(arr.dtype)

For i, u, f, S and U we can define size as well.

Create an array with data type 4 bytes integer:

import numpy as np

arr = np.array([1, 2, 3, 4], dtype='i4')

print(arr)
print(arr.dtype)

What if a Value Can Not Be Converted?

If a type is given in which elements can't be casted then NumPy will raise a ValueError.

ValueError: In Python ValueError is raised when the type of passed argument to a function is
unexpected/incorrect.

A non integer string like 'a' can not be converted to integer (will raise an error):

import numpy as np

arr = np.array(['a', '2', '3'], dtype='i')

Converting Data Type on Existing Arrays

The best way to change the data type of an existing array, is to make a copy of the array with
the astype() method.

The astype() function creates a copy of the array, and allows you to specify the data type as a
parameter.

The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use
the data type directly like float for float and int for integer.

Change data type from float to integer by using 'i' as parameter value:

import numpy as np

arr = np.array([1.1, 2.1, 3.1])

newarr = arr.astype('i')

print(newarr)

print(newarr.dtype)

Change data type from float to integer by using int as parameter value:

import numpy as np

arr = np.array([1.1, 2.1, 3.1])

newarr = arr.astype(int)

print(newarr)

print(newarr.dtype)

Change data type from integer to boolean:


import numpy as np

arr = np.array([1, 0, 3])

newarr = arr.astype(bool)

print(newarr)

print(newarr.dtype)

NumPy contains a large number of various mathematical operations. NumPy provides


standard trigonometric functions, functions for arithmetic operations, handling complex
numbers, etc.

Trigonometric Functions

NumPy has standard trigonometric functions which return trigonometric ratios for a given
angle in radians.

import numpy as np

a = np.array([0,30,45,60,90])

print 'Sine of different angles:'

# Convert to radians by multiplying with pi/180

print np.sin(a*np.pi/180)

print '\n'

print 'Cosine values for angles in array:'

print np.cos(a*np.pi/180)

print '\n'

print 'Tangent values for given angles:'

print np.tan(a*np.pi/180)

Here is its output −

Sine of different angles:

[ 0. 0.5 0.70710678 0.8660254 1. ]

Cosine values for angles in array:

[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01

6.12323400e-17]
Tangent values for given angles:

[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00

1.63312394e+16]

arcsin, arcos, and arctan functions return the trigonometric inverse of sin, cos, and tan of the
given angle. The result of these functions can be verified by numpy.degrees() function by
converting radians to degrees.

import numpy as np

a = np.array([0,30,45,60,90])

print 'Array containing sine values:'

sin = np.sin(a*np.pi/180)

print sin

print '\n'

print 'Compute sine inverse of angles. Returned values are in radians.'

inv = np.arcsin(sin)

print inv

print '\n'

print 'Check result by converting to degrees:'

print np.degrees(inv)

print '\n'

print 'arccos and arctan functions behave similarly:'

cos = np.cos(a*np.pi/180)

print cos

print '\n'

print 'Inverse of cos:'

inv = np.arccos(cos)

print inv

print '\n'

print 'In degrees:'


print np.degrees(inv)

print '\n'

print 'Tan function:'

tan = np.tan(a*np.pi/180)

print tan

print '\n'

print 'Inverse of tan:'

inv = np.arctan(tan)

print inv

print '\n'

print 'In degrees:'

print np.degrees(inv)

Its output is as follows −

Array containing sine values:

[ 0. 0.5 0.70710678 0.8660254 1. ]

Compute sine inverse of angles. Returned values are in radians.

[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]

Check result by converting to degrees:

[ 0. 30. 45. 60. 90.]

arccos and arctan functions behave similarly:

[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01

6.12323400e-17]

Inverse of cos:

[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]

In degrees:

[ 0. 30. 45. 60. 90.]

Tan function:
[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00

1.63312394e+16]

Inverse of tan:

[ 0. 0.52359878 0.78539816 1.04719755 1.57079633]

In degrees:

[ 0. 30. 45. 60. 90.]

Functions for Rounding

numpy.around()

This is a function that returns the value rounded to the desired precision. The function takes the
following parameters.

numpy.around(a,decimals)

Where,

Sr.No. Parameter & Description

Input data

decimals

The number of decimals to round to. Default is 0. If negative, the integer is rounded to position
to the left of the decimal point

import numpy as np

a = np.array([1.0,5.55, 123, 0.567, 25.532])

print 'Original array:'

print a

print '\n'

print 'After rounding:'

print np.around(a)

print np.around(a, decimals = 1)


print np.around(a, decimals = -1)

It produces the following output −

Original array:

[ 1. 5.55 123. 0.567 25.532]

After rounding:

[ 1. 6. 123. 1. 26. ]

[ 1. 5.6 123. 0.6 25.5]

[ 0. 10. 120. 0. 30. ]

numpy.floor()

This function returns the largest integer not greater than the input parameter. The floor of the
scalar x is the largest integer i, such that i <= x. Note that in Python, flooring always is rounded
away from 0.

import numpy as np

a = np.array([-1.7, 1.5, -0.2, 0.6, 10])

print 'The given array:'

print a

print '\n'

print 'The modified array:'

print np.floor(a)

It produces the following output −

The given array:

[ -1.7 1.5 -0.2 0.6 10. ]

The modified array:

[ -2. 1. -1. 0. 10.]

numpy.ceil()

The ceil() function returns the ceiling of an input value, i.e. the ceil of the scalar x is the smallest
integer i, such that i >= x.

import numpy as np
a = np.array([-1.7, 1.5, -0.2, 0.6, 10])

print 'The given array:'

print a

print '\n'

print 'The modified array:'

print np.ceil(a)

It will produce the following output −

The given array:

[ -1.7 1.5 -0.2 0.6 10. ]

The modified array:

[ -1. 2. -0. 1. 10.]

Contents of ndarray object can be accessed and modified by indexing or slicing, just like
Python's in-built container objects.

As mentioned earlier, items in ndarray object follows zero-based index. Three types of indexing
methods are available − field access, basic slicing and advanced indexing.

Basic slicing is an extension of Python's basic concept of slicing to n dimensions. A Python slice
object is constructed by giving start, stop, and step parameters to the built-in slice function. This
slice object is passed to the array to extract a part of array.

import numpy as np

a = np.arange(10)

s = slice(2,7,2)

print a[s]

Its output is as follows −

[2 4 6]

In the above example, an ndarray object is prepared by arange() function. Then a slice object is
defined with start, stop, and step values 2, 7, and 2 respectively. When this slice object is passed
to the ndarray, a part of it starting with index 2 up to 7 with a step of 2 is sliced.

The same result can also be obtained by giving the slicing parameters separated by a colon :
(start:stop:step) directly to the ndarray object.
import numpy as np

a = np.arange(10)

b = a[2:7:2]

print b

Here, we will get the same output −

[2 4 6]

If only one parameter is put, a single item corresponding to the index will be returned. If a : is
inserted in front of it, all items from that index onwards will be extracted. If two parameters
(with : between them) is used, items between the two indexes (not including the stop index)
with default step one are sliced.

# slice single item

import numpy as np

a = np.arange(10)

b = a[5]

print b

Its output is as follows −

# slice items starting from index

import numpy as np

a = np.arange(10)

print a[2:]

Now, the output would be −

[2 3 4 5 6 7 8 9]

# slice items between indexes

import numpy as np

a = np.arange(10)

print a[2:5]

Here, the output would be −


[2 3 4]

The above description applies to multi-dimensional ndarray too.

import numpy as np

a = np.array([[1,2,3],[3,4,5],[4,5,6]])

print a

# slice items starting from index

print 'Now we will slice the array from the index a[1:]'

print a[1:]

The output is as follows −

[[1 2 3]

[3 4 5]

[4 5 6]]

Now we will slice the array from the index a[1:]

[[3 4 5]

[4 5 6]]

Slicing can also include ellipsis (…) to make a selection tuple of the same length as the
dimension of an array. If ellipsis is used at the row position, it will return an ndarray comprising
of items in rows.

# array to begin with

import numpy as np

a = np.array([[1,2,3],[3,4,5],[4,5,6]])

print 'Our array is:'

print a

print '\n'

# this returns array of items in the second column

print 'The items in the second column are:'

print a[...,1]
print '\n'

# Now we will slice all items from the second row

print 'The items in the second row are:'

print a[1,...]

print '\n'

# Now we will slice all items from column 1 onwards

print 'The items column 1 onwards are:'

print a[...,1:]

The output of this program is as follows −

Our array is:

[[1 2 3]

[3 4 5]

[4 5 6]]

The items in the second column are:

[2 4 5]

The items in the second row are:

[3 4 5]

The items column 1 onwards are:

[[2 3]

[4 5]

[5 6]]

NumPy Ndarray

Ndarray is the n-dimensional array object defined in the numpy which stores the collection of
the similar type of elements. In other words, we can define a ndarray as the collection of the
data type (dtype) objects.

The ndarray object can be accessed by using the 0 based indexing. Each element of the Array
object contains the same size in the memory.
Creating a ndarray object

The ndarray object can be created by using the array routine of the numpy module. For this
purpose, we need to import the numpy.

1. >>> a = numpy.array

Consider the below image.

We can also pass a collection object into the array routine to create the equivalent n-
dimensional array. The syntax is given below.

1. >>> numpy.array(object, dtype = None, copy = True, order = None, subok = False, nd
min = 0)

The parameters are described in the following table.

SN Parameter Description

1 object It represents the collection object. It can be


a list, tuple, dictionary, set, etc.

2 dtype We can change the data type of the array elements by changing this option
to the specified type. The default is none.
3 copy It is optional. By default, it is true which means the object is copied.

4 order There can be 3 possible values assigned to this option. It can be C (column
order), R (row order), or A (any)

5 subok The returned array will be base class array by default. We can change this
to make the subclasses passes through by setting this option to true.

6 ndmin It represents the minimum dimensions of the resultant array.

To create an array using the list, use the following syntax.

1. >>> a = numpy.array([1, 2, 3])

To create a multi-dimensional array object, use the following syntax.

1. >>> a = numpy.array([[1, 2, 3], [4, 5, 6]])

To change the data type of the array elements, mention the name of the data type along with
the collection.
1. >>> a = numpy.array([1, 3, 5, 7], complex)

Finding the dimensions of the Array

The ndim function can be used to find the dimensions of the array.

1. >>> import numpy as np


2. >>> arr = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [9, 10, 11, 23]])
3.
4. >>> print(arr.ndim)

ADVERTISEMENT

Finding the size of each array element

The itemsize function is used to get the size of each array item. It returns the number of bytes
taken by each array element.

Consider the following example.

Example
1. #finding the size of each item in the array
2. import numpy as np
3. a = np.array([[1,2,3]])
4. print("Each item contains",a.itemsize,"bytes")

Output:

Each item contains 8 bytes.

Finding the data type of each array item

To check the data type of each array item, the dtype function is used. Consider the following
example to check the data type of the array items.

Example

1. #finding the data type of each array item


2. import numpy as np
3. a = np.array([[1,2,3]])
4. print("Each item is of the type",a.dtype)

Output:

Each item is of the type int64

Finding the shape and size of the array

To get the shape and size of the array, the size and shape function associated with the numpy
array is used.

Consider the following example.

Example

1. import numpy as np
2. a = np.array([[1,2,3,4,5,6,7]])
3. print("Array Size:",a.size)
4. print("Shape:",a.shape)

Output:

Array Size: 7
Shape: (1, 7)
Reshaping the array objects

By the shape of the array, we mean the number of rows and columns of a multi-dimensional
array. However, the numpy module provides us the way to reshape the array by changing the
number of rows and columns of the multi-dimensional array.

The reshape() function associated with the ndarray object is used to reshape the array. It accepts
the two parameters indicating the row and columns of the new shape of the array.

Let's reshape the array given in the following image.

Example

1. import numpy as np
2. a = np.array([[1,2],[3,4],[5,6]])
3. print("printing the original array..")
4. print(a)
5. a=a.reshape(2,3)
6. print("printing the reshaped array..")
7. print(a)

Output:

printing the original array..


[[1 2]
[3 4]
[5 6]]
printing the reshaped array..
[[1 2 3]
[4 5 6]]

Slicing in the Array

Slicing in the NumPy array is the way to extract a range of elements from an array. Slicing in
the array is performed in the same way as it is performed in the python list.

Consider the following example to print a particular element of the array.


Example

1. import numpy as np
2. a = np.array([[1,2],[3,4],[5,6]])
3. print(a[0,1])
4. print(a[2,0])

Output:

2
5

The above program prints the 2nd element from the 0th index and 0th element from the 2nd index
of the array.

Linspace

The linspace() function returns the evenly spaced values over the given interval. The following
example returns the 10 evenly separated values over the given interval 5-15

Example

1. import numpy as np
2. a=np.linspace(5,15,10) #prints 10 values which are evenly spaced over the given inter
val 5-15
3. print(a)

Output:

[ 5. 6.11111111 7.22222222 8.33333333 9.44444444 10.55555556


11.66666667 12.77777778 13.88888889 15. ]

Finding the maximum, minimum, and sum of the array elements

The NumPy provides the max(), min(), and sum() functions which are used to find the
maximum, minimum, and sum of the array elements respectively.

Consider the following example.

Example

1. import numpy as np
2. a = np.array([1,2,3,10,15,4])
3. print("The array:",a)
4. print("The maximum element:",a.max())
5. print("The minimum element:",a.min())
6. print("The sum of the elements:",a.sum())

Output:

The array: [ 1 2 3 10 15 4]
The maximum element: 15
The minimum element: 1
The sum of the elements: 35

NumPy Array Axis

A NumPy multi-dimensional array is represented by the axis where axis-0 represents the
columns and axis-1 represents the rows. We can mention the axis to perform row-level or
column-level calculations like the addition of row or column elements.

To calculate the maximum element among each column, the minimum element among each
row, and the addition of all the row elements, consider the following example.

Example

1. import numpy as np
2. a = np.array([[1,2,30],[10,15,4]])
3. print("The array:",a)
4. print("The maximum elements of columns:",a.max(axis = 0))
5. print("The minimum element of rows",a.min(axis = 1))
6. print("The sum of all rows",a.sum(axis = 1))

Output:

The array: [[1 2 30]


[10 15 4]]
The maximum elements of columns: [10 15 30]
The minimum element of rows [1 4]
The sum of all rows [33 29]

Finding square root and standard deviation

The sqrt() and std() functions associated with the numpy array are used to find the square root
and standard deviation of the array elements respectively.

Standard deviation means how much each element of the array varies from the mean value of
the numpy array.

Consider the following example.

Example

1. import numpy as np
2. a = np.array([[1,2,30],[10,15,4]])
3. print(np.sqrt(a))
4. print(np.std(a))

Output:

[[1. 1.41421356 5.47722558]


[3.16227766 3.87298335 2. ]]
10.044346115546242

Arithmetic operations on the array

The numpy module allows us to perform the arithmetic operations on multi-dimensional arrays
directly.

In the following example, the arithmetic operations are performed on the two multi-
dimensional arrays a and b.

Example

1. import numpy as np
2. a = np.array([[1,2,30],[10,15,4]])
3. b = np.array([[1,2,3],[12, 19, 29]])
4. print("Sum of array a and b\n",a+b)
5. print("Product of array a and b\n",a*b)
6. print("Division of array a and b\n",a/b)
Array Concatenation

The numpy provides us with the vertical stacking and horizontal stacking which allows us to
concatenate two multi-dimensional arrays vertically or horizontally.

Consider the following example.

Example

1. import numpy as np
2. a = np.array([[1,2,30],[10,15,4]])
3. b = np.array([[1,2,3],[12, 19, 29]])
4. print("Arrays vertically concatenated\n",np.vstack((a,b)));
5. print("Arrays horizontally concatenated\n",np.hstack((a,b)))

Output:

Arrays vertically concatenated


[[ 1 2 30]
[10 15 4]
[ 1 2 3]
[12 19 29]]
Arrays horizontally concatenated
[[ 1 2 30 1 2 3]

[10 15 4 12 19 29]]

A data type is a way to specify the type of data that will be stored in an array. For example,

array1 = np.array([2, 4, 6])

Here, the array1 array contains three integer elements, so the data type is Integer(int64)), by
default.
NumPy provides us with several built-in data types to efficiently represent numerical data.
NumPy Data Types

NumPy offers a wider range of numerical data types than what is available in Python. Here's
the list of most commonly used numeric data types in NumPy:

1. int8, int16, int32, int64 - signed integer types with different bit sizes
2. uint8, uint16, uint32, uint64 - unsigned integer types with different bit sizes
3. float32, float64 - floating-point types with different precision levels
4. complex64, complex128 - complex number types with different precision levels

Check Data Type of a NumPy Array

To check the data type of a NumPy array, we can use the dtype attribute. For example,
import numpy as np

# create an array of integers


array1 = np.array([2, 4, 6])

# check the data type of array1


print(array1.dtype)

# Output: int64
Run Code

In the above example, we have used the dtype attribute to check the data type of
the array1 array.
Since array1 is an array of integers, the data type of array1 is inferred as int64 by default.
Example: Check Data Type of NumPy Array
import numpy as np

# create an array of integers


int_array = np.array([-3, -1, 0, 1])

# create an array of floating-point numbers


float_array = np.array([0.1, 0.2, 0.3])

# create an array of complex numbers


complex_array = np.array([1+2j, 2+3j, 3+4j])

# check the data type of int_array


print(int_array.dtype) # prints int64
# check the data type of float_array
print(float_array.dtype) # prints float64

# check the data type of complex_array


print(complex_array.dtype) # prints complex128
Run Code

Output

int64
float64
complex128

Here, we have created types of arrays and checked the default data types of these arrays using
the dtype attribute.
 int_array - contains four integer elements whose default data type is int64
 float_array - contains three floating-point numbers whose default data type is float64
 complex_array - contains three complex numbers whose default data type
is complex128

Creating NumPy Arrays With a Defined Data Type

In NumPy, we can create an array with a defined data type by passing the dtype parameter
while calling the np.array() function. For example,
import numpy as np

# create an array of 32-bit integers


array1 = np.array([1, 3, 7], dtype='int32')

print(array1, array1.dtype)

Output

[1 3 7] int32

In the above example, we have created a NumPy array named array1 with a defined data type.

np.array([1, 3, 7], dtype='int32')

Here, inside np.array(), we have passed an array [1, 3, 7] and set the dtype parameter to int32.
Since we have set the data type of the array to int32, each element of the array is represented
as a 32-bit integer.

Example: Creating NumPy Arrays With a Defined Data Type


import numpy as np

# create an array of 8-bit integers


array1 = np.array([1, 3, 7], dtype='int8')

# create an array of unsigned 16-bit integers


array2 = np.array([2, 4, 6], dtype='uint16')

# create an array of 32-bit floating-point numbers


array3 = np.array([1.2, 2.3, 3.4], dtype='float32')

# create an array of 64-bit complex numbers


array4 = np.array([1+2j, 2+3j, 3+4j], dtype='complex64')

# print the arrays and their data types


print(array1, array1.dtype)
print(array2, array2.dtype)
print(array3, array3.dtype)
print(array4, array4.dtype)
Run Code

Output

[1 3 7] int8
[2 4 6] uint16
[1.2 2.3 3.4] float32
[1.+2.j 2.+3.j 3.+4.j] complex64

NumPy Type Conversion

In NumPy, we can convert the data type of an array using the astype() method. For example,
import numpy as np

# create an array of integers


int_array = np.array([1, 3, 5, 7])

# convert data type of int_array to float


float_array = int_array.astype('float')
# print the arrays and their data types
print(int_array, int_array.dtype)
print(float_array, float_array.dtype)
Run Code

Output

[1 3 5 7] int64
[1. 3. 5. 7.] float64

Here, int_array.astype('float') converts the data type


of int_array from int64 to float64 using astype().

NumPy Array Attributes

In NumPy, attributes are properties of NumPy arrays that provide information about the array's
shape, size, data type, dimension, and so on.

For example, to get the dimension of an array, we can use the ndim attribute.

Common NumPy Attributes

Here are some of the commonly used NumPy attributes:

Attributes Description

ndim returns number of dimension of the array

size returns number of elements in the array

dtype returns data type of elements in the array

shape returns the size of the array in each dimension.

itemsize returns the size (in bytes) of each elements in the array
data returns the buffer containing actual elements of the array in memory

To access the Numpy attributes, we use the . notation. For example,

array1.ndim

This returns the number of dimensions in array1.

Numpy Array ndim Attribute

The ndim attribute returns the number of dimensions in the numpy array. For example,
import numpy as np

# create a 2-D array


array1 = np.array([[2, 4, 6],
[1, 3, 5]])

# check the dimension of array1


print(array1.ndim)

# Output: 2
Run Code

In this example, array1.ndim returns the number of dimensions present in array1. As array1 is
a 2D array, we got 2 as an output.

NumPy Array size Attribute

The size attribute returns the total number of elements in the given array.
Let's see an example.

import numpy as np

array1 = np.array([[1, 2, 3],


[6, 7, 8]])
# return total number of elements in array1
print(array1.size)

# Output: 6
Run Code

In this example, array1.size returns the total number of elements in the array1 array, regardless
of the number of dimensions.
Since these are a total of 6 elements in array1, the size attribute returns 6.

NumPy Array shape Attribute

In NumPy, the shape attribute returns a tuple of integers that gives the size of the array in each
dimension. For example,
import numpy as np

array1 = np.array([[1, 2, 3],


[6, 7, 8]])

# return a tuple that gives size of array in each dimension


print(array1.shape)

# Output: (2,3)
Run Code

Here, array1 is a 2-D array that has 2 rows and 3 columns. So array1.shape returns the
tuple (2,3) as an output.

NumPy Array dtype Attribute

We can use the dtype attribute to check the datatype of a NumPy array. For example,
import numpy as np
# create an array of integers
array1 = np.array([6, 7, 8])

# check the data type of array1


print(array1.dtype)

# Output: int64
Run Code

In the above example, the dtype attribute returns the data type of array1.
Since array1 is an array of integers, the data type of array1 is inferred as int64 by default.

Note: To learn more about the dtype attribute to check the datatype of an array, visit NumPy
Data Types.

NumPy Array itemsize Attribute

In NumPy, the itemsize attribute determines size (in bytes) of each element in the array. For
example,
import numpy as np

# create a default 1-D array of integers


array1 = np.array([6, 7, 8, 10, 13])

# create a 1-D array of 32-bit integers


array2 = np.array([6, 7, 8, 10, 13], dtype=np.int32)

# use of itemsize to determine size of each array element of array1 and array2
print(array1.itemsize) # prints 8
print(array2.itemsize) # prints 4
Run Code

Output

8
4

Here,
 array1 is an array containing 64-bit integers by default, which uses 8 bytes of memory
per element. So, itemsize returns 8 as the size of each element.
 array2 is an array of 32-bit integers, so each element in this array uses only 4 bytes of
memory. So, itemsize returns 4 as the size of each element.

NumPy Array data Attribute

In NumPy, we can get a buffer containing actual elements of the array in memory using
the data attribute.
In simpler terms, the data attribute is like a pointer to the memory location where the array's
data is stored in the computer's memory.
Let's see an example.

import numpy as np

array1 = np.array([6, 7, 8])


array2 = np.array([[1, 2, 3],
[6, 7, 8]])

# print memory address of array1's and array2's data


print("\nData of array1 is: ",array1.data)
print("Data of array2 is: ",array2.data)
Run Code

Output

Data of array1 is: <memory at 0x7f746fea4a00>


Data of array2 is: <memory at 0x7f746ff6a5a0>

Here, the data attribute returns the memory addresses of the data
for array1 and array2 respectively.

Numpy Array Indexing


In NumPy, each element in an array is associated with a number. The number is known as
an array index.
Let's see an example to demonstrate NumPy array indexing.

Array Indexing
in NumPy
In the above array, 5 is the 3rd element. However, its index is 2.

This is because the array indexing starts from 0, that is, the first element of the array has index
0, the second element has index 1, and so on.

Now, we'll see how we can access individual items from the array using the index number.

Access Array Elements Using Index

We can use indices to access individual elements of a NumPy array.

Suppose we have a NumPy array:

array1 = np.array([1, 3, 5, 7, 9])

Now, we can use the index number to access array elements as:

 array1[0] - to access the first element, i.e. 1


 array1[2] - to access the third element, i.e. 5
 array1[4] - to access the fifth element, i.e. 9

Example: Access Array Elements Using Index


import numpy as np

array1 = np.array([1, 3, 5, 7, 9])

# access numpy elements using index


print(array1[0]) # prints 1
print(array1[2]) # prints 5
print(array1[4]) # prints 9
Run Code

Note: Since the last element of array1 is at index 4, if we try to access the element beyond that,
say index 5, we will get an index error: IndexError: index 5 is out of bounds for axis 0 with
size 5

Modify Array Elements Using Index

We can use indices to change the value of an element in a NumPy array. For example,

import numpy as np

# create a numpy array


numbers = np.array([2, 4, 6, 8, 10])

# change the value of the first element


numbers[0] = 12
print("After modifying first element:",numbers) # prints [12 4 6 8 10]

# change the value of the third element


numbers[2] = 14
print("After modifying third element:",numbers) # prints [12 4 14 8 10]
Run Code

Output

After modifying first element: [12 4 6 8 10]


After modifying third element: [12 4 14 8 10]

In the above example, we have modified elements of the numbers array using array indexing.
 numbers[0] = 12 - modifies the first element of numbers and sets its value to 12
 numbers[2] = 14 - modifies the third element of numbers and sets its value to 14

NumPy Negative Array Indexing

NumPy allows negative indexing for its array. The index of -1 refers to the last item, -2 to the
second last item and so on.

NumPy Array
Negative Indexing
Let's see an example.

import numpy as np

# create a numpy array


numbers = np.array([1, 3, 5, 7, 9])

# access the last element


print(numbers[-1]) # prints 9

# access the second-to-last element


print(numbers[-2]) # prints 7
Run Code

Output

9
7

Modify Array Elements Using Negative Indexing

Similar to regular indexing, we can also modify array elements using negative indexing. For
example,

import numpy as np

# create a numpy array


numbers = np.array([2, 3, 5, 7, 11])

# modify the last element


numbers[-1] = 13
print(numbers) # Output: [2 3 5 7 13]

# modify the second-to-last element


numbers[-2] = 17
print(numbers) # Output: [2 3 5 17 13]
Run Code

Here, numbers[-1] = 13 modifies the last element to 13 and numbers[-2] = 17 modifies the
second-to-last element to 17.

Note: Unlike regular indexing, negative indexing starts from -1 (not 0) and it starts counting
from the end of the array.
2-D NumPy Array Indexing

Array indexing in NumPy allows us to access and manipulate elements in a 2-D array.

To access an element of array1, we need to specify the row index and column index of the
element. Suppose we have following 2-D array,

array1 = np.array([[1, 3, 5],


[7, 9, 2],
[4, 6, 8]])

Now, say we want to access the element in the third row and second column we specify the
index as:

array1[2, 1] # returns 6

Since we know indexing starts from 0. So to access the element in the third row and second
column, we need to use index 2 for the third row and index 1 for the second column
respectively.

Example: 2-D NumPy Array Indexing


import numpy as np

# create a 2D array
array1 = np.array([[1, 3, 5, 7],
[9, 11, 13, 15],
[2, 4, 6, 8]])

# access the element at the second row and fourth column


element1 = array1[1, 3] # returns 15
print("4th Element at 2nd Row:",element1)

# access the element at the first row and second column


element2 = array1[0, 1] # returns 3
print("2nd Element at First Row:",element2)
Run Code
Output

4th Element at 2nd Row: 15


2nd Element at First Row: 3

Access Row or Column of 2D Array Using Indexing

In NumPy, we can access specific rows or columns of a 2-D array using array indexing.

Let's see an example.

import numpy as np

# create a 2D array
array1 = np.array([[1, 3, 5],
[7, 9, 2],
[4, 6, 8]])

# access the second row of the array


second_row = array1[1, :]
print("Second Row:", second_row) # Output: [7 9 2]

# access the third column of the array


third_col = array1[:, 2]
print("Third Column:", third_col) # Output: [5 2 8]
Run Code

Output

Second Row: [7 9 2]
Third Column: [5 2 8]

Here,

 array1[1, :] - access the second row of array1


 array1[:, 2] - access the third column of array1
3-D NumPy Array Indexing

We learned how to access elements in a 2D array. We can also access elements in higher
dimensional arrays.

To access an element of a 3D array, we use three indices separated by commas.


 The first index refers to the slice
 The second index refers to the row
 The third index refers to the column.

Note: In 3D arrays, slice is a 2D array that is obtained by taking a subset of the elements in one
of the dimensions.

Let's see an example.

import numpy as np

# create a 3D array with shape (2, 3, 4)


array1 = np.array([[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]],

[[13, 14, 15, 16],


[17, 18, 19, 20],
[21, 22, 23, 24]]])

# access a specific element of the array


element = array1[1, 2, 1]

# print the value of the element


print(element)

# Output: 22
Run Code

Here, we created a 3D array called array1 with shape (2, 3, 4). This array contains 2 2D arrays,
each with 3 rows and 4 columns.
Then, we used indexing to access a specific element of array1. Notice the code,

array1[1, 2, 1]
Here,

 array1[1, , ,] - access the second 2D array, i.e.

[13, 14, 15, 16],


[17, 18, 19, 20],
[21, 22, 23, 24]

 array1[ ,2, ] - access the third row of the 2D array, i.e.

[21, 22, 23, 24]

 array1[ , ,1] - access the second element of the third row, i.e.

[22]

NumPy Array Slicing

Array Slicing is the process of extracting a portion of an array.

With slicing, we can easily access elements in the array. It can be done on one or more
dimensions of a NumPy array.

Syntax of NumPy Array Slicing

Here's the syntax of array slicing in NumPy:

array[start:stop:step]

Here,
 start - index of the first element to be included in the slice
 stop - index of the last element (exclusive)
 step - step size between each element in the slice
Note: When we slice arrays, the start index is inclusive but the stop index is exclusive.
 If we omit start, slicing starts from the first element
 If we omit stop, slicing continues up to the last element
 If we omit step, default step size is 1

1D NumPy Array Slicing

In NumPy, it's possible to access the portion of an array using the slicing operator :. For
example,
import numpy as np

# create a 1D array
array1 = np.array([1, 3, 5, 7, 8, 9, 2, 4, 6])

# slice array1 from index 2 to index 6 (exclusive)


print(array1[2:6]) # [5 7 8 9]

# slice array1 from index 0 to index 8 (exclusive) with a step size of 2


print(array1[0:8:2]) # [1 5 8 2]

# slice array1 from index 3 up to the last element


print(array1[3:]) # [7 8 9 2 4 6]

# items from start to end


print(array1[:]) # [1 3 5 7 8 9 2 4 6]
Run Code

In the above example, we have created the array named array1 with 9 elements.
Then, we used the slicing operator : to slice array elements.
 array1[2:6] - slices array1 from index 2 to index 6, not including index 6
 array1[0:8:2] - slices array1 from index 0 to index 8, not including index 8
 array1[3:] - slices array1 from index 3 up to the last element
 array1[:] - returns all items from beginning to end

Modify Array Elements Using Slicing

With slicing, we can also modify array elements using:

 start parameter
 stop parameter
 start and stop parameter
 start, stop, and step parameter
1. Using start Parameter
import numpy as np

# create a numpy array


numbers = np.array([2, 4, 6, 8, 10, 12])

# modify elements from index 3 onwards


numbers[3:] = 20
print(numbers)

# Output: [ 2 4 6 20 20 20]
Run Code

Here, numbers[3:] = 20 replaces all the elements from index 3 onwards with new value 20.
2. Using stop Parameter
import numpy as np

# create a numpy array


numbers = np.array([2, 4, 6, 8, 10, 12])

# modify the first 3 elements


numbers[:3] = 40
print(numbers)

# Output: [40 40 40 8 10 12]


Run Code
Here, numbers[:3] = 20 replaces the first 3 elements with the new value 40.
3. Using start and stop parameter
import numpy as np

# create a numpy array


numbers = np.array([2, 4, 6, 8, 10, 12])

# modify elements from indices 2 to 5


numbers[2:5] = 22
print(numbers)

# Output: [2 4 22 22 22 12]
Run Code

Here, numbers[2:5] = 22 selects elements from index 2 to index 4 and replaces them with new
value 22.
4. Using start, stop, and step parameter
import numpy as np

# create a numpy array


numbers = np.array([2, 4, 6, 8, 10, 12])

# modify every second element from indices 1 to 5


numbers[1:5:2] = 16
print(numbers)

# Output: [ 2 16 6 16 10 12]
Run Code

In the above example,

numbers[1:5:2] = 16

modifies every second element from index 1 to index 5 with a new value 16.

NumPy Array Negative Slicing


We can also use negative indices to perform negative slicing in NumPy arrays. During negative
slicing, elements are accessed from the end of the array.
Let's see an example.

import numpy as np

# create a numpy array


numbers = np.array([2, 4, 6, 8, 10, 12])

# slice the last 3 elements of the array


# using the start parameter
print(numbers[-3:]) # [8 10 12]

# slice elements from 2nd-to-last to 4th-to-last element


# using the start and stop parameters
print(numbers[-5:-2]) # [4 6 8]

# slice every other element of the array from the end


# using the start, stop, and step parameters
print(numbers[-1::-2]) # [12 8 4]
Run Code

Output

Using numbers[-3:]- [ 8 10 12]


Using numbers[-5:-2]- [4 6 8]
Using numbers[-1::-2]- [12 8 4]

Here,

 numbers[-3:] - slices last 3 elements of numbers


 numbers[-5:-2] - slices numbers elements from 5th last to 2nd last(excluded)
 numbers[-1::-2] - slices every other numbers elements from the end with step size 2

Reverse NumPy Array Using Negative Slicing

In NumPy, we can also reverse array elements using the negative slicing. For example,

import numpy as np
# create a numpy array
numbers = np.array([2, 4, 6, 8, 10, 12])

# generate reversed array


reversed_numbers = numbers[::-1]
print(reversed_numbers)

# Output: [12 10 8 6 4 2]
Run Code

Here, the slice numbers[::-1] selects all the elements of the array with a step size of -1, which
reverses the order of the elements.

2D NumPy Array Slicing

A 2D NumPy array can be thought of as a matrix, where each element has two indices, row
index and column index.

To slice a 2D NumPy array, we can use the same syntax as for slicing a 1D NumPy array. The
only difference is that we need to specify a slice for each dimension of the array.

Syntax of 2D NumPy Array Slicing

array[row_start:row_stop:row_step, col_start:col_stop:col_step]

Here,row_start,row_stop,row_step - specifies starting index, stopping index, and step size for
the rows respectively

 col_start,col_stop,col_step - specifies starting index, stopping index, and step size for
the columns respectively
Let's understand this with an example.

# create a 2D array
array1 = np.array([[1, 3, 5, 7],
[9, 11, 13, 15]])
print(array1[:2, :2])

# Output

[[ 1 3]
[ 9 11]]

Here, the , in [:2, :2] separates the rows of the array.


The first :2 returns first 2 rows i.e., entire array1. This results in

[1 3]

The second :2 returns first 2 columns from the 2 rows. This results in

[9 11]

Example: 2D NumPy Array Slicing


import numpy as np

# create a 2D array
array1 = np.array([[1, 3, 5, 7],
[9, 11, 13, 15],
[2, 4, 6, 8]])

# slice the array to get the first two rows and columns
subarray1 = array1[:2, :2]

# slice the array to get the last two rows and columns
subarray2 = array1[1:3, 2:4]

# print the subarrays


print("First Two Rows and Columns: \n",subarray1)
print("Last two Rows and Columns: \n",subarray2)
Run Code

Output
First Two Rows and Columns:
[[ 1 3]
[ 9 11]]
Last two Rows and Columns:
[[13 15]
[ 6 8]]

Here,

 array1[:2, :2] - slices array1 that starts at the first row and first column (default values),
and ends at the second row and second column (exclusive)
 array1[1:3, 2:4] - slices array1 that starts at the second row and third column
(index 1 and 2), and ends at the third row and fourth column (index 2 and 3)

NumPy Array Functions

NumPy array functions are the built-in functions provided by NumPy that allow us to create
and manipulate arrays, and perform different operations on them.

Common NumPy Array Functions

There are many NumPy array functions available but here are some of the most commonly
used ones.

Array Operations Functions

Array Creation Functions np.array(), np.zeros(), np.ones(), np.empty(), etc.

Array Manipulation Functions np.reshape(), np.transpose(), etc.

Array Mathematical Functions np.add(), np.subtract(), np.sqrt(), np.power(), etc.

Array Statistical Functions np.median(), np.mean(), np.std(), and np.var().

Array Input and Output Functions np.save(), np.load(), np.loadtxt(), etc.


NumPy Array Creation Functions

Array creation functions allow us to create new NumPy arrays. For example,

import numpy as np

# create an array using np.array()


array1 = np.array([1, 3, 5])
print("np.array():\n", array1)

# create an array filled with zeros using np.zeros()


array2 = np.zeros((3, 3))
print("\nnp.zeros():\n", array2)

# create an array filled with ones using np.ones()


array3 = np.ones((2, 4))
print("\nnp.ones():\n", array3)
Run Code

Output

np.array():
[1 3 5]

np.zeros():
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

np.ones():
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]

Here,

 np.array() - creates an array from a Python List


 np.zeros() - creates an array filled with zeros of the specified shape
 np.ones() - creates an array filled with ones of the specified shape

Note: To learn more about NumPy Array Creation, please visit NumPy Array
Creation and NumPy N-d Array Creation.
NumPy Array Manipulation Functions

NumPy array manipulation functions allow us to modify or rearrange NumPy arrays. For
example,

import numpy as np

# create a 1D array
array1 = np.array([1, 3, 5, 7, 9, 11])

# reshape the 1D array into a 2D array


array2 = np.reshape(array1, (2, 3))

# transpose the 2D array


array3 = np.transpose(array2)

print("Original array:\n", array1)


print("\nReshaped array:\n", array2)
print("\nTransposed array:\n", array3)
Run Code

Output

Original array:
[ 1 3 5 7 9 11]

Reshaped array:
[[ 1 3 5]
[ 7 9 11]]

Transposed array:
[[ 1 7]
[ 3 9]
[ 5 11]]

In this example,

 np.reshape(array1, (2, 3)) - reshapes array1 into 2D array with shape (2,3)
 np.transpose(array2) - transposes 2D array array2
NumPy Array Mathematical Functions
In NumPy, there are tons of mathematical functions to perform on arrays. For example,

import numpy as np

# create two arrays


array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([4, 9, 16, 25, 36])

# add the two arrays element-wise


arr_sum = np.add(array1, array2)

# subtract the array2 from array1 element-wise


arr_diff = np.subtract(array1, array2)

# compute square root of array2 element-wise


arr_sqrt = np.sqrt(array2)

print("\nSum of arrays:\n", arr_sum)


print("\nDifference of arrays:\n", arr_diff)
print("\nSquare root of first array:\n", arr_sqrt)
Run Code

Output

Sum of arrays:
[ 5 11 19 29 41]

Difference of arrays:
[ -3 -7 -13 -21 -31]

Square root of first array:


[2. 3. 4. 5. 6.]

Matplotlib (Python Plotting Library)


Human minds are more adaptive for the visual representation of data rather than textual data.
We can easily understand things when they are visualized. It is better to represent the data
through the graph where we can analyze the data more efficiently and make the specific
decision according to data analysis.

Matplotlib

Before start working with the Matplotlib or its plotting functions first, it needs to be installed.
The installation of matplotlib is dependent on the distribution that is installed on your computer.
These installation methods are following:

Use the Anaconda distribution of Python

The easiest way to install Matplotlib is to download the Anaconda distribution of Python.
Matplotlib is pre-installed in the anaconda distribution No further installation steps are
necessary.

o Visit the official site of Anaconda and click on the Download Button
o Choose download according to your Python interpreter configuration.
Install Matplotlib using with Anaconda Prompt

Matplotlib can be installed using with the Anaconda Prompt by typing command. To install
matplotlib, open Anaconda Prompt and type the following command:

1. conda install matplotlib

Install Matplotlib with pip

ADVERTISEMENT

The python package manager pip is also used to install matplotlib. Open the command prompt
window, and type the following command:

1. pip install matplotlib


Verify the Installation

To verify that matplotlib is installed properly or not, type the following command includes
calling .__version __ in the terminal.

1. import matplotlib
2. matplotlib.__version__
3. '3.1.1'
Pyplot

Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under
the plt alias:

import matplotlib.pyplot as plt

Now the Pyplot package can be referred to as plt.

Draw a line in a diagram from position (0,0) to position (6,250):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([0, 6])


ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)
plt.show()
Result:
Plotting x and y points

The plot() function is used to draw points (markers) in a diagram.

By default, the plot() function draws a line from point to point.

The function takes parameters for specifying points in the diagram.

Parameter 1 is an array containing the points on the x-axis.

Parameter 2 is an array containing the points on the y-axis.

If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to
the plot function.

Draw a line in a diagram from position (1, 3) to position (8, 10):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 8])


ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints)
plt.show()
Result:

he x-axis is the horizontal axis.

The y-axis is the vertical axis.

Plotting Without Line

To plot only the markers, you can use shortcut string notation parameter 'o', which means
'rings'.

Example

Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 8])


ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints, 'o')
plt.show()
Result:

Multiple Points

You can plot as many points as you like, just make sure you have the same number of points
in both axis.

Example

Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8,
10):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 2, 6, 8])


ypoints = np.array([3, 8, 1, 10])
plt.plot(xpoints, ypoints)
plt.show()
Result:

Default X-Points

If we do not specify the points on the x-axis, they will get the default values 0, 1, 2, 3 etc.,
depending on the length of the y-points.

So, if we take the same example as above, and leave out the x-points, the diagram will look
like this:

Example

Plotting without x-points:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10, 5, 7])


plt.plot(ypoints)
plt.show()
Result:

The x-points in the example above are [0, 1, 2, 3, 4, 5].

Markers

You can use the keyword argument marker to emphasize each point with a specified marker:

Mark each point with a circle:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o')


plt.show()
Result:
Example

Use a dashed line:

plt.plot(ypoints, linestyle = 'dashed')

Result:
Shorter Syntax

The line style can be written in a shorter syntax:

linestyle can be written as ls.

dotted can be written as :.

dashed can be written as --.

Example

Shorter syntax:

plt.plot(ypoints, ls = ':')

Result:
Line Styles

You can choose any of these styles:

Style Or

'solid' (default) '-'

'dotted' ':'

'dashed' '--'
'dashdot' '-.'

'None' '' or ' '

Line Color

You can use the keyword argument color or the shorter c to set the color of the line:

Example

Set the line color to red:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, color = 'r')


plt.show()
Result:
You can also use Hexadecimal color values:

Example

Plot with a beautiful green line:

...
plt.plot(ypoints, c = '#4CAF50')
...
Result:
Example

Plot with the color named "hotpink":

...
plt.plot(ypoints, c = 'hotpink')
...
Result:
Line Width

You can use the keyword argument linewidth or the shorter lw to change the width of the line.

The value is a floating number, in points:

Example

Plot with a 20.5pt wide line:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linewidth = '20.5')


plt.show()
Result:
np.array([3, 8, 1, 10])

plt.bar(x, y, color = "hotpink")


plt.show()
Result:
Try it Yourself »
Color Hex

Or you can use Hexadecimal color values:

Example

Draw 4 bars with a beautiful green color:

import matplotlib.pyplot as plt


import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, color = "#4CAF50")


plt.show()
Result:
Try it Yourself »

Bar Width

The bar() takes the keyword argument width to set the width of the bars:

Example

Draw 4 very thin bars:

import matplotlib.pyplot as plt


import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, width = 0.1)


plt.show()
Result:
The default width value is 0.8

Note: For horizontal bars, use height instead of width.

Bar Height

The barh() takes the keyword argument height to set the height of the bars:

Example

Draw 4 very thin bars:

import matplotlib.pyplot as plt


import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.barh(x, y, height = 0.1)


plt.show()
Result:

The default height value is 0.8

Histogram

A histogram is a graph showing frequency distributions.

It is a graph showing the number of observations within each given interval.

Example: Say you ask for the height of 250 people, you might end up with a histogram like
this:
You can read from the histogram that there are approximately:

2 people from 140 to 145cm


5 people from 145 to 150cm
15 people from 151 to 156cm
31 people from 157 to 162cm
46 people from 163 to 168cm
53 people from 168 to 173cm
45 people from 173 to 178cm
28 people from 179 to 184cm
21 people from 185 to 190cm
4 people from 190 to 195cm

Create Histogram

In Matplotlib, we use the hist() function to create histograms.

The hist() function will use an array of numbers to create a histogram, the array is sent into the
function as an argument.

.
Example

A Normal Data Distribution by NumPy:

import numpy as np

x = np.random.normal(170, 10, 250)

print(x)
Result:

This will generate a random result, and could look like this:

[167.62255766 175.32495609 152.84661337 165.50264047 163.17457988


162.29867872 172.83638413 168.67303667 164.57361342 180.81120541
170.57782187 167.53075749 176.15356275 176.95378312 158.4125473
187.8842668 159.03730075 166.69284332 160.73882029 152.22378865
164.01255164 163.95288674 176.58146832 173.19849526 169.40206527
166.88861903 149.90348576 148.39039643 177.90349066 166.72462233
177.44776004 170.93335636 173.26312881 174.76534435 162.28791953
166.77301551 160.53785202 170.67972019 159.11594186 165.36992993
178.38979253 171.52158489 173.32636678 159.63894401 151.95735707
175.71274153 165.00458544 164.80607211 177.50988211 149.28106703
179.43586267 181.98365273 170.98196794 179.1093176 176.91855744
168.32092784 162.33939782 165.18364866 160.52300507 174.14316386
163.01947601 172.01767945 173.33491959 169.75842718 198.04834503
192.82490521 164.54557943 206.36247244 165.47748898 195.26377975
164.37569092 156.15175531 162.15564208 179.34100362 167.22138242
147.23667125 162.86940215 167.84986671 172.99302505 166.77279814
196.6137667 159.79012341 166.5840824 170.68645637 165.62204521
174.5559345 165.0079216 187.92545129 166.86186393 179.78383824
161.0973573 167.44890343 157.38075812 151.35412246 171.3107829
162.57149341 182.49985133 163.24700057 168.72639903 169.05309467
167.19232875 161.06405208 176.87667712 165.48750185 179.68799986
158.7913483 170.22465411 182.66432721 173.5675715 176.85646836
157.31299754 174.88959677 183.78323508 174.36814558 182.55474697
180.03359793 180.53094948 161.09560099 172.29179934 161.22665588
171.88382477 159.04626132 169.43886536 163.75793589 157.73710983
174.68921523 176.19843414 167.39315397 181.17128255 174.2674597
186.05053154 177.06516302 171.78523683 166.14875436 163.31607668
174.01429569 194.98819875 169.75129209 164.25748789 180.25773528
170.44784934 157.81966006 171.33315907 174.71390637 160.55423274
163.92896899 177.29159542 168.30674234 165.42853878 176.46256226
162.61719142 166.60810831 165.83648812 184.83238352 188.99833856
161.3054697 175.30396693 175.28109026 171.54765201 162.08762813
164.53011089 189.86213299 170.83784593 163.25869004 198.68079225
166.95154328 152.03381334 152.25444225 149.75522816 161.79200594
162.13535052 183.37298831 165.40405341 155.59224806 172.68678385
179.35359654 174.19668349 163.46176882 168.26621173 162.97527574
192.80170974 151.29673582 178.65251432 163.17266558 165.11172588
183.11107905 169.69556831 166.35149789 178.74419135 166.28562032
169.96465166 178.24368042 175.3035525 170.16496554 158.80682882
187.10006553 178.90542991 171.65790645 183.19289193 168.17446717
155.84544031 177.96091745 186.28887898 187.89867406 163.26716924
169.71242393 152.9410412 158.68101969 171.12655559 178.1482624
187.45272185 173.02872935 163.8047623 169.95676819 179.36887054
157.01955088 185.58143864 170.19037101 157.221245 168.90639755
178.7045601 168.64074373 172.37416382 165.61890535 163.40873027
168.98683006 149.48186389 172.20815568 172.82947206 173.71584064
189.42642762 172.79575803 177.00005573 169.24498561 171.55576698
161.36400372 176.47928342 163.02642822 165.09656415 186.70951892
153.27990317 165.59289527 180.34566865 189.19506385 183.10723435
173.48070474 170.28701875 157.24642079 157.9096498 176.4248199 ]

The hist() function will read the array and produce a histogram:

Example

A simple histogram:

import matplotlib.pyplot as plt


import numpy as np

x = np.random.normal(170, 10, 250)

plt.hist(x)
plt.show()
Result:
Creating Pie Charts

With Pyplot, you can use the pie() function to draw pie charts:

ExampleGet your own Python Server

A simple pie chart:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])

plt.pie(y)
plt.show()
Result:
As you can see the pie chart draws one piece (called a wedge) for each value in the array (in
this case [35, 25, 25, 15]).

By default the plotting of the first wedge starts from the x-axis and moves counterclockwise:
Note: The size of each wedge is determined by comparing the value with all the other values,
by using this formula:

The value divided by the sum of all values: x/sum(x)

Labels

Add labels to the pie chart with the labels parameter.

The labels parameter must be an array with one label for each wedge:

Example

A simple pie chart:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels = mylabels)
plt.show()
Result:

Start Angle

As mentioned the default start angle is at the x-axis, but you can change the start angle by
specifying a startangle parameter.

The startangle parameter is defined with an angle in degrees, default angle is 0:


Example

Start the first wedge at 90 degrees:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels, startangle = 90)


plt.show()
Result:
Try it Yourself »

Explode

Maybe you want one of the wedges to stand out? The explode parameter allows you to do that.

The explode parameter, if specified, and not None, must be an array with one value for each
wedge.

Each value represents how far from the center each wedge is displayed:

Example

Pull the "Apples" wedge 0.2 from the center of the pie:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels = mylabels, explode = myexplode)
plt.show()
Result:

Shadow

Add a shadow to the pie chart by setting the shadows parameter to True:

Example

Add a shadow:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]

plt.pie(y, labels = mylabels, explode = myexplode, shadow = True)


plt.show()
Result:

Colors

You can set the color of each wedge with the colors parameter.

The colors parameter, if specified, must be an array with one value for each wedge:

Example

Specify a new color for each wedge:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["black", "hotpink", "b", "#4CAF50"]

plt.pie(y, labels = mylabels, colors = mycolors)


plt.show()
Result:

'r' - Red
'g' - Green
'b' - Blue
'c' - Cyan
'm' - Magenta
'y' - Yellow
'k' - Black
'w' - White

Legend

To add a list of explanation for each wedge, use the legend() function:

Example

Add a legend:

import matplotlib.pyplot as plt


import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)


plt.legend()
plt.show()
Result:

Legend With Header

To add a header to the legend, add the title parameter to the legend function.

Example

Add a legend with a header:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
plt.pie(y, labels = mylabels)
plt.legend(title = "Four Fruits:")
plt.show()
Result:

Introduction to Pandas

Pandas is a powerful Python library for data manipulation and analysis. It provides flexible
data structures like Series and DataFrame, which are essential for data handling.

1. Series

A Series is a one-dimensional labeled array that can hold any data type.

Example:

python
Copy code
import pandas as pd

# Creating a Series
data = pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd'])
print(data)

2. DataFrame

A DataFrame is a two-dimensional labeled data structure with columns of potentially


different types.

Example:

python
Copy code
# Creating a DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [24, 27, 22],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
print(df)

3. Panel

A Panel is a three-dimensional data structure. However, it is largely deprecated in favor of


MultiIndex DataFrames.

Example:

python
Copy code
# Creating a Panel (not recommended for new code)
import numpy as np

# Creating random data


data = np.random.rand(2, 3, 4) # 2 items, 3 rows, 4 columns
panel = pd.Panel(data)
print(panel)

4. Basic Functions

Pandas offers a variety of basic functions for data manipulation.

Example:

python
Copy code
# Basic DataFrame functions
print(df.head()) # First 5 rows
print(df.describe()) # Descriptive statistics
print(df.info()) # Info about the DataFrame

5. Descriptive Statistics Functions


These functions provide insights into the data.

Example:

python
Copy code
# Descriptive statistics
print(df['Age'].mean()) # Mean of Age
print(df['Age'].std()) # Standard deviation of Age
print(df.describe()) # Summary statistics for all columns

6. Iterating DataFrames

You can iterate through rows of a DataFrame using iterrows().

Example:

python
Copy code
# Iterating through rows
for index, row in df.iterrows():
print(f"Index: {index}, Name: {row['Name']}, Age: {row['Age']}")

7. Statistical Functions

Pandas provides various statistical functions.

Example:

python
Copy code
# Statistical functions
print(df['Age'].min()) # Minimum Age
print(df['Age'].max()) # Maximum Age
print(df['Age'].sum()) # Sum of Ages

8. Aggregations

You can aggregate data using groupby and aggregate functions.

Example:

python
Copy code
# Aggregation using groupby
grouped = df.groupby('City').agg({'Age': ['mean', 'max', 'min']})
print(grouped)

9. Visualization

Pandas integrates with libraries like Matplotlib and Plotly for data visualization.

Example using Plotly:


python
Copy code
import plotly.express as px

# Plotting using Plotly


fig = px.bar(df, x='Name', y='Age', title='Age of Individuals')
fig.show()

You might also like