From 5c3e1c2ed86b57838d16151e4d9c305eae565084 Mon Sep 17 00:00:00 2001 From: Ananyagra24 <155280507+Ananyagra24@users.noreply.github.com> Date: Sun, 12 May 2024 21:49:55 +0530 Subject: [PATCH 01/66] Create History --- contrib/History | 1 + 1 file changed, 1 insertion(+) create mode 100644 contrib/History diff --git a/contrib/History b/contrib/History new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/contrib/History @@ -0,0 +1 @@ + From b8e682d2fd427f8c76bde3bc30207cad327aec5d Mon Sep 17 00:00:00 2001 From: Ananyagra24 <155280507+Ananyagra24@users.noreply.github.com> Date: Mon, 13 May 2024 08:40:22 +0530 Subject: [PATCH 02/66] Delete contrib/History --- contrib/History | 1 - 1 file changed, 1 deletion(-) delete mode 100644 contrib/History diff --git a/contrib/History b/contrib/History deleted file mode 100644 index 8b13789..0000000 --- a/contrib/History +++ /dev/null @@ -1 +0,0 @@ - From 83809066303a7f962f2063d1613bad26f9776c1d Mon Sep 17 00:00:00 2001 From: Ananyagra24 <155280507+Ananyagra24@users.noreply.github.com> Date: Mon, 13 May 2024 21:10:18 +0530 Subject: [PATCH 03/66] Create dice_roller.md --- contrib/mini-projects/dice_roller.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 contrib/mini-projects/dice_roller.md diff --git a/contrib/mini-projects/dice_roller.md b/contrib/mini-projects/dice_roller.md new file mode 100644 index 0000000..c083934 --- /dev/null +++ b/contrib/mini-projects/dice_roller.md @@ -0,0 +1,19 @@ +``` +import random +def dice(): + dice_no = random.choice([1,2,3,4,5,6]) + return "You got " + str(dice_no) +def roll_dice(): + print("Hey Guys, you will now roll a single dice using Python!") + while True: + start=input("Type \'k\' to roll the dice: ").lower() + if start != 'k': + print("Invalid input. Please try again.") + continue + print(dice()) + roll_again = input("Do you want to reroll? (Yes/No): ").lower() + if roll_again != 'yes': + break + print("Thanks for rolling the dice.") +roll_dice() +``` From f8a67cf65dc3d70cf0a81a0cebd55770f0ffc728 Mon Sep 17 00:00:00 2001 From: Ananyagra24 <155280507+Ananyagra24@users.noreply.github.com> Date: Mon, 13 May 2024 21:11:01 +0530 Subject: [PATCH 04/66] Update index.md --- contrib/mini-projects/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/mini-projects/index.md b/contrib/mini-projects/index.md index 82596a2..f60b286 100644 --- a/contrib/mini-projects/index.md +++ b/contrib/mini-projects/index.md @@ -1,3 +1,3 @@ # List of sections -- [Section title](filename.md) +- [Dice Roller](dice_roller.md) From ea32f2bd8ce5c16960c66e72b6e886d9b260f51d Mon Sep 17 00:00:00 2001 From: Hema Sree Date: Wed, 15 May 2024 17:49:44 +0530 Subject: [PATCH 05/66] Added intro to numpy --- contrib/numpy/Introduction.md | 36 +++++++++++++++++++++++++++++++++++ contrib/numpy/index.md | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 contrib/numpy/Introduction.md diff --git a/contrib/numpy/Introduction.md b/contrib/numpy/Introduction.md new file mode 100644 index 0000000..59a1938 --- /dev/null +++ b/contrib/numpy/Introduction.md @@ -0,0 +1,36 @@ +# Introduction + +## What is Numpy? + +NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. It is open-source software. + +## Features of NumPy + +NumPy has various features including these important ones: + +1. A powerful N-dimensional array object +2. Sophisticated (broadcasting) functions +3. Tools for integrating C/C++ and Fortran code +4. Useful linear algebra, Fourier transform, and random number capabilities + +## Install Numpy + +Before installing Numpy, ensure you have Python installed on your system. You can download and install Python from the [official Python website](https://www.python.org/). + +Numpy can be installed via the following pip command: + +```bash +pip install numpy + +``` + +# How to import NumPy + +To access NumPy and its functions import it in your Python code like this: + +```bash +import numpy as np + +``` + +We shorten the imported name to np for better readability of code using NumPy. This is a widely adopted convention that makes your code more readable for everyone working on it. We recommend to always use import numpy as np. diff --git a/contrib/numpy/index.md b/contrib/numpy/index.md index 82596a2..c6bf23c 100644 --- a/contrib/numpy/index.md +++ b/contrib/numpy/index.md @@ -1,3 +1,3 @@ # List of sections -- [Section title](filename.md) +- [Introduction](Introduction.md) From 59e79c7627db41589dc554e1440d0ee5aacf3c9c Mon Sep 17 00:00:00 2001 From: Hema Sree Date: Wed, 15 May 2024 17:57:30 +0530 Subject: [PATCH 06/66] Added numpy intro --- contrib/numpy/Introduction.md | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/contrib/numpy/Introduction.md b/contrib/numpy/Introduction.md index 59a1938..c437c48 100644 --- a/contrib/numpy/Introduction.md +++ b/contrib/numpy/Introduction.md @@ -1,36 +1,30 @@ # Introduction -## What is Numpy? +## What is NumPy? -NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. It is open-source software. +NumPy is a powerful array-processing library in Python, essential for scientific computing. It provides efficient data structures and tools for working with multidimensional arrays. -## Features of NumPy +## Key Features -NumPy has various features including these important ones: +1. **Efficient Arrays:** NumPy offers high-performance N-dimensional array objects for swift data manipulation. +2. **Broadcasting:** Advanced broadcasting enables seamless element-wise operations on arrays of varying shapes. +3. **Interoperability:** NumPy seamlessly integrates with C, C++, and Fortran, enhancing performance and versatility. +4. **Mathematical Tools:** Comprehensive support for linear algebra, Fourier transforms, and random number generation. -1. A powerful N-dimensional array object -2. Sophisticated (broadcasting) functions -3. Tools for integrating C/C++ and Fortran code -4. Useful linear algebra, Fourier transform, and random number capabilities +## Installation -## Install Numpy - -Before installing Numpy, ensure you have Python installed on your system. You can download and install Python from the [official Python website](https://www.python.org/). - -Numpy can be installed via the following pip command: +Ensure Python is installed in your system. If not you can install it from here([official Python website](https://www.python.org/)),then install NumPy via: ```bash pip install numpy - ``` -# How to import NumPy +# Importing NumPy -To access NumPy and its functions import it in your Python code like this: +To access NumPy functions, import it with the alias `np`: -```bash +```python import numpy as np - ``` -We shorten the imported name to np for better readability of code using NumPy. This is a widely adopted convention that makes your code more readable for everyone working on it. We recommend to always use import numpy as np. +Using `np` as an alias enhances code readability and is a widely adopted convention. From 9f482ac8181b47e80c5469025ab067bab96ae54a Mon Sep 17 00:00:00 2001 From: Hema Sree Date: Wed, 15 May 2024 17:58:33 +0530 Subject: [PATCH 07/66] intro numpy --- contrib/numpy/Introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/numpy/Introduction.md b/contrib/numpy/Introduction.md index c437c48..c0f5f52 100644 --- a/contrib/numpy/Introduction.md +++ b/contrib/numpy/Introduction.md @@ -19,7 +19,7 @@ Ensure Python is installed in your system. If not you can install it from here([ pip install numpy ``` -# Importing NumPy +## Importing NumPy To access NumPy functions, import it with the alias `np`: From 2189d3914cb4194df2ca02eb33a2f0d11cd07844 Mon Sep 17 00:00:00 2001 From: Ritesh <112797055+Antiquely3059@users.noreply.github.com> Date: Wed, 15 May 2024 18:53:35 +0530 Subject: [PATCH 08/66] Create Rock Paper Scissors Game.md --- .../mini-projects/Rock Paper Scissors Game.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 contrib/mini-projects/Rock Paper Scissors Game.md diff --git a/contrib/mini-projects/Rock Paper Scissors Game.md b/contrib/mini-projects/Rock Paper Scissors Game.md new file mode 100644 index 0000000..ed815d5 --- /dev/null +++ b/contrib/mini-projects/Rock Paper Scissors Game.md @@ -0,0 +1,48 @@ +```python +import tkinter as tk +import random + +def determine_winner(user_choice, computer_choice): + """Determine the winner of the game.""" + if user_choice == computer_choice: + return "It's a tie!", computer_choice + elif (user_choice == "rock" and computer_choice == "scissors") or \ + (user_choice == "paper" and computer_choice == "rock") or \ + (user_choice == "scissors" and computer_choice == "paper"): + return "You win!", computer_choice + else: + return "Computer wins!", computer_choice + +def play_game(): + """Play the game and display the result.""" + user_choice = user_var.get() + computer_choice = random.choice(["rock", "paper", "scissors"]) + result, computer_pick = determine_winner(user_choice, computer_choice) + result_label.config(text=result) + computer_label.config(text=f"Computer picked: {computer_pick}") + +# Create main window +root = tk.Tk() +root.title("Rock Paper Scissors") + +# User choice options +user_var = tk.StringVar() +user_var.set("rock") # Default choice +choices = ["rock", "paper", "scissors"] +for choice in choices: + rb = tk.Radiobutton(root, text=choice, variable=user_var, value=choice) + rb.pack() + +# Play button +play_button = tk.Button(root, text="Play", command=play_game) +play_button.pack() + +# Result label +result_label = tk.Label(root, text="", font=("Helvetica", 16)) +result_label.pack() + +# Computer pick label +computer_label = tk.Label(root, text="", font=("Helvetica", 12)) +computer_label.pack() + +root.mainloop() From 8f0c49efa87011c70be9d9c89889c2d9d63b8ec1 Mon Sep 17 00:00:00 2001 From: Ritesh <112797055+Antiquely3059@users.noreply.github.com> Date: Wed, 15 May 2024 18:59:12 +0530 Subject: [PATCH 09/66] Update Rock Paper Scissors Game.md --- contrib/mini-projects/Rock Paper Scissors Game.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/contrib/mini-projects/Rock Paper Scissors Game.md b/contrib/mini-projects/Rock Paper Scissors Game.md index ed815d5..9cc17ee 100644 --- a/contrib/mini-projects/Rock Paper Scissors Game.md +++ b/contrib/mini-projects/Rock Paper Scissors Game.md @@ -1,3 +1,12 @@ + +Here's how it looks: + +# Rock Paper Scissors Game + +This is a simple implementation of the classic rock-paper-scissors game in Python. + +## Code: + ```python import tkinter as tk import random From c79f13a28c7f21033c4c85d14a15fb675ee5bd84 Mon Sep 17 00:00:00 2001 From: Ritesh <112797055+Antiquely3059@users.noreply.github.com> Date: Wed, 15 May 2024 19:02:05 +0530 Subject: [PATCH 10/66] Update Rock Paper Scissors Game.md --- contrib/mini-projects/Rock Paper Scissors Game.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/contrib/mini-projects/Rock Paper Scissors Game.md b/contrib/mini-projects/Rock Paper Scissors Game.md index 9cc17ee..b08a268 100644 --- a/contrib/mini-projects/Rock Paper Scissors Game.md +++ b/contrib/mini-projects/Rock Paper Scissors Game.md @@ -1,6 +1,3 @@ - -Here's how it looks: - # Rock Paper Scissors Game This is a simple implementation of the classic rock-paper-scissors game in Python. From 25a319ff3cb82a26ddc8f0b272713bb68a8fea77 Mon Sep 17 00:00:00 2001 From: Ritesh <112797055+Antiquely3059@users.noreply.github.com> Date: Wed, 15 May 2024 19:54:12 +0530 Subject: [PATCH 11/66] Update Rock Paper Scissors Game.md --- .../mini-projects/Rock Paper Scissors Game.md | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/contrib/mini-projects/Rock Paper Scissors Game.md b/contrib/mini-projects/Rock Paper Scissors Game.md index b08a268..c1a5ecf 100644 --- a/contrib/mini-projects/Rock Paper Scissors Game.md +++ b/contrib/mini-projects/Rock Paper Scissors Game.md @@ -2,8 +2,19 @@ This is a simple implementation of the classic rock-paper-scissors game in Python. -## Code: +## Code Explanation: +In this section, we import the required libraries (`tkinter` for GUI and `random` for generating computer choices) and define two functions: + +- `determine_winner(user_choice, computer_choice)`: + - This function determines the winner of the game based on the choices made by the user and the computer. + - It returns a tuple containing the result of the game and the computer's choice. + +- `play_game()`: + - This function handles the gameplay logic. + - It gets the user's choice from the radio buttons, generates a random choice for the computer, determines the winner using the `determine_winner()` function, and updates the result and computer pick labels accordingly. + +### Imports and Function Definitions: ```python import tkinter as tk import random @@ -26,7 +37,9 @@ def play_game(): result, computer_pick = determine_winner(user_choice, computer_choice) result_label.config(text=result) computer_label.config(text=f"Computer picked: {computer_pick}") - +``` +### GUI Setup: +```python # Create main window root = tk.Tk() root.title("Rock Paper Scissors") @@ -38,7 +51,14 @@ choices = ["rock", "paper", "scissors"] for choice in choices: rb = tk.Radiobutton(root, text=choice, variable=user_var, value=choice) rb.pack() - +``` +- Here, we create the main window for the game using `tkinter.Tk()`. We set the title to "Rock Paper Scissors". +- We define a StringVar to store the user's choice and set the default choice to "rock". +- We create radio buttons for the user to choose from ("rock", "paper", "scissors") and pack them into the main window. +``` +``` +### Play Button and Result Labels: +```python # Play button play_button = tk.Button(root, text="Play", command=play_game) play_button.pack() @@ -50,5 +70,15 @@ result_label.pack() # Computer pick label computer_label = tk.Label(root, text="", font=("Helvetica", 12)) computer_label.pack() +``` +- We create a "Play" button that triggers the play_game() function when clicked, using tkinter.Button. +- We create two labels to display the result of the game (result_label) and the computer's choice (computer_label). Both labels initially display no text and are packed into the main window. +``` +``` +### Mainloop: +```python root.mainloop() +``` +- Finally, we start the Tkinter event loop using root.mainloop(), which keeps the GUI window open and responsive until the user closes it. +- From 1196e6ee89d05deb8186e815838c90ab1b49caec Mon Sep 17 00:00:00 2001 From: Ritesh <112797055+Antiquely3059@users.noreply.github.com> Date: Wed, 15 May 2024 19:57:34 +0530 Subject: [PATCH 12/66] Update Rock Paper Scissors Game.md --- contrib/mini-projects/Rock Paper Scissors Game.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contrib/mini-projects/Rock Paper Scissors Game.md b/contrib/mini-projects/Rock Paper Scissors Game.md index c1a5ecf..36326b0 100644 --- a/contrib/mini-projects/Rock Paper Scissors Game.md +++ b/contrib/mini-projects/Rock Paper Scissors Game.md @@ -53,7 +53,7 @@ for choice in choices: rb.pack() ``` - Here, we create the main window for the game using `tkinter.Tk()`. We set the title to "Rock Paper Scissors". -- We define a StringVar to store the user's choice and set the default choice to "rock". +- We define a `StringVar` to store the user's choice and set the default choice to "rock". - We create radio buttons for the user to choose from ("rock", "paper", "scissors") and pack them into the main window. ``` ``` @@ -71,8 +71,8 @@ result_label.pack() computer_label = tk.Label(root, text="", font=("Helvetica", 12)) computer_label.pack() ``` -- We create a "Play" button that triggers the play_game() function when clicked, using tkinter.Button. -- We create two labels to display the result of the game (result_label) and the computer's choice (computer_label). Both labels initially display no text and are packed into the main window. +- We create a "Play" button that triggers the `play_game()` function when clicked, using `tkinter.Button`. +- We create two labels to display the result of the game (`result_label`) and the computer's choice (`computer_label`). Both labels initially display no text and are packed into the main window. ``` ``` @@ -80,5 +80,5 @@ computer_label.pack() ```python root.mainloop() ``` -- Finally, we start the Tkinter event loop using root.mainloop(), which keeps the GUI window open and responsive until the user closes it. +- Finally, we start the Tkinter event loop using `root.mainloop()`, which keeps the GUI window open and responsive until the user closes it. - From 989278ebe3ac868d63f6f66b9461474fb2f8679e Mon Sep 17 00:00:00 2001 From: Hema Sree Date: Thu, 16 May 2024 13:08:37 +0530 Subject: [PATCH 13/66] change file name --- contrib/numpy/Introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/numpy/Introduction.md b/contrib/numpy/Introduction.md index c0f5f52..eb3af67 100644 --- a/contrib/numpy/Introduction.md +++ b/contrib/numpy/Introduction.md @@ -21,7 +21,7 @@ pip install numpy ## Importing NumPy -To access NumPy functions, import it with the alias `np`: +To access NumPy functions, import it with the alias `np`. ```python import numpy as np From 7ae451cc067a73adc6068b18ddb461cea84942c6 Mon Sep 17 00:00:00 2001 From: Hema Sree Date: Thu, 16 May 2024 13:12:41 +0530 Subject: [PATCH 14/66] change file name --- contrib/numpy/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/numpy/index.md b/contrib/numpy/index.md index c6bf23c..5d3f63b 100644 --- a/contrib/numpy/index.md +++ b/contrib/numpy/index.md @@ -1,3 +1,3 @@ # List of sections -- [Introduction](Introduction.md) +- [Introduction](introduction.md) From 9bb50dcf528efb9cd1dbbd8970e5a52130c86482 Mon Sep 17 00:00:00 2001 From: HEMA SREE VEMULAMADA <109607812+Hemav009@users.noreply.github.com> Date: Thu, 16 May 2024 13:14:26 +0530 Subject: [PATCH 15/66] Rename Introduction.md to introduction.md --- contrib/numpy/{Introduction.md => introduction.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename contrib/numpy/{Introduction.md => introduction.md} (100%) diff --git a/contrib/numpy/Introduction.md b/contrib/numpy/introduction.md similarity index 100% rename from contrib/numpy/Introduction.md rename to contrib/numpy/introduction.md From 2ff1b14b5cb95f5280b709cff64dd399ce570de4 Mon Sep 17 00:00:00 2001 From: Ritesh <112797055+Antiquely3059@users.noreply.github.com> Date: Fri, 17 May 2024 08:45:09 +0530 Subject: [PATCH 16/66] Update index.md --- contrib/mini-projects/index.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contrib/mini-projects/index.md b/contrib/mini-projects/index.md index 82596a2..37ebc9b 100644 --- a/contrib/mini-projects/index.md +++ b/contrib/mini-projects/index.md @@ -1,3 +1,4 @@ # List of sections -- [Section title](filename.md) +- [Rock Paper Scissors Game](Rock Paper Scissors Game.md) + From 7af22b698cca0a7bbf993319894d8ca69e62bf89 Mon Sep 17 00:00:00 2001 From: Ritesh <112797055+Antiquely3059@users.noreply.github.com> Date: Fri, 17 May 2024 08:45:55 +0530 Subject: [PATCH 17/66] Rename Rock Paper Scissors Game.md to Rock_Paper_Scissors_Game.md --- .../{Rock Paper Scissors Game.md => Rock_Paper_Scissors_Game.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename contrib/mini-projects/{Rock Paper Scissors Game.md => Rock_Paper_Scissors_Game.md} (100%) diff --git a/contrib/mini-projects/Rock Paper Scissors Game.md b/contrib/mini-projects/Rock_Paper_Scissors_Game.md similarity index 100% rename from contrib/mini-projects/Rock Paper Scissors Game.md rename to contrib/mini-projects/Rock_Paper_Scissors_Game.md From f0d0b515a21467b985766163a027f3a1d6cf9899 Mon Sep 17 00:00:00 2001 From: Ritesh <112797055+Antiquely3059@users.noreply.github.com> Date: Fri, 17 May 2024 08:46:20 +0530 Subject: [PATCH 18/66] Update index.md --- contrib/mini-projects/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/mini-projects/index.md b/contrib/mini-projects/index.md index 37ebc9b..7d0285a 100644 --- a/contrib/mini-projects/index.md +++ b/contrib/mini-projects/index.md @@ -1,4 +1,4 @@ # List of sections -- [Rock Paper Scissors Game](Rock Paper Scissors Game.md) +- [Rock Paper Scissors Game](Rock_Paper_Scissors_Game.md) From 78cfa1f238e2d0a6c90c8bfc73d151a7d4cc3da4 Mon Sep 17 00:00:00 2001 From: iABn0rma1 Date: Fri, 17 May 2024 16:47:41 +0530 Subject: [PATCH 19/66] Understanding and Building the ANNs from Scratch --- .../ArtificialNeuralNetwork.md | 172 ++++++++++++++++++ contrib/machine-learning/index.md | 1 + 2 files changed, 173 insertions(+) create mode 100644 contrib/machine-learning/ArtificialNeuralNetwork.md diff --git a/contrib/machine-learning/ArtificialNeuralNetwork.md b/contrib/machine-learning/ArtificialNeuralNetwork.md new file mode 100644 index 0000000..0fafc6c --- /dev/null +++ b/contrib/machine-learning/ArtificialNeuralNetwork.md @@ -0,0 +1,172 @@ +# Understanding the Neural Network + +## Table of Contents +
+Click to expand + +- [Introduciton](#introduction) +- [Neuron to Perceptron](#neuron-to-perceptron) +- [Key concepts](#key-concepts) + - [Layers](#layers) + - [Weights and Biases](#weights-and-biases) + - [Activation Function](#activation-functions) + - [Forward and Backward Pass](#forward-and-backward-propagation) +- [Implementation](#building-from-scratch) + +
+ + +## Introduction + +This guide will walk you through a fundamental neural network implementation in Python. We'll build a `Neural Network` from scratch, allowing you to grasp the core concepts of how neural networks learn and make predictions. + +### Let's start by Understanding the Basic Architecture of Neural Nets + +## Neuron to Perceptron + +| `Neuron` cells forming the humand nervous system | `Perceptron` inspired from human brain | +| :----------------------------------------------- | -------------------------------------: | +| | | +| Neurons are nerve cells that send messages all over your body to allow you to do everything from breathing to talking, eating, walking, and thinking. | The perceptron is a mathematical model of a biological neuron. Performing heavy computations to think like humans. | +| Neuron collects signals from dendrites. | The first layer is knownn as Input Layer, acting like dendritres to receive the input signal. | +| Synapses are the connections between neurons where signals are transmitted. | Weights represent synapses. | +The axon terminal releases neurotransmitters to transmit the signal to other neurons. | The output is the final result – between 1 & 0, representing classification or prediction. | +--- +> Human brain has a Network of Neurons, about 86 billion neurons and more than a 100 trillion synapses connections! + + +## **Key Concepts** + +Artificial neurons are the fundamental processing units in an ANN. They receive inputs, multiply them by weights (representing the strength of connections), sum those weighted inputs, and then apply an activation function to produce an output. + +### Layers +Neurons in ANNs are organized into layers: +* **Input Layer:** Receives the raw data. +* **(n) Hidden Layers:** (Optional) Intermediate layers where complex transformations occur. They learn to detect patterns and features in the data. +* **Output Layer:** Produces the final result (prediction or classification). +

+ +

+ +### Weights and Biases +- For each input $(x_i)$, a weight $(w_i)$ is associated with it. Weights, multiplied with input units $(w_i \cdot x_i)$, determine the influence of one neuron's output on another. +- A bias $(b_i)$ is added to help influence the end product, giving the equation as $(w_i \cdot x_i + b_i)$. +- During training, the network adjusts these weights and biases to minimize errors and improve its predictions. + +

+ +

+ +### Activation Functions +- An activation function is applied to the result to introduce non-linearity in the model, allowing ANNs to learn more complex relationships from the data. +- The resulting equation: $y = f(g(x))$, determines whether the neuron will "fire" or not, i.e., if its output will be used as input for the next neuron. +- Common activation functions include the sigmoid function, tanh (hyperbolic tangent), and ReLU (Rectified Linear Unit). + +

+ +

+ +### Forward and Backward Propagation +- **Flow of Information:** All the above steps are part of Forward Propagation. It gives the output equation as $y = f\left(\sum_{i=1}^n w_i x_i + b_i\right)$ +- **Error Correction:** Backpropagation is the algorithm used to train ANNs by calculating the gradient of error at the output layer and then propagating this error backward through the network. This allows the network to adjust its weights and biases in the direction that reduces the error. +- The chain rule of calculus is the foundational concept to compute the gradient of the error: + $ + \delta_{ij}(E) = \frac{\partial E}{\partial w_{ij}} = \frac{\partial E}{\partial \hat{y}_j} \cdot \frac{\partial \hat{y}_j}{\partial \theta_j} \cdot \frac{\partial \theta_j}{\partial w_{ij}} + $ + where $E$ is the error, $\hat{y}_j$ is the predicted output, $\theta_j$ is the input to the activation function of the $j^{th}$ neuron, and $w_{ij}$ is the weight from neuron $i$ to neuron $j$. + +

+ +

+ + +## Building From Scratch + +```python +# Import required libraries +import numpy as np +import matplotlib.pyplot as plt + +class SimpleNeuralNetwork: + def __init__(self, input_size, hidden_size, output_size): + self.input_size = input_size + self.hidden_size = hidden_size + self.output_size = output_size + + # Initialize weights and biases + self.weights_input_hidden = np.random.randn(input_size, hidden_size) + self.bias_hidden = np.random.randn(hidden_size) + self.weights_hidden_output = np.random.randn(hidden_size, output_size) + self.bias_output = np.random.randn(output_size) + + def sigmoid(self, x): + return 1 / (1 + np.exp(-x)) + + def sigmoid_derivative(self, x): + return x * (1 - x) + + def forward(self, X): + self.hidden_layer_input = np.dot(X, self.weights_input_hidden) + self.bias_hidden + self.hidden_layer_output = self.sigmoid(self.hidden_layer_input) + + self.output_layer_input = np.dot(self.hidden_layer_output, self.weights_hidden_output) + self.bias_output + self.output = self.sigmoid(self.output_layer_input) + + return self.output + + def backward(self, X, y, learning_rate): + output_error = y - self.output + output_delta = output_error * self.sigmoid_derivative(self.output) + + hidden_error = output_delta.dot(self.weights_hidden_output.T) + hidden_delta = hidden_error * self.sigmoid_derivative(self.hidden_layer_output) + + self.weights_hidden_output += self.hidden_layer_output.T.dot(output_delta) * learning_rate + self.bias_output += np.sum(output_delta, axis=0) * learning_rate + self.weights_input_hidden += X.T.dot(hidden_delta) * learning_rate + self.bias_hidden += np.sum(hidden_delta, axis=0) * learning_rate + + def train(self, X, y, epochs, learning_rate): + self.losses = [] + for epoch in range(epochs): + self.forward(X) + self.backward(X, y, learning_rate) + loss = np.mean(np.square(y - self.output)) + self.losses.append(loss) + if epoch % 1000 == 0: + print(f"Epoch {epoch}, Loss: {loss}") + + def plot_loss(self): + plt.plot(self.losses) + plt.xlabel('Epochs') + plt.ylabel('Loss') + plt.title('Training Loss Over Epochs') + plt.show() +``` + +### Creating the Input & Output Array +Let's create a dummy input and outpu dataset. Here, the first two columns will be useful, while the rest might be noise. +```python +X = np.array([[0,0], [0,1], [1,0], [1,1]]) +y = np.array([[0], [1], [1], [1]]) +``` + +### Defining the Neural Network +With our input and output data ready, we'll define a simple neural network with one hidden layer containing three neurons. +```python +# neural network architecture +input_size = 2 +hidden_layers = 1 +hidden_neurons = [2] +output_size = 1 +``` + +### Visualizing the Training Loss +To understand how well our model is learning, let's visualize the training loss over epochs. +```python +model = NeuralNetwork(input_size, hidden_layers, hidden_neurons, output_size) +model.train(X, y, 100) +``` +

+ +

diff --git a/contrib/machine-learning/index.md b/contrib/machine-learning/index.md index 82596a2..c6f4df2 100644 --- a/contrib/machine-learning/index.md +++ b/contrib/machine-learning/index.md @@ -1,3 +1,4 @@ # List of sections - [Section title](filename.md) +- [Artificial Neural Network from the Ground Up](ArtificialNeuralNetwork.md) From 04caabe01b99ba9337e158c9c1018335eed35bcc Mon Sep 17 00:00:00 2001 From: HimakarC <116370824+HimakarC@users.noreply.github.com> Date: Fri, 17 May 2024 17:06:47 +0530 Subject: [PATCH 20/66] Update index.md --- contrib/pandas/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/pandas/index.md b/contrib/pandas/index.md index 5c0f2b4..aadd970 100644 --- a/contrib/pandas/index.md +++ b/contrib/pandas/index.md @@ -1,3 +1,4 @@ # List of sections - [Pandas Series Vs NumPy ndarray](pandas_series_vs_numpy_ndarray.md) +- [Excel Operations using Pandas DataFrame](excel_with_pandas.md) From 4ffc87b269773a98dfc5824abb32117684b85211 Mon Sep 17 00:00:00 2001 From: HimakarC <116370824+HimakarC@users.noreply.github.com> Date: Fri, 17 May 2024 17:07:17 +0530 Subject: [PATCH 21/66] Create excel_with_pandas.md --- contrib/pandas/excel_with_pandas.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 contrib/pandas/excel_with_pandas.md diff --git a/contrib/pandas/excel_with_pandas.md b/contrib/pandas/excel_with_pandas.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/contrib/pandas/excel_with_pandas.md @@ -0,0 +1 @@ + From 56170159e1ca6e88cfcee929e3316c7dac4432da Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 17 May 2024 18:09:43 +0530 Subject: [PATCH 22/66] written --- contrib/pandas/excel_with_pandas.md | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/contrib/pandas/excel_with_pandas.md b/contrib/pandas/excel_with_pandas.md index 8b13789..6902c3f 100644 --- a/contrib/pandas/excel_with_pandas.md +++ b/contrib/pandas/excel_with_pandas.md @@ -1 +1,51 @@ +# Pandas DataFrame + +The Pandas DataFrame is a two-dimensional, size-mutable, and possibly heterogeneous tabular data format with labelled axes. A data frame is a two-dimensional data structure in which the data can be organised in rows and columns. Pandas DataFrames are comprised of three main components: data, rows, and columns. + +In the real world, Pandas DataFrames are formed by importing datasets from existing storage, which can be a Excel file, a SQL database or CSV file. Pandas DataFrames may be constructed from lists, dictionaries, or lists of dictionaries, etc. + + +### Installation of libraries + +`pip install pandas + pip install xlrd` + + +Example for reading data from an Excel File: + +```python +import pandas as pd + +l = pd.read_excel('example.xlsx') +d = pd.DataFrame(l) +print(d) +``` +Output: +'''python + Name Age +0 John 12 +''' + +- **Note:** The above program can also be used for loading/retrieving the data. + + +Example for Inserting Data into Excel File: + +```python +import pandas as pd + +l = pd.read_excel('file_name.xlsx') +d = {'Name': ['Bob', 'John'], 'Age': [12, 28]} +d = pd.DataFrame(d) +L = pd.concat([l, d], ignore_index = True) +L.to_excel('file_name.xlsx', index = False) +print(L) +``` + +Output: +'''python + Name Age +0 Bob 12 +1 John 28 +''' From 9eb63b951d03e283aa76ee0d9ce980dc4ea509ee Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 17 May 2024 18:11:53 +0530 Subject: [PATCH 23/66] updated --- contrib/pandas/excel_with_pandas.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contrib/pandas/excel_with_pandas.md b/contrib/pandas/excel_with_pandas.md index 6902c3f..4e67964 100644 --- a/contrib/pandas/excel_with_pandas.md +++ b/contrib/pandas/excel_with_pandas.md @@ -21,10 +21,10 @@ d = pd.DataFrame(l) print(d) ``` Output: -'''python +```python Name Age 0 John 12 -''' +``` - **Note:** The above program can also be used for loading/retrieving the data. @@ -43,9 +43,9 @@ print(L) ``` Output: -'''python +```python Name Age 0 Bob 12 1 John 28 -''' +``` From 0b6bd74a3b7daae3b28f8fc66576d23baee4bbc0 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 17 May 2024 18:12:42 +0530 Subject: [PATCH 24/66] updated --- contrib/pandas/excel_with_pandas.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/pandas/excel_with_pandas.md b/contrib/pandas/excel_with_pandas.md index 4e67964..b09b8c3 100644 --- a/contrib/pandas/excel_with_pandas.md +++ b/contrib/pandas/excel_with_pandas.md @@ -7,7 +7,7 @@ In the real world, Pandas DataFrames are formed by importing datasets from exis ### Installation of libraries -`pip install pandas +`pip install pandas
pip install xlrd` From f8044fb421f609c0ae67e65eefb4feabac914568 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 17 May 2024 18:13:28 +0530 Subject: [PATCH 25/66] updated --- contrib/pandas/excel_with_pandas.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/pandas/excel_with_pandas.md b/contrib/pandas/excel_with_pandas.md index b09b8c3..af87b7b 100644 --- a/contrib/pandas/excel_with_pandas.md +++ b/contrib/pandas/excel_with_pandas.md @@ -7,8 +7,8 @@ In the real world, Pandas DataFrames are formed by importing datasets from exis ### Installation of libraries -`pip install pandas
- pip install xlrd` +`pip install pandas` +`pip install xlrd` Example for reading data from an Excel File: From 9fe61b8ec04b71037dff7506dd6169f259111bfa Mon Sep 17 00:00:00 2001 From: HimakarC <116370824+HimakarC@users.noreply.github.com> Date: Fri, 17 May 2024 18:15:20 +0530 Subject: [PATCH 26/66] Update excel_with_pandas.md --- contrib/pandas/excel_with_pandas.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/pandas/excel_with_pandas.md b/contrib/pandas/excel_with_pandas.md index af87b7b..137ef45 100644 --- a/contrib/pandas/excel_with_pandas.md +++ b/contrib/pandas/excel_with_pandas.md @@ -7,7 +7,7 @@ In the real world, Pandas DataFrames are formed by importing datasets from exis ### Installation of libraries -`pip install pandas` +`pip install pandas`
`pip install xlrd` From e4e9ad812fe87d70862390074deba405c2f9fe13 Mon Sep 17 00:00:00 2001 From: HimakarC <116370824+HimakarC@users.noreply.github.com> Date: Fri, 17 May 2024 18:16:41 +0530 Subject: [PATCH 27/66] excel_with_pandas.md --- contrib/pandas/excel_with_pandas.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/contrib/pandas/excel_with_pandas.md b/contrib/pandas/excel_with_pandas.md index 137ef45..13218d6 100644 --- a/contrib/pandas/excel_with_pandas.md +++ b/contrib/pandas/excel_with_pandas.md @@ -26,8 +26,6 @@ Output: 0 John 12 ``` -- **Note:** The above program can also be used for loading/retrieving the data. - Example for Inserting Data into Excel File: From e830336a54f5f6bce6e597df9900ee35b7a71709 Mon Sep 17 00:00:00 2001 From: HimakarC <116370824+HimakarC@users.noreply.github.com> Date: Fri, 17 May 2024 18:25:40 +0530 Subject: [PATCH 28/66] Create excel_with_pandas.md --- contrib/pandas/excel_with_pandas.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/contrib/pandas/excel_with_pandas.md b/contrib/pandas/excel_with_pandas.md index 13218d6..5798444 100644 --- a/contrib/pandas/excel_with_pandas.md +++ b/contrib/pandas/excel_with_pandas.md @@ -5,11 +5,20 @@ The Pandas DataFrame is a two-dimensional, size-mutable, and possibly heterogene In the real world, Pandas DataFrames are formed by importing datasets from existing storage, which can be a Excel file, a SQL database or CSV file. Pandas DataFrames may be constructed from lists, dictionaries, or lists of dictionaries, etc. +Features of Pandas `DataFrame`: + +- **Size mutable**: DataFrames are mutable in size, meaning that new rows and columns can be added or removed as needed. +- **Labeled axes**: DataFrames have labeled axes, which makes it easy to keep track of the data. +- **Arithmetic operations**: DataFrames support arithmetic operations on rows and columns. +- **High performance**: DataFrames are highly performant, making them ideal for working with large datasets. + + ### Installation of libraries `pip install pandas`
`pip install xlrd` +- **Note**: The `xlrd` library is used for Excel operations. Example for reading data from an Excel File: From 7fb2baf809f2cd40c0ccdd406fd75f8cb3e29d1b Mon Sep 17 00:00:00 2001 From: HimakarC <116370824+HimakarC@users.noreply.github.com> Date: Fri, 17 May 2024 18:31:22 +0530 Subject: [PATCH 29/66] Update excel_with_pandas.md --- contrib/pandas/excel_with_pandas.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/contrib/pandas/excel_with_pandas.md b/contrib/pandas/excel_with_pandas.md index 5798444..d325e46 100644 --- a/contrib/pandas/excel_with_pandas.md +++ b/contrib/pandas/excel_with_pandas.md @@ -56,3 +56,8 @@ Output: 1 John 28 ``` +### Usage of Pandas DataFrame: + +- Can be used to store and analyze financial data, such as stock prices, trading data, and economic data. +- Can be used to store and analyze sensor data, such as data from temperature sensors, motion sensors, and GPS sensors. +- Can be used to store and analyze log data, such as web server logs, application logs, and system logs From fc55d32e87621268fa32e16f0c9bec55464f7ead Mon Sep 17 00:00:00 2001 From: Vrisha Shah <74671946+Vrisha213@users.noreply.github.com> Date: Fri, 17 May 2024 18:44:42 +0530 Subject: [PATCH 30/66] Create confusion-matrix.md Added content on Confusion Matrix with code example and heatmap visualization. --- contrib/machine-learning/confusion-matrix.md | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 contrib/machine-learning/confusion-matrix.md diff --git a/contrib/machine-learning/confusion-matrix.md b/contrib/machine-learning/confusion-matrix.md new file mode 100644 index 0000000..959df9a --- /dev/null +++ b/contrib/machine-learning/confusion-matrix.md @@ -0,0 +1,61 @@ +Confusion Matrix - A confusion matrix is a fundamental performance evaluation tool used in machine learning to assess the accuracy of a classification model. It is an N x N matrix, where N represents the number of target classes. + +For binary classification, it results in a 2 x 2 matrix that outlines four key parameters: +1. True Positive (TP) - The predicted value matches the actual value, or the predicted class matches the actual class. +For example - the actual value was positive, and the model predicted a positive value. +2. True Negative (TN) - The predicted value matches the actual value, or the predicted class matches the actual class. +For example - the actual value was negative, and the model predicted a negative value. +3. False Positive (FP)/Type I Error - The predicted value was falsely predicted. +For example - the actual value was negative, but the model predicted a positive value. +4. False Negative (FN)/Type II Error - The predicted value was falsely predicted. +For example - the actual value was positive, but the model predicted a negative value. + +The confusion matrix enables the calculation of various metrics like accuracy, precision, recall, F1-Score and specificity. +1. Accuracy - It represents the proportion of correctly classified instances out of the total number of instances in the dataset. +2. Precision - It quantifies the accuracy of positive predictions made by the model. +3. Recall - It quantifies the ability of a model to correctly identify all positive instances in the dataset and is also known as sensitivity or true positive rate. +4. F1-Score - It is a single measure that combines precision and recall, offering a balanced evaluation of a classification model's effectiveness. + +To implement the confusion matrix in Python, we can use the confusion_matrix() function from the sklearn.metrics module of the scikit-learn library. +The function returns a 2D array that represents the confusion matrix. +We can also visualize the confusion matrix using a heatmap. + +# Import necessary libraries +import numpy as np +from sklearn.metrics import confusion_matrix,classification_report +import seaborn as sns +import matplotlib.pyplot as plt + +# Create the NumPy array for actual and predicted labels +actual = np.array(['Apple', 'Apple', 'Apple', 'Not Apple', 'Apple', 'Not Apple', 'Apple', 'Apple', 'Not Apple', 'Not Apple']) +predicted = np.array(['Apple', 'Not Apple', 'Apple', 'Not Apple', 'Apple', 'Apple', 'Apple', 'Apple', 'Not Apple', 'Not Apple']) + +# Compute the confusion matrix +cm = confusion_matrix(actual,predicted) + +# Plot the confusion matrix with the help of the seaborn heatmap +sns.heatmap(cm, + annot=True, + fmt='g', + xticklabels=['Apple', 'Not Apple'], + yticklabels=['Apple', 'Not Apple']) +plt.xlabel('Prediction', fontsize=13) +plt.ylabel('Actual', fontsize=13) +plt.title('Confusion Matrix', fontsize=17) +plt.show() + +# Classifications Report based on Confusion Metrics +print(classification_report(actual, predicted)) + +# Results +1. Confusion Matrix: +[[5 1] +[1 3]] +2. Classification Report: + precision recall f1-score support +Apple 0.83 0.83 0.83 6 +Not Apple 0.75 0.75 0.75 4 + +accuracy 0.80 10 +macro avg 0.79 0.79 0.79 10 +weighted avg 0.80 0.80 0.80 10 From 1e543beeab7c458c3a22060e3c68c73b282fa874 Mon Sep 17 00:00:00 2001 From: Vrisha Shah <74671946+Vrisha213@users.noreply.github.com> Date: Fri, 17 May 2024 18:46:04 +0530 Subject: [PATCH 31/66] Update confusion-matrix.md --- contrib/machine-learning/confusion-matrix.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/machine-learning/confusion-matrix.md b/contrib/machine-learning/confusion-matrix.md index 959df9a..ac5cd31 100644 --- a/contrib/machine-learning/confusion-matrix.md +++ b/contrib/machine-learning/confusion-matrix.md @@ -1,6 +1,6 @@ Confusion Matrix - A confusion matrix is a fundamental performance evaluation tool used in machine learning to assess the accuracy of a classification model. It is an N x N matrix, where N represents the number of target classes. -For binary classification, it results in a 2 x 2 matrix that outlines four key parameters: +For binary classification, it results in a 2 x 2 matrix that outlines four key parameters: 1. True Positive (TP) - The predicted value matches the actual value, or the predicted class matches the actual class. For example - the actual value was positive, and the model predicted a positive value. 2. True Negative (TN) - The predicted value matches the actual value, or the predicted class matches the actual class. From 08c59bff565efbfff5fa8553e50ec5190046f37d Mon Sep 17 00:00:00 2001 From: Vrisha Shah <74671946+Vrisha213@users.noreply.github.com> Date: Fri, 17 May 2024 18:58:33 +0530 Subject: [PATCH 32/66] Update index.md Added link to new content on Confusion Matrix. This includes the title "Confusion Matrix" and the corresponding markdown file link "confusion-matrix.md". --- contrib/machine-learning/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/machine-learning/index.md b/contrib/machine-learning/index.md index 82596a2..2f27fd5 100644 --- a/contrib/machine-learning/index.md +++ b/contrib/machine-learning/index.md @@ -1,3 +1,3 @@ # List of sections -- [Section title](filename.md) +- [Confusion Matrix](confusion-matrix.md) From 37280da9d5fdb1ee4b9ac17f89b7c46229fc9cf4 Mon Sep 17 00:00:00 2001 From: Stuti_07 <110724092+StutiRajput07@users.noreply.github.com> Date: Fri, 17 May 2024 19:05:26 +0530 Subject: [PATCH 33/66] Create path-finder.md --- contrib/mini-projects/path-finder.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 contrib/mini-projects/path-finder.md diff --git a/contrib/mini-projects/path-finder.md b/contrib/mini-projects/path-finder.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/contrib/mini-projects/path-finder.md @@ -0,0 +1 @@ + From bb1e0e389b9261e8b04087973829f290ef05a44a Mon Sep 17 00:00:00 2001 From: Stuti_07 <110724092+StutiRajput07@users.noreply.github.com> Date: Fri, 17 May 2024 19:20:11 +0530 Subject: [PATCH 34/66] Update path-finder.md --- contrib/mini-projects/path-finder.md | 119 +++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/contrib/mini-projects/path-finder.md b/contrib/mini-projects/path-finder.md index 8b13789..98e9b87 100644 --- a/contrib/mini-projects/path-finder.md +++ b/contrib/mini-projects/path-finder.md @@ -1 +1,120 @@ +# Path Finder +This Python script uses the curses library to visualize the process of finding a path through a maze in real-time within a terminal window. The program represents the maze as a list of lists, where each list represents a row in the maze, and each string element in the lists represents a cell in the maze. The maze includes walls (#), a start point (O), and an end point (X), with empty spaces ( ) that can be traversed. +## The script includes the following main components: +- Visualization Functions:
+ print_maze(maze, stdscr, path=[]): This function is used to display the maze in the terminal. It utilizes color pairs to distinguish between the maze walls, the path, and unexplored spaces. The current path being explored is displayed with a different color to make it stand out. + +- Utility Functions:
+ find_start(maze, start): This function searches the maze for the starting point (marked as O) and returns its position as a tuple (row, col).
+ find_neighbors(maze, row, col): This function identifies the valid adjacent cells (up, down, left, right) that can be moved to from the current position, + ignoring any walls or out-of-bound positions. + +- Pathfinding Logic:
+ find_path(maze, stdscr): This function implements a Breadth-First Search (BFS) algorithm to find a path from the start point to the end point (X). It uses a + queue to explore each possible path sequentially. As it explores the maze, it updates the display in real-time, allowing the viewer to follow the progress + visually. Each visited position is marked and not revisited, ensuring the algorithm efficiently covers all possible paths without repetition. + +Overall, the script demonstrates an effective use of the curses library to create a dynamic visual representation of the BFS algorithm solving a maze, providing both an educational tool for understanding pathfinding and an example of real-time data visualization in a terminal. + +#### Below is the code of the path finder + + +```python +import curses +from curses import wrapper +import queue +import time + +# Define the structure of the maze as a list of lists where each inner list represents a row. +maze = [ + ["#", "O", "#", "#", "#", "#", "#", "#", "#"], + ["#", " ", " ", " ", " ", " ", " ", " ", "#"], + ["#", " ", "#", "#", " ", "#", "#", " ", "#"], + ["#", " ", "#", " ", " ", " ", "#", " ", "#"], + ["#", " ", "#", " ", "#", " ", "#", " ", "#"], + ["#", " ", "#", " ", "#", " ", "#", " ", "#"], + ["#", " ", "#", " ", "#", " ", "#", "#", "#"], + ["#", " ", " ", " ", " ", " ", " ", " ", "#"], + ["#", "#", "#", "#", "#", "#", "#", "X", "#"] +] + +# Function to print the current state of the maze in the terminal. +def print_maze(maze, stdscr, path=[]): + BLUE = curses.color_pair(1) # Color pair for walls and free paths + RED = curses.color_pair(2) # Color pair for the current path + + for i, row in enumerate(maze): + for j, value in enumerate(row): + if (i, j) in path: + stdscr.addstr(i, j*2, "X", RED) # Print path character with red color + else: + stdscr.addstr(i, j*2, value, BLUE) # Print walls and free paths with blue color + +# Function to locate the starting point (marked 'O') in the maze. +def find_start(maze, start): + for i, row in enumerate(maze): + for j, value in enumerate(row): + if value == start: + return i, j + return None + +# Function to find a path from start ('O') to end ('X') using BFS. +def find_path(maze, stdscr): + start = "O" + end = "X" + start_pos = find_start(maze, start) # Get the start position + + q = queue.Queue() + q.put((start_pos, [start_pos])) # Initialize the queue with the start position + + visited = set() # Set to keep track of visited positions + + while not q.empty(): + current_pos, path = q.get() # Get the current position and path + row, col = current_pos + + stdscr.clear() # Clear the screen + print_maze(maze, stdscr, path) # Print the current state of the maze + time.sleep(0.2) # Delay for visibility + stdscr.refresh() # Refresh the screen + + if maze[row][col] == end: # Check if the current position is the end + return path # Return the path if end is reached + + # Get neighbors (up, down, left, right) that are not walls + neighbors = find_neighbors(maze, row, col) + for neighbor in neighbors: + if neighbor not in visited: + r, c = neighbor + if maze[r][c] != "#": + new_path = path + [neighbor] + q.put((neighbor, new_path)) + visited.add(neighbor) + +# Function to find the valid neighboring cells (not walls or out of bounds). +def find_neighbors(maze, row, col): + neighbors = [] + if row > 0: # UP + neighbors.append((row - 1, col)) + if row + 1 < len(maze): # DOWN + neighbors.append((row + 1, col)) + if col > 0: # LEFT + neighbors.append((row, col - 1)) + if col + 1 < len(maze[0]): # RIGHT + neighbors.append((row, col + 1)) + return neighbors + +# Main function to setup curses and run the pathfinding algorithm. +def main(stdscr): + curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_BLACK) # Initialize color pair for blue + curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK) # Initialize color pair for red + + find_path(maze, stdscr) # Find the path using BFS + stdscr.getch() # Wait for a key press before exiting + +wrapper(main) # Use the wrapper to initialize and finalize curses automatically. + +``` + + From c339a064e55185b92f88dd6ec9c71d11b3755167 Mon Sep 17 00:00:00 2001 From: Stuti_07 <110724092+StutiRajput07@users.noreply.github.com> Date: Fri, 17 May 2024 19:21:04 +0530 Subject: [PATCH 35/66] Update index.md --- contrib/mini-projects/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/mini-projects/index.md b/contrib/mini-projects/index.md index 82596a2..d99a43e 100644 --- a/contrib/mini-projects/index.md +++ b/contrib/mini-projects/index.md @@ -1,3 +1,3 @@ # List of sections -- [Section title](filename.md) +- [Path Finder](path-finder.md) From 0ad50df483d3179134a5e3551a661f6473a3500b Mon Sep 17 00:00:00 2001 From: HimakarC <116370824+HimakarC@users.noreply.github.com> Date: Fri, 17 May 2024 19:28:54 +0530 Subject: [PATCH 36/66] Update index.md --- contrib/pandas/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/pandas/index.md b/contrib/pandas/index.md index aadd970..834d1f4 100644 --- a/contrib/pandas/index.md +++ b/contrib/pandas/index.md @@ -1,4 +1,4 @@ # List of sections - [Pandas Series Vs NumPy ndarray](pandas_series_vs_numpy_ndarray.md) -- [Excel Operations using Pandas DataFrame](excel_with_pandas.md) +- [Excel using Pandas DataFrame](excel_with_pandas.md) From a758ff832d439bb225dc7f292974204e8f500643 Mon Sep 17 00:00:00 2001 From: Yogesh Vishwakarma <103316955+Yogeshkarma@users.noreply.github.com> Date: Fri, 17 May 2024 19:43:23 +0530 Subject: [PATCH 37/66] Create Regression.md --- contrib/machine-learning/Regression.md | 171 +++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 contrib/machine-learning/Regression.md diff --git a/contrib/machine-learning/Regression.md b/contrib/machine-learning/Regression.md new file mode 100644 index 0000000..6ff6d28 --- /dev/null +++ b/contrib/machine-learning/Regression.md @@ -0,0 +1,171 @@ +# Regression + + +* Regression is a supervised machine learning technique which is used to predict continuous values. + + +> Now, Supervised learning is a category of machine learning that uses labeled datasets to train algorithms to predict outcomes and recognize patterns. + +* Regression is a statistical method used to model the relationship between a dependent variable (often denoted as 'y') and one or more independent variables (often denoted as 'x'). The goal of regression analysis is to understand how the dependent variable changes as the independent variables change. + # Types Of Regression + +1. Linear Regression +2. Polynomial Regression +3. Stepwise Regression +4. Decision Tree Regression +5. Random Forest Regression +6. Ridge Regression +7. Lasso Regression +8. ElasticNet Regression +9. Bayesian Linear Regression +10. Support Vector Regression + +But, we'll first start with Linear Regression +# Linear Regression + +* Linear regression is a fundamental statistical method used to model the relationship between a dependent variable (often denoted as +𝑌) and one or more independent variables (often denoted as +𝑋). The relationship is assumed to be linear, meaning that changes in the independent variables are associated with changes in the dependent variable in a straight-line fashion. + +The basic form of linear regression for a single independent variable is: + +**𝑌=𝛽0+𝛽1𝑋+𝜖** + +Where: + +* Y is the dependent variable. +* X is the independent variable. +* 𝛽0 is the intercept, representing the value of Y when X is zero +* 𝛽1 is the slope coefficient, representing the change in Y for a one-unit change in X +* ϵ is the error term, representing the variability in Y that is not explained by the linear relationship with X. + +# Basic Code of Linear Regression + +* This line imports the numpy library, which is widely used for numerical operations in Python. We use np as an alias for numpy, making it easier to reference functions and objects from the library. +``` +import numpy as np +``` + +* This line imports the LinearRegression class from the linear_model module of the scikit-learn library.scikit-learn is a powerful library for machine learning tasks in Python, and LinearRegression is a class provided by it for linear regression. +``` +from sklearn.linear_model import LinearRegression +``` +* This line creates a NumPy array X containing the independent variable values. In this example, we have a simple one-dimensional array representing the independent variable. The reshape(-1, 1) method reshapes the array into a column vector, necessary for use with scikit-learn + +``` +X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1) +``` +* This line creates a NumPy array Y containing the corresponding dependent variable values. These are the observed values of the dependent variable corresponding to the independent variable values in X. +``` +Y = np.array([2, 4, 5, 8, 5]) +``` + +* This line creates an instance of the LinearRegression class, which represents the linear regression model. We'll use this object to train the model and make predictions. +``` +model = LinearRegression() +``` + +* This line fits the linear regression model to the data. The fit() method takes two arguments: the independent variable (X) and the dependent variable (Y). This method estimates the coefficients of the linear regression equation that best fit the given data. +``` +model.fit(X, Y) +``` +* These lines print out the intercept (beta_0) and coefficient (beta_1) of the linear regression model. model.intercept_ gives the intercept value, and model.coef_ gives an array of coefficients, where model.coef_[0] corresponds to the coefficient of the first independent variable (in this case, there's only one). +``` +print("Intercept:", model.intercept_) +print("Coefficient:", model.coef_[0]) +``` + +* These lines demonstrate how to use the trained model to make predictions for new data. +* We create a new NumPy array new_data containing the values of the independent variable for which we want to predict the dependent variable values. +* We then use the predict() method of the model to obtain the predictions for these new data points. Finally, we print out the predicted values. +``` +new_data = np.array([[6], [7]]) +predictions = model.predict(new_data) +print("Predictions:", predictions) +``` +# Assumptions of Linear Regression + +# Linearity: + +* To assess the linearity assumption, we can visually inspect a scatter plot of the observed values versus the predicted values. +* If the relationship between them appears linear, it suggests that the linearity assumption is reasonable. +``` +import matplotlib.pyplot as plt +predictions = model.predict(X) +plt.scatter(predictions,Y) +plt.xlabel("Predicted Values") +plt.ylabel("Observed Values") +plt.title("Linearity Check: Observed vs Predicted") +plt.show() +``` +# Homoscedasticity: +* Homoscedasticity refers to the constant variance of the residuals across all levels of the independent variable(s). We can visually inspect a plot of residuals versus predicted values to check for homoscedasticity. +``` +residuals = Y - predictions +plt.scatter(predictions, residuals) +plt.xlabel("Predicted Values") +plt.ylabel("Residuals") +plt.title("Homoscedasticity Check: Residuals vs Predicted Values") +plt.axhline(y=0, color='red', linestyle='--') # Add horizontal line at y=0 +plt.show() + +``` +# Normality of Residuals: +* To assess the normality of residuals, we can visually inspect a histogram or a Q-Q plot of the residuals. +``` +import seaborn as sns + +sns.histplot(residuals, kde=True) +plt.xlabel("Residuals") +plt.ylabel("Frequency") +plt.title("Normality of Residuals: Histogram") +plt.show() + +import scipy.stats as stats + +stats.probplot(residuals, dist="norm", plot=plt) +plt.title("Normal Q-Q Plot") +plt.show() + +``` +# Metrics for Regression + + +# Mean Absolute Error (MAE) + +* MAE measures the average magnitude of the errors in a set of predictions, without considering their direction. It is the average of the absolute differences between predicted and actual values. +``` +from sklearn.metrics import mean_absolute_error + +mae = mean_absolute_error(Y, predictions) +print(f"Mean Absolute Error (MAE): {mae}") + +``` +# Mean Squared Error (MSE) + +* MSE measures the average of the squares of the errors. It gives more weight to larger errors, making it sensitive to outliers. +``` +from sklearn.metrics import mean_squared_error + +mse = mean_squared_error(Y, predictions) +print(f"Mean Squared Error (MSE): {mse}") +``` +# Root Mean Squared Error (RMSE) +* RMSE is the square root of the MSE. It provides an error metric that is in the same units as the dependent variable, making it more interpretable. +``` +rmse = np.sqrt(mse) +print(f"Root Mean Squared Error (RMSE): {rmse}") + +``` +# R-squared (Coefficient of Determination) +* R-squared measures the proportion of the variance in the dependent variable that is predictable from the independent variables. It ranges from 0 to 1, where 1 indicates a perfect fit. +``` +from sklearn.metrics import r2_score + +r2 = r2_score(Y, predictions) +print(f"R-squared (R^2): {r2}") +``` + +> In this tutorial, The sample dataset is there for learning purpose only + + From d51207d7c9966ee7122fc687661d6dc271742435 Mon Sep 17 00:00:00 2001 From: Yogesh Vishwakarma <103316955+Yogeshkarma@users.noreply.github.com> Date: Fri, 17 May 2024 19:44:58 +0530 Subject: [PATCH 38/66] Update index.md --- contrib/machine-learning/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/machine-learning/index.md b/contrib/machine-learning/index.md index 82596a2..094b211 100644 --- a/contrib/machine-learning/index.md +++ b/contrib/machine-learning/index.md @@ -1,3 +1,3 @@ # List of sections -- [Section title](filename.md) +- [Regression in Machine Learning](Regression.md) From c60a01491bfbb42a8a9d65b2f28a6e691a4b7e4e Mon Sep 17 00:00:00 2001 From: iABn0rma1 Date: Fri, 17 May 2024 23:38:40 +0530 Subject: [PATCH 39/66] updated content --- .../ArtificialNeuralNetwork.md | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/contrib/machine-learning/ArtificialNeuralNetwork.md b/contrib/machine-learning/ArtificialNeuralNetwork.md index 0fafc6c..c577c94 100644 --- a/contrib/machine-learning/ArtificialNeuralNetwork.md +++ b/contrib/machine-learning/ArtificialNeuralNetwork.md @@ -26,7 +26,6 @@ This guide will walk you through a fundamental neural network implementation in | `Neuron` cells forming the humand nervous system | `Perceptron` inspired from human brain | | :----------------------------------------------- | -------------------------------------: | -| | | | Neurons are nerve cells that send messages all over your body to allow you to do everything from breathing to talking, eating, walking, and thinking. | The perceptron is a mathematical model of a biological neuron. Performing heavy computations to think like humans. | | Neuron collects signals from dendrites. | The first layer is knownn as Input Layer, acting like dendritres to receive the input signal. | | Synapses are the connections between neurons where signals are transmitted. | Weights represent synapses. | @@ -44,28 +43,17 @@ Neurons in ANNs are organized into layers: * **Input Layer:** Receives the raw data. * **(n) Hidden Layers:** (Optional) Intermediate layers where complex transformations occur. They learn to detect patterns and features in the data. * **Output Layer:** Produces the final result (prediction or classification). -

- -

### Weights and Biases - For each input $(x_i)$, a weight $(w_i)$ is associated with it. Weights, multiplied with input units $(w_i \cdot x_i)$, determine the influence of one neuron's output on another. - A bias $(b_i)$ is added to help influence the end product, giving the equation as $(w_i \cdot x_i + b_i)$. - During training, the network adjusts these weights and biases to minimize errors and improve its predictions. -

- -

- ### Activation Functions - An activation function is applied to the result to introduce non-linearity in the model, allowing ANNs to learn more complex relationships from the data. - The resulting equation: $y = f(g(x))$, determines whether the neuron will "fire" or not, i.e., if its output will be used as input for the next neuron. - Common activation functions include the sigmoid function, tanh (hyperbolic tangent), and ReLU (Rectified Linear Unit). -

- -

- ### Forward and Backward Propagation - **Flow of Information:** All the above steps are part of Forward Propagation. It gives the output equation as $y = f\left(\sum_{i=1}^n w_i x_i + b_i\right)$ - **Error Correction:** Backpropagation is the algorithm used to train ANNs by calculating the gradient of error at the output layer and then propagating this error backward through the network. This allows the network to adjust its weights and biases in the direction that reduces the error. @@ -75,10 +63,6 @@ Neurons in ANNs are organized into layers: $ where $E$ is the error, $\hat{y}_j$ is the predicted output, $\theta_j$ is the input to the activation function of the $j^{th}$ neuron, and $w_{ij}$ is the weight from neuron $i$ to neuron $j$. -

- -

- ## Building From Scratch @@ -167,6 +151,3 @@ To understand how well our model is learning, let's visualize the training loss model = NeuralNetwork(input_size, hidden_layers, hidden_neurons, output_size) model.train(X, y, 100) ``` -

- -

From 6574b726fecffba739e6fc4d01776944b76333f7 Mon Sep 17 00:00:00 2001 From: Vrisha Shah <74671946+Vrisha213@users.noreply.github.com> Date: Sat, 18 May 2024 12:44:43 +0530 Subject: [PATCH 40/66] Update index.md --- contrib/machine-learning/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/machine-learning/index.md b/contrib/machine-learning/index.md index 2f27fd5..40a2237 100644 --- a/contrib/machine-learning/index.md +++ b/contrib/machine-learning/index.md @@ -1,3 +1,4 @@ # List of sections - [Confusion Matrix](confusion-matrix.md) +- [Support Vector Machine Algorithm](support-vector-machine.md) From 72909dc3abda33ad617b81007e9f1da69a15f5ba Mon Sep 17 00:00:00 2001 From: Yatharth Date: Sat, 18 May 2024 13:18:25 +0530 Subject: [PATCH 41/66] JSON MDODULE load loads dump dumps update and append --- contrib/advanced-python/index.md | 1 + contrib/advanced-python/json-module.md | 294 +++++++++++++++++++++++++ 2 files changed, 295 insertions(+) create mode 100644 contrib/advanced-python/json-module.md diff --git a/contrib/advanced-python/index.md b/contrib/advanced-python/index.md index 5ea5081..8f4f26a 100644 --- a/contrib/advanced-python/index.md +++ b/contrib/advanced-python/index.md @@ -1,3 +1,4 @@ # List of sections - [Decorators/\*args/**kwargs](decorator-kwargs-args.md) +- [JSON module](json-module.md) diff --git a/contrib/advanced-python/json-module.md b/contrib/advanced-python/json-module.md new file mode 100644 index 0000000..20a9408 --- /dev/null +++ b/contrib/advanced-python/json-module.md @@ -0,0 +1,294 @@ +# JSON Module + +## What is JSON? + +- JSON (JavaScript Object Notation) is a format for structuring data. +- JSON is a lightweight, text-based data interchange format that is completely language-independent. +- Similar to XML, JSON is a format for structuring data commonly used by web applications to communicate with each other. + +## Why JSON? + +- Whenever we declare a variable and assign a value to it, the variable itself doesn't hold the value. Instead, the variable holds an address in memory where the value is stored. For example: + +```python +age = 21 +``` + +- When we use `age`, it gets replaced with `21`. However, age doesn't contain 21, it contains the address of the memory location where 21 is stored. + +- While this works locally, transferring this data, such as through an API, poses a challenge. Sending your computer’s entire memory with the addresses is impractical and insecure. This is where JSON comes to the rescue. + +### Example JSON + +- JSON supports most widely used data types including String + , Number, Boolean, Null, Array and Object. +- Here is an example of JSON file + +```json +{ + "name": "John Doe", + "age": 21, + "isStudent": true, + "address": null, + "courses": ["Math", "Science", "History"], + "grades": { + "Math": 95, + "Science": 89, + "History": 76 + } +} +``` + +# Python JSON + +Python too supports JSON with a built-in package called `json`. This package provides all the necessary tools for working with JSON Objects including `parsing, serializing, deserializing, and many more`. + +## 1. Python parse JSON string. + +- To parse JSON string Python firstly we import the JSON module. +- JSON string is converted to a Python object using `json.loads()` method of JSON module in Python. +- Example Code: + +```python +# Python program to convert JSON to Python +import json + +# JSON string +students ='{"id":"01", "name": "Yatharth", "department":"Computer Science Engineering"}' + +# Convert string to Python dict +students_dict = json.loads(students) +print(students_dict) + +print(students_dict['name']) + +``` + +- Ouput: + +```json +{'id': '01', 'name': 'Yatharth', 'department': 'Computer Science Engineering'} +Yatharth +``` + +## 2. Python load JSON file. + +- JSON data can also be directly fetch from a json file +- Example: + +```python +import json +# Opening JSON file +f = open('input.json',) + +# Returns JSON object as a dictionary +data = json.load(f) + +# Iterating through the json file +for i in data['students']: + print(i) + +# Closing file +f.close() +``` + +- JSON file + +```json +{ + "students":{ + { + "id": "01", + "name": "Yatharth", + "department": "Computer Science Engineering" + }, + { + "id": "02", + "name": "Raj", + "department": "Mechanical Engineering" + } + } +} +``` + +- Ouput + +```json +{'id': '01', 'name': 'Yatharth', 'department': 'Computer Science Engineering'} +{'id': '02', 'name': 'Raj', 'department': 'Mechanical Engineering'} +``` +- `json.load()`: Reads JSON data from a file object and deserializes it into a Python object. +- `json.loads()`: Deserializes JSON data from a string into a Python object. +
+ +### Addtiotnal Context +Relation between python data types and json data types is given in table below. +
+ +| Python Object | JSON Object | +|-----------------|-------------| +| Dict | object | +| list, tuple | array | +| str | string | +| int, long, float | numbers | +| True | true | +| False | false | +| None | null | + + + +## 3. Python Dictionary to JSON String +- Parsing python dictionary to json string using `json.dumps()`. +- Example Code: +```python +import json + +# Data to be written +dictionary ={ + "id": "03", + "name": "Suraj", + "department": "Civil Engineering" +} + +# Serializing json +json_object = json.dumps(dictionary, indent = 4) +print(json_object) +``` +- Output: +``` json +{ + "department": "Civil Engineering", + "id": "02", + "name": "Suraj" +} +``` +## 4. Python Dictionary to JSON file. +- - Parsing python dictionary to json string using `json.dump()`. +- Example Code: +``` python +import json + +# Data to be written +dictionary ={ + "name" : "Satyendra", + "rollno" : 51, + "cgpa" : 8.8, + "phonenumber" : "123456789" +} + +with open("sample.json", "w") as outfile: + json.dump(dictionary, outfile) + +``` +- Ouput: `sample.json` +``` json +{ + "name" : "Satyendra", + "rollno" : 51, + "cgpa" : 8.8, + "phonenumber" : "123456789" +} + +``` +## 5. Append Python Dictionary to JSON String. +- Append to an already existing string using `json.update()`. +- Example : +```python +import json +# JSON data: +x = { + "id": "03", + "name": "Suraj" +} + +# python object to be appended +y = { "department": "Civil Engineering"} + +# parsing JSON string: +z = json.loads(x) + +# appending the data +z.update(y) + +# the result is a JSON string: +print(json.dumps(z)) + +``` +- Ouput: +```json +{"id": "03", "name": "Suraj", "department": "Civil Engineering"} +``` + + +## 6. Append Python Dictionary to JSON File. +- There is no direct function to append in file. So, we will load file in a dictionary, update dictionary then update content and convert back to json file format. +- `data.json` +``` json +{ + "students":{ + { + "id": "01", + "name": "Yatharth", + "department": "Computer Science Engineering" + }, + { + "id": "02", + "name": "Raj", + "department": "Mechanical Engineering" + } + } +} +``` +- Example Code: +``` python +import json + +# function to add to JSON +def write_json(new_data, filename='data.json'): + with open(filename,'r+') as file: + # First we load existing data into a dict. + file_data = json.load(file) + # Join new_data with file_data inside students + file_data["students"].append(new_data) + # Sets file's current position at offset. + file.seek(0) + # convert back to json. + json.dump(file_data, file, indent = 4) + +# python object to be appended +y = { + "id": "03", + "name": "Suraj", + "department": "Civil Engineering" +} + +write_json(y) + +``` +- Output: +```json +{ + "students":{ + { + "id": "01", + "name": "Yatharth", + "department": "Computer Science Engineering" + }, + { + "id": "02", + "name": "Raj", + "department": "Mechanical Engineering" + }, + { + "id": "03", + "name": "Suraj", + "department": "Civil Engineering" + } + } +} +``` + +
+
+ +The Python json module simplifies the handling of JSON data, offering a bridge between Python data structures and JSON representations, vital for data exchange and storage in modern applications. \ No newline at end of file From a478e70c4bea7637ba6795ab1cf81c7d00d5ca2a Mon Sep 17 00:00:00 2001 From: Anushka Mokashi Date: Sat, 18 May 2024 14:10:53 +0530 Subject: [PATCH 42/66] Descriptive Statistics with Python and Pandas --- contrib/pandas/Descriptive_Statistics.md | 599 +++++++++++++++++++++++ contrib/pandas/index.md | 1 + 2 files changed, 600 insertions(+) create mode 100644 contrib/pandas/Descriptive_Statistics.md diff --git a/contrib/pandas/Descriptive_Statistics.md b/contrib/pandas/Descriptive_Statistics.md new file mode 100644 index 0000000..7775965 --- /dev/null +++ b/contrib/pandas/Descriptive_Statistics.md @@ -0,0 +1,599 @@ +## Descriptive Statistics + +In the realm of data science, understanding the characteristics of data is fundamental. Descriptive statistics provide the tools and techniques to succinctly summarize and present the key features of a dataset. It serves as the cornerstone for exploring, visualizing, and ultimately gaining insights from data. + +Descriptive statistics encompasses a range of methods designed to describe the central tendency, dispersion, and shape of a dataset. Through measures such as mean, median, mode, standard deviation, and variance, descriptive statistics offer a comprehensive snapshot of the data's distribution and variability. + +Data scientists utilize descriptive statistics to uncover patterns, identify outliers, and assess the overall structure of data before delving into more advanced analyses. By summarizing large and complex datasets into manageable and interpretable summaries, descriptive statistics facilitate informed decision-making and actionable insights. + + +```python +import pandas as pd +import numpy as np +``` + + +```python +df = pd.read_csv("Age-Income-Dataset.csv") +df +``` +| | Age | Income | +| --- | ----------- | ------ | +| 0 | Young | 25000 | +| 1 | Middle Age | 54000 | +| 2 | Old | 60000 | +| 3 | Young | 15000 | +| 4 | Young | 45000 | +| 5 | Young | 65000 | +| 6 | Young | 70000 | +| 7 | Young | 30000 | +| 8 | Middle Age | 27000 | +| 9 | Young | 23000 | +| 10 | Young | 48000 | +| 11 | Old | 52000 | +| 12 | Young | 33000 | +| 13 | Old | 80000 | +| 14 | Old | 75000 | +| 15 | Old | 35000 | +| 16 | Middle Age | 29000 | +| 17 | Middle Age | 57000 | +| 18 | Old | 43000 | +| 19 | Middle Age | 56000 | +| 20 | Old | 63000 | +| 21 | Old | 32000 | +| 22 | Old | 45000 | +| 23 | Old | 89000 | +| 24 | Middle Age | 90000 | +| 25 | Middle Age | 93000 | +| 26 | Young | 80000 | +| 27 | Young | 87000 | +| 28 | Young | 38000 | +| 29 | Young | 23000 | +| 30 | Middle Age | 38900 | +| 31 | Middle Age | 53200 | +| 32 | Old | 43800 | +| 33 | Middle Age | 25600 | +| 34 | Middle Age | 65400 | +| 35 | Old | 76800 | +| 36 | Old | 89700 | +| 37 | Old | 41800 | +| 38 | Young | 31900 | +| 39 | Old | 25600 | +| 40 | Middle Age | 45700 | +| 41 | Old | 35600 | +| 42 | Young | 54300 | +| 43 | Middle Age | 65400 | +| 44 | Old | 67800 | +| 45 | Old | 24500 | +| 46 | Middle Age | 34900 | +| 47 | Old | 45300 | +| 48 | Young | 68400 | +| 49 | Middle Age | 51700 | + + + +```python +df.describe() +``` + + + +| | Income | +|-------|-------------| +| count | 50.000000 | +| mean | 50966.000000 | +| std | 21096.683268 | +| min | 15000.000000 | +| 25% | 33475.000000 | +| 50% | 46850.000000 | +| 75% | 65400.000000 | +| max | 93000.000000 | + + +### Mean + +The mean, also known as the average, is a measure of central tendency in a dataset. It represents the typical value of a set of numbers. The formula to calculate the mean of a dataset is: + +$$ \overline{x} = \frac{\sum\limits_{i=1}^{n} x_i}{n} $$ + +* $\overline{x}$ (pronounced "x bar") represents the mean value. +* $x_i$ represents the individual value in the dataset (where i goes from 1 to n). +* $\sum$ (sigma) represents the summation symbol, indicating we add up all the values from i=1 to n. +* $n$ represents the total number of values in the dataset. + + + +```python +df['Income'].mean() +``` + + + + +50966.0 + + + + +```python +def mean_f(df): + for col in df.columns: + if df[col].dtype != 'O': + temp = 0 + for i in df[col]: + temp = temp +i + print("Without pandas Library -> ") + print("Average of {} is {}".format(col,(temp/len(df[col])))) + print() + print("With pandas Library -> ") + print(df[col].mean()) + +mean_f(df) +``` + +Without pandas Library -> +Average of Income is 50966.0 + +With pandas Library -> +50966.0 + + +### Median + + +The median is another measure of central tendency in a dataset. Unlike the mean, which is the average value of all data points, the median represents the middle value when the dataset is ordered from smallest to largest. If the dataset has an odd number of observations, the median is the middle value. If the dataset has an even number of observations, the median is the average of the two middle values. + +The median represents the "middle" value in a dataset. There are two cases to consider depending on whether the number of observations (n) is odd or even: + +**Odd number of observations (n):** + +In this case, the median (M) is the value located at the middle position when the data is ordered from least to greatest. We can calculate the position using the following formula: + +$$ M = x_{n+1/2} $$ + +**Even number of observations (n):** + +When we have an even number of observations, there isn't a single "middle" value. Instead, the median is the average of the two middle values after ordering the data. Here's the formula to find the median: + +$$ M = \frac{x_{n/2} + x_{(n/2)+1}}{2} $$ + +**Explanation:** + +* M represents the median value. +* n represents the total number of observations in the dataset. +* $x$ represents the individual value. + + + + + +```python +df['Income'].median() +``` + + + + +46850.0 + + + + +```python +def median_f(df): + for col in df.columns: + if df[col].dtype != 'O': + sorted_data = sorted(df[col]) + n = len(df[col]) + if n%2 == 0: + x1 =sorted_data[int((n/2))] + x2 =sorted_data[int((n/2))+1] + median=(x1+x2)/2 + else: + median = sorted_data[(n+1)/2] + print("Median without library ->") + print("Median of {} is {} ".format(col,median)) + print("Median with library ->") + print(df[col].median()) +median_f(df) +``` + +Median without library -> +Median of Income is 49850.0 \ +Median with library -> +46850.0 + + +### Mode + + +The mode is a measure of central tendency that represents the value or values that occur most frequently in a dataset. Unlike the mean and median, which focus on the average or middle value, the mode identifies the most common value(s) in the dataset. + + +```python +def mode_f(df): + for col in df.columns: + if df[col].dtype == 'O': + print("Column:", col) + arr = df[col].sort_values() + + prevcnt = 0 + cnt = 0 + ans = arr[0] + temp = arr[0] + + for i in arr: + if(temp == i) : + cnt += 1 + else: + prevcnt = cnt + cnt = 1 + temp = i + if(cnt > prevcnt): + ans = i + + print("Without pandas Library -> ") + print("Mode of {} is {}".format(col,ans)) + print() + print("With pandas Library -> ") + print(df[col].mode()) +mode_f(df) +``` + +Column: Age +Without pandas Library -> \ +Mode of Age is Old + +With pandas Library -> \ +0 Old \ +Name: Age, dtype: object + + + + + +### Standard Deviation + +Standard deviation is a measure of the dispersion or spread of a dataset. It quantifies the amount of variation or dispersion of a set of values from the mean. In other words, it indicates how much individual values in a dataset deviate from the mean. + +$$s = \sqrt{\frac{\sum(x_i-\overline{x})^{2}}{n-1}}$$ + +* $s$ represents the standard deviation. +* $\sum$ (sigma) represents the summation symbol, indicating we add up the values for all data points. +* $x_i$ represents the individual value in the dataset. +* $\overline{x}$ (x bar) represents the mean value of the dataset. +* $n$ represents the total number of values in the dataset. + + +```python +df['Income'].std() +``` + + + + +21096.683267707253 + + + + +```python +import math +def std_f(df): + for col in df.columns: + if len(df[col]) == 0: + print("Column is empty") + if df[col].dtype != 'O': + sum = 0 + mean = df[col].mean() + for i in df[col]: + sum = sum + (i - mean)**2 + + std = math.sqrt(sum/len(df[col])) + print("Without pandas library ->") + print("Std : " , std) + print("With pandas library: ->") + print("Std : {}".format(np.std(df[col]))) ##ddof = 1 + +std_f(df) +``` + +Without pandas library -> +Std : 20884.6509187968 \ +With pandas library: -> +Std : 20884.6509187968 + + + +```python +df['Income'].count() +``` + + + + +50 + + + +### Minimum + + +```python +df['Income'].min() +``` + + + + +15000 + + + + +```python +def min_f(df): + for col in df.columns: + if df[col].dtype != "O": + sorted_data = sorted(df[col]) + min = sorted_data[0] + print("Without pandas Library->",min) + print("With pandas Library->",df[col].min()) + +min_f(df) +``` + +Without pandas Library-> 15000 \ +With pandas Library-> 15000 + + +### Maximum + + +```python +df['Income'].max() +``` + + + + +93000 + + + + +```python +def max_f(df): + for col in df.columns: + if df[col].dtype != "O": + sorted_data = sorted(df[col]) + max = sorted_data[len(df[col])-1] + print("Without pandas Library->",max) + print("With pandas Library->",df[col].max()) + +max_f(df) +``` + + Without pandas Library-> 93000 + With pandas Library-> 93000 + + +### Percentile + + +```python +df['Income'].quantile(0.25) +``` + + + + +33475.0 + + + + +```python +df['Income'].quantile(0.75) +``` + + + + +65400.0 + + + + +```python +def percentile_f(df,percentile): + for col in df.columns: + if df[col].dtype != 'O': + sorted_data = sorted(df[col]) + index = int(percentile*len(df[col])) + percentile_result = sorted_data[index] + print(f"{percentile} Percentile is : ",percentile_result) + +percentile_f(df,0.25) +``` + +0.25 Percentile is : 33000 + + +We have used the method of nearest rank to calculate percentile manually. + +Pandas uses linear interpolation of data to calculate percentiles. + +## Correlation and Covariance + + +```python +df = pd.read_csv('Iris.csv') +df.head(5) +``` + + + + +| | Id | SepalLengthCm | SepalWidthCm | PetalLengthCm | PetalWidthCm | Species | +|---|----|---------------|--------------|---------------|--------------|-------------| +| 0 | 1 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa | +| 1 | 2 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa | +| 2 | 3 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa | +| 3 | 4 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa | +| 4 | 5 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa | + + + + +```python +df.drop(['Id','Species'],axis=1,inplace= True) +``` + +### Covarience + +Covariance measures the degree to which two variables change together. If the covariance between two variables is positive, it means that they tend to increase or decrease together. If the covariance is negative, it means that as one variable increases, the other tends to decrease. However, covariance does not provide a standardized measure, making it difficult to interpret the strength of the relationship between variables, especially if the variables are measured in different units. + +$$ COV(X,Y) = \frac{\sum\limits_{i=1}^{n} (X_i - \overline{X}) (Y_i - \overline{Y})}{n - 1}$$ + +**Explanation:** + +* $COV(X, Y)$ represents the covariance between variables X and Y. +* $X_i$ and $Y_i$ represent the individual values for variables X and Y in the i-th observation. +* $\overline{X}$ and $\overline{Y}$ represent the mean values for variables X and Y, respectively. +* $n$ represents the total number of observations in the dataset. + + + +```python +df.cov() +``` + + +| | SepalLengthCm | SepalWidthCm | PetalLengthCm | PetalWidthCm | +|-------------------|-------------- |---------------|-----------------|--------------| +| **SepalLengthCm** | 0.685694 | -0.039268 | 1.273682 | 0.516904 | +| **SepalWidthCm** | -0.039268 | 0.188004 | -0.321713 | -0.117981 | +| **PetalLengthCm** | 1.273682 | -0.321713 | 3.113179 | 1.296387 | +| **PetalWidthCm** | 0.516904 | -0.117981 | 1.296387 | 0.582414 | + + + +```python +def cov_f(df): + for x in df.columns: + for y in df.columns: + mean_x = df[x].mean() + mean_y = df[y].mean() + + sum = 0 + n = len(df[x]) + + for val in range(n): + sum += (df[x].iloc[val] - mean_x)*(df[y].iloc[val] - mean_y) + print("Covariance of {} and {} is : {}".format(x,y, sum/(n-1))) + print() +cov_f(df) +``` + +Covariance of SepalLengthCm and SepalLengthCm is : 0.6856935123042504 \ +Covariance of SepalLengthCm and SepalWidthCm is : -0.03926845637583892 \ +Covariance of SepalLengthCm and PetalLengthCm is : 1.2736823266219246 \ +Covariance of SepalLengthCm and PetalWidthCm is : 0.5169038031319911 + +Covariance of SepalWidthCm and SepalLengthCm is : -0.03926845637583892 \ +Covariance of SepalWidthCm and SepalWidthCm is : 0.1880040268456377 \ +Covariance of SepalWidthCm and PetalLengthCm is : -0.32171275167785235 \ +Covariance of SepalWidthCm and PetalWidthCm is : -0.11798120805369115 + +Covariance of PetalLengthCm and SepalLengthCm is : 1.2736823266219246 \ +Covariance of PetalLengthCm and SepalWidthCm is : -0.32171275167785235 \ +Covariance of PetalLengthCm and PetalLengthCm is : 3.113179418344519 \ +Covariance of PetalLengthCm and PetalWidthCm is : 1.2963874720357946 + +Covariance of PetalWidthCm and SepalLengthCm is : 0.5169038031319911 \ +Covariance of PetalWidthCm and SepalWidthCm is : -0.11798120805369115 \ +Covariance of PetalWidthCm and PetalLengthCm is : 1.2963874720357946 \ +Covariance of PetalWidthCm and PetalWidthCm is : 0.5824143176733781 + + + +### Correlation + +Correlation, on the other hand, standardizes the measure of relationship between two variables, making it easier to interpret. It measures both the strength and direction of the linear relationship between two variables. Correlation values range between -1 and 1, where: + +$$r = \frac{n(\sum xy) - (\sum x)(\sum y)}{\sqrt{n(\sum x^2) - (\sum x)^2} \cdot \sqrt{n(\sum y^2) - (\sum y)^2}}$$ + +* r represents the correlation coefficient. +* n is the number of data points. + + + + +```python +df.corr() +``` + +| | SepalLengthCm | SepalWidthCm | PetalLengthCm | PetalWidthCm | +|-------------------|---------------|--------------|---------------|--------------| +| **SepalLengthCm** | 1.000000 | -0.109369 | 0.871754 | 0.817954 | +| **SepalWidthCm** | -0.109369 | 1.000000 | -0.420516 | -0.356544 | +| **PetalLengthCm** | 0.871754 | -0.420516 | 1.000000 | 0.962757 | +| **PetalWidthCm** | 0.817954 | -0.356544 | 0.962757 | 1.000000 | + + +```python +import math +def corr_f(df): + for i in df.columns: + for j in df.columns: + n = len(df[i]) + + sumX = 0 + for x in df[i]: + sumX += x + sumY = 0 + for y in df[j]: + sumY += y + + sumXY = 0 + for xy in range(n): + sumXY += (df[i].iloc[xy] * df[j].iloc[xy]) + + sumX2 = 0 + for x in df[i]: + sumX2 += (x**2) + sumY2 = 0 + for y in df[j]: + sumY2 += (y**2) + + NR = (n * sumXY) - (sumX*sumY) + DR = math.sqrt( ( (n * sumX2) - (sumX**2))*( (n * sumY2) - (sumY ** 2) ) ) + + print("Correlation of {} and {} :{}".format(i,j,NR/DR)) + print() + +corr_f(df) +``` + +Correlation of SepalLengthCm and SepalLengthCm :1.0 \ +Correlation of SepalLengthCm and SepalWidthCm :-0.10936924995067286 \ +Correlation of SepalLengthCm and PetalLengthCm :0.8717541573048861 \ +Correlation of SepalLengthCm and PetalWidthCm :0.8179536333691775 + +Correlation of SepalWidthCm and SepalLengthCm :-0.10936924995067286 \ +Correlation of SepalWidthCm and SepalWidthCm :1.0 \ +Correlation of SepalWidthCm and PetalLengthCm :-0.42051609640118826 \ +Correlation of SepalWidthCm and PetalWidthCm :-0.3565440896138223 + +Correlation of PetalLengthCm and SepalLengthCm :0.8717541573048861 \ +Correlation of PetalLengthCm and SepalWidthCm :-0.42051609640118826 \ +Correlation of PetalLengthCm and PetalLengthCm :1.0 \ +Correlation of PetalLengthCm and PetalWidthCm :0.9627570970509656 + +Correlation of PetalWidthCm and SepalLengthCm :0.8179536333691775 \ +Correlation of PetalWidthCm and SepalWidthCm :-0.3565440896138223 \ +Correlation of PetalWidthCm and PetalLengthCm :0.9627570970509656 \ +Correlation of PetalWidthCm and PetalWidthCm :1.0 + + diff --git a/contrib/pandas/index.md b/contrib/pandas/index.md index 5c0f2b4..ae90788 100644 --- a/contrib/pandas/index.md +++ b/contrib/pandas/index.md @@ -1,3 +1,4 @@ # List of sections - [Pandas Series Vs NumPy ndarray](pandas_series_vs_numpy_ndarray.md) +- [Pandas Descriptive Statistics](Descriptive_Statistics.md) From f322f740d2ed2fbb669733b07e6dfc939acb53a9 Mon Sep 17 00:00:00 2001 From: AmrutaJayanti <142327526+AmrutaJayanti@users.noreply.github.com> Date: Sat, 18 May 2024 18:24:03 +0530 Subject: [PATCH 43/66] Update index.md --- contrib/advanced-python/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/advanced-python/index.md b/contrib/advanced-python/index.md index 5ea5081..6fe4a9f 100644 --- a/contrib/advanced-python/index.md +++ b/contrib/advanced-python/index.md @@ -1,3 +1,4 @@ # List of sections - [Decorators/\*args/**kwargs](decorator-kwargs-args.md) +- [OOPs](OOPs.md) From 95c272e87edbdbce3593360d36bd1deae6e54432 Mon Sep 17 00:00:00 2001 From: AmrutaJayanti <142327526+AmrutaJayanti@users.noreply.github.com> Date: Sat, 18 May 2024 18:24:30 +0530 Subject: [PATCH 44/66] Added OOPs --- contrib/advanced-python/OOPs.md | 470 ++++++++++++++++++++++++++++++++ 1 file changed, 470 insertions(+) create mode 100644 contrib/advanced-python/OOPs.md diff --git a/contrib/advanced-python/OOPs.md b/contrib/advanced-python/OOPs.md new file mode 100644 index 0000000..7e355f2 --- /dev/null +++ b/contrib/advanced-python/OOPs.md @@ -0,0 +1,470 @@ +--- +jupyter: + colab: + kernelspec: + display_name: Python 3 + name: python3 + language_info: + name: python + nbformat: 4 + nbformat_minor: 0 +--- + +::: {.cell .markdown id="WpcHLEIgeEdz"} +**PYTHON OOP CONCEPTS** +::: + +::: {.cell .markdown id="3j1W5YpNeEmS"} +In Python object-oriented Programming (OOPs) is a programming paradigm +that uses objects and classes in programming. It aims to implement +real-world entities like inheritance, polymorphisms, encapsulation, etc. +in the programming. The main concept of object-oriented Programming +(OOPs) or oops concepts in Python is to bind the data and the functions +that work together as a single unit so that no other part of the code +can access this data. + +**OOPs Concepts in Python** + +1. Class in Python + +2. Objects in Python + +3. Polymorphism in Python + +4. Encapsulation in Python + +5. Inheritance in Python + +6. Data Abstraction in Python + ::: + +::: {.cell .markdown id="3KQXMPlVeEyT"} +Python Class A class is a collection of objects. A class contains the +blueprints or the prototype from which the objects are being created. It +is a logical entity that contains some attributes and methods. +::: + +::: {.cell .code execution_count="2" id="6CdZhHSMerae"} + +```python +#Simple Class in Python +class Dog: + pass +``` + +::: + +::: {.cell .markdown id="y5t7jyHAfAHl"} +**Python Objects** In object oriented programming Python, The object is +an entity that has a state and behavior associated with it. It may be +any real-world object like a mouse, keyboard, chair, table, pen, etc. +Integers, strings, floating-point numbers, even arrays, and +dictionaries, are all objects. +::: + +::: {.cell .code execution_count="3" id="u8Y0G-hse-lm"} + +```python +obj = Dog() +``` + +::: + +::: {.cell .markdown id="\_FtRAPlNfLkb"} +**The Python **init** Method ** + +The **init** method is similar to constructors in C++ and Java. It is +run as soon as an object of a class is instantiated. The method is +useful to do any initialization you want to do with your object. +::: + +::: {.cell .code execution_count="4" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="aMmXUxLHfSz6" outputId="ed23bfbd-445a-4d02-ebba-c210471e0af2"} + +```python +class Dog: + + # class attribute + attr1 = "mammal" + + # Instance attribute + def __init__(self, name): + self.name = name + +# Object instantiation +Rodger = Dog("Rodger") +Tommy = Dog("Tommy") + +# Accessing class attributes +print("Rodger is a {}".format(Rodger.__class__.attr1)) +print("Tommy is also a {}".format(Tommy.__class__.attr1)) + +# Accessing instance attributes +print("My name is {}".format(Rodger.name)) +print("My name is {}".format(Tommy.name)) +``` + +::: {.output .stream .stdout} +Rodger is a mammal +Tommy is also a mammal +My name is Rodger +My name is Tommy +::: +::: + +::: {.cell .markdown id="YFMnh417fZI6"} +**Inheritance** + +In Python object oriented Programming, Inheritance is the capability of +one class to derive or inherit the properties from another class. The +class that derives properties is called the derived class or child class +and the class from which the properties are being derived is called the +base class or parent class. + +Types of Inheritances: + +- Single Inheritance + +- Multilevel Inheritance + +- Multiple Inheritance + +- Hierarchial Inheritance + ::: + +::: {.cell .code execution_count="6" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="PeQoseXogE2f" outputId="9b3a069e-c6f0-41b0-b5f3-704aa0f95160"} + +```python +#Single Inheritance +# Parent class +class Animal: + def __init__(self, name, sound): + self.name = name + self.sound = sound + + def make_sound(self): + print(f"{self.name} says {self.sound}") + +# Child class inheriting from Animal +class Dog(Animal): + def __init__(self, name): + # Call the constructor of the parent class + super().__init__(name, "Woof") + +# Child class inheriting from Animal +class Cat(Animal): + def __init__(self, name): + # Call the constructor of the parent class + super().__init__(name, "Meow") + +# Creating objects of the derived classes +dog = Dog("Buddy") +cat = Cat("Whiskers") + +# Accessing methods of the parent class +dog.make_sound() +cat.make_sound() +``` + +::: {.output .stream .stdout} +Buddy says Woof +Whiskers says Meow +::: +::: + +::: {.cell .code execution_count="7" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="CKEMq39BgSu9" outputId="b252ac43-6116-4a88-9ac9-0475f54e63c0"} + +```python +#Multilevel Inheritance +# Parent class +class Animal: + def __init__(self, name): + self.name = name + + def speak(self): + print(f"{self.name} speaks") + +# Child class inheriting from Animal +class Dog(Animal): + def bark(self): + print(f"{self.name} barks") + +# Grandchild class inheriting from Dog +class GermanShepherd(Dog): + def guard(self): + print(f"{self.name} guards") + +# Creating objects of the derived classes +german_shepherd = GermanShepherd("Rocky") + +# Accessing methods from all levels of inheritance +german_shepherd.speak() # Accessing method from the Animal class +german_shepherd.bark() # Accessing method from the Dog class +german_shepherd.guard() # Accessing method from the GermanShepherd class +``` + +::: {.output .stream .stdout} +Rocky speaks +Rocky barks +Rocky guards +::: +::: + +::: {.cell .code execution_count="8" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="wZ5qxZXBgdQ6" outputId="cd1c9d75-790c-49b3-8040-3334b896d779"} + +```python +#Hierarchial Inheritance +# Parent class +class Animal: + def __init__(self, name): + self.name = name + + def speak(self): + print(f"{self.name} speaks") + +# Child class 1 inheriting from Animal +class Dog(Animal): + def bark(self): + print(f"{self.name} barks") + +# Child class 2 inheriting from Animal +class Cat(Animal): + def meow(self): + print(f"{self.name} meows") + +# Creating objects of the derived classes +dog = Dog("Buddy") +cat = Cat("Whiskers") + +# Accessing methods from the parent and child classes +dog.speak() # Accessing method from the Animal class +dog.bark() # Accessing method from the Dog class +cat.speak() # Accessing method from the Animal class +cat.meow() # Accessing method from the Cat class +``` + +::: {.output .stream .stdout} +Buddy speaks +Buddy barks +Whiskers speaks +Whiskers meows +::: +::: + +::: {.cell .code execution_count="9" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="8nPMri12glO5" outputId="a3c93e8c-f10f-4cd7-c402-3500342c9e28"} + +```python +#Multiple Inheritance +# Parent class 1 +class Herbivore: + def eat_plants(self): + print("Eating plants") + +# Parent class 2 +class Carnivore: + def eat_meat(self): + print("Eating meat") + +# Child class inheriting from both Herbivore and Carnivore +class Omnivore(Herbivore, Carnivore): + def eat(self): + print("Eating everything") + +# Creating an object of the Omnivore class +omnivore = Omnivore() + +# Accessing methods from both parent classes +omnivore.eat_plants() # Accessing method from Herbivore +omnivore.eat_meat() # Accessing method from Carnivore +omnivore.eat() # Accessing method from Omnivore +``` + +::: {.output .stream .stdout} +Eating plants +Eating meat +Eating everything +::: +::: + +::: {.cell .markdown id="KSw-WMePgvCa"} +**Polymorphism** In object oriented Programming Python, Polymorphism +simply means having many forms +::: + +::: {.cell .code execution_count="10" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="cIYtM2_Pg8Ja" outputId="e887848d-060a-4317-a805-cfb0ca4e187f"} + +```python +class Bird: + + def intro(self): + print("There are many types of birds.") + + def flight(self): + print("Most of the birds can fly but some cannot.") + +class sparrow(Bird): + + def flight(self): + print("Sparrows can fly.") + +class ostrich(Bird): + + def flight(self): + print("Ostriches cannot fly.") + +obj_bird = Bird() +obj_spr = sparrow() +obj_ost = ostrich() + +obj_bird.intro() +obj_bird.flight() + +obj_spr.intro() +obj_spr.flight() + +obj_ost.intro() +obj_ost.flight() +``` + +::: {.output .stream .stdout} +There are many types of birds. +Most of the birds can fly but some cannot. +There are many types of birds. +Sparrows can fly. +There are many types of birds. +Ostriches cannot fly. +::: +::: + +::: {.cell .markdown id="NzsPIifmg-FI"} +**Python Encapsulation** + +In Python object oriented programming, Encapsulation is one of the +fundamental concepts in object-oriented programming (OOP). It describes +the idea of wrapping data and the methods that work on data within one +unit. This puts restrictions on accessing variables and methods directly +and can prevent the accidental modification of data. To prevent +accidental change, an object's variable can only be changed by an +object's method. Those types of variables are known as private +variables. +::: + +::: {.cell .code execution_count="11" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="etbhALMHhGb9" outputId="75f27d20-7cee-4c85-b722-b9beb5ffe2b8"} + +```python +class Car: + def __init__(self, make, model, year): + self._make = make # Encapsulated attribute with single underscore + self._model = model # Encapsulated attribute with single underscore + self._year = year # Encapsulated attribute with single underscore + self._odometer_reading = 0 # Encapsulated attribute with single underscore + + def get_make(self): + return self._make + + def get_model(self): + return self._model + + def get_year(self): + return self._year + + def get_odometer_reading(self): + return self._odometer_reading + + def update_odometer(self, mileage): + if mileage >= self._odometer_reading: + self._odometer_reading = mileage + else: + print("You can't roll back an odometer!") + + def increment_odometer(self, miles): + self._odometer_reading += miles + +# Creating an instance of the Car class +my_car = Car("Toyota", "Camry", 2021) + +# Accessing encapsulated attributes through methods +print("Make:", my_car.get_make()) +print("Model:", my_car.get_model()) +print("Year:", my_car.get_year()) + +# Modifying encapsulated attribute through method +my_car.update_odometer(100) +print("Odometer Reading:", my_car.get_odometer_reading()) + +# Incrementing odometer reading +my_car.increment_odometer(50) +print("Odometer Reading after increment:", my_car.get_odometer_reading()) +``` + +::: {.output .stream .stdout} +Make: Toyota +Model: Camry +Year: 2021 +Odometer Reading: 100 +Odometer Reading after increment: 150 +::: +::: + +::: {.cell .markdown id="hJkQ9Tn5hUEV"} +**Data Abstraction** It hides unnecessary code details from the user. +Also, when we do not want to give out sensitive parts of our code +implementation and this is where data abstraction came. +::: + +::: {.cell .code execution_count="12" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="FoMRMWEEhc-Z" outputId="3a77fd0d-8116-4997-c35f-dfebfc786b72"} + +```python +from abc import ABC, abstractmethod + +# Abstract class defining the interface for a Shape +class Shape(ABC): + def __init__(self, name): + self.name = name + + @abstractmethod + def area(self): + pass + + @abstractmethod + def perimeter(self): + pass + +# Concrete class implementing the Shape interface for a Rectangle +class Rectangle(Shape): + def __init__(self, name, length, width): + super().__init__(name) + self.length = length + self.width = width + + def area(self): + return self.length * self.width + + def perimeter(self): + return 2 * (self.length + self.width) + +# Concrete class implementing the Shape interface for a Circle +class Circle(Shape): + def __init__(self, name, radius): + super().__init__(name) + self.radius = radius + + def area(self): + return 3.14 * self.radius * self.radius + + def perimeter(self): + return 2 * 3.14 * self.radius + +# Creating objects of the derived classes +rectangle = Rectangle("Rectangle", 5, 4) +circle = Circle("Circle", 3) + +# Accessing methods defined by the Shape interface +print(f"{rectangle.name}: Area = {rectangle.area()}, Perimeter = {rectangle.perimeter()}") +print(f"{circle.name}: Area = {circle.area()}, Perimeter = {circle.perimeter()}") +``` + +::: {.output .stream .stdout} +Rectangle: Area = 20, Perimeter = 18 +Circle: Area = 28.259999999999998, Perimeter = 18.84 +::: +::: From dd8db2ea7cca92d35f0398c82b8f021e3ed6ae2c Mon Sep 17 00:00:00 2001 From: AmrutaJayanti <142327526+AmrutaJayanti@users.noreply.github.com> Date: Sat, 18 May 2024 18:27:49 +0530 Subject: [PATCH 45/66] Update OOPs.md --- contrib/advanced-python/OOPs.md | 107 +++----------------------------- 1 file changed, 7 insertions(+), 100 deletions(-) diff --git a/contrib/advanced-python/OOPs.md b/contrib/advanced-python/OOPs.md index 7e355f2..d6d1256 100644 --- a/contrib/advanced-python/OOPs.md +++ b/contrib/advanced-python/OOPs.md @@ -1,20 +1,4 @@ ---- -jupyter: - colab: - kernelspec: - display_name: Python 3 - name: python3 - language_info: - name: python - nbformat: 4 - nbformat_minor: 0 ---- -::: {.cell .markdown id="WpcHLEIgeEdz"} -**PYTHON OOP CONCEPTS** -::: - -::: {.cell .markdown id="3j1W5YpNeEmS"} In Python object-oriented Programming (OOPs) is a programming paradigm that uses objects and classes in programming. It aims to implement real-world entities like inheritance, polymorphisms, encapsulation, etc. @@ -36,15 +20,15 @@ can access this data. 5. Inheritance in Python 6. Data Abstraction in Python - ::: -::: {.cell .markdown id="3KQXMPlVeEyT"} + + Python Class A class is a collection of objects. A class contains the blueprints or the prototype from which the objects are being created. It is a logical entity that contains some attributes and methods. -::: -::: {.cell .code execution_count="2" id="6CdZhHSMerae"} + + ```python #Simple Class in Python @@ -54,31 +38,25 @@ class Dog: ::: -::: {.cell .markdown id="y5t7jyHAfAHl"} + **Python Objects** In object oriented programming Python, The object is an entity that has a state and behavior associated with it. It may be any real-world object like a mouse, keyboard, chair, table, pen, etc. Integers, strings, floating-point numbers, even arrays, and dictionaries, are all objects. -::: -::: {.cell .code execution_count="3" id="u8Y0G-hse-lm"} ```python obj = Dog() ``` -::: -::: {.cell .markdown id="\_FtRAPlNfLkb"} **The Python **init** Method ** The **init** method is similar to constructors in C++ and Java. It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object. -::: -::: {.cell .code execution_count="4" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="aMmXUxLHfSz6" outputId="ed23bfbd-445a-4d02-ebba-c210471e0af2"} ```python class Dog: @@ -103,15 +81,7 @@ print("My name is {}".format(Rodger.name)) print("My name is {}".format(Tommy.name)) ``` -::: {.output .stream .stdout} -Rodger is a mammal -Tommy is also a mammal -My name is Rodger -My name is Tommy -::: -::: -::: {.cell .markdown id="YFMnh417fZI6"} **Inheritance** In Python object oriented Programming, Inheritance is the capability of @@ -129,10 +99,7 @@ Types of Inheritances: - Multiple Inheritance - Hierarchial Inheritance - ::: - -::: {.cell .code execution_count="6" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="PeQoseXogE2f" outputId="9b3a069e-c6f0-41b0-b5f3-704aa0f95160"} - + ```python #Single Inheritance # Parent class @@ -165,13 +132,8 @@ dog.make_sound() cat.make_sound() ``` -::: {.output .stream .stdout} -Buddy says Woof -Whiskers says Meow -::: -::: -::: {.cell .code execution_count="7" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="CKEMq39BgSu9" outputId="b252ac43-6116-4a88-9ac9-0475f54e63c0"} + ```python #Multilevel Inheritance @@ -202,14 +164,7 @@ german_shepherd.bark() # Accessing method from the Dog class german_shepherd.guard() # Accessing method from the GermanShepherd class ``` -::: {.output .stream .stdout} -Rocky speaks -Rocky barks -Rocky guards -::: -::: -::: {.cell .code execution_count="8" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="wZ5qxZXBgdQ6" outputId="cd1c9d75-790c-49b3-8040-3334b896d779"} ```python #Hierarchial Inheritance @@ -242,15 +197,6 @@ cat.speak() # Accessing method from the Animal class cat.meow() # Accessing method from the Cat class ``` -::: {.output .stream .stdout} -Buddy speaks -Buddy barks -Whiskers speaks -Whiskers meows -::: -::: - -::: {.cell .code execution_count="9" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="8nPMri12glO5" outputId="a3c93e8c-f10f-4cd7-c402-3500342c9e28"} ```python #Multiple Inheritance @@ -278,19 +224,9 @@ omnivore.eat_meat() # Accessing method from Carnivore omnivore.eat() # Accessing method from Omnivore ``` -::: {.output .stream .stdout} -Eating plants -Eating meat -Eating everything -::: -::: -::: {.cell .markdown id="KSw-WMePgvCa"} **Polymorphism** In object oriented Programming Python, Polymorphism simply means having many forms -::: - -::: {.cell .code execution_count="10" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="cIYtM2_Pg8Ja" outputId="e887848d-060a-4317-a805-cfb0ca4e187f"} ```python class Bird: @@ -325,17 +261,7 @@ obj_ost.intro() obj_ost.flight() ``` -::: {.output .stream .stdout} -There are many types of birds. -Most of the birds can fly but some cannot. -There are many types of birds. -Sparrows can fly. -There are many types of birds. -Ostriches cannot fly. -::: -::: -::: {.cell .markdown id="NzsPIifmg-FI"} **Python Encapsulation** In Python object oriented programming, Encapsulation is one of the @@ -346,9 +272,7 @@ and can prevent the accidental modification of data. To prevent accidental change, an object's variable can only be changed by an object's method. Those types of variables are known as private variables. -::: -::: {.cell .code execution_count="11" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="etbhALMHhGb9" outputId="75f27d20-7cee-4c85-b722-b9beb5ffe2b8"} ```python class Car: @@ -396,22 +320,10 @@ my_car.increment_odometer(50) print("Odometer Reading after increment:", my_car.get_odometer_reading()) ``` -::: {.output .stream .stdout} -Make: Toyota -Model: Camry -Year: 2021 -Odometer Reading: 100 -Odometer Reading after increment: 150 -::: -::: -::: {.cell .markdown id="hJkQ9Tn5hUEV"} **Data Abstraction** It hides unnecessary code details from the user. Also, when we do not want to give out sensitive parts of our code implementation and this is where data abstraction came. -::: - -::: {.cell .code execution_count="12" colab="{\"base_uri\":\"https://localhost:8080/\"}" id="FoMRMWEEhc-Z" outputId="3a77fd0d-8116-4997-c35f-dfebfc786b72"} ```python from abc import ABC, abstractmethod @@ -463,8 +375,3 @@ print(f"{rectangle.name}: Area = {rectangle.area()}, Perimeter = {rectangle.peri print(f"{circle.name}: Area = {circle.area()}, Perimeter = {circle.perimeter()}") ``` -::: {.output .stream .stdout} -Rectangle: Area = 20, Perimeter = 18 -Circle: Area = 28.259999999999998, Perimeter = 18.84 -::: -::: From c56bbb3d4733f0c6770a4bb5755ea03043778182 Mon Sep 17 00:00:00 2001 From: Aditi Bansal <142652964+Aditi22Bansal@users.noreply.github.com> Date: Sat, 18 May 2024 18:51:39 +0530 Subject: [PATCH 46/66] Update index.md --- contrib/mini-projects/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/mini-projects/index.md b/contrib/mini-projects/index.md index 82596a2..0c76d72 100644 --- a/contrib/mini-projects/index.md +++ b/contrib/mini-projects/index.md @@ -1,3 +1,4 @@ # List of sections - [Section title](filename.md) +- [Hangman Game Based on Movies](hangman_game.md) From 7375e91a255113141a9a6e729ceaa56e1898d5dd Mon Sep 17 00:00:00 2001 From: Yatharth Date: Sat, 18 May 2024 18:56:11 +0530 Subject: [PATCH 47/66] removed html tags, removed weird color at line 70 and 117 --- contrib/advanced-python/json-module.md | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/contrib/advanced-python/json-module.md b/contrib/advanced-python/json-module.md index 20a9408..47e1561 100644 --- a/contrib/advanced-python/json-module.md +++ b/contrib/advanced-python/json-module.md @@ -2,7 +2,7 @@ ## What is JSON? -- JSON (JavaScript Object Notation) is a format for structuring data. +- [JSON]("https://www.json.org/json-en.html") (JavaScript Object Notation) is a format for structuring data. - JSON is a lightweight, text-based data interchange format that is completely language-independent. - Similar to XML, JSON is a format for structuring data commonly used by web applications to communicate with each other. @@ -14,7 +14,7 @@ age = 21 ``` -- When we use `age`, it gets replaced with `21`. However, age doesn't contain 21, it contains the address of the memory location where 21 is stored. +- When we use `age`, it gets replaced with `21`. However, *age doesn't contain 21, it contains the address of the memory location where 21 is stored*. - While this works locally, transferring this data, such as through an API, poses a challenge. Sending your computer’s entire memory with the addresses is impractical and insecure. This is where JSON comes to the rescue. @@ -67,7 +67,7 @@ print(students_dict['name']) - Ouput: ```json -{'id': '01', 'name': 'Yatharth', 'department': 'Computer Science Engineering'} +{"id": "01", "name": "Yatharth", "department": "Computer Science Engineering"} Yatharth ``` @@ -114,16 +114,15 @@ f.close() - Ouput ```json -{'id': '01', 'name': 'Yatharth', 'department': 'Computer Science Engineering'} -{'id': '02', 'name': 'Raj', 'department': 'Mechanical Engineering'} +{"id": "01", "name": "Yatharth", "department": "Computer Science Engineering"} +{"id": "02", "name": "Raj", "department": "Mechanical Engineering"} ``` - `json.load()`: Reads JSON data from a file object and deserializes it into a Python object. - `json.loads()`: Deserializes JSON data from a string into a Python object. -
-### Addtiotnal Context + +## Addtiotnal Context Relation between python data types and json data types is given in table below. -
| Python Object | JSON Object | |-----------------|-------------| @@ -288,7 +287,4 @@ write_json(y) } ``` -
-
- The Python json module simplifies the handling of JSON data, offering a bridge between Python data structures and JSON representations, vital for data exchange and storage in modern applications. \ No newline at end of file From bc5469772704fa51bae90feb259a2adbfcaa0563 Mon Sep 17 00:00:00 2001 From: Yatharth Date: Sat, 18 May 2024 18:57:26 +0530 Subject: [PATCH 48/66] line error 71 removed --- contrib/advanced-python/json-module.md | 1 - 1 file changed, 1 deletion(-) diff --git a/contrib/advanced-python/json-module.md b/contrib/advanced-python/json-module.md index 47e1561..eed0393 100644 --- a/contrib/advanced-python/json-module.md +++ b/contrib/advanced-python/json-module.md @@ -68,7 +68,6 @@ print(students_dict['name']) ```json {"id": "01", "name": "Yatharth", "department": "Computer Science Engineering"} -Yatharth ``` ## 2. Python load JSON file. From 6063f595bdfb82655580a13d0260d547b920cd1c Mon Sep 17 00:00:00 2001 From: Manmita Das <34617961+manmita@users.noreply.github.com> Date: Sat, 18 May 2024 19:03:18 +0530 Subject: [PATCH 49/66] Create recursion.md --- contrib/ds-algorithms/recursion.md | 107 +++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 contrib/ds-algorithms/recursion.md diff --git a/contrib/ds-algorithms/recursion.md b/contrib/ds-algorithms/recursion.md new file mode 100644 index 0000000..7ab3136 --- /dev/null +++ b/contrib/ds-algorithms/recursion.md @@ -0,0 +1,107 @@ +# Introduction to Recursions + +When a function calls itself to solve smaller instances of the same problem until a specified condition is fulfilled is called recursion. It is used for tasks that can be divided into smaller sub-tasks. + +# How Recursion Works + +To solve a problem using recursion we must define: +- Base condition :- The condition under which recursion ends. +- Recursive case :- The part of function which calls itself to solve a smaller instance of problem. + +Steps of Recursion + +When a recursive function is called, the following sequence of events occurs: +- Function Call: The function is invoked with a specific argument. +- Base Condition Check: The function checks if the argument satisfies the base case. +- Recursive Call: If the base case is not met, the function performs some operations and makes a recursive call with a modified argument. +- Stack Management: Each recursive call is placed on the call stack. The stack keeps track of each function call, its argument, and the point to return to once the call completes. +- Unwinding the Stack: When the base case is eventually met, the function returns a value, and the stack starts unwinding, returning values to previous function calls until the initial call is resolved. + +# What is Stack Overflow in Recursion + +Stack overflow is an error that occurs when the call stack memory limit is exceeded. During execution of recursion calls they are simultaneously stored in a recursion stack waiting for the recursive function to be completed. Without a base case, the function would call itself indefinitely, leading to a stack overflow. + +# Example + +- Factorial of a Number + + The factorial of i natural numbers is nth integer multiplied by factorial of (i-1) numbers. The base case is if i=0 we return 1 as factorial of 0 is 1. + +```python +def factorial(i): + #base case + if i==0 : + return 1 + #recursive case + else : + return i * factorial(i-1) +i = 6 +print("Factorial of i is :", factorial(i)) # Output- Factorial of i is :720 +``` +# What is Backtracking + +Backtracking is a recursive algorithmic technique used to solve problems by exploring all possible solutions and discarding those that do not meet the problem's constraints. It is particularly useful for problems involving combinations, permutations, and finding paths in a grid. + +# How Backtracking Works + +- Incremental Solution Building: Solutions are built one step at a time. +- Feasibility Check: At each step, a check is made to see if the current partial solution is valid. +- Backtracking: If a partial solution is found to be invalid, the algorithm backtracks by removing the last added part of the solution and trying the next possibility. +- Exploration of All Possibilities: The process continues recursively, exploring all possible paths, until a solution is found or all possibilities are exhausted. + +# Example + +- Word Search + + Given a 2D grid of characters and a word, determine if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where "adjacent" cells are horizontally or vertically neighboring. The same letter cell may not be used more than once. + +Algorithm for Solving the Word Search Problem with Backtracking: +- Start at each cell: Attempt to find the word starting from each cell. +- Check all Directions: From each cell, try all four possible directions (up, down, left, right). +- Mark Visited Cells: Use a temporary marker to indicate cells that are part of the current path to avoid revisiting. +- Backtrack: If a path does not lead to a solution, backtrack by unmarking the visited cell and trying the next possibility. + +```python +def exist(board, word): + rows, cols = len(board), len(board[0]) + + def backtrack(r, c, suffix): + if not suffix: + return True + + if r < 0 or r >= rows or c < 0 or c >= cols or board[r][c] != suffix[0]: + return False + + # Mark the cell as visited by replacing its character with a placeholder + ret = False + board[r][c], temp = '#', board[r][c] + + # Explore the four possible directions + for row_offset, col_offset in [(0, 1), (1, 0), (0, -1), (-1, 0)]: + ret = backtrack(r + row_offset, c + col_offset, suffix[1:]) + if ret: + break + + # Restore the cell's original value + board[r][c] = temp + return ret + + for row in range(rows): + for col in range(cols): + if backtrack(row, col, word): + return True + + return False + +# Test case +board = [ + ['A','B','C','E'], + ['S','F','C','S'], + ['A','D','E','E'] +] +word = "ABCES" +print(exist(board, word)) # Output: True +``` + + + From 2c56f3ef278be8220a1a6a10499f21fc7e4be192 Mon Sep 17 00:00:00 2001 From: Manmita Das <34617961+manmita@users.noreply.github.com> Date: Sat, 18 May 2024 19:05:51 +0530 Subject: [PATCH 50/66] Update index.md --- contrib/ds-algorithms/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/ds-algorithms/index.md b/contrib/ds-algorithms/index.md index 5b52155..ab16ebe 100644 --- a/contrib/ds-algorithms/index.md +++ b/contrib/ds-algorithms/index.md @@ -2,3 +2,4 @@ - [Section title](filename.md) - [Sorting Algorithms](sorting-algorithms.md) +- [Recursion and Backtracking](recursion.md) From acf7acdd8f347a8d762ee0fc912de5ea85d34447 Mon Sep 17 00:00:00 2001 From: Aditi Bansal <142652964+Aditi22Bansal@users.noreply.github.com> Date: Sat, 18 May 2024 19:10:19 +0530 Subject: [PATCH 51/66] Create hangman_game.md This Hangman game script is a simple Python program designed to let players guess movie titles. It starts by importing the random module to select a movie from a predefined list. The game displays the movie title as underscores and reveals correctly guessed letters. Players have six attempts to guess the entire title, entering one letter at a time. The script checks if the input is valid, updates the list of guessed letters, and adjusts the number of attempts based on the correctness of the guess. The game continues until the player either guesses the title correctly or runs out of attempts. Upon completion, it congratulates the player for a correct guess or reveals the movie title if the attempts are exhausted. The main execution block ensures the game runs only when the script is executed directly. --- contrib/mini-projects/hangman_game.md | 220 ++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 contrib/mini-projects/hangman_game.md diff --git a/contrib/mini-projects/hangman_game.md b/contrib/mini-projects/hangman_game.md new file mode 100644 index 0000000..d62db91 --- /dev/null +++ b/contrib/mini-projects/hangman_game.md @@ -0,0 +1,220 @@ +# Hangman - Movies Edition +The Hangman game script is a simple Python program designed to let players guess movie titles. It starts by importing the random module to select a movie from a predefined list. The game displays the movie title as underscores and reveals correctly guessed letters. Players have six attempts to guess the entire title, entering one letter at a time. The script checks if the input is valid, updates the list of guessed letters, and adjusts the number of attempts based on the correctness of the guess. The game continues until the player either guesses the title correctly or runs out of attempts. Upon completion, it congratulates the player for a correct guess or reveals the movie title if the attempts are exhausted. The main execution block ensures the game runs only when the script is executed directly.Below is first the code and then an explanation of the code and its components. + +## Code + +``` +import random + +def choose_movie(): + movies = ['avatar', 'titanic', 'inception', 'jurassicpark', 'thegodfather', 'forrestgump', 'interstellar', 'pulpfiction', 'shawshank'] + return random.choice(movies) + +def display_word(movie, guessed_letters): + display = "" + for letter in movie: + if letter in guessed_letters: + display += letter + " " + else: + display += "_ " + return display + +def hangman_movies(): + movie = choose_movie() + guessed_letters = [] + attempts = 6 + + print("Welcome to Hangman - Movies Edition!") + print("Try to guess the name of the movie. You have 6 attempts.") + + while attempts > 0: + print("\n" + display_word(movie, guessed_letters)) + guess = input("Guess a letter: ").lower() + + if len(guess) != 1 or not guess.isalpha(): + print("Please enter a single letter.") + continue + + if guess in guessed_letters: + print("You've already guessed that letter.") + continue + + guessed_letters.append(guess) + + if guess not in movie: + attempts -= 1 + print(f"Sorry, '{guess}' is not in the movie name. You have {attempts} attempts left.") + else: + print(f"Good guess! '{guess}' is in the movie name.") + + if "_" not in display_word(movie, guessed_letters): + print(f"\nCongratulations! You guessed the movie '{movie.capitalize()}' correctly!") + break + + if attempts == 0: + print(f"\nSorry, you ran out of attempts. The movie was '{movie.capitalize()}'.") + +if __name__ == "__main__": + hangman_movies() +``` + +## Code Explanation + +### Importing the Random Module + +```python + +import random + +``` + +The `random` module is imported to use the `choice` function, which will help in selecting a random movie from a predefined list. + +### Choosing a Movie + +```python + +def choose_movie(): + +movies = ['avatar', 'titanic', 'inception', 'jurassicpark', 'thegodfather', 'forrestgump', 'interstellar', 'pulpfiction', 'shawshank'] + +return random.choice(movies) + +``` + +The `choose_movie` function returns a random movie title from the `movies` list. + +### Displaying the Word + +```python + +def display_word(movie, guessed_letters): + +display = "" + +for letter in movie: + +if letter in guessed_letters: + +display += letter + " " + +else: + +display += "_ " + +return display + +``` + +The `display_word` function takes the movie title and a list of guessed letters as arguments. It constructs a string where correctly guessed letters are shown in their positions, and unknown letters are represented by underscores (`_`). + +### Hangman Game Logic + +```python + +def hangman_movies(): + +movie = choose_movie() + +guessed_letters = [] + +attempts = 6 + +print("Welcome to Hangman - Movies Edition!") + +print("Try to guess the name of the movie. You have 6 attempts.") + +while attempts > 0: + +print("\n" + display_word(movie, guessed_letters)) + +guess = input("Guess a letter: ").lower() + +if len(guess) != 1 or not guess.isalpha(): + +print("Please enter a single letter.") + +continue + +if guess in guessed_letters: + +print("You've already guessed that letter.") + +continue + +guessed_letters.append(guess) + +if guess not in movie: + +attempts -= 1 + +print(f"Sorry, '{guess}' is not in the movie name. You have {attempts} attempts left.") + +else: + +print(f"Good guess! '{guess}' is in the movie name.") + +if "_" not in display_word(movie, guessed_letters): + +print(f"\nCongratulations! You guessed the movie '{movie.capitalize()}' correctly!") + +break + +if attempts == 0: + +print(f"\nSorry, you ran out of attempts. The movie was '{movie.capitalize()}'.") + +``` + +The `hangman_movies` function manages the game's flow: + +1. It selects a random movie title using `choose_movie`. + +2. Initializes an empty list `guessed_letters` and sets the number of attempts to 6. + +3. Prints a welcome message and the initial game state. + +4. Enters a loop that continues until the player runs out of attempts or guesses the movie title. + +5. Displays the current state of the movie title with guessed letters revealed. + +6. Prompts the player to guess a letter. + +7. Validates the player's input: + +- Ensures it is a single alphabetic character. + +- Checks if the letter has already been guessed. + +8. Adds the guessed letter to `guessed_letters`. + +9. Updates the number of attempts if the guessed letter is not in the movie title. + +10. Congratulates the player if they guess the movie correctly. + +11. Informs the player of the correct movie title if they run out of attempts. + +### Main Execution Block + +```python + +if __name__ == "__main__": + +hangman_movies() + +``` +## Conclusion +This block ensures that the game runs only when the script is executed directly, not when it is imported as a module. + +## Output Screenshots: + +![image](https://github.com/Aditi22Bansal/learn-python/assets/142652964/a7af1f7e-c80e-4f83-b1f7-c7c5c72158b4) +![image](https://github.com/Aditi22Bansal/learn-python/assets/142652964/082e54dc-ce68-48fd-85da-3252d7629df8) + + + +## Conclusion + +This script provides a simple yet entertaining Hangman game focused on guessing movie titles. It demonstrates the use of functions, loops, conditionals, and user input handling in Python. + + From 391a0e591b6866c981e1e746473a2ad475b1315e Mon Sep 17 00:00:00 2001 From: Anshika Yadav <14anshika7yadav@gmail.com> Date: Sat, 18 May 2024 19:54:24 +0530 Subject: [PATCH 52/66] Closes #294 - Mini Project : Password Strength Checker --- contrib/mini-projects/index.md | 1 + .../password_strength_checker.md | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 contrib/mini-projects/password_strength_checker.md diff --git a/contrib/mini-projects/index.md b/contrib/mini-projects/index.md index 82596a2..71908f8 100644 --- a/contrib/mini-projects/index.md +++ b/contrib/mini-projects/index.md @@ -1,3 +1,4 @@ # List of sections - [Section title](filename.md) +- [Password strength checker](password_strength_checker.md) \ No newline at end of file diff --git a/contrib/mini-projects/password_strength_checker.md b/contrib/mini-projects/password_strength_checker.md new file mode 100644 index 0000000..ca65b8e --- /dev/null +++ b/contrib/mini-projects/password_strength_checker.md @@ -0,0 +1,100 @@ +# about password strength + +> This code is a simple password strength checker. +It evaluates the strength of a user's password based on the presence of +uppercase letters, lowercase letters, digits, spaces, and special characters. + +### About the code: + +- The codebase is break down in two file `password_strength_checker.py` and `main.py`. + +`password_strength_checker.py` The function evaluates password strength based on character types (uppercase, lowercase, digits, spaces, special characters) and provides feedback on its security. +and `main.py` contains basic code. + +``` +import string + + +class password_checker: + def __init__(self, password): + self.password = password + + def check_password_strength(self): + """This function prompts the user to enter a password and then evaluates its strength.""" + + password_strength = 0 + upper_count = 0 + lower_count = 0 + num_count = 0 + space_count = 0 + specialcharacter_count = 0 + review = "" + + for char in list(password): + if char in string.ascii_uppercase: + upper_count += 1 + elif char in string.ascii_lowercase: + lower_count += 1 + elif char in string.digits: + num_count += 1 + elif char == " ": + space_count += 1 + else: + specialcharacter_count += 1 + + if upper_count >= 1: + password_strength += 1 + if lower_count >= 1: + password_strength += 1 + if num_count >= 1: + password_strength += 1 + if space_count >= 1: + password_strength += 1 + if specialcharacter_count >= 1: + password_strength += 1 + + if password_strength == 1: + review = "That's a very easy password, Not good for use" + elif password_strength == 2: + review = ( + "That's a weak password, You should change it to some strong password." + ) + elif password_strength == 3: + review = "Your password is just okay, you may change it." + elif password_strength == 4: + review = "Your password is hard to guess." + elif password_strength == 5: + review = "Its the strong password, No one can guess this password " + + about_password = { + "uppercase_letters ": upper_count, + "lowercase_letters": lower_count, + "space_count": space_count, + "specialcharacter_count": specialcharacter_count, + "password_strength": password_strength, + "about_password_strength": review, + } + print(about_password) + + def check_password(): + """This function prompts the user to decide if they want to check their password strength.""" + + choice = input("Do you want to check your password's strength? (Y/N): ") + if choice.upper() == "Y": + return True + elif choice.upper() == "N": + return False + else: + print("Invalid input. Please enter 'Y' for Yes or 'N' for No.") + return password_checker.check_password() + +``` +### Here's the implementation of 'main.py' +``` +import password_checker from password_strength_checker + +while password_checker.check_password(): + password = input("Enter your password: ") + p = password_checker(password) + p.check_password_strength() +``` \ No newline at end of file From 07d7643c6a0283db1b03c69e518a006a9d646f99 Mon Sep 17 00:00:00 2001 From: AmrutaJayanti <142327526+AmrutaJayanti@users.noreply.github.com> Date: Sat, 18 May 2024 20:01:02 +0530 Subject: [PATCH 53/66] Update OOPs.md --- contrib/advanced-python/OOPs.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/contrib/advanced-python/OOPs.md b/contrib/advanced-python/OOPs.md index d6d1256..0dbd855 100644 --- a/contrib/advanced-python/OOPs.md +++ b/contrib/advanced-python/OOPs.md @@ -36,7 +36,7 @@ class Dog: pass ``` -::: + **Python Objects** In object oriented programming Python, The object is @@ -49,9 +49,9 @@ dictionaries, are all objects. ```python obj = Dog() ``` +This creates an instance for class Dog - -**The Python **init** Method ** +**The Python **init** Method** The **init** method is similar to constructors in C++ and Java. It is run as soon as an object of a class is instantiated. The method is @@ -80,7 +80,7 @@ print("Tommy is also a {}".format(Tommy.__class__.attr1)) print("My name is {}".format(Rodger.name)) print("My name is {}".format(Tommy.name)) ``` - +In the above mentioned code, init method is used to initialize the name. **Inheritance** @@ -131,7 +131,7 @@ cat = Cat("Whiskers") dog.make_sound() cat.make_sound() ``` - +The above code depicts the Single Inheritance, in case of single inheritance there's only a single base class and a derived class. Here, Dog and Cat are the derived classes with Animal as the parent class. They can access the methods of the base class or derive their own methods. @@ -163,7 +163,7 @@ german_shepherd.speak() # Accessing method from the Animal class german_shepherd.bark() # Accessing method from the Dog class german_shepherd.guard() # Accessing method from the GermanShepherd class ``` - +Multilevel inheritance is a concept in object-oriented programming where a class inherits properties and behaviors from another class, which itself may inherit from another class. In other words, it involves a chain of inheritance where a subclass inherits from a superclass, and that subclass can then become a superclass for another subclass.Its similar to GrandFather ,Father and Son .In the above code,Animal class is the superclass, Dog is derived from Animal and Dog is the parent of GermanShepherd. GermenShepherd is the child class of Dog. GermenShepherd can access methods of both Animal and Dog. ```python @@ -196,7 +196,7 @@ dog.bark() # Accessing method from the Dog class cat.speak() # Accessing method from the Animal class cat.meow() # Accessing method from the Cat class ``` - +Hierarchical inheritance is a type of inheritance in object-oriented programming where one class serves as a superclass for multiple subclasses. In this inheritance model, each subclass inherits properties and behaviors from the same superclass, creating a hierarchical tree-like structure. ```python #Multiple Inheritance @@ -223,7 +223,7 @@ omnivore.eat_plants() # Accessing method from Herbivore omnivore.eat_meat() # Accessing method from Carnivore omnivore.eat() # Accessing method from Omnivore ``` - +Multiple inheritance is a concept in object-oriented programming where a class can inherit properties and behaviors from more than one parent class. This means that a subclass can have multiple immediate parent classes, allowing it to inherit features from each of them. **Polymorphism** In object oriented Programming Python, Polymorphism simply means having many forms @@ -260,7 +260,7 @@ obj_spr.flight() obj_ost.intro() obj_ost.flight() ``` - +Poly stands for 'many' and morphism for 'forms'. In the above code, method flight() has many forms. **Python Encapsulation** @@ -374,4 +374,6 @@ circle = Circle("Circle", 3) print(f"{rectangle.name}: Area = {rectangle.area()}, Perimeter = {rectangle.perimeter()}") print(f"{circle.name}: Area = {circle.area()}, Perimeter = {circle.perimeter()}") ``` - +To implement Data Abstraction , we have to import abc . ABC stands for Abstract Base Class . All those classes which want to implement data abstraction have to inherit from ABC. +@abstractmethod is a decorator provided by the abc module, which stands for "abstract method". It's used to define abstract methods within abstract base classes (ABCs). An abstract method is a method declared in a class, but it does not contain an implementation. Instead, it serves as a placeholder, and its concrete implementation must be provided by subclasses. +Abstract methods can be implemented by the derived classes. From be8993b6164d12fab5d3ec6e94172b8da5deadef Mon Sep 17 00:00:00 2001 From: Vrisha Shah <74671946+Vrisha213@users.noreply.github.com> Date: Sun, 19 May 2024 09:28:58 +0530 Subject: [PATCH 54/66] Create support-vector-machine.md added conetnt on SVM --- .../support-vector-machine.md | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 contrib/machine-learning/support-vector-machine.md diff --git a/contrib/machine-learning/support-vector-machine.md b/contrib/machine-learning/support-vector-machine.md new file mode 100644 index 0000000..90ee922 --- /dev/null +++ b/contrib/machine-learning/support-vector-machine.md @@ -0,0 +1,52 @@ +Support Vector Machine or SVM is one of the most popular Supervised Learning algorithms, which is used for Classification as well as Regression problems. However, primarily, it is used for Classification problems in Machine Learning. +SVM can be of two types - +1. Linear SVM: Linear SVM is used for linearly separable data, which means if a dataset can be classified into two classes by using a single straight line, then such data is termed as linearly separable data, and classifier is used called as Linear SVM classifier. +2. Non-linear SVM: Non-Linear SVM is used for non-linearly separated data, which means if a dataset cannot be classified by using a straight line, then such data is termed as non-linear data and classifier used is called as Non-linear SVM classifier. + +Working of SVM - The goal of SVM is to find a hyperplane that separates the data points into different classes. A hyperplane is a line in 2D space, a plane in 3D space, or a higher-dimensional surface in n-dimensional space. The hyperplane is chosen in such a way that it maximizes the margin, which is the distance between the hyperplane and the closest data points of each class. The closest data points are called the support vectors. +The distance between the hyperplane and a data point "x" can be calculated using the formula − +distance = (w . x + b) / ||w|| +where "w" is the weight vector, "b" is the bias term, and "||w||" is the Euclidean norm of the weight vector. The weight vector "w" is perpendicular to the hyperplane and determines its orientation, while the bias term "b" determines its position. +The optimal hyperplane is found by solving an optimization problem, which is to maximize the margin subject to the constraint that all data points are correctly classified. In other words, we want to find the hyperplane that maximizes the margin between the two classes while ensuring that no data point is misclassified. This is a convex optimization problem that can be solved using quadratic programming. If the data points are not linearly separable, we can use a technique called kernel trick to map the data points into a higher-dimensional space where they become separable. The kernel function computes the inner product between the mapped data points without computing the mapping itself. This allows us to work with the data points in the higherdimensional space without incurring the computational cost of mapping them. + +1. Hyperplane: +There can be multiple lines/decision boundaries to segregate the classes in n-dimensional space, but we need to find out the best decision boundary that helps to classify the data points. This best boundary is known as the hyperplane of SVM. +The dimensions of the hyperplane depend on the features present in the dataset, which means if there are 2 features, then hyperplane will be a straight line. And if there are 3 features, then hyperplane will be a 2-dimension plane. We always create a hyperplane that has a maximum margin, which means the maximum distance between the data points. +2. Support Vectors: +The data points or vectors that are the closest to the hyperplane and which affect the position of the hyperplane are termed as Support Vector. Since these vectors support the hyperplane, hence called a Support vector. +3. Margin: +It may be defined as the gap between two lines on the closet data points of different classes. It can be calculated as the perpendicular distance from the line to the support vectors. Large margin is considered as a good margin and small margin is considered as a bad margin. + +We will use the famous Iris dataset, which contains the sepal length, sepal width, petal length, and petal width of three species of iris flowers: Iris setosa, Iris versicolor, and Iris virginica. The goal is to classify the flowers into their respective species based on these four features. We load the iris dataset using load_iris and split the data into training and testing sets using train_test_split. We use a test size of 0.2, which means that 20% of the data will be used for testing and 80% for training. We set the random state to 42 to ensure reproducibility of the results. + +# Implemetation of SVM in Python + +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split +from sklearn.svm import SVC +from sklearn.metrics import accuracy_score + +# load the iris dataset +iris = load_iris() + +# split the data into training and testing sets +X_train, X_test, y_train, y_test = train_test_split(iris.data, +iris.target, test_size=0.2, random_state=42) + +# create an SVM classifier with a linear kernel +svm = SVC(kernel='linear') + +# train the SVM classifier on the training set +svm.fit(X_train, y_train) + +# make predictions on the testing set +y_pred = svm.predict(X_test) + +# calculate the accuracy of the classifier +accuracy = accuracy_score(y_test, y_pred) +print("Accuracy:", accuracy) + +# Output +Accuracy: 1 + + From 4be728eb4764637fa8072aeebcaa9965e619d578 Mon Sep 17 00:00:00 2001 From: Ananyagra24 <155280507+Ananyagra24@users.noreply.github.com> Date: Sun, 19 May 2024 09:44:37 +0530 Subject: [PATCH 56/66] Update dice_roller.md --- contrib/mini-projects/dice_roller.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/contrib/mini-projects/dice_roller.md b/contrib/mini-projects/dice_roller.md index c083934..6e1b793 100644 --- a/contrib/mini-projects/dice_roller.md +++ b/contrib/mini-projects/dice_roller.md @@ -1,8 +1,15 @@ +Dice Roller +
+The aim of this project is to replicate a dice and generate a random number from the numbers 1 to 6.

