Python Pandas Num Py By Mrs Sangeeta M
Python Pandas Num. Py By : Mrs Sangeeta M Chauhan , Gwalior https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
Introduction to. Num. Py • Num. Py 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, Num. Py 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 >>>import numpy as np >>> a 1 = np. array([2, 6, 1, 7]) >>> a 2 = np. array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 1 D Array 2 D Array https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
Difference between Num. Py and List Ø Num. Py 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 - Sci. Py and Num. Py have optimized functions such as linear algebra operations built in. https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
Ø Num. Py arrays must be of the same data type, but this can include Python objects Example 1 Example 2 >>> b=np. array([2, 6, 1, 7]) >>> 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]) >>> 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 • 2 D ARRAY Shape – (9, ) Rank - 1 Size 9 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 a 2 = np. zeros((3, 5)) print(a 2. ndim) # 2 print(a 2. shape) # (3, 5) print(a 2. size) # 15 print(a 2. dtype) # float 64 print(a 2. itemsize) # 8(float 64 is an 8 byte quantity) print(a 2. 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. ]]) >>> np. ones( (2, 3, 4), dtype=np. int 16 ) # dtype can also be specified array([[[1, 1, 1, 1], [1, 1, 1, 1]], [[1, 1, 1, 1], [1, 1, 1, 1]]], dtype=int 16) https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
>>> np. empty( (2, 3) ) # uninitialized, output may vary array([[1. 39069238 e-309, 1. 39069238 e-309], [1. 39069238 e-309, 1. 39069238 e-309]]) To create sequences of numbers, Num. Py provides a function analogous to range that returns arrays instead of lists >>> np. arange( 10, 30, 5 ) array([10, 15, 20, 25]) >>> np. arange( 0, 2, 0. 5 ) # Float Array array([0. , 0. 5, 1. 5]) To create sequences of numbers 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_like. Return an array of ones with shape and type of input. • full_like. Return a new array with shape of input filled with value. • zeros. Return 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. ]) https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
random(shape) – creates arrays with random floats over the interval [0, 1). >>> np. random((2, 3)) array([[0. 2777547 , 0. 01277338, 0. 78517305], [0. 34933865, 0. 03929481, 0. 85493335]]) >>> np. 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]) >>> b = np. arange(9). reshape(3, 3) array. reshape( >>> b shape, order = [[0 1 2] {'C‘/’F’}) : shapes [3 4 5] an array without [6 7 8]] >>> c=np. arange(8). reshape(2, 2, 2) changing data of >>> c array. [[[0 1] [2 3]] [[4 5] [6 7]]] reshape() https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
>>> import numpy as np >>> c=np. arange(8). reshape(2, 2, 2) >>> np. reshape(c, (4, 2)) array([[0, 1], [2, 3], [4, 5], [6, 7]]) numpy. reshape( >>> c. reshape((4, 2)) array, shape, array([[0, 1], order = 'C') : [2, 3], shapes an array [4, 5], without [6, 7]]) >>> c changing data of array([[[0, 1], array. [2, 3]], reshape() [[4, 5], by Sangeeta M Chauhan, Gwalior [6, https: //pythonclassroomdiary. wordpress. com 7]]])
Array Indexing: Accessing Single Elements https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
arr=[1 2 3 4] arr 2=[[1 2 3 4] [5 6 7 8]] https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
What happened if ? ? ? arr 2=[[1 2 3 4] • Arr 2[1, 2]=4. 5 output [[1 2 3 4] [5 6 4 8]] [5 6 7 8]] Num. Py 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 Num. Py 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 1 D ARRAY OUTPUT https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
Slicing with 2 D array https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
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 Num. Py, 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
Lets take an example Cov Commercials Watched Product Purchase 14 7 12 15 6 7 7 1 5 10 var(a) cov(a, b) 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 Corrcoef Corr(x) corr(a, b) corr(b, a) Corr(y) https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
Num. Py Arithmatic Operations https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
THANKS https: //pythonclassroomdiary. wordpress. com by Sangeeta M Chauhan, Gwalior
- Slides: 41