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

1 Numpy

The document discusses NumPy, an open source Python library used for working with arrays. NumPy contains N-dimensional array objects, tools for integrating C/C++ and Fortran code, and functions for linear algebra, Fourier transforms, and random number generation. It also provides various functions and methods for creating, manipulating, and accessing multi-dimensional arrays.

Uploaded by

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

1 Numpy

The document discusses NumPy, an open source Python library used for working with arrays. NumPy contains N-dimensional array objects, tools for integrating C/C++ and Fortran code, and functions for linear algebra, Fourier transforms, and random number generation. It also provides various functions and methods for creating, manipulating, and accessing multi-dimensional arrays.

Uploaded by

rahul.mani
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Python Pandas

NumPy
By : Mrs Sangeeta M Chauhan , Gwalior

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Introduction toNumPy
• NumPy is the fundamental package needed for
scientific computing with Python. It contains:
• A powerful N-dimensional array object.
• Sophisticated (broadcasting/universal) functions.
• Tools for integrating C/C++ and Fortran code.
• Useful linear algebra, Fourier transform, and random
number capabilities.
• Besides its obvious scientific uses, NumPy can also be
used as an efficient multi-dimensional container of
generic data.
https://pythonclassroomdiary.wordpress.com by
Sangeeta M Chauhan, Gwalior
Example: creating an array
1D
>>>import numpy as np Array

>>> a1 = np.array([2,6,1,7])

>>> a2 = np.array([[1,2,3],
[4,5,6], 2D
Array
[7,8,9]])

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Difference between NumPy and List
 NumPy arrays have a fixed size. Modifying the size
means creating a new array.
 More efficient mathematical operations than built-in
sequence types. With list can’t use directly with
arithmetical operators (+, -, *, /, …)
 Numpy data structures perform better in:
• Size - Numpy data structures take up less space
• Performance - they have a need for speed and are
faster than lists
• Functionality - SciPy and NumPy have optimized
functions such as linear algebra operations built in.
https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior
 NumPy arrays must be of the same data
type, but this can include Python objects

>>> b=np.array([2,6,1,7])
Example 1 >>> x=np.array([2.5,4.5,b])
>>> x
array([2.5, 4.5, array([2, 6, 1, 7])], dtype=object)

>>>y=np.array([1,2,5.5,5,6])
Example 2
>>> y
array([1. , 2. , 5.5, 5. , 6. ])

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


ANATOMY OF NUMPY ARRAY
• RANK- DIMENSIONS (AXES)

• SHAPE – LENGTH OF ARRAY IN EACH DIMENSION

• SIZE – TOTAL NO OF ELEMENTS

• DTYPE – DATA TYPE OF ARRAY

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


1 D ARRAY
• Shape – (9, )
Rank - 1
Size- 9
2 D ARRAY

Shape – (3,5 )
Rank - 2
Size- 15

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


creating a 3 by 5 array of zeros
• import numpy as np
• a2 = np.zeros((3, 5))
• print(a2.ndim) # 2
• print(a2.shape) # (3, 5)
• print(a2.size) # 15
• print(a2.dtype) # float64
• print(a2.itemsize) # 8(float64 is an 8 byte quantity)
• print(a2.data) # <memory at XXXX> (base address)

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Various ways to create Arrays
>>> np.zeros( (3,4) )
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
>>> np.ones( (2,3,4), dtype=np.int16 )
# dtype can also be specified
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],

[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]], dtype=int16)
https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior
>>> np.empty( (2,3) )
# uninitialized, output may vary
array([[1.39069238e-309, 1.39069238e-309, 1.39069238e-309],
[1.39069238e-309, 1.39069238e-309, 1.39069238e-309]])

To create sequences of numbers, NumPy provides a function


analogous to range that returns arrays instead of lists

>>> np.arange( 10, 30, 5 )


array([10, 15, 20, 25])
To create
>>> np.arange( 0, 2, 0.5 ) # Float Array sequences of
numbers
array([0. , 0.5, 1. , 1.5])

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


When arange() is used with floating point arguments, it is
generally not possible to predict the number of elements
obtained, due to the finite floating point precision.

>>> np.linspace (1,15,20)


array([ 1. , 1.73684211, 2.47368421, 3.21052632, 3.94736842,
4.68421053, 5.42105263, 6.15789474, 6.89473684, 7.63157895,
8.36842105, 9.10526316, 9.84210526, 10.57894737, 11.31578947,
12.05263158, 12.78947368, 13.52631579, 14.26315789, 15. ])

** Here total 20 numbers are generated