+For this first we will import the random library which will help make random choices. ``` import random def dice(): dice_no = random.choice([1,2,3,4,5,6]) return "You got " + str(dice_no) +``` +The above snippet of code defines a function called "dice( )" which makes the random choice and returns the number that is generated. +``` def roll_dice(): print("Hey Guys, you will now roll a single dice using Python!") while True: @@ -17,3 +24,6 @@ def roll_dice(): print("Thanks for rolling the dice.") roll_dice() ``` +The above code defines a function called "roll_dice( )" which interacts with the user.
+It prompts the user to give an input and if the input is k,the code proceeds further to generate a random number or gives the message of invalid input and asks the user to try again.
+After the dice has been rolled once, the function asks the user whether they want a reroll in the form of a yes or no question.The dice is rolled again if the user gives 'yes' as an answer and exits the code if the user replies with anything other than yes. From 4ee5b32b8e217eac48799391a2f8182ac2abae35 Mon Sep 17 00:00:00 2001 From: Pradnya <116059908+PradnyaGaitonde@users.noreply.github.com> Date: Tue, 21 May 2024 21:51:48 +0530 Subject: [PATCH 57/66] Create dates_and_times.md Added content for Working with dates & times in python --- contrib/advanced-python/dates_and_times.md | 129 +++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 contrib/advanced-python/dates_and_times.md diff --git a/contrib/advanced-python/dates_and_times.md b/contrib/advanced-python/dates_and_times.md new file mode 100644 index 0000000..f599e26 --- /dev/null +++ b/contrib/advanced-python/dates_and_times.md @@ -0,0 +1,129 @@ +## Working with Dates and Times in Python +Handling dates and times is an essential aspect of many programming tasks. +Python provides robust modules to work with dates and times, making it easier to perform operations like formatting, parsing, and arithmetic. +This guide provides an overview of these modules and their key functionalities. + +## 1. 'datetime' Module +The datetime module supplies classes for manipulating dates and times. The main classes in the datetime module are: + +* date: Represents a date (year, month, day). +* time: Represents a time (hour, minute, second, microsecond). +* datetime: Combines date and time information. +* timedelta: Represents the difference between two dates or times. +* tzinfo: Provides time zone information objects. + +**Key Concepts:** + +* Naive vs. Aware: Naive datetime objects do not contain time zone information, while aware datetime objects do. +* Immutability: date and time objects are immutable; once created, they cannot be changed. + +Example: +```bash +python +Copy code +import datetime +# Get the current date and time +now = datetime.datetime.now() +print("Current date and time:", now) +``` + +## 2. Formatting Dates and Times +Formatting involves converting datetime objects into human-readable strings. This is achieved using the strftime method, which stands for "string format time." +You can specify various format codes to dictate how the output string should be structured. + +**Common Format Codes:** + +* %Y: Year with century (e.g., 2024) +* %m: Month as a zero-padded decimal number (e.g., 01) +* %d: Day of the month as a zero-padded decimal number (e.g., 15) +* %H: Hour (24-hour clock) as a zero-padded decimal number (e.g., 13) +* %M: Minute as a zero-padded decimal number (e.g., 45) +* %S: Second as a zero-padded decimal number (e.g., 30) + +Example: +```bash +python +Copy code +import datetime + +now = datetime.datetime.now() +formatted_now = now.strftime("%Y-%m-%d %H:%M:%S") +print("Formatted current date and time:", formatted_now) +``` + +## 3. Parsing Dates and Times +Parsing is the process of converting strings representing dates and times into datetime objects. The strptime method, which stands for "string parse time," +allows you to specify the format of the input string. + +Example: +```bash +python +Copy code +import datetime + +date_string = "2024-05-15 13:45:30" +date_object = datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S") +print("Parsed date and time:", date_object) +``` + +## 4. Working with Time Differences +The timedelta class is used to represent the difference between two datetime objects. This is useful for calculations involving durations, such as finding the +number of days between two dates or adding a certain period to a date. + +Example: +```bash +python +Copy code +import datetime + +date1 = datetime.datetime(2024, 5, 15, 12, 0, 0) +date2 = datetime.datetime(2024, 5, 20, 14, 30, 0) + +difference = date2 - date1 +print("Difference:", difference) +print("Days:", difference.days) +print("Total seconds:", difference.total_seconds()) +``` + +## 5. Time Zones +Time zone handling in Python is facilitated by the pytz library. It allows you to convert naive datetime objects into timezone-aware objects and perform +operations across different time zones. + +**Key Concepts:** + +* Timezone-aware: A datetime object that includes timezone information. +* Localization: The process of associating a naive datetime with a time zone. + +Example: +```bash +python +Copy code +import datetime +import pytz + +# Define a timezone +tz = pytz.timezone('Asia/Kolkata') + +# Get the current time in a specific timezone +now = datetime.datetime.now(tz) +print("Current time in Asia/Kolkata:", now) +``` + +## 6. Date Arithmetic +Date arithmetic involves performing operations like addition or subtraction on date or datetime objects using timedelta. This is useful for calculating future +or past dates based on a given date. + +Example: +```bash +python +Copy code +import datetime + +today = datetime.date.today() +future_date = today + datetime.timedelta(days=10) +print("Date after 10 days:", future_date) +``` + +## Summary +Python’s datetime module and the pytz library provide comprehensive tools for working with dates, times, and time zones. They enable you to perform a wide range +of operations, from basic date manipulations to complex time zone conversions. From affc013943c380df51e90c08553573a6da46a143 Mon Sep 17 00:00:00 2001 From: Pradnya <116059908+PradnyaGaitonde@users.noreply.github.com> Date: Tue, 21 May 2024 21:55:23 +0530 Subject: [PATCH 58/66] Update index.md Added index for dates and times --- contrib/advanced-python/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/advanced-python/index.md b/contrib/advanced-python/index.md index 5ea5081..bf377fc 100644 --- a/contrib/advanced-python/index.md +++ b/contrib/advanced-python/index.md @@ -1,3 +1,4 @@ # List of sections - [Decorators/\*args/**kwargs](decorator-kwargs-args.md) +- [Working with Dates & Times in Python](dates_and_times.md) From 2967206c16c69227da131aa560efe0831148bc12 Mon Sep 17 00:00:00 2001 From: Pradnya <116059908+PradnyaGaitonde@users.noreply.github.com> Date: Tue, 21 May 2024 22:01:17 +0530 Subject: [PATCH 59/66] Create regular_expressions.md Added content for regular expressions in python --- .../advanced-python/regular_expressions.md | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 contrib/advanced-python/regular_expressions.md diff --git a/contrib/advanced-python/regular_expressions.md b/contrib/advanced-python/regular_expressions.md new file mode 100644 index 0000000..65ff2c2 --- /dev/null +++ b/contrib/advanced-python/regular_expressions.md @@ -0,0 +1,96 @@ +## Regular Expressions in Python +Regular expressions (regex) are a powerful tool for pattern matching and text manipulation. +Python's re module provides comprehensive support for regular expressions, enabling efficient text processing and validation. + +## 1. Introduction to Regular Expressions +A regular expression is a sequence of characters defining a search pattern. Common use cases include validating input, searching within text, and extracting +specific patterns. + +## 2. Basic Syntax +Literal Characters: Match exact characters (e.g., abc matches "abc"). +Metacharacters: Special characters like ., *, ?, +, ^, $, [ ], and | used to build patterns. + +**Common Metacharacters:** + +* .: Any character except newline. +* ^: Start of the string. +* $: End of the string. +* *: 0 or more repetitions. +* +: 1 or more repetitions. +* ?: 0 or 1 repetition. +* []: Any one character inside brackets (e.g., [a-z]). +* |: Either the pattern before or after. + +## 3. Using the re Module + +**Key functions in the re module:** + +* re.match(): Checks for a match at the beginning of the string. +* re.search(): Searches for a match anywhere in the string. +* re.findall(): Returns a list of all matches. +* re.sub(): Replaces matches with a specified string. + +Examples: +```bash +import re + +# Match at the beginning +print(re.match(r'\d+', '123abc').group()) # Output: 123 + +# Search anywhere +print(re.search(r'\d+', 'abc123').group()) # Output: 123 + +# Find all matches +print(re.findall(r'\d+', 'abc123def456')) # Output: ['123', '456'] + +# Substitute matches +print(re.sub(r'\d+', '#', 'abc123def456')) # Output: abc#def# +``` + +## 4. Compiling Regular Expressions +Compiling regular expressions improves performance for repeated use. + +Example: +```bash +import re + +pattern = re.compile(r'\d+') +print(pattern.match('123abc').group()) # Output: 123 +print(pattern.search('abc123').group()) # Output: 123 +print(pattern.findall('abc123def456')) # Output: ['123', '456'] +``` + +## 5. Groups and Capturing +Parentheses () group and capture parts of the match. + +Example: +```bash +import re + +match = re.match(r'(\d{3})-(\d{2})-(\d{4})', '123-45-6789') +if match: + print(match.group()) # Output: 123-45-6789 + print(match.group(1)) # Output: 123 + print(match.group(2)) # Output: 45 + print(match.group(3)) # Output: 6789 +``` + +## 6. Special Sequences +Special sequences are shortcuts for common patterns: + +* \d: Any digit. +* \D: Any non-digit. +* \w: Any alphanumeric character. +* \W: Any non-alphanumeric character. +* \s: Any whitespace character. +* \S: Any non-whitespace character. +Example: +```bash +import re + +print(re.search(r'\w+@\w+\.\w+', 'Contact: support@example.com').group()) # Output: support@example.com +``` + +## Summary +Regular expressions are a versatile tool for text processing in Python. The re module offers powerful functions and metacharacters for pattern matching, +searching, and manipulation, making it an essential skill for handling complex text processing tasks. From d44f8fc253ae6a74af3894ecd77ceb2ef5f01adf Mon Sep 17 00:00:00 2001 From: Pradnya <116059908+PradnyaGaitonde@users.noreply.github.com> Date: Tue, 21 May 2024 22:03:27 +0530 Subject: [PATCH 60/66] Update index.md added index for regular --- contrib/advanced-python/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/advanced-python/index.md b/contrib/advanced-python/index.md index 5ea5081..ebb567a 100644 --- a/contrib/advanced-python/index.md +++ b/contrib/advanced-python/index.md @@ -1,3 +1,4 @@ # List of sections - [Decorators/\*args/**kwargs](decorator-kwargs-args.md) +- [Regular Expressions in Python](regular_expressions.md) From d7dc67b678124cd54a93c5b917af9d7a903fb82d Mon Sep 17 00:00:00 2001 From: Pradnya <116059908+PradnyaGaitonde@users.noreply.github.com> Date: Tue, 21 May 2024 22:05:01 +0530 Subject: [PATCH 61/66] Update dates_and_times.md --- contrib/advanced-python/dates_and_times.md | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/contrib/advanced-python/dates_and_times.md b/contrib/advanced-python/dates_and_times.md index f599e26..983f6b2 100644 --- a/contrib/advanced-python/dates_and_times.md +++ b/contrib/advanced-python/dates_and_times.md @@ -19,8 +19,6 @@ The datetime module supplies classes for manipulating dates and times. The main Example: ```bash -python -Copy code import datetime # Get the current date and time now = datetime.datetime.now() @@ -42,8 +40,6 @@ You can specify various format codes to dictate how the output string should be Example: ```bash -python -Copy code import datetime now = datetime.datetime.now() @@ -57,8 +53,6 @@ allows you to specify the format of the input string. Example: ```bash -python -Copy code import datetime date_string = "2024-05-15 13:45:30" @@ -72,8 +66,6 @@ number of days between two dates or adding a certain period to a date. Example: ```bash -python -Copy code import datetime date1 = datetime.datetime(2024, 5, 15, 12, 0, 0) @@ -96,8 +88,6 @@ operations across different time zones. Example: ```bash -python -Copy code import datetime import pytz @@ -115,8 +105,6 @@ or past dates based on a given date. Example: ```bash -python -Copy code import datetime today = datetime.date.today() From 5e95059765b6cb99498c1302b94f68c8d5ee482c Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Thu, 23 May 2024 02:57:11 +0530 Subject: [PATCH 62/66] Update dice_roller.md --- contrib/mini-projects/dice_roller.md | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/contrib/mini-projects/dice_roller.md b/contrib/mini-projects/dice_roller.md index 6e1b793..0f4e5f9 100644 --- a/contrib/mini-projects/dice_roller.md +++ b/contrib/mini-projects/dice_roller.md @@ -1,14 +1,18 @@ -Dice Roller -
-The aim of this project is to replicate a dice and generate a random number from the numbers 1 to 6.

