Introduction to NumPy, NumPy Installation, Data Types, NumPy ndarray, Basic Indexing and Slicing, Boolean Indexing, Fancy Indexing, Data Processing using Arrays, Expressing Conditional Logic, Methods for Boolean Arrays, Sorting, Unique.
Size: 125.42 KB
Language: en
Added: Dec 17, 2022
Slides: 15 pages
Slide Content
Introduction to NumPy
Introduction NumPy is a Python package. It stands for 'Numerical Python'. It is a library consisting of multidimensional array objects and a collection of routines for processing of array.
NumPy Installation Open command prompt and set current working directory as C:\Users\UserName\AppData\Local\Programs\Python\Python310\Scripts> Then enter the following command pip install numpy
Data Types Python have strings, integer, float, boolean and complex. NumPy has some extra data types, and refer to data types with following character: i - integer M - datetime b - boolean O - object u - unsigned integer S - string f - float U - unicode string c - complex float V - fixed chunk of memory for other type (void) m - timedelta
NumPy Basics np.arange () - used to get evenly spaced values within a given interval. numpy.arange ([start, ]stop, [step, ] dtype =None) numpy.random.random () - used to generate random floats number in the half-open interval (0.0, 1.0). numpy.random.random ([size])
NumPy-ndarray NumPy is an N-dimensional array type called ndarray . It describes the collection of items of the same type. numpy.array (object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
Basic Indexing and Slicing Contents of ndarray object can be accessed and modified by indexing or slicing, just like Python's in-built container objects. slice( start,stop,step )
Boolean Indexing You can index specific values from a NumPy array using another NumPy array of Boolean values on one axis to specify the indices you want to access.
Fancy Indexing NumPy provides you with a convenient way to index an array called fancy indexing . Fancy indexing allows you to index a numpy array using the following: Another numpy array A Python list
Data Processing using Arrays With the NumPy package, we can easily solve many kinds of data processing tasks without writing complex loops. NumPy contains a large number of various mathematical operations arithmetic operations, trigonometric functions, handling complex numbers, etc.
Expressing Conditional Logic The numpy.where () function returns the indices of elements in an input array where the given condition is satisfied. numpy.where (condition[, x, y]) Here, x – yield True value y – yield False value
Methods for Boolean Array Boolean array is a type of array (collection of values) that can be used to represent logical ‘True’ or ‘False’ values stored. Declarations: boolAry = np.array ( objects,dtype = bool ) boolAry = condition using array Logical Methods: boolAry = np.logical_and (ary1,ary2) boolAry = np.logical_or (ary1,ary2) boolAry = np.logical_xor (ary1,ary2) boolAry = np.logical_not ( ary )
NumPy -Sorting numpy.sort () – Returns sorted copy of input array. numpy.sort ( ary , axis, kind, order) numpy.argsort () – Returns the indices that would sort an array. numpy.argsort ( a , axis=-1 , kind=None , order=None ) numpy.lexsort () - Perform an indirect stable sort using a sequence of keys. numpy.lexsort ( keys , axis=- 1 )
NumPy -Unique numpy.unique () - returns an array of unique elements in the input array. numpy.unique ( arr , return_index , return_inverse , return_counts )