between values 1 and 15

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Some more functions
• empty_like Return an empty array with shape
and type of input.
• ones_likeReturn an array of ones with shape
and type of input.
• full_likeReturn a new array with shape of
input filled with value.
• zerosReturn a new array setting values to zero.

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Examples
>>> x = np.arange(6)
>>> x = x.reshape((2, 3))
>>> x
array([[0, 1, 2], [3, 4, 5]])
>>> np.zeros_like(x)
array([[0, 0, 0], [0, 0, 0]])
>>> y = np.arange(3, dtype=float)
>>> y
array([0., 1., 2.])
>>> np.zeros_like(y)
array([0., 0., 0.])

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


random.random(shape) –
creates arrays with random floats over the interval [0,1).

>>> np.random.random((2,3))
array([[0.2777547 , 0.01277338, 0.78517305],
[0.34933865, 0.03929481, 0.85493335]])
>>> np.random.random((2,3))
array([[0.57912242, 0.68148459, 0.82677265],
[0.61008824, 0.52260207, 0.33867352]])

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


>>> import numpy as np
>>> a = np.arange(3)
>>> a
[0 1 2]
>>> a
array([0, 1, 2]) reshape()
>>> b = np.arange(9).reshape(3,3) array.reshape( s
hape, order =
>>> b
[[0 1 2] {'C‘/’F’}) : shapes
[3 4 5] an array without
[6 7 8]] changing data of
>>> c=np.arange(8).reshape(2,2,2) array.
>>> c
[[[0 1]
[2 3]]
https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior
[[4 5]
>>> import numpy as np
>>> c=np.arange(8).reshape(2,2,2)
>>> np.reshape(c,(4,2))
array([[0, 1],
[2, 3],
[4, 5], reshape()
[6, 7]]) numpy.reshape(
>>> c.reshape((4,2)) array, shape,
array([[0, 1],
order = 'C') :
[2, 3],
[4, 5], shapes an array
[6, 7]]) without
>>> c changing data of
array([[[0, 1], array.
[2, 3]],

[[4, 5],
[6, https://pythonclassroomdiary.wordpress.com
7]]]) by Sangeeta M Chauhan, Gwalior
Array Indexing: Accessing Single Elements

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


arr2=[[1 2 3 4]
arr=[1 2 3 4]
[5 6 7
8]]

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


What happened if ???
arr2=[[1 2 3 4]
• Arr2[1,2]=4.5
[5 6 7
output 8]]
[[1 2 3 4]
[5 6 4 8]]

NumPy arrays have a fixed type. This means, for example,


that if you attempt to insert a floating-point value to an
integer array, the value will be silently truncated

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Array Slicing: Accessing Subarrays
• The NumPy slicing syntax follows that of the
standard Python list
• to access a slice of an array arr, use this:
arr[start:stop:step]

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


SLICING OUTPUT
WITH 1D ARRAY
OUTPUT

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Slicing with 2D array

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


OUTPUT

OUTPUT

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


SUBSETS : 1 D Array

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


SUBSETS : 2 D Array

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Creating Copy of Arrays : copy()

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Joining arrays ()
Joining of two arrays in NumPy, is primarily
accomplished using the routines :
•np.concatenate ,
•np.vstack, and
•np.hstack.
np.concatenate takes a tuple or list of arrays as
its first argument :

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Concatenate ()

By default it will join array on axis 0 (Column)


https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior
Concatenated on axis 1 (row)

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


working with arrays of mixed dimensions

• numpy.vstack() function is used to stack the


sequence of input arrays vertically to make a
single array

• numpy.hstack() function is used to stack the


sequence of input arrays horizontally (i.e.
column wise) to make a single array.

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Vstack : vertical dimension should be same

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


hstack : horizontal dimension should be
same

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


SPLITING ARRAYS :
vsplit(),hsplit(),split()

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Calculating Co-Variance
• Variance is a measure of variability from the
mean
• Covariance is a measure of relationship
between the variability (the variance) of 2
variables. This measure is scale dependent
because it is not standardized.
• Correlation/Correlation coefficient is a measure
of relationship between the variability (the
variance) of 2 variables. This measure is
standardized and is not scale dependent.
https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior
Commercials
Product Purchase
Watched
Lets take
14 7
an example
12 15

6 7

7 1

5 10

var(a) cov(a,b)
Cov
cov(b,a) var(b)
https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior
Calculating Variance

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Calculating co variance

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Similarly we can use other available
functions

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


Correlation Coefficient
• The function np.corrcoef(x,y) gives the matrix
with following values
Corr(x) corr(a,b)
Corrcoef
corr(b,a) Corr(y)

x= √var (x)

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


NumPy Arithmatic Operations

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior


THANKS

https://pythonclassroomdiary.wordpress.com by Sangeeta M Chauhan, Gwalior

You might also like