+## Dice Roller + +The aim of this project is to replicate a dice and generate a random number from the numbers 1 to 6. + For this first we will import the random library which will help make random choices. + ``` import random def dice(): dice_no = random.choice([1,2,3,4,5,6]) return "You got " + str(dice_no) ``` -The above snippet of code defines a function called "dice( )" which makes the random choice and returns the number that is generated. + +The above snippet of code defines a function called `dice()` which makes the random choice and returns the number that is generated. + ``` def roll_dice(): print("Hey Guys, you will now roll a single dice using Python!") @@ -24,6 +28,9 @@ def roll_dice(): print("Thanks for rolling the dice.") roll_dice() ``` -The above code defines a function called "roll_dice( )" which interacts with the user.
-It prompts the user to give an input and if the input is k,the code proceeds further to generate a random number or gives the message of invalid input and asks the user to try again.
-After the dice has been rolled once, the function asks the user whether they want a reroll in the form of a yes or no question.The dice is rolled again if the user gives 'yes' as an answer and exits the code if the user replies with anything other than yes. + +The above code defines a function called `roll_dice()` which interacts with the user. + +It prompts the user to give an input and if the input is `k`,the code proceeds further to generate a random number or gives the message of invalid input and asks the user to try again. + +After the dice has been rolled once, the function asks the user whether they want a reroll in the form of a `yes` or `no` question. The dice is rolled again if the user gives `yes` as an answer and exits the code if the user replies with anything other than yes. From 4d86a0af83d35990a71144e88c8979e5146529e8 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Thu, 23 May 2024 04:15:56 +0530 Subject: [PATCH 63/66] Update index.md --- contrib/machine-learning/index.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contrib/machine-learning/index.md b/contrib/machine-learning/index.md index 40a2237..44b39d4 100644 --- a/contrib/machine-learning/index.md +++ b/contrib/machine-learning/index.md @@ -1,4 +1,6 @@ # List of sections +- [Section title](filename.md) - [Confusion Matrix](confusion-matrix.md) - [Support Vector Machine Algorithm](support-vector-machine.md) + From 428bd62cd219830b77dde615f5a0efb3432afbb1 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Thu, 23 May 2024 04:20:12 +0530 Subject: [PATCH 64/66] Update confusion-matrix.md --- contrib/machine-learning/confusion-matrix.md | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/contrib/machine-learning/confusion-matrix.md b/contrib/machine-learning/confusion-matrix.md index ac5cd31..4bedf66 100644 --- a/contrib/machine-learning/confusion-matrix.md +++ b/contrib/machine-learning/confusion-matrix.md @@ -1,4 +1,6 @@ -Confusion Matrix - A confusion matrix is a fundamental performance evaluation tool used in machine learning to assess the accuracy of a classification model. It is an N x N matrix, where N represents the number of target classes. +## Confusion Matrix + +A confusion matrix is a fundamental performance evaluation tool used in machine learning to assess the accuracy of a classification model. It is an N x N matrix, where N represents the number of target classes. For binary classification, it results in a 2 x 2 matrix that outlines four key parameters: 1. True Positive (TP) - The predicted value matches the actual value, or the predicted class matches the actual class. @@ -20,15 +22,18 @@ To implement the confusion matrix in Python, we can use the confusion_matrix() f The function returns a 2D array that represents the confusion matrix. We can also visualize the confusion matrix using a heatmap. +```python # Import necessary libraries import numpy as np -from sklearn.metrics import confusion_matrix,classification_report +from sklearn.metrics import confusion_matrix, classification_report import seaborn as sns import matplotlib.pyplot as plt # Create the NumPy array for actual and predicted labels -actual = np.array(['Apple', 'Apple', 'Apple', 'Not Apple', 'Apple', 'Not Apple', 'Apple', 'Apple', 'Not Apple', 'Not Apple']) -predicted = np.array(['Apple', 'Not Apple', 'Apple', 'Not Apple', 'Apple', 'Apple', 'Apple', 'Apple', 'Not Apple', 'Not Apple']) +actual = np.array(['Apple', 'Apple', 'Apple', 'Not Apple', 'Apple', + 'Not Apple', 'Apple', 'Apple', 'Not Apple', 'Not Apple']) +predicted = np.array(['Apple', 'Not Apple', 'Apple', 'Not Apple', 'Apple', + 'Apple', 'Apple', 'Apple', 'Not Apple', 'Not Apple']) # Compute the confusion matrix cm = confusion_matrix(actual,predicted) @@ -46,8 +51,11 @@ plt.show() # Classifications Report based on Confusion Metrics print(classification_report(actual, predicted)) +``` -# Results +### Results + +``` 1. Confusion Matrix: [[5 1] [1 3]] @@ -59,3 +67,4 @@ Not Apple 0.75 0.75 0.75 4 accuracy 0.80 10 macro avg 0.79 0.79 0.79 10 weighted avg 0.80 0.80 0.80 10 +``` From 34131322a2753f32d44630e1c127a6716b099fca Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Thu, 23 May 2024 04:22:17 +0530 Subject: [PATCH 65/66] Update support-vector-machine.md --- .../support-vector-machine.md | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/contrib/machine-learning/support-vector-machine.md b/contrib/machine-learning/support-vector-machine.md index 90ee922..0117e9f 100644 --- a/contrib/machine-learning/support-vector-machine.md +++ b/contrib/machine-learning/support-vector-machine.md @@ -1,12 +1,19 @@ +## Support Vector Machine + Support Vector Machine or SVM is one of the most popular Supervised Learning algorithms, which is used for Classification as well as Regression problems. However, primarily, it is used for Classification problems in Machine Learning. + SVM can be of two types - 1. Linear SVM: Linear SVM is used for linearly separable data, which means if a dataset can be classified into two classes by using a single straight line, then such data is termed as linearly separable data, and classifier is used called as Linear SVM classifier. 2. Non-linear SVM: Non-Linear SVM is used for non-linearly separated data, which means if a dataset cannot be classified by using a straight line, then such data is termed as non-linear data and classifier used is called as Non-linear SVM classifier. -Working of SVM - The goal of SVM is to find a hyperplane that separates the data points into different classes. A hyperplane is a line in 2D space, a plane in 3D space, or a higher-dimensional surface in n-dimensional space. The hyperplane is chosen in such a way that it maximizes the margin, which is the distance between the hyperplane and the closest data points of each class. The closest data points are called the support vectors. -The distance between the hyperplane and a data point "x" can be calculated using the formula − +Working of SVM - The goal of SVM is to find a hyperplane that separates the data points into different classes. A hyperplane is a line in 2D space, a plane in 3D space, or a higher-dimensional surface in n-dimensional space. The hyperplane is chosen in such a way that it maximizes the margin, which is the distance between the hyperplane and the closest data points of each class. The closest data points are called the support vectors. + +The distance between the hyperplane and a data point "x" can be calculated using the formula − +``` distance = (w . x + b) / ||w|| +``` where "w" is the weight vector, "b" is the bias term, and "||w||" is the Euclidean norm of the weight vector. The weight vector "w" is perpendicular to the hyperplane and determines its orientation, while the bias term "b" determines its position. + The optimal hyperplane is found by solving an optimization problem, which is to maximize the margin subject to the constraint that all data points are correctly classified. In other words, we want to find the hyperplane that maximizes the margin between the two classes while ensuring that no data point is misclassified. This is a convex optimization problem that can be solved using quadratic programming. If the data points are not linearly separable, we can use a technique called kernel trick to map the data points into a higher-dimensional space where they become separable. The kernel function computes the inner product between the mapped data points without computing the mapping itself. This allows us to work with the data points in the higherdimensional space without incurring the computational cost of mapping them. 1. Hyperplane: @@ -19,8 +26,9 @@ It may be defined as the gap between two lines on the closet data points of diff We will use the famous Iris dataset, which contains the sepal length, sepal width, petal length, and petal width of three species of iris flowers: Iris setosa, Iris versicolor, and Iris virginica. The goal is to classify the flowers into their respective species based on these four features. We load the iris dataset using load_iris and split the data into training and testing sets using train_test_split. We use a test size of 0.2, which means that 20% of the data will be used for testing and 80% for training. We set the random state to 42 to ensure reproducibility of the results. -# Implemetation of SVM in Python +### Implemetation of SVM in Python +```python from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.svm import SVC @@ -45,8 +53,10 @@ y_pred = svm.predict(X_test) # calculate the accuracy of the classifier accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy) +``` -# Output +#### Output +``` Accuracy: 1 - +``` From 5b74a4be6581161649b2056588b93ac4702ba3d2 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Thu, 23 May 2024 04:54:25 +0530 Subject: [PATCH 66/66] Update Descriptive_Statistics.md --- contrib/pandas/Descriptive_Statistics.md | 190 ++++++++++------------- 1 file changed, 82 insertions(+), 108 deletions(-) diff --git a/contrib/pandas/Descriptive_Statistics.md b/contrib/pandas/Descriptive_Statistics.md index 7775965..abdb33b 100644 --- a/contrib/pandas/Descriptive_Statistics.md +++ b/contrib/pandas/Descriptive_Statistics.md @@ -10,13 +10,11 @@ Data scientists utilize descriptive statistics to uncover patterns, identify out ```python import pandas as pd import numpy as np -``` - -```python df = pd.read_csv("Age-Income-Dataset.csv") df ``` + | | Age | Income | | --- | ----------- | ------ | | 0 | Young | 25000 | @@ -70,14 +68,10 @@ df | 48 | Young | 68400 | | 49 | Middle Age | 51700 | - - ```python df.describe() ``` - - | | Income | |-------|-------------| | count | 50.000000 | @@ -101,18 +95,17 @@ $$ \overline{x} = \frac{\sum\limits_{i=1}^{n} x_i}{n} $$ * $\sum$ (sigma) represents the summation symbol, indicating we add up all the values from i=1 to n. * $n$ represents the total number of values in the dataset. - - ```python df['Income'].mean() ``` +#### Result - - +``` 50966.0 +``` - +#### Without pandas ```python @@ -131,12 +124,10 @@ def mean_f(df): mean_f(df) ``` -Without pandas Library -> -Average of Income is 50966.0 +Average of Income: -With pandas Library -> -50966.0 - +- Without pandas Library -> 50966.0 +- With pandas Library -> 50966.0 ### Median @@ -163,21 +154,17 @@ $$ M = \frac{x_{n/2} + x_{(n/2)+1}}{2} $$ * n represents the total number of observations in the dataset. * $x$ represents the individual value. - - - - ```python df['Income'].median() ``` +#### Result - - +``` 46850.0 +``` - - +#### Without pandas ```python def median_f(df): @@ -198,18 +185,15 @@ def median_f(df): median_f(df) ``` -Median without library -> -Median of Income is 49850.0 \ -Median with library -> -46850.0 - +Median of Income: + +- Median without library -> 49850.0 +- Median with library -> 46850.0 ### Mode - The mode is a measure of central tendency that represents the value or values that occur most frequently in a dataset. Unlike the mean and median, which focus on the average or middle value, the mode identifies the most common value(s) in the dataset. - ```python def mode_f(df): for col in df.columns: @@ -240,17 +224,17 @@ def mode_f(df): mode_f(df) ``` +#### Result + +``` Column: Age -Without pandas Library -> \ +Without pandas Library -> Mode of Age is Old -With pandas Library -> \ -0 Old \ +With pandas Library -> +0 Old Name: Age, dtype: object - - - - +``` ### Standard Deviation @@ -264,18 +248,17 @@ $$s = \sqrt{\frac{\sum(x_i-\overline{x})^{2}}{n-1}}$$ * $\overline{x}$ (x bar) represents the mean value of the dataset. * $n$ represents the total number of values in the dataset. - ```python df['Income'].std() ``` +#### Result - - +``` 21096.683267707253 +``` - - +#### Without pandas ```python import math @@ -304,17 +287,17 @@ With pandas library: -> Std : 20884.6509187968 +### Count ```python df['Income'].count() ``` +#### Result - - +``` 50 - - +``` ### Minimum @@ -323,13 +306,13 @@ df['Income'].count() df['Income'].min() ``` +#### Result - - +``` 15000 +``` - - +#### Without pandas ```python def min_f(df): @@ -354,13 +337,13 @@ With pandas Library-> 15000 df['Income'].max() ``` +#### Result - - +``` 93000 +``` - - +#### Without pandas ```python def max_f(df): @@ -374,8 +357,8 @@ def max_f(df): max_f(df) ``` - Without pandas Library-> 93000 - With pandas Library-> 93000 +Without pandas Library-> 93000 +With pandas Library-> 93000 ### Percentile @@ -385,25 +368,23 @@ max_f(df) df['Income'].quantile(0.25) ``` +#### Result - - +``` 33475.0 - - - +``` ```python df['Income'].quantile(0.75) ``` +#### Result - - +``` 65400.0 +``` - - +#### Without pandas ```python def percentile_f(df,percentile): @@ -432,9 +413,6 @@ df = pd.read_csv('Iris.csv') df.head(5) ``` - - - | | Id | SepalLengthCm | SepalWidthCm | PetalLengthCm | PetalWidthCm | Species | |---|----|---------------|--------------|---------------|--------------|-------------| | 0 | 1 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa | @@ -443,9 +421,6 @@ df.head(5) | 3 | 4 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa | | 4 | 5 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa | - - - ```python df.drop(['Id','Species'],axis=1,inplace= True) ``` @@ -463,13 +438,10 @@ $$ COV(X,Y) = \frac{\sum\limits_{i=1}^{n} (X_i - \overline{X}) (Y_i - \overline{ * $\overline{X}$ and $\overline{Y}$ represent the mean values for variables X and Y, respectively. * $n$ represents the total number of observations in the dataset. - - ```python df.cov() ``` - | | SepalLengthCm | SepalWidthCm | PetalLengthCm | PetalWidthCm | |-------------------|-------------- |---------------|-----------------|--------------| | **SepalLengthCm** | 0.685694 | -0.039268 | 1.273682 | 0.516904 | @@ -477,7 +449,7 @@ df.cov() | **PetalLengthCm** | 1.273682 | -0.321713 | 3.113179 | 1.296387 | | **PetalWidthCm** | 0.516904 | -0.117981 | 1.296387 | 0.582414 | - +#### Without pandas ```python def cov_f(df): @@ -496,27 +468,29 @@ def cov_f(df): cov_f(df) ``` -Covariance of SepalLengthCm and SepalLengthCm is : 0.6856935123042504 \ -Covariance of SepalLengthCm and SepalWidthCm is : -0.03926845637583892 \ -Covariance of SepalLengthCm and PetalLengthCm is : 1.2736823266219246 \ +#### Result + +``` +Covariance of SepalLengthCm and SepalLengthCm is : 0.6856935123042504 +Covariance of SepalLengthCm and SepalWidthCm is : -0.03926845637583892 +Covariance of SepalLengthCm and PetalLengthCm is : 1.2736823266219246 Covariance of SepalLengthCm and PetalWidthCm is : 0.5169038031319911 -Covariance of SepalWidthCm and SepalLengthCm is : -0.03926845637583892 \ -Covariance of SepalWidthCm and SepalWidthCm is : 0.1880040268456377 \ -Covariance of SepalWidthCm and PetalLengthCm is : -0.32171275167785235 \ +Covariance of SepalWidthCm and SepalLengthCm is : -0.03926845637583892 +Covariance of SepalWidthCm and SepalWidthCm is : 0.1880040268456377 +Covariance of SepalWidthCm and PetalLengthCm is : -0.32171275167785235 Covariance of SepalWidthCm and PetalWidthCm is : -0.11798120805369115 -Covariance of PetalLengthCm and SepalLengthCm is : 1.2736823266219246 \ -Covariance of PetalLengthCm and SepalWidthCm is : -0.32171275167785235 \ -Covariance of PetalLengthCm and PetalLengthCm is : 3.113179418344519 \ +Covariance of PetalLengthCm and SepalLengthCm is : 1.2736823266219246 +Covariance of PetalLengthCm and SepalWidthCm is : -0.32171275167785235 +Covariance of PetalLengthCm and PetalLengthCm is : 3.113179418344519 Covariance of PetalLengthCm and PetalWidthCm is : 1.2963874720357946 -Covariance of PetalWidthCm and SepalLengthCm is : 0.5169038031319911 \ -Covariance of PetalWidthCm and SepalWidthCm is : -0.11798120805369115 \ -Covariance of PetalWidthCm and PetalLengthCm is : 1.2963874720357946 \ +Covariance of PetalWidthCm and SepalLengthCm is : 0.5169038031319911 +Covariance of PetalWidthCm and SepalWidthCm is : -0.11798120805369115 +Covariance of PetalWidthCm and PetalLengthCm is : 1.2963874720357946 Covariance of PetalWidthCm and PetalWidthCm is : 0.5824143176733781 - - +```` ### Correlation @@ -527,9 +501,6 @@ $$r = \frac{n(\sum xy) - (\sum x)(\sum y)}{\sqrt{n(\sum x^2) - (\sum x)^2} \cdot * r represents the correlation coefficient. * n is the number of data points. - - - ```python df.corr() ``` @@ -541,6 +512,7 @@ df.corr() | **PetalLengthCm** | 0.871754 | -0.420516 | 1.000000 | 0.962757 | | **PetalWidthCm** | 0.817954 | -0.356544 | 0.962757 | 1.000000 | +#### Without using pandas ```python import math @@ -576,24 +548,26 @@ def corr_f(df): corr_f(df) ``` -Correlation of SepalLengthCm and SepalLengthCm :1.0 \ -Correlation of SepalLengthCm and SepalWidthCm :-0.10936924995067286 \ -Correlation of SepalLengthCm and PetalLengthCm :0.8717541573048861 \ +#### Result + +``` +Correlation of SepalLengthCm and SepalLengthCm :1.0 +Correlation of SepalLengthCm and SepalWidthCm :-0.10936924995067286 +Correlation of SepalLengthCm and PetalLengthCm :0.8717541573048861 Correlation of SepalLengthCm and PetalWidthCm :0.8179536333691775 -Correlation of SepalWidthCm and SepalLengthCm :-0.10936924995067286 \ -Correlation of SepalWidthCm and SepalWidthCm :1.0 \ -Correlation of SepalWidthCm and PetalLengthCm :-0.42051609640118826 \ +Correlation of SepalWidthCm and SepalLengthCm :-0.10936924995067286 +Correlation of SepalWidthCm and SepalWidthCm :1.0 +Correlation of SepalWidthCm and PetalLengthCm :-0.42051609640118826 Correlation of SepalWidthCm and PetalWidthCm :-0.3565440896138223 -Correlation of PetalLengthCm and SepalLengthCm :0.8717541573048861 \ -Correlation of PetalLengthCm and SepalWidthCm :-0.42051609640118826 \ -Correlation of PetalLengthCm and PetalLengthCm :1.0 \ +Correlation of PetalLengthCm and SepalLengthCm :0.8717541573048861 +Correlation of PetalLengthCm and SepalWidthCm :-0.42051609640118826 +Correlation of PetalLengthCm and PetalLengthCm :1.0 Correlation of PetalLengthCm and PetalWidthCm :0.9627570970509656 -Correlation of PetalWidthCm and SepalLengthCm :0.8179536333691775 \ -Correlation of PetalWidthCm and SepalWidthCm :-0.3565440896138223 \ -Correlation of PetalWidthCm and PetalLengthCm :0.9627570970509656 \ +Correlation of PetalWidthCm and SepalLengthCm :0.8179536333691775 +Correlation of PetalWidthCm and SepalWidthCm :-0.3565440896138223 +Correlation of PetalWidthCm and PetalLengthCm :0.9627570970509656 Correlation of PetalWidthCm and PetalWidthCm :1.0 - - +```