intoruction to python for machine learning.pptx

mrsam3062 10 views 94 slides Oct 01, 2024
Slide 1
Slide 1 of 94
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94

About This Presentation

cadvs


Slide Content

Introduction to Python 1 National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Introduction to Python It was created by Guido van Rossum in Netherland, and released in 1991. The name "Python" was adopted from the Rossum’s favourite comedy series "Monty Python's Flying Circus". It is a high level general purpose programming language. It is compiled to byte code and executed in Python Virtual Machine. It is suitable for use as a scripting language, Web application implementation language, etc. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Introduction to Python It has a strong structuring constructs (nested code blocks, functions, classes, modules, and packages) and the use of objects and object oriented programming, enables us to write clear, logical applications for small and large tasks. Python is interpreted language. Python is available as Free and Open Source. Python run on different platform like Windows , Linux , Unix etc National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Anaconda Installation National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

To launch a Jupyter notebook, Open your Anaconda Prompt Navigate to the directory where you would like to save your notebook files. Then type the command jupyter notebook and the program will instantiate a local server at localhost:8888. Launch a Jupyter Notebook National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

To Create a Jupyter notebook File, Click on New and select python 3(ipykernal) Creating , Saving and Executing Jupyter Notebook File National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

To Save a Jupyter notebook File, Click on Untitled1 and type suitable file name. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Varieties(Flavors) of Python: Flavors of python refer to the different types of python compilers. These are useful to integrate various programming lanluages into python. CPython Standard Python 2.x implemented in C.(python programs run in c) Jython Python programs run in java environment http://www.jython.org/ PyPy Python with a JIT compiler and stackless mode http://pypy.org/(written in python) Stackless Python with enhanced thread support and microthreads etc. http://www.stackless.com/ (tasklets->threads->process) IronPython Python for .NET and the CLR http://ironpython.net/ Python 3 – The new, new Python. This is intended as a replacement for Python 2.x. http://www.python.org/doc/. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Setting Environment Variable To add the Python directory to the path for a particular session in Windows − At the command prompt, type path %path%;C:\Python38-32 and press Enter. Where C:\Python38-32 is the path of the Python directory. You can also select the – “ Add Python to Environment Variable ” , while installing the Python. After setting the path variable C:\>python Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Execution of a python program Python file first get compiled to give us byte code and that byte code is interpreted into machine language. This is performed by PVM. Execution: x.py x.pyc x.exe Machine Code Compile Using Python Compiler Interpreter Run using Python Virtual Machine Python Code Python Compiled File National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

List of the Python keywords >>>help("keywords") False class from or None continue global pass True def if raise and del import return as elif in try assert else is while async except lambda with await finally nonlocal yield break for not National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Python Programming Basic Data Types The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has five standard data types − Numbers String List Tuple Dictionary National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Data types: Hierarchical View National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Python Programming Python Identifiers It is a name used to identify a variable, function, class, module or other objects in Python program. An identifier starts with a letter (A to Z) or (a to z) or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). Python does not allow the characters - @, $, and % . Valid Identifier- myvar, my_var , _my_var, myVar, MYVAR, myvar2 National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Literals, Constants and Data Type National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Literal is a raw data assigned to a variable or constant. Literals are immutable. Types of Literal Text : str Numeric : int, float, complex Boolean Type : bool Special : None Numeric Literal – 10 ,10.76 , 10+12j Text Literal – 'Mango' , ''Mango'' , '''Mango''' Boolean Literal - True , False Literals National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Constants A constant is a type of variable whose value cannot be changed, during the execution of program. In python, it does not prevent reassignment. Constants are usually declared and assigned in a module. Module is a file containing variables, functions, etc which is imported to the main file. Usually, constants are written in all capital letters and underscores separating the words. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Types of Literals Numeric Literal Numeric : int, float, complex Integer Literal No fractional part allowed. Types of Integer Literal Decimal Literal (base 10) - 10 Binary Literal (base 2) – 0b 1010 Octal Literal (base 8) – 0o 10 Hexadecimal Literal (base 16) - 0x 12c a=10 b=0b10 c=0o10 d=0x10 print('Decimal ', a) print('Binary ',b) print('Octal ',c) print('Hexadecimal ',d) Output Decimal 10 Binary 2 Octal 8 Hexadecimal 16 What is the output, if a=0x1C ? National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Types of Literals Float Literal Fractional part allowed. Types of Float Literal Fixed Literal - 10.45 Scientific Literal – 10e2 , 10E2 10e2 is equivalent to 10 X 10 2 10e-2 is equivalent to 10 X 10 -2 Complex Literal It has real and imaginary part 10+12j Float Literal 10 E 2 Mantissa Exponent 10 X 10 2 = 1000 Complex Literal 10 + 12j Real Imaginary National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Types of Literals Complex Literal d=10 +12j print('Complex ', d) print('Real Part ', d.real) print('Imaginary Part ',d.imag) Output Complex (10+12j) Real Part 10.0 Imaginary Part 12.0 Float Literals a=10.45 b=10E2 c=10E-2 print('Fixed ', a) print('Scientific ',b) print('Scientific ',c) Output Fixed 10.45 Scientific 1000.0 Scientific 0.1 What is the output, if a=0.1E3 ? Integer Literals a=0b10 b=0x10 c=0o10 d=a+b+c print(‘Sum ', d) Output Sum 26 What is the output, if a=0x1c+0b1c ? National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Types of Literals String Literal It is a sequence of characters surrounded by quotes. Quotes can be single, double, or triple quotes for a string. A string that is prefixed with an  r or R  before the opening quotes is a “raw” string . Backslash ( \ ) allow the line to be continued as single line. Example s1="This is Python" s2= "P" s3 = """This is a multiline string with more than one line code.""" s4=r"First \n Second“ s5 ="This is \ Python" Character pairs start with backslash(\) \n = Newline \r = Carriage Return \t = Tab \\ = \ \u =Marks the start of a Unicode character code \" or \' = A double or single quote character  without  ending the current string. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Types of Literals s1="This is Python" s2= "P" s3 = """This is a multiline string with more than one line code.""" s4="First \n Second" s5=r"First \n Second" s6="This is \ Python" print("s1 ",s1) print("s2 ",s2) print("s3 ",s3) print("s4 ",s4) print("s5 ",s5) print("s6 ",s6) Output s1 This is Python s2 P s3 This is a multiline string with more than one line code. s4 First Second s5 First \n Second s6 This is Python lang\tver\nPython\t3 lang\tver\nPython\t3 National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Types of Literals Boolean literals It can have two values:  True or False. True has numerical value 1. False has numerical value 0. Example x1 = (1 == True) x2 = (1 == False) x3 = True + 4 x4 = False + 10 x1 = (1 == True) x2 = (1 == False) x3 = True + 4 x4 = False + 10 print(“x1 is", x1) print(“x2 is", x2) print(“x3:", x3) print(“x4:", x4) Output x1 is True x2 is False x3: 5 x4: 10 National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Types of Literals Special literals Python has one special literal i.e.  None . It to specify that the variable has not been created. Example x = 10 y = None Memory allocated to variable when the literal assigned to the variable for the first time . x = 10 y = None print("x is", x) print("y is", y) Output x is 10 y is None National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Python Data Types Data type allows the type of data stored in memory. For example, A person's age is stored as a numeric value and Address is stored as alphanumeric characters. Data Types defines the operations possible on them and the storage method for each of them. Variables can store data of different types, and different types can do different things. Python has five standard data types − Numbers String List Tuple Dictionary National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Python Data Types Python has the following data types built-in under the categories: Text Type : str Numeric Types : int, float, complex Sequence Types : list, tuple, range Mapping Type : dict Set Types : set Boolean Type : bool Example Data Type x = "Hello World" Str x = 20 Int x = 20.5 Float x = 1j Complex x = [ "apple", "banana", "cherry" ] list x = ("apple", "banana", "cherry") Tuple x = range(6) Range x = {"name" : “Ajay", "age" : 36} Dict x = {"apple", "banana", "cherry"} Set x = True bool National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Python Data Types x1 = "Hello World" x2 = 20 x3 = 20.5 x4 = 1j x5 = True print("String : ", x1) print("Integer : ", x2) print("Float : ", x3) print("Complex : ", x4) print("Bool : ", x5) Output String : Hello World Integer : 20 Float : 20.5 Complex : 1j x6 = ["apple", "banana", "cherry"] x7= ("apple", "banana", "cherry") x8 = range(6) x9= {"name" : “Ajay", "age" : 36} x10= {"apple", "banana", "cherry"} print("List : ", x6) print("Tuple : ", x7) print("Range : ", x8) print("Dictionary : ", x9) print("Set : ", x10) Output List : ['apple', 'banana', 'cherry'] Tuple : ('apple', 'banana', 'cherry') Range : range(0, 6) Dictionary : {'name': ‘Ajay', 'age': 36} Set : {'banana', 'apple', 'cherry‘} National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Type Conversion Casting allows to specify a type on to a variable. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Type Conversion Integers: x = int(1) # x will be 1 y = int(2.8) # y will be 2 z = int("3") # z will be 3 Floats: x = float(1)  # x will be 1.0 y = float(2.8)   # y will be 2.8 z = float("3")   # z will be 3.0 w = float("4.2") # w will be 4.2 Strings: x = str("s1") # x will be 's1' y = str(2) # y will be '2' z = str(3.0) # z will be '3.0' National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Python libraries for ML National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Numerical Python (NumPy) NumPy is the most foundational package for numerical computing in Python. If you are going to work on data analysis or machine learning projects, then having a solid understanding of NumPy is nearly mandatory. Indeed, many other libraries, such as pandas and scikit-learn, use NumPy’s array objects as the lingua franca for data exchange. One of the reasons as to why NumPy is so important for numerical computations is because it is designed for efficiency with large arrays of data. The reasons for this include: - It stores data internally in a continuous block of memory, independent of other in-built Python objects. - It performs complex computations on entire arrays without the need for for loops. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

The NumPy ndarray: A multi-dimensional array object The NumPy ndarray object is a fast and flexible container for large data sets in Python. NumPy arrays are a bit like Python lists, but are still a very different beast at the same time. Arrays enable you to store multiple items of the same data type. It is the facilities around the array object that makes NumPy so convenient for performing math and data manipulations. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Ndarray vs. lists By now, you are familiar with Python lists and how incredibly useful they are. So, you may be asking yourself: “ I can store numbers and other objects in a Python list and do all sorts of computations and manipulations through list comprehensions, for-loops etc. What do I need a NumPy array for?” There are very significant advantages of using NumPy arrays overs lists. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Creating a NumPy array To understand these advantages, lets create an array. One of the most common, of the many, ways to create a NumPy array is to create one from a list by passing it to the np.array() function. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Differences between lists and ndarrays The key difference between an array and a list is that arrays are designed to handle vectorised operations while a python lists are not. That means, if you apply a function, it is performed on every item in the array, rather than on the whole array object. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Let’s suppose you want to add the number 2 to every item in the list. The intuitive way to do this is something like this: That was not possible with a list, but you can do that on an array: In: Out: In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

It should be noted here that, once a Numpy array is created, you cannot increase its size. To do so, you will have to create a new array. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Create a 2d array from a list of list You can pass a list of lists to create a matrix-like a 2d array. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

The dtype argument You can specify the data-type by setting the dtype() argument. Some of the most commonly used NumPy dtypes are: float , int , bool , str , and object. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

The astype argument You can also convert it to a different data-type using the astype method. In: Out: Remember that, unlike lists, all items in an array have to be of the same type. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

dtype=‘object’ However, if you are uncertain about what data type your array will hold, or if you want to hold characters and numbers in the same array, you can set the dtype as 'object'. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

The tolist() function You can always convert an array into a list using the tolist() command. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Inspecting a NumPy array There are a range of functions built into NumPy that allow you to inspect different aspects of an array: In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Extracting specific items from an array You can extract portions of the array using indices, much like when you’re working with lists. Unlike lists, however, arrays can optionally accept as many parameters in the square brackets as there are number of dimensions In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Boolean indexing A boolean index array is of the same shape as the array-to-be-filtered, but it only contains TRUE and FALSE values. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Pandas Pandas, like NumPy, is one of the most popular Python libraries for data analysis. It is a high-level abstraction over low-level NumPy, which is written in pure C. Pandas provides high-performance, easy-to-use data structures and data analysis tools. There are two main structures used by pandas; data frames and series . National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Indices in a pandas series A pandas series is similar to a list, but differs in the fact that a series associates a label with each element. This makes it look like a dictionary. If an index is not explicitly provided by the user, pandas creates a RangeIndex ranging from 0 to N -1. Each series object also has a data type. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

As you may suspect by this point, a series has ways to extract all of the values in the series, as well as individual elements by index. In: Out: You can also provide an index manually. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

It is easy to retrieve several elements of a series by their indices or make group assignments. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Filtering and maths operations Filtering and maths operations are easy with Pandas as well. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Pandas data frame Simplistically, a data frame is a table, with rows and columns. Each column in a data frame is a series object. Rows consist of elements inside series. Case ID Variable one Variable two Variable 3 1 123 ABC 10 2 456 DEF 20 3 789 XYZ 30 National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Creating a Pandas data frame Pandas data frames can be constructed using Python dictionaries. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

You can also create a data frame from a list. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

You can ascertain the type of a column with the type() function. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

A Pandas data frame object as two indices; a column index and row index. Again, if you do not provide one, Pandas will create a RangeIndex from 0 to N -1. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

There are numerous ways to provide row indices explicitly. For example, you could provide an index when creating a data frame: In: Out: or do it during runtime. Here, I also named the index ‘country code’. In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Row access using index can be performed in several ways. First, you could use .loc() and provide an index label. Second, you could use .iloc() and provide an index number In: Out: In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

A selection of particular rows and columns can be selected this way. In: Out: You can feed .loc() two arguments, index list and column list, slicing operation is supported as well: In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Filtering Filtering is performed using so-called Boolean arrays. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Deleting columns You can delete a column using the drop() function. In: Out: In: Out: National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Reading from and writing to a file Pandas supports many popular file formats including CSV, XML, HTML, Excel, SQL, JSON, etc. Out of all of these, CSV is the file format that you will work with the most. You can read in the data from a CSV file using the read_csv () function. Similarly, you can write a data frame to a csv file with the to_csv () function. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Pandas has the capacity to do much more than what we have covered here, such as grouping data and even data visualisation. However, as with NumPy , we don’t have enough time to cover every aspect of pandas here. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Plotting a histogram in Python National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Bins You may have noticed the two histograms we’ve seen so far look different, despite using the exact same data. This is because they have different bin values. The left graph used the default bins generated by plt.hist () , while the one on the right used bins that I specified. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

There are a couple of ways to manipulate bins in matplotlib . Here, I specified where the edges of the bars of the histogram are; the bin edges. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

You could also specify the number of bins, and Matplotlib will automatically generate a number of evenly spaced bins. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Seaborn Matplotlib is a powerful, but sometimes unwieldy, Python library. Seaborn provides a high-level interface to  Matplotlib and makes it easier to produce graphs like the one on the right. Some IDEs incorporate elements of this “under the hood” nowadays. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Benefits of Seaborn Seaborn offers: - Using default themes that are aesthetically pleasing. - Setting custom colour palettes. - Making attractive statistical plots. - Easily and flexibly displaying distributions. - Visualising information from matrices and DataFrames. The last three points have led to Seaborn becoming the exploratory data analysis tool of choice for many Python users. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Plotting with Seaborn One of Seaborn's greatest strengths is its diversity of plotting functions.  Most plots can be created with one line of code. For example…. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Histograms Allow you to plot the distributions of numeric variables. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Other types of graphs: Creating a scatter plot Seaborn “linear model plot” function for creating a scatter graph Name of variable we want on the y-axis Name of variable we want on the x-axis Name of our dataframe fed to the “data=“ command National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Seaborn doesn't have a dedicated scatter plot function. We used  Seaborn's function for fitting and plotting a regression line; hence lmplot () However, Seaborn makes it easy to alter plots. To remove the regression line, we use the fit_reg =False command National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

The hue function Another useful function in Seaborn is the hue function, which enables us to use a variable to colour code our data points. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Factor plots Make it easy to separate plots by categorical classes. Colour by stage. Separate by stage. Generate using a swarmplot. Rotate axis on x-ticks by 45 degrees. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

A box plot National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

The total, stage, and legendary entries are not combat stats so we should remove them. Pandas makes this easy to do, we just create a new dataframe We just use Pandas’ .drop() function to create a dataframe that doesn’t include the variables we don’t want. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Seaborn’s theme Seaborn has a number of themes you can use to alter the appearance of plots. For example, we can use “ whitegrid ” to add grid lines to our boxplot. National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Heatmaps Useful for visualising matrix-like data. Here, we’ll plot the correlation of the stats_df variables National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center

Bar plot Visualises the distributions of categorical variables. Rotates the x-ticks 45 degrees National Institute of Electronics & Information Technology Ministry of Electronics & Information Technology (MeitY), Government of India Gorakhpur Center
Tags