├── README.md ├── Python-Assignment ├── Python_Assignment_7.ipynb ├── Python_Assignment_5.ipynb ├── Python_Assignment_8.ipynb ├── Python_Assignment_10.ipynb ├── Python_Assignment_9.ipynb ├── Python_Assignment_11.ipynb ├── Python_Assignment_3.ipynb ├── Python_Assignment_2.ipynb ├── Python_Assignment_1.ipynb ├── Python_Assignment_14.ipynb └── Python_Assignment_4.ipynb ├── Numpy └── Numpy_Assignment.ipynb ├── Machine_Learning_Algorithm ├── SVM │ └── SVM_Assignmnet_3.ipynb ├── Regression │ ├── Regression_Assignment_4.ipynb │ ├── Regression_Assignment_6.ipynb │ └── Regression_Assignment_5.ipynb └── NaiveBayes │ └── nb_1.ipynb ├── Flask_and_WebAPI ├── WebApi.ipynb ├── webscrapping.ipynb └── imagescrapping.ipynb ├── Pandas └── Pandas_Assignment_1.ipynb └── Introduction To Machine learning ├── Assignment_1.ipynb └── Assignment_2.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # PW-Skills-Data-Master-Assignment 2 | Assignment Solution of PW Skills Data Master Course 3 | -------------------------------------------------------------------------------- /Python-Assignment/Python_Assignment_7.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "attachments": {}, 5 | "cell_type": "markdown", 6 | "id": "91371235", 7 | "metadata": {}, 8 | "source": [ 9 | "'''Q.1> Create a function which will take a list as an argument and return the product of all the numbers \n", 10 | " after creating a flat list.\n", 11 | "\n", 12 | " Use the below-given list as an argument for your function.\n", 13 | "\n", 14 | "\n", 15 | " list1 = [1,2,3,4, [44,55,66, True], False, (34,56,78,89,34), {1,2,3,3,2,1}, {1:34, \"key2\": [55, 67, 78, 89], 4: (45, \n", 16 | " 22, 61, 34)}, [56, 'data science'], 'Machine Learning']'''" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "id": "ccd0636a-65fc-459c-96ef-ff90eb4e6fc8", 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "2315262738624675840\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "\n", 35 | "from typing import List\n", 36 | "\n", 37 | "def get_product(lst: List):\n", 38 | " flat_list = []\n", 39 | " def flatten(lst):\n", 40 | " for item in lst:\n", 41 | " if type(item) in (list, tuple, set, dict):\n", 42 | " flatten(item)\n", 43 | " elif type(item) == int:\n", 44 | " flat_list.append(item)\n", 45 | " flatten(lst)\n", 46 | " product = 1\n", 47 | " for num in flat_list:\n", 48 | " product *= num\n", 49 | " return product\n", 50 | "\n", 51 | "list1 = [1,2,3,4, [44,55,66, True], False, (34,56,78,89,34), {1,2,3,3,2,1}, {1:34, \"key2\": [55, 67, 78, 89], 4: (45, 22, 61, 34)}, [56, 'data science'], 'Machine Learning']\n", 52 | "print(get_product(list1))\n" 53 | ] 54 | }, 55 | { 56 | "attachments": {}, 57 | "cell_type": "markdown", 58 | "id": "9b72262d", 59 | "metadata": {}, 60 | "source": [ 61 | "'''Q.2> Write a python program for encrypting a message sent to you by your friend. The logic of encryption \n", 62 | " should be such that, for a the output should be z. For b, the output should be y. For c, the output should \n", 63 | " be x respectively. Also, the whitespace should be replaced with a dollar sign. Keep the punctuation \n", 64 | " marks unchanged.\n", 65 | " Input Sentence: I want to become a Data Scientist.\n", 66 | " Encrypt the above input sentence using the program you just created'''" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 1, 72 | "id": "89eb28f3-db13-4aec-90b2-9c8ed2ceeee0", 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "Encrypted sentence: r$dzmg$gl$yvxlnv$z$wzgz$hxrvmgrhg.\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "\n", 85 | "def encrypt(sentence):\n", 86 | " sentence = sentence.lower()\n", 87 | " encryption = {}\n", 88 | " for i in range(26):\n", 89 | " encryption[chr(i + ord('a'))] = chr((25 - i) + ord('a'))\n", 90 | " encryption[' '] = '$'\n", 91 | " encrypted_sentence = ''\n", 92 | " for char in sentence:\n", 93 | " if char in encryption:\n", 94 | " encrypted_sentence += encryption[char]\n", 95 | " else:\n", 96 | " encrypted_sentence += char\n", 97 | " return encrypted_sentence\n", 98 | "\n", 99 | "sentence = \"I want to become a Data Scientist.\"\n", 100 | "encrypted_sentence = encrypt(sentence)\n", 101 | "print(\"Encrypted sentence:\", encrypted_sentence)\n" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "id": "5f3363e7-f8bc-4c3a-b9cb-cb5d3c29cfb9", 108 | "metadata": {}, 109 | "outputs": [], 110 | "source": [] 111 | } 112 | ], 113 | "metadata": { 114 | "kernelspec": { 115 | "display_name": "Python 3", 116 | "language": "python", 117 | "name": "python3" 118 | }, 119 | "language_info": { 120 | "codemirror_mode": { 121 | "name": "ipython", 122 | "version": 3 123 | }, 124 | "file_extension": ".py", 125 | "mimetype": "text/x-python", 126 | "name": "python", 127 | "nbconvert_exporter": "python", 128 | "pygments_lexer": "ipython3", 129 | "version": "3.9.0" 130 | }, 131 | "vscode": { 132 | "interpreter": { 133 | "hash": "bf9ab843c2c44a0abbe96a3f5fa8fc8f7b5aa1503133f196762388041b9614c2" 134 | } 135 | } 136 | }, 137 | "nbformat": 4, 138 | "nbformat_minor": 5 139 | } 140 | -------------------------------------------------------------------------------- /Python-Assignment/Python_Assignment_5.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "2b54c7cd-0edb-407c-9949-11c067de7dfa", 6 | "metadata": {}, 7 | "source": [ 8 | "1. Create a python program to sort the given list of tuples based on integer value using a \n", 9 | " lambda function. \n", 10 | " [('Sachin Tendulkar', 34357), ('Ricky Ponting', 27483), ('Jack Kallis', 25534), ('Virat Kohli', 24936)]" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "635c1963-1251-441f-bba7-4512a79fd4e6", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stdout", 21 | "output_type": "stream", 22 | "text": [ 23 | "[('Sachin Tendulkar', 34357), ('Ricky Ponting', 27483), ('Jack Kallis', 25534), ('Virat Kohli', 24936)]\n" 24 | ] 25 | } 26 | ], 27 | "source": [ 28 | "\n", 29 | "players = [('Sachin Tendulkar', 34357), ('Ricky Ponting', 27483), ('Jack Kallis', 25534), ('Virat Kohli', 24936)]\n", 30 | "\n", 31 | "sorted_players = sorted(players, key=lambda x: x[1], reverse=True)\n", 32 | "\n", 33 | "print(sorted_players)\n" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "id": "9cb5d05c-8b67-45c1-a328-e4c8712b733f", 39 | "metadata": {}, 40 | "source": [ 41 | "'''Q.2> Write a Python Program to find the squares of all the numbers in the given list of integers using \n", 42 | " lambda and map functions.\n", 43 | " [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]'''" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "id": "ad3b3468-b9ca-4ebb-9d56-f75e73af42e3", 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" 57 | ] 58 | } 59 | ], 60 | "source": [ 61 | "\n", 62 | "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 63 | "\n", 64 | "squared_numbers = list(map(lambda x: x**2, numbers))\n", 65 | "\n", 66 | "print(squared_numbers)\n" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "id": "5e3b0dd0-81d4-4dd2-bdde-b3e5959f2784", 72 | "metadata": {}, 73 | "source": [ 74 | "'''Q.3> Write a python program to convert the given list of integers into a tuple of strings. Use map and \n", 75 | " lambda functions\n", 76 | " Given String: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 77 | " Expected output: ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10')'''" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 4, 83 | "id": "69b45d85-7961-461b-88f6-64508565a48b", 84 | "metadata": {}, 85 | "outputs": [ 86 | { 87 | "name": "stdout", 88 | "output_type": "stream", 89 | "text": [ 90 | "('1', '2', '3', '4', '5', '6', '7', '8', '9', '10')\n" 91 | ] 92 | } 93 | ], 94 | "source": [ 95 | "\n", 96 | "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 97 | "\n", 98 | "string_numbers = tuple(map(lambda x: str(x), numbers))\n", 99 | "\n", 100 | "print(string_numbers)\n" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "id": "dd73e68c-6042-4e1f-b332-9c246ebb618a", 106 | "metadata": {}, 107 | "source": [ 108 | "'''Q.4> Write a python program using reduce function to compute the product of a list containing numbers \n", 109 | " from 1 to 25.'''" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": 5, 115 | "id": "c1d22f82-6fcb-49b9-b79a-3b7dbb1f2ff0", 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "15511210043330985984000000\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "\n", 128 | "from functools import reduce\n", 129 | "\n", 130 | "numbers = range(1, 26)\n", 131 | "\n", 132 | "product = reduce(lambda x, y: x * y, numbers)\n", 133 | "\n", 134 | "print(product)\n" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "id": "a7864881-153f-47a7-842a-5e1e7defc137", 140 | "metadata": {}, 141 | "source": [ 142 | "'''Q.5> Write a python program to filter the numbers in a given list that are divisible by\n", 143 | " 2 and 3 using the filter function.\n", 144 | " [2, 3, 6, 9, 27, 60, 90, 120, 55, 46]'''" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 6, 150 | "id": "e7f7c818-f5d9-4efe-abcf-72999967b4ee", 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "[6, 60, 90, 120]\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "\n", 163 | "numbers = [2, 3, 6, 9, 27, 60, 90, 120, 55, 46]\n", 164 | "\n", 165 | "filtered_numbers = list(filter(lambda x: x % 2 == 0 and x % 3 == 0, numbers))\n", 166 | "\n", 167 | "print(filtered_numbers)\n" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "id": "8564f356-4b54-4c8f-bc32-67924248a2b5", 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [] 177 | } 178 | ], 179 | "metadata": { 180 | "kernelspec": { 181 | "display_name": "Python 3 (ipykernel)", 182 | "language": "python", 183 | "name": "python3" 184 | }, 185 | "language_info": { 186 | "codemirror_mode": { 187 | "name": "ipython", 188 | "version": 3 189 | }, 190 | "file_extension": ".py", 191 | "mimetype": "text/x-python", 192 | "name": "python", 193 | "nbconvert_exporter": "python", 194 | "pygments_lexer": "ipython3", 195 | "version": "3.10.8" 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 5 200 | } 201 | -------------------------------------------------------------------------------- /Python-Assignment/Python_Assignment_8.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "attachments": {}, 5 | "cell_type": "markdown", 6 | "id": "16530b13", 7 | "metadata": {}, 8 | "source": [ 9 | "You are writing code for a company. The requirement of the company is that you create a python\n", 10 | "function that will check whether the password entered by the user is correct or not. The function should\n", 11 | "take the password as input and return the string “Valid Password” if the entered password follows the\n", 12 | "below-given password guidelines else it should return “Invalid Password”.\n", 13 | "Note: 1. The Password should contain at least two uppercase letters and at least two lowercase letters.\n", 14 | "2. The Password should contain at least a number and three special characters.\n", 15 | "3. The length of the password should be 10 characters long." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 3, 21 | "id": "777ae581-6803-4340-8c75-84d2b46f31d5", 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "Valid Password\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "\n", 34 | "\n", 35 | "import re\n", 36 | "password = \"R@m@_f0rtu9e$\"\n", 37 | "flag = 0\n", 38 | "while True:\n", 39 | " if (len(password)<=8):\n", 40 | " flag = -1\n", 41 | " break\n", 42 | " elif not re.search(\"[a-z]\", password):\n", 43 | " flag = -1\n", 44 | " break\n", 45 | " elif not re.search(\"[A-Z]\", password):\n", 46 | " flag = -1\n", 47 | " break\n", 48 | " elif not re.search(\"[0-9]\", password):\n", 49 | " flag = -1\n", 50 | " break\n", 51 | " elif not re.search(\"[_@$]\" , password):\n", 52 | " flag = -1\n", 53 | " break\n", 54 | " elif re.search(\"\\s\" , password):\n", 55 | " flag = -1\n", 56 | " break\n", 57 | " else:\n", 58 | " flag = 0\n", 59 | " print(\"Valid Password\")\n", 60 | " break\n", 61 | " \n", 62 | "if flag == -1:\n", 63 | " print(\"Not a Valid Password \")" 64 | ] 65 | }, 66 | { 67 | "attachments": {}, 68 | "cell_type": "markdown", 69 | "id": "7b986184", 70 | "metadata": {}, 71 | "source": [ 72 | "'''Q2. Solve the below-given questions using at least one of the following:\n", 73 | "1. Lambda functioJ\n", 74 | "2. Filter functioJ\n", 75 | "3. Zap functioJ\n", 76 | "4. List ComprehensioI\n", 77 | " Check if the string starts with a particular letterY\n", 78 | " Check if the string is numeric\n", 79 | " Sort a list of tuples having fruit names and their quantity. [(\"mango\",99),(\"orange\",80), (\"grapes\", 1000)-\n", 80 | " Find the squares of numbers from 1 to 10Y\n", 81 | " Find the cube root of numbers from 1 to 10Y\n", 82 | " Check if a given number is evenY\n", 83 | " Filter odd numbers from the given list.\n", 84 | "1,2,3,4,5,6,7,8,9,10-\n", 85 | " Sort a list of integers into positive and negative integers lists.\n", 86 | "[1,2,3,4,5,6,-1,-2,-3,-4,-5,0]'''" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 2, 92 | "id": "2ab00749-5b04-4b7a-8176-76d9c79e95b1", 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "name": "stdout", 97 | "output_type": "stream", 98 | "text": [ 99 | "True\n", 100 | "False\n", 101 | "True\n", 102 | "False\n", 103 | "[('orange', 80), ('mango', 99), ('grapes', 1000)]\n", 104 | "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n", 105 | "[1.0, 1.2599210498948732, 1.4422495703074083, 1.5874010519681994, 1.7099759466766968, 1.8171205928321397, 1.912931182772389, 2.0, 2.080083823051904, 2.154434690031884]\n", 106 | "True\n", 107 | "False\n", 108 | "[1, 3, 5, 7, 9]\n", 109 | "[1, 2, 3, 4, 5, 6]\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "\n", 115 | "#Ans> 1.Check if the string starts with a particular letter:\n", 116 | "starts_with = lambda string, letter: string[0] == letter\n", 117 | "\n", 118 | "print(starts_with(\"Hello\", \"H\")) # True\n", 119 | "print(starts_with(\"Hello\", \"h\")) # False\n", 120 | "\n", 121 | "#2.Check if the string is numeric:\n", 122 | "def is_numeric(string):\n", 123 | " return string.isdigit()\n", 124 | "\n", 125 | "print(is_numeric(\"123\")) # True\n", 126 | "print(is_numeric(\"abc\")) # False\n", 127 | "\n", 128 | "#3.Sort a list of tuples having fruit names and their quantity:\n", 129 | "fruits = [(\"mango\", 99), (\"orange\", 80), (\"grapes\", 1000)]\n", 130 | "\n", 131 | "sorted_fruits = sorted(fruits, key=lambda x: x[1])\n", 132 | "\n", 133 | "print(sorted_fruits) # [(\"mango\", 99), (\"orange\", 80), (\"grapes\", 1000)]\n", 134 | "\n", 135 | "#4.Find the squares of numbers from 1 to 10:\n", 136 | "squared_numbers = [x**2 for x in range(1, 11)]\n", 137 | "\n", 138 | "print(squared_numbers) # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n", 139 | "\n", 140 | "#5.Find the cube root of numbers from 1 to 10:\n", 141 | "import math\n", 142 | "\n", 143 | "cuberoot_numbers = [math.pow(x, 1/3) for x in range(1, 11)]\n", 144 | "\n", 145 | "print(cuberoot_numbers)\n", 146 | "\n", 147 | "#6.Check if a given number is even:\n", 148 | "\n", 149 | "def is_even(number):\n", 150 | " return number % 2 == 0\n", 151 | "\n", 152 | "print(is_even(2)) # True\n", 153 | "print(is_even(3)) # False\n", 154 | "#7.Filter odd numbers from the given list:\n", 155 | "\n", 156 | "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 157 | "\n", 158 | "odd_numbers = [x for x in numbers if x % 2 != 0]\n", 159 | "\n", 160 | "print(odd_numbers) # [1, 3, 5, 7, 9]\n", 161 | "#8.Sort a list of integers into positive and negative integers lists:\n", 162 | "\n", 163 | "numbers = [1, 2, 3, 4, 5, 6, -1, -2, -3, -4, -5, 0]\n", 164 | "\n", 165 | "positive_numbers = [x for x in numbers if x > 0]\n", 166 | "negative_numbers = [x for x in numbers if x < 0]\n", 167 | "\n", 168 | "print(positive_numbers) # [1, 2, 3, 4," 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "id": "c431bfc6", 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [] 178 | } 179 | ], 180 | "metadata": { 181 | "kernelspec": { 182 | "display_name": "Python 3", 183 | "language": "python", 184 | "name": "python3" 185 | }, 186 | "language_info": { 187 | "codemirror_mode": { 188 | "name": "ipython", 189 | "version": 3 190 | }, 191 | "file_extension": ".py", 192 | "mimetype": "text/x-python", 193 | "name": "python", 194 | "nbconvert_exporter": "python", 195 | "pygments_lexer": "ipython3", 196 | "version": "3.9.0" 197 | }, 198 | "vscode": { 199 | "interpreter": { 200 | "hash": "bf9ab843c2c44a0abbe96a3f5fa8fc8f7b5aa1503133f196762388041b9614c2" 201 | } 202 | } 203 | }, 204 | "nbformat": 4, 205 | "nbformat_minor": 5 206 | } 207 | -------------------------------------------------------------------------------- /Python-Assignment/Python_Assignment_10.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "attachments": {}, 5 | "cell_type": "markdown", 6 | "id": "cf5ad83c", 7 | "metadata": {}, 8 | "source": [ 9 | "#Q.1> Create a vehicle class with an init method having instance variables as name_of_vehicle,max_speed and average_of_vehicle.\n" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "id": "88a7e2e0-51ef-4af8-8bc3-6600efbb6a1d", 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "Car\n", 23 | "200\n", 24 | "20\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "\n", 30 | "class Vehicle:\n", 31 | " def __init__(self, name_of_vehicle, max_speed, average_of_vehicle):\n", 32 | " self.name_of_vehicle = name_of_vehicle\n", 33 | " self.max_speed = max_speed\n", 34 | " self.average_of_vehicle = average_of_vehicle\n", 35 | " \n", 36 | "car = Vehicle(\"Car\", 200, 20)\n", 37 | "print(car.name_of_vehicle) # \"Car\"\n", 38 | "print(car.max_speed) # 200\n", 39 | "print(car.average_of_vehicle) # 20\n" 40 | ] 41 | }, 42 | { 43 | "attachments": {}, 44 | "cell_type": "markdown", 45 | "id": "5c81f96c", 46 | "metadata": {}, 47 | "source": [ 48 | "'''Q.2> Create a child class car from the vehicle class created in Que 1, which will inherit the vehicle class. \n", 49 | " Create a method named seating_capacity which takes capacity as an argument and returns the name of \n", 50 | " the vehicle and its seating capacity.'''" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 7, 56 | "id": "b8135b24-a6de-4fb0-828f-485199af27cc", 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "The My Car has a seating capacity of 5.\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "\n", 69 | "class Car(Vehicle):\n", 70 | " def seating_capacity(self, capacity):\n", 71 | " return f\"The {self.name_of_vehicle} has a seating capacity of {capacity}.\"\n", 72 | " \n", 73 | "my_car = Car(\"My Car\", 150, 15)\n", 74 | "print(my_car.seating_capacity(5)) # \"The My Car has a seating capacity of 5.\"" 75 | ] 76 | }, 77 | { 78 | "attachments": {}, 79 | "cell_type": "markdown", 80 | "id": "6cf6590d", 81 | "metadata": {}, 82 | "source": [ 83 | "Q.3> What is multiple inheritance? Write a python code to demonstrate multiple inheritance." 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 2, 89 | "id": "25892f80-dce9-4992-af62-c92c98c20844", 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "name": "stdout", 94 | "output_type": "stream", 95 | "text": [ 96 | "Engine started.\n", 97 | "Fuel tank filled with 50 liters.\n", 98 | "Car is driving.\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "'''\n", 104 | "Ans> Multiple inheritance is a feature of object-oriented programming that allows a class to inherit \n", 105 | " attributes and behaviors from multiple parent classes. This allows for greater flexibility and \n", 106 | " code reuse, as a single class can inherit the characteristics of multiple existing classes.'''\n", 107 | "class Engine:\n", 108 | " def start(self):\n", 109 | " return \"Engine started.\"\n", 110 | "\n", 111 | "class FuelSystem:\n", 112 | " def fill_fuel(self, fuel_amount):\n", 113 | " return f\"Fuel tank filled with {fuel_amount} liters.\"\n", 114 | "\n", 115 | "class Car(Engine, FuelSystem):\n", 116 | " def drive(self):\n", 117 | " return \"Car is driving.\"\n", 118 | "\n", 119 | "my_car = Car()\n", 120 | "print(my_car.start()) # \"Engine started.\"\n", 121 | "print(my_car.fill_fuel(50)) # \"Fuel tank filled with 50 liters.\"\n", 122 | "print(my_car.drive()) # \"Car is driving.\"" 123 | ] 124 | }, 125 | { 126 | "attachments": {}, 127 | "cell_type": "markdown", 128 | "id": "cae54235", 129 | "metadata": {}, 130 | "source": [ 131 | "Q.4> What are getter and setter in python? Create a class and create a getter and a setter method in this class" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 3, 137 | "id": "fbaa0070-77c2-49dd-865d-b0ed56f895fa", 138 | "metadata": {}, 139 | "outputs": [ 140 | { 141 | "name": "stdout", 142 | "output_type": "stream", 143 | "text": [ 144 | "John Doe\n", 145 | "Jane Doe\n", 146 | "30\n", 147 | "35\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "'''\n", 153 | "Ans> Getters and setters are methods in object-oriented programming that allow you to control access to the \n", 154 | " attributes of an object. A getter method is used to retrieve the value of an attribute, while a setter \n", 155 | " method is used to set the value of an attribute.'''\n", 156 | "class Person:\n", 157 | " def __init__(self, name, age):\n", 158 | " self._name = name\n", 159 | " self._age = age\n", 160 | "\n", 161 | " @property\n", 162 | " def name(self):\n", 163 | " return self._name\n", 164 | "\n", 165 | " @name.setter\n", 166 | " def name(self, value):\n", 167 | " if not isinstance(value, str):\n", 168 | " raise TypeError(\"Name must be a string.\")\n", 169 | " self._name = value\n", 170 | "\n", 171 | " @property\n", 172 | " def age(self):\n", 173 | " return self._age\n", 174 | "\n", 175 | " @age.setter\n", 176 | " def age(self, value):\n", 177 | " if not isinstance(value, int):\n", 178 | " raise TypeError(\"Age must be an integer.\")\n", 179 | " self._age = value\n", 180 | "\n", 181 | "person = Person(\"John Doe\", 30)\n", 182 | "print(person.name) # \"John Doe\"\n", 183 | "person.name = \"Jane Doe\"\n", 184 | "print(person.name) # \"Jane Doe\"\n", 185 | "print(person.age) # 30\n", 186 | "person.age = 35\n", 187 | "print(person.age) # 35" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "id": "f818124a-9ea8-418d-a181-46d6da966977", 194 | "metadata": {}, 195 | "outputs": [], 196 | "source": [] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": null, 201 | "id": "ec030991", 202 | "metadata": {}, 203 | "outputs": [], 204 | "source": [] 205 | } 206 | ], 207 | "metadata": { 208 | "kernelspec": { 209 | "display_name": "Python 3", 210 | "language": "python", 211 | "name": "python3" 212 | }, 213 | "language_info": { 214 | "codemirror_mode": { 215 | "name": "ipython", 216 | "version": 3 217 | }, 218 | "file_extension": ".py", 219 | "mimetype": "text/x-python", 220 | "name": "python", 221 | "nbconvert_exporter": "python", 222 | "pygments_lexer": "ipython3", 223 | "version": "3.9.0" 224 | }, 225 | "vscode": { 226 | "interpreter": { 227 | "hash": "bf9ab843c2c44a0abbe96a3f5fa8fc8f7b5aa1503133f196762388041b9614c2" 228 | } 229 | } 230 | }, 231 | "nbformat": 4, 232 | "nbformat_minor": 5 233 | } 234 | -------------------------------------------------------------------------------- /Python-Assignment/Python_Assignment_9.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "28db60b2-d66c-425c-924a-ba4445709b89", 6 | "metadata": {}, 7 | "source": [ 8 | "1. What is Abstraction in OOps?\n" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "8d2e2dba-7af7-43c5-8c85-13cd65336ea8", 14 | "metadata": {}, 15 | "source": [ 16 | "Abstraction in python is defined as a process of handling complexity by hiding unnecessary information from the user. This is one of the core concepts of object-oriented programming (OOP) languages. That enables the user to implement even more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden background/back-end complexity.\n", 17 | "\n", 18 | "Example" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 1, 24 | "id": "4fa288b3-8261-412f-b6bd-22804db42a2a", 25 | "metadata": {}, 26 | "outputs": [ 27 | { 28 | "name": "stdout", 29 | "output_type": "stream", 30 | "text": [ 31 | "The mileage is 30kmph\n", 32 | "The mileage is 27kmph \n", 33 | "The mileage is 25kmph \n", 34 | "The mileage is 24kmph \n" 35 | ] 36 | } 37 | ], 38 | "source": [ 39 | "from abc import ABC, abstractmethod \n", 40 | "class Car(ABC): \n", 41 | " def mileage(self): \n", 42 | " pass \n", 43 | " \n", 44 | "class Tesla(Car): \n", 45 | " def mileage(self): \n", 46 | " print(\"The mileage is 30kmph\") \n", 47 | "class Suzuki(Car): \n", 48 | " def mileage(self): \n", 49 | " print(\"The mileage is 25kmph \") \n", 50 | "class Duster(Car): \n", 51 | " def mileage(self): \n", 52 | " print(\"The mileage is 24kmph \") \n", 53 | " \n", 54 | "class Renault(Car): \n", 55 | " def mileage(self): \n", 56 | " print(\"The mileage is 27kmph \") \n", 57 | " \n", 58 | "# Driver code \n", 59 | "t= Tesla () \n", 60 | "t.mileage() \n", 61 | " \n", 62 | "r = Renault() \n", 63 | "r.mileage() \n", 64 | " \n", 65 | "s = Suzuki() \n", 66 | "s.mileage() \n", 67 | "d = Duster() \n", 68 | "d.mileage() " 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "id": "d214eab3-d09d-492e-8299-93fabb295b5e", 74 | "metadata": {}, 75 | "source": [ 76 | "2. Differentiate between Abstraction and Encapsulation. Explain with an example.\n" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "id": "091486d9-6154-45bf-8155-62c15090fa46", 82 | "metadata": {}, 83 | "source": [ 84 | "Abstraction is the process or method of gaining the information.\tWhile encapsulation is the process or method to contain the information.\n", 85 | "In abstraction, problems are solved at the design or interface level.\tWhile in encapsulation, problems are solved at the implementation level." 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "id": "9c0b3abf-813a-40d8-9a56-baf8ec54e338", 91 | "metadata": {}, 92 | "source": [ 93 | "3. What is abc module in python? Why is it used?" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "id": "e920c7a4-3dc9-456b-8656-95a7ac436468", 99 | "metadata": {}, 100 | "source": [ 101 | "The abc module in Python is the Abstract Base Class module. It is used to define abstract base classes, which provide a way to define interfaces in Python. An abstract base class is a class that cannot be instantiated, but can be subclassed. The purpose of an abstract base class is to specify the interface that its subclasses must implement, without specifying how that implementation should be done.\n", 102 | "\n", 103 | "The abc module provides a way to create abstract base classes in Python by using the ABC class, as well as several decorators and methods to define abstract methods and properties." 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "id": "b4f8320b-b650-490f-b963-e7335778ed39", 109 | "metadata": {}, 110 | "source": [ 111 | "4. How can we achieve data abstraction?" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "id": "147f2ec2-e987-4ea7-b593-1c77652727d3", 117 | "metadata": {}, 118 | "source": [ 119 | "Data abstraction is a programming concept that allows you to define the interface of an object without revealing the underlying implementation details. In other words, it provides a way to separate the implementation details of an object from the way it is used by other objects.\n", 120 | "\n", 121 | "There are several ways to achieve data abstraction in programming:\n", 122 | "\n", 123 | "Abstraction through classes: One of the most common ways to achieve data abstraction is through the use of classes. In object-oriented programming, classes provide a way to define objects with certain properties and methods, but hide the underlying implementation details from the rest of the code. For example, you can define a class with public methods that expose its behavior, but keep the implementation details private.\n", 124 | "\n", 125 | "Interfaces: An interface is a collection of abstract methods that must be implemented by any class that implements the interface. Interfaces are used to define a common set of behaviors that can be implemented by different classes, without specifying the implementation details. For example, in Java, you can define an interface that specifies the methods that must be implemented by a class that implements the interface.\n", 126 | "\n", 127 | "Modules: In some programming languages, like Python, you can use modules to define a set of related functions and variables, and hide the implementation details behind a well-defined interface. Modules provide a way to encapsulate the implementation details of a set of functions, and expose them to the rest of the code through a simple API.\n", 128 | "\n", 129 | "Information hiding: Information hiding is a programming concept that involves hiding the implementation details of an object or function behind a well-defined interface. Information hiding is used to protect the internal state of an object from being modified by external code, and to provide a clear and well-defined way for other objects to interact with the object." 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "id": "157a62e9-4b66-4eef-9961-ddd5c84cdd95", 135 | "metadata": {}, 136 | "source": [ 137 | "5. Can we create an instance of an abstract class? Explain your answer." 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "id": "6f19a55f-7343-4c78-810a-8e17b03371e6", 143 | "metadata": {}, 144 | "source": [ 145 | "No, you cannot create an instance of an abstract class. An abstract class is a class that cannot be instantiated on its own, but is intended to be subclassed and have its abstract methods implemented by the subclasses. The purpose of an abstract class is to provide a common interface for a set of related classes, but not to be used as a standalone object." 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "id": "0c3abc07-7053-4981-8c4c-85c73e53d8ea", 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [] 155 | } 156 | ], 157 | "metadata": { 158 | "kernelspec": { 159 | "display_name": "Python 3 (ipykernel)", 160 | "language": "python", 161 | "name": "python3" 162 | }, 163 | "language_info": { 164 | "codemirror_mode": { 165 | "name": "ipython", 166 | "version": 3 167 | }, 168 | "file_extension": ".py", 169 | "mimetype": "text/x-python", 170 | "name": "python", 171 | "nbconvert_exporter": "python", 172 | "pygments_lexer": "ipython3", 173 | "version": "3.10.8" 174 | } 175 | }, 176 | "nbformat": 4, 177 | "nbformat_minor": 5 178 | } 179 | -------------------------------------------------------------------------------- /Numpy/Numpy_Assignment.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "d8aa293c-ab84-4937-acd3-d3e963931559", 6 | "metadata": {}, 7 | "source": [ 8 | "# Consider the below code to answer further questions:" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 7, 14 | "id": "6aee910d-be2b-4e64-bc40-20b292e1db23", 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import numpy as np\n", 19 | "list_ = [ '1' , '2', '3' , '4' , '5' ]\n", 20 | "array_list = np.array(object = list_)" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "182f2d2e-82cf-4d7c-95ec-6e4c15aaa505", 26 | "metadata": {}, 27 | "source": [ 28 | "# Q1. Is there any difference in the data type of variables list_ and array_list?If there is then write a code to print the data types of both the variables." 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 8, 34 | "id": "38076539-a4e4-4416-8e2d-8b597d0e578b", 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "\n", 42 | "\n" 43 | ] 44 | } 45 | ], 46 | "source": [ 47 | "'''Yes, there is a difference in the data type of variables list_ and array_list. list_ is a Python list object while array_list \n", 48 | " is a NumPy array object.'''\n", 49 | "# printing data types of both variables\n", 50 | "print(type(list_)) \n", 51 | "print(type(array_list))" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "id": "b477c548-bcc4-4dc2-b9ab-9ddcb5d359dd", 57 | "metadata": {}, 58 | "source": [ 59 | "# Q2. Write a code to print the data type of each and every element of both the variables list_ and array_list." 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 9, 65 | "id": "2dd3501a-8172-4082-bdc7-cd0e52fcd88f", 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "name": "stdout", 70 | "output_type": "stream", 71 | "text": [ 72 | "\n", 73 | "\n", 74 | "\n", 75 | "\n", 76 | "\n", 77 | "\n", 78 | "\n", 79 | "\n", 80 | "\n", 81 | "\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "# Print data type of each element in list_\n", 87 | "for element in list_:\n", 88 | " print(type(element))\n", 89 | "\n", 90 | "# Print data type of each element in array_list\n", 91 | "for element in array_list:\n", 92 | " print(type(element))" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "id": "a4a48d61-43f0-47e2-9b2c-bd2a89d23d12", 98 | "metadata": {}, 99 | "source": [ 100 | "# Q3. Considering the following changes in the variable, array_list:\n", 101 | "array_list = np.array(object = list_, dtype = int)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 16, 107 | "id": "a3af6cc9-128a-40a0-a9d1-330ae4f63c8b", 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "\n", 115 | "\n", 116 | "\n", 117 | "\n", 118 | "\n", 119 | "\n", 120 | "\n", 121 | "\n", 122 | "\n", 123 | "\n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "array_list = np.array(object = list_ , dtype = int)\n", 129 | "for element in list_:\n", 130 | " print(type(element))\n", 131 | "for element in array_list:\n", 132 | " print(type(element))\n" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "id": "4ea1b805-03f3-4996-b210-d71d5077e2ea", 138 | "metadata": {}, 139 | "source": [ 140 | "# Consider the below code to answer further questions:" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 17, 146 | "id": "3d831813-49c3-486b-aef5-b08a2cea01c1", 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "import numpy as np\n", 151 | "num_list = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ]\n", 152 | "num_array = np.array(object = num_list)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "id": "24bf6815-f5d0-4c8d-950e-6c1dc9f9ecef", 158 | "metadata": {}, 159 | "source": [ 160 | "# Q4. Write a code to find the following characteristics of variable, num_array:\n", 161 | "(i) shape\n", 162 | "(ii) size" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 18, 168 | "id": "1cc50e23-e74b-45d2-a5c6-076ecb7597fb", 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "(2, 3)\n", 176 | "6\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "# Print the shape of num_array\n", 182 | "print(num_array.shape)\n", 183 | "\n", 184 | "# Print the size of num_array\n", 185 | "print(num_array.size)" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "id": "74f07faa-8f2f-488e-922a-3e2cc95355cf", 191 | "metadata": {}, 192 | "source": [ 193 | "# Q5. Write a code to create numpy array of 3*3 matrix containing zeros only, using a numpy array creation function." 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 29, 199 | "id": "47c4583a-fd05-4061-851e-933007aae163", 200 | "metadata": {}, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "[[0. 0. 0.]\n", 207 | " [0. 0. 0.]\n", 208 | " [0. 0. 0.]]\n" 209 | ] 210 | } 211 | ], 212 | "source": [ 213 | "# Creating a 3*3 matrix containing zeros only \n", 214 | "zeros_matrix = np.zeros((3,3))\n", 215 | "print(zeros_matrix)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "id": "3dee836a-849c-42f8-bc3f-9590e0568536", 221 | "metadata": {}, 222 | "source": [ 223 | "# Q6. Create an identity matrix of shape (5,5) using numpy functions?" 224 | ] 225 | }, 226 | { 227 | "cell_type": "code", 228 | "execution_count": 28, 229 | "id": "fc4c58f1-3dfa-4282-a55b-6be3d260d4d6", 230 | "metadata": {}, 231 | "outputs": [ 232 | { 233 | "name": "stdout", 234 | "output_type": "stream", 235 | "text": [ 236 | "[[1. 0. 0. 0. 0.]\n", 237 | " [0. 1. 0. 0. 0.]\n", 238 | " [0. 0. 1. 0. 0.]\n", 239 | " [0. 0. 0. 1. 0.]\n", 240 | " [0. 0. 0. 0. 1.]]\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "# Creating a identity matrix of shape (5,5)\n", 246 | "identity_matrix = np.eye(5)\n", 247 | "print(identity_matrix)" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "id": "3ced3fed-7679-42ce-b072-f650fedea127", 254 | "metadata": {}, 255 | "outputs": [], 256 | "source": [] 257 | } 258 | ], 259 | "metadata": { 260 | "kernelspec": { 261 | "display_name": "Python 3 (ipykernel)", 262 | "language": "python", 263 | "name": "python3" 264 | }, 265 | "language_info": { 266 | "codemirror_mode": { 267 | "name": "ipython", 268 | "version": 3 269 | }, 270 | "file_extension": ".py", 271 | "mimetype": "text/x-python", 272 | "name": "python", 273 | "nbconvert_exporter": "python", 274 | "pygments_lexer": "ipython3", 275 | "version": "3.10.8" 276 | } 277 | }, 278 | "nbformat": 4, 279 | "nbformat_minor": 5 280 | } 281 | -------------------------------------------------------------------------------- /Machine_Learning_Algorithm/SVM/SVM_Assignmnet_3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "edde7563-73a9-43d0-a905-da5ad53f7db9", 6 | "metadata": {}, 7 | "source": [ 8 | "**Q1. In order to predict house price based on several characteristics, such as location, square footage, number of bedrooms, etc., you are developing an SVM regression model. Which regression metric in this situation would be the best to employ?**" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "dc055c63-287a-4845-a0a5-8bc36e0feb17", 14 | "metadata": {}, 15 | "source": [ 16 | "In regression problems, Mean Squared Error (MSE) and Root Mean Squared Error (RMSE) are common evaluation metrics. They measure the average squared difference between the predicted values and the actual values.\n", 17 | "\n", 18 | "In the context of predicting house prices, RMSE is a more suitable metric to use as it provides an interpretable measure of the average error in dollars. This is particularly important because the goal is to predict house prices accurately, and RMSE provides an intuitive understanding of how well the model is performing in that respect.\n", 19 | "\n", 20 | "Another useful metric for regression problems is R-squared, which measures the proportion of the variance in the target variable that is explained by the model. However, in the case of SVM regression, where the model is optimized for minimizing the error, RMSE is more commonly used." 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "2a713cdd-e978-4e0e-ad50-6f0fb40277d4", 26 | "metadata": {}, 27 | "source": [ 28 | "**Q2. You have built an SVM regression model and are trying to decide between using MSE or R-squared as your evaluation metric. Which metric would be more appropriate if your goal is to predict the actual price of a house as accurately as possible?**" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "id": "cd04dcf1-e4aa-4f3f-80b1-d4b6ec9af98e", 34 | "metadata": {}, 35 | "source": [ 36 | "If your goal is to predict the actual price of a house as accurately as possible, then Mean Squared Error (MSE) would be the more appropriate evaluation metric to use.\n", 37 | "\n", 38 | "MSE measures the average squared difference between the predicted values and the actual values. In the context of predicting house prices, MSE provides an intuitive understanding of how far the predictions are, on average, from the actual prices in terms of dollars squared. This is important because the goal is to minimize the error in predicting house prices as much as possible.\n", 39 | "\n", 40 | "On the other hand, R-squared measures the proportion of the variance in the target variable that is explained by the model. While R-squared can be a useful metric for understanding how well the model fits the data, it is not as directly related to the accuracy of individual predictions as MSE." 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "id": "e4bdba5f-1ec2-4791-b606-400214cf113a", 46 | "metadata": {}, 47 | "source": [ 48 | "**Q3. You have a dataset with a significant number of outliers and are trying to select an appropriate regression metric to use with your SVM model. Which metric would be the most appropriate in this scenario?**" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "id": "d972a86f-30d5-4ff7-ae44-0b967e01c2ef", 54 | "metadata": {}, 55 | "source": [ 56 | "When dealing with a dataset that contains a significant number of outliers, Mean Absolute Error (MAE) is a more appropriate metric to use with an SVM regression model than Mean Squared Error (MSE).\n", 57 | "\n", 58 | "The reason for this is that MSE is more sensitive to outliers since it squares the errors, making the contribution of large errors to the overall error much larger than that of smaller errors. On the other hand, MAE measures the absolute difference between the predicted values and the actual values, and is therefore less sensitive to outliers.\n", 59 | "\n", 60 | "Using MAE as the evaluation metric for the SVM regression model would ensure that the model is evaluated based on its performance in predicting the actual price of the house, regardless of any outliers that may be present in the dataset." 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "id": "a3d04cb0-9811-45bf-a095-2f71668fe9aa", 66 | "metadata": {}, 67 | "source": [ 68 | "**Q4. You have built an SVM regression model using a polynomial kernel and are trying to select the best metric to evaluate its performance. You have calculated both MSE and RMSE and found that both values are very close. Which metric should you choose to use in this case?**" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "id": "c057bec0-a806-41fb-a49e-6ee224f3e3fb", 74 | "metadata": {}, 75 | "source": [ 76 | "When evaluating the performance of an SVM regression model that uses a polynomial kernel, if the values of Mean Squared Error (MSE) and Root Mean Squared Error (RMSE) are very close, then Root Mean Squared Error (RMSE) should be used as the evaluation metric.\n", 77 | "\n", 78 | "The reason for this is that RMSE is more interpretable since it is expressed in the same units as the target variable. In the case of predicting house prices, RMSE would be expressed in dollars, which makes it easier to understand how well the model is performing in terms of its predictive accuracy.\n", 79 | "\n", 80 | "Additionally, RMSE is less sensitive to the scale of the target variable than MSE. This means that even if the scale of the target variable changes, the RMSE values would still be comparable across different models.\n", 81 | "\n", 82 | "Therefore, in the scenario where MSE and RMSE are very close for an SVM regression model with a polynomial kernel, using RMSE as the evaluation metric would be the better choice." 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "id": "ea13e4b5-351b-4a14-9595-02ee411861b5", 88 | "metadata": {}, 89 | "source": [ 90 | "**Q5. You are comparing the performance of different SVM regression models using different kernels (linear, polynomial, and RBF) and are trying to select the best evaluation metric. Which metric would be most appropriate if your goal is to measure how well the model explains the variance in the target variable?**" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "id": "eee59052-0099-48a2-8443-a3415e3fdd77", 96 | "metadata": {}, 97 | "source": [ 98 | "When comparing the performance of different SVM regression models using different kernels, if the goal is to measure how well the model explains the variance in the target variable, then R-squared (R2) would be the most appropriate evaluation metric to use.\n", 99 | "\n", 100 | "R-squared measures the proportion of the variance in the target variable that is explained by the model. A higher R-squared value indicates that the model is explaining more of the variance in the target variable and is therefore a better fit for the data.\n", 101 | "\n", 102 | "Since the goal is to measure how well the model explains the variance in the target variable, R-squared would provide an intuitive understanding of how well each model is performing in that respect. It would also allow for easy comparison between the different SVM regression models, regardless of the kernel used.\n", 103 | "\n", 104 | "Therefore, R-squared would be the most appropriate evaluation metric to use when comparing the performance of different SVM regression models using different kernels, with the goal of measuring how well the model explains the variance in the target variable." 105 | ] 106 | } 107 | ], 108 | "metadata": { 109 | "kernelspec": { 110 | "display_name": "Python 3 (ipykernel)", 111 | "language": "python", 112 | "name": "python3" 113 | }, 114 | "language_info": { 115 | "codemirror_mode": { 116 | "name": "ipython", 117 | "version": 3 118 | }, 119 | "file_extension": ".py", 120 | "mimetype": "text/x-python", 121 | "name": "python", 122 | "nbconvert_exporter": "python", 123 | "pygments_lexer": "ipython3", 124 | "version": "3.10.8" 125 | } 126 | }, 127 | "nbformat": 4, 128 | "nbformat_minor": 5 129 | } 130 | -------------------------------------------------------------------------------- /Flask_and_WebAPI/WebApi.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ea91915c-3f6c-4096-9d92-cb81a0d415d6", 6 | "metadata": {}, 7 | "source": [ 8 | "Q1. What is an API? Give an example, where an API is used in real life." 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "bdfa32f2-8530-404d-8cbf-bc4c2e59b7d2", 14 | "metadata": {}, 15 | "source": [ 16 | "An API, or Application Programming Interface, is a set of protocols, routines, and tools that allow different software applications to communicate with each other. APIs provide a standard way for applications to interact and exchange information, which makes it easier for developers to integrate different services and functionalities into their own applications.\n", 17 | "\n", 18 | "One real-life example of an API is the Google Maps API, which allows developers to embed Google Maps into their own applications and websites. By using the Google Maps API, developers can add maps and location-based features to their applications without having to build the maps themselves from scratch. This saves time and resources, and also ensures that the maps are accurate and up-to-date.\n", 19 | "\n", 20 | "Another example of an API is the Twitter API, which allows developers to access Twitter data and functionality, such as retrieving tweets, posting tweets, and searching for tweets based on specific criteria. This API has been used to build a variety of Twitter-related applications, including social media monitoring tools, sentiment analysis tools, and Twitter bots.\n", 21 | "\n", 22 | "Overall, APIs are a critical component of modern software development and enable applications to communicate and work together in a seamless way." 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "id": "90b032a0-6c8c-42a2-85e5-bfedd519d99b", 28 | "metadata": {}, 29 | "source": [ 30 | "Q2. Give advantages and disadvantages of using API." 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "id": "d8ca6a8a-c70e-4652-b4a7-771b94393d50", 36 | "metadata": {}, 37 | "source": [ 38 | "Increased efficiency: APIs can increase efficiency by allowing developers to reuse code and functionalities that have already been built by others. This saves time and resources and can speed up the development process.\n", 39 | "\n", 40 | "Standardization: APIs provide a standardized way for applications to communicate with each other, which makes it easier to integrate different services and functionalities into an application.\n", 41 | "\n", 42 | "Scalability: APIs can help to scale an application by allowing it to handle a large number of requests from different sources. By separating the frontend from the backend, APIs enable developers to scale the backend independently of the frontend.\n", 43 | "\n", 44 | "Improved user experience: APIs can improve the user experience by allowing applications to access data and functionality from other sources. For example, integrating Google Maps into an application can provide users with a better mapping experience than if the application had built its own mapping system.\n", 45 | "\n", 46 | "Innovation: APIs can enable innovation by providing developers with access to new technologies and functionalities that they may not have been able to build themselves." 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "b0cc4fc1-c54e-478c-8a0e-b39308b4a4c4", 52 | "metadata": {}, 53 | "source": [ 54 | "Q3. What is a Web API? Differentiate between API and Web API." 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "id": "d554f604-3791-43f1-a83f-0cf7ec14f176", 60 | "metadata": {}, 61 | "source": [ 62 | "A Web API, or Web Application Programming Interface, is a type of API that is specifically designed to be used over the internet, using the HTTP protocol. A Web API typically uses standard web technologies, such as XML or JSON, to format and transfer data.\n", 63 | "\n", 64 | "The main difference between an API and a Web API is that an API is a more general term that can refer to any type of interface that enables communication between different software systems, while a Web API specifically refers to an API that is accessed over the web using HTTP." 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "id": "acc1e3be-0819-46f5-9305-477ea3bb462e", 70 | "metadata": {}, 71 | "source": [ 72 | "Q4. Explain REST and SOAP Architecture. Mention shortcomings of SOAP." 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "id": "231136fa-a935-4e7a-ae5a-f9fb651ba1c5", 78 | "metadata": {}, 79 | "source": [ 80 | "REST (Representational State Transfer) and SOAP (Simple Object Access Protocol) are two common architectures used for building web services.\n", 81 | "\n", 82 | "REST is a lightweight and flexible architecture that uses HTTP protocols to access resources and transfer data in different formats, such as JSON or XML. RESTful APIs have gained popularity due to their simplicity, scalability, and compatibility with various programming languages and platforms. They follow a stateless client-server architecture where the server provides a set of URLs (Uniform Resource Locators) that can be accessed by clients to perform specific operations.\n", 83 | "\n", 84 | "SOAP, on the other hand, is a more complex architecture that uses XML messaging over HTTP or other protocols to exchange data between applications. SOAP APIs typically follow a more rigid structure and use a set of rules to define message formats and procedures. It is designed to work with distributed enterprise-level systems and provides security and error handling features." 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "id": "e51fada9-95fd-452b-a62c-06ddcee1f218", 90 | "metadata": {}, 91 | "source": [ 92 | "Q5. Differentiate between REST and SOAP." 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "id": "d2ed42ca-791d-4fa5-88c0-1b5abda8e7c6", 98 | "metadata": {}, 99 | "source": [ 100 | "REST (Representational State Transfer) and SOAP (Simple Object Access Protocol) are two common architectures used for building web services. Here are some key differences between the two:\n", 101 | "\n", 102 | "Data format: REST typically uses lightweight data formats like JSON or XML, while SOAP uses XML exclusively.\n", 103 | "\n", 104 | "Message structure: REST uses simple HTTP protocols for exchanging data, while SOAP uses a more complex messaging protocol that includes a message envelope, header, and body.\n", 105 | "\n", 106 | "Statelessness: REST is stateless, meaning that each request contains all the information necessary for the server to respond, while SOAP is stateful, meaning that the server maintains the state of the request and response.\n", 107 | "\n", 108 | "Scalability: REST is more scalable and easier to implement than SOAP due to its simpler message structure and statelessness.\n", 109 | "\n", 110 | "Security: SOAP has built-in security features like WS-Security, while REST relies on HTTPS and other security protocols to ensure secure communication.\n", 111 | "\n", 112 | "Performance: REST is generally faster and has better performance than SOAP due to its lightweight data formats and stateless architecture.\n", 113 | "\n", 114 | "Overall, REST is a simpler and more flexible architecture, making it a popular choice for building web services. SOAP, on the other hand, is better suited for complex enterprise-level systems that require more robust security and messaging features." 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "id": "0bf6129b-3d7f-4dec-9162-1e7d64959bb4", 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [] 124 | } 125 | ], 126 | "metadata": { 127 | "kernelspec": { 128 | "display_name": "Python 3 (ipykernel)", 129 | "language": "python", 130 | "name": "python3" 131 | }, 132 | "language_info": { 133 | "codemirror_mode": { 134 | "name": "ipython", 135 | "version": 3 136 | }, 137 | "file_extension": ".py", 138 | "mimetype": "text/x-python", 139 | "name": "python", 140 | "nbconvert_exporter": "python", 141 | "pygments_lexer": "ipython3", 142 | "version": "3.10.8" 143 | } 144 | }, 145 | "nbformat": 4, 146 | "nbformat_minor": 5 147 | } 148 | -------------------------------------------------------------------------------- /Python-Assignment/Python_Assignment_11.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "80fda792-c5fb-466a-87df-03268a2a0b31", 6 | "metadata": {}, 7 | "source": [ 8 | "1. Which function is used to open a file? What are the different modes of opening a file? Explain each mode of file opening." 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "a7ae422e-1250-405f-9232-b324dd821230", 14 | "metadata": {}, 15 | "source": [ 16 | "The open() function is used to open a file in Python. When you open a file, you can specify the mode in which you want to open it. The different modes of opening a file in Python are:\n", 17 | "\n", 18 | "'r' (read mode) - This mode is used when you only want to read the contents of a file. When you open a file in read mode, you cannot write to it or make any changes to it. If the file does not exist, a FileNotFoundError will be raised.\n", 19 | "\n", 20 | "'w' (write mode) - This mode is used when you want to write to a file. If the file already exists, its contents will be overwritten. If the file does not exist, a new file will be created.\n", 21 | "\n", 22 | "'a' (append mode) - This mode is used when you want to add new content to the end of an existing file. If the file does not exist, a new file will be created. In append mode, you can only write to the end of the file, and not anywhere else.\n", 23 | "\n", 24 | "'x' (exclusive creation mode) - This mode is used when you want to create a new file and write to it, but only if the file does not already exist. If the file already exists, a FileExistsError will be raised.\n", 25 | "\n", 26 | "'b' (binary mode) - This mode is used when you want to read or write binary data to a file. When opening a file in binary mode, you should use 'rb' to read binary data and 'wb' to write binary data.\n", 27 | "\n", 28 | "'t' (text mode) - This mode is used when you want to read or write text data to a file. When opening a file in text mode, you should use 'rt' to read text data and 'wt' to write text data.\n", 29 | "\n", 30 | "You can combine these modes when opening a file, for example: 'rb' to open a binary file in read mode, 'wt' to open a text file in write mode, etc. The default mode is 'rt' (read text mode) if no mode is specified." 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "id": "07c84a11-94b7-4cd2-abda-bd25a28e7bba", 36 | "metadata": {}, 37 | "source": [ 38 | "2. Why close() function is used? Why is it important to close a file?" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "81f7d3d9-2c3d-4210-85bc-ccec6b7896ee", 44 | "metadata": {}, 45 | "source": [ 46 | "The close() function is used to close an open file. When a file is opened in Python using the open function, the operating system reserves some resources to handle the file. This can include memory, disk space, file handles, etc. When you are done with a file and no longer need to read from or write to it, it is important to close it so that these resources can be released and made available for other applications.\n", 47 | "\n", 48 | "In addition to freeing up resources, closing a file also ensures that any changes you made to the file are saved and written to disk. This makes it a good practice to close a file after you have finished using it.\n", 49 | "\n", 50 | "If you forget to close a file, the operating system will eventually release the resources that were reserved for it when the Python process ends. However, it's still a good idea to close files explicitly, as it makes the intent of your code more clear and reduces the chance of resource leaks or other unintended consequences." 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "id": "0f0888d1-55f5-44d3-bae9-ec5a01f0f10e", 56 | "metadata": {}, 57 | "source": [ 58 | "3. Write a python program to create a text file. Write ‘I want to become a Data Scientist’ in that file. Then close the file. Open this file and read the content of the file." 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 1, 64 | "id": "5cc23e6a-e570-4c85-9cb9-5b56402034c1", 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "I want to become a Data Scientist\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "# create a text file\n", 77 | "file = open(\"textfile.txt\", \"w\")\n", 78 | "\n", 79 | "# write to the file\n", 80 | "file.write(\"I want to become a Data Scientist\")\n", 81 | "\n", 82 | "# close the file\n", 83 | "file.close()\n", 84 | "\n", 85 | "# open the file in read mode\n", 86 | "file = open(\"textfile.txt\", \"r\")\n", 87 | "\n", 88 | "# read the contents of the file\n", 89 | "contents = file.read()\n", 90 | "\n", 91 | "# print the contents of the file\n", 92 | "print(contents)\n", 93 | "\n", 94 | "# close the file\n", 95 | "file.close()" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "id": "3c326c4f-8bae-4ec8-b9d5-66d52394b2d7", 101 | "metadata": {}, 102 | "source": [ 103 | "4. Explain the following with python code: read(), readline() and readlines()." 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "id": "1f4bfd37-b2c2-43e9-b094-0d62978558c2", 109 | "metadata": {}, 110 | "source": [ 111 | "read(): The read() method reads the entire contents of a file and returns it as a single string\n", 112 | "\n", 113 | "readline(): The readline() method reads one line of the file at a time and returns it as a string.\n", 114 | "\n", 115 | "readlines(): The readlines() method reads all the lines of the file and returns them as a list of strings, where each string is a line of the file" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "id": "a3ec96e7-73d4-43f2-a5dd-39e2321234b6", 121 | "metadata": {}, 122 | "source": [ 123 | "5. Explain why with statement is used with open(). What is the advantage of using with statement and open() together?" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "id": "a9841100-4994-46d2-9290-a59fa15556ac", 129 | "metadata": {}, 130 | "source": [ 131 | "In Python, the with statement is often used with the open function to handle files. The with statement provides a convenient way to manage the resources that a program uses, including files.\n", 132 | "\n", 133 | "When you open a file with the open function, you need to close the file after you're done with it. If you forget to close the file, it can cause problems, such as data corruption or resource leaks.\n", 134 | "\n", 135 | "The with statement provides a convenient way to ensure that resources are properly managed. When you use the with statement with the open function, you don't have to worry about manually closing the file. The with statement automatically closes the file for you, even if an exception is raised within the with block." 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "id": "e7edca03-7917-4dda-8995-aa80b69ec091", 141 | "metadata": {}, 142 | "source": [ 143 | "6. Explain the write() and writelines() functions. Give a suitable example." 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "id": "234b1e16-4cee-4b76-a61e-9ae8ed7408a7", 149 | "metadata": {}, 150 | "source": [ 151 | "write(): The write() function is used to write a string to a file.\n", 152 | "\n", 153 | "writelines(): The writelines() function is used to write a list of strings to a file. " 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "id": "acdf1bb9-2a72-4a8d-9673-66aba46a5ff6", 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [] 163 | } 164 | ], 165 | "metadata": { 166 | "kernelspec": { 167 | "display_name": "Python 3 (ipykernel)", 168 | "language": "python", 169 | "name": "python3" 170 | }, 171 | "language_info": { 172 | "codemirror_mode": { 173 | "name": "ipython", 174 | "version": 3 175 | }, 176 | "file_extension": ".py", 177 | "mimetype": "text/x-python", 178 | "name": "python", 179 | "nbconvert_exporter": "python", 180 | "pygments_lexer": "ipython3", 181 | "version": "3.10.8" 182 | } 183 | }, 184 | "nbformat": 4, 185 | "nbformat_minor": 5 186 | } 187 | -------------------------------------------------------------------------------- /Python-Assignment/Python_Assignment_3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "2a92dc28-7da2-418d-a096-0c459b7a860e", 6 | "metadata": {}, 7 | "source": [ 8 | "Q1. Explain with an example each when to use a for loop and a while loop." 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 5, 14 | "id": "06ffbe49-c8f0-4f34-aab5-fee739faa140", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "1\n", 22 | "2\n", 23 | "3\n", 24 | "4\n", 25 | "5\n", 26 | "6\n", 27 | "7\n", 28 | "8\n", 29 | "9\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "for i in range(1,10):\n", 35 | " print(i)" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 4, 41 | "id": "6c0092fb-98ff-4ba6-8b98-6859b81bad67", 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "0\n", 49 | "1\n", 50 | "2\n", 51 | "3\n", 52 | "4\n", 53 | "5\n", 54 | "6\n", 55 | "7\n", 56 | "8\n", 57 | "9\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "i =0\n", 63 | "while(i<10):\n", 64 | " print(i)\n", 65 | " i =i+1" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "id": "aa18fc0e-b933-4125-9924-f049ea19598f", 71 | "metadata": {}, 72 | "source": [ 73 | "Q2. Write a python program to print the sum and product of the first 10 natural numbers using for\n", 74 | "and while loop.\n" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 12, 80 | "id": "bbb8e90d-a3a1-403e-894d-a3408b612c37", 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "sum of 10 nos are: 55\n", 88 | "product of 10 nos are: 3628800\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "#for loop\n", 94 | "sum=0\n", 95 | "product =1\n", 96 | "for i in range(1,11):\n", 97 | " sum = sum+i\n", 98 | " product = product*i\n", 99 | "\n", 100 | "print(\"sum of 10 nos are:\",sum)\n", 101 | "print(\"product of 10 nos are:\",product)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 10, 107 | "id": "6ab287dc-4663-4d68-ab53-0ecc93c22230", 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "sum of 10 nos are: 55\n", 115 | "product of 10 nos are: 3628800\n" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "#while loop\n", 121 | "sum=0\n", 122 | "products =1\n", 123 | "i=1\n", 124 | "while (i<11):\n", 125 | " sum = sum+i\n", 126 | " products = products*i\n", 127 | " i +=1\n", 128 | "print(\"sum of 10 nos are:\",sum)\n", 129 | "print(\"product of 10 nos are:\",products)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "id": "c86dcb7f-4354-4f1b-8b0a-a423aab4a05a", 135 | "metadata": {}, 136 | "source": [ 137 | "Q3. Create a python program to compute the electricity bill for a household.\n", 138 | "The per-unit charges in rupees are as follows: For the first 100 units, the user will be charged Rs. 4.5 per\n", 139 | "unit, for the next 100 units, the user will be charged Rs. 6 per unit, and for the next 100 units, the user will\n", 140 | "be charged Rs. 10 per unit, After 300 units and above the user will be charged Rs. 20 per unit.\n", 141 | "Your program must pass this test case: when the unit of electricity consumed by the user in a month is\n", 142 | "310, the total electricity bill should be 2250." 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 14, 148 | "id": "1bbb3690-e72d-499e-98b7-9b11d3ae9366", 149 | "metadata": {}, 150 | "outputs": [ 151 | { 152 | "name": "stdin", 153 | "output_type": "stream", 154 | "text": [ 155 | "Enter Units: 310\n" 156 | ] 157 | }, 158 | { 159 | "name": "stdout", 160 | "output_type": "stream", 161 | "text": [ 162 | "Total Electricity Bill Rs.2250\n" 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "units = int(input('Enter Units: '))\n", 168 | "bill = 0\n", 169 | "if units <= 100:\n", 170 | " bill += 4.5*units\n", 171 | " units -= 100\n", 172 | "elif units > 100 and units <= 200:\n", 173 | " bill += 450\n", 174 | " units -= 100\n", 175 | " bill += 6*units\n", 176 | "elif units > 200 and units <= 300:\n", 177 | " bill += 450+600\n", 178 | " units -= 200\n", 179 | " bill += 10*units\n", 180 | "elif units > 300:\n", 181 | " bill += 450+600+1000\n", 182 | " units -= 300\n", 183 | " bill += 20*units\n", 184 | "print(f'Total Electricity Bill Rs.{bill}')" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "id": "f70f562b-7192-4727-b105-8993724fedf1", 190 | "metadata": {}, 191 | "source": [ 192 | "Q4. Create a list of numbers from 1 to 100. Use for loop and while loop to calculate the cube of each\n", 193 | "number and if the cube of that number is divisible by 4 or 5 then append that number in a list and print\n", 194 | "that list." 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 18, 200 | "id": "6a68dbfe-9ab1-4345-a37b-cb2085ae1d5a", 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "name": "stdout", 205 | "output_type": "stream", 206 | "text": [ 207 | "[2, 4, 5, 6, 8, 10, 12, 14, 15, 16, 18, 20, 22, 24, 25, 26, 28, 30, 32, 34, 35, 36, 38, 40, 42, 44, 45, 46, 48, 50, 52, 54, 55, 56, 58, 60, 62, 64, 65, 66, 68, 70, 72, 74, 75, 76, 78, 80, 82, 84, 85, 86, 88, 90, 92, 94, 95, 96, 98, 100]\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "#for loop\n", 213 | "l = list(range(1,101))\n", 214 | "\n", 215 | "newlist =[]\n", 216 | "\n", 217 | "for i in l:\n", 218 | " c= i**3\n", 219 | " if c %4 ==0 or c%5==0:\n", 220 | " newlist.append(i)\n", 221 | "print(newlist)" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 19, 227 | "id": "3574c7fb-2c2f-479d-bc79-b4ccce0d1de9", 228 | "metadata": {}, 229 | "outputs": [ 230 | { 231 | "name": "stdout", 232 | "output_type": "stream", 233 | "text": [ 234 | "[2, 4, 5, 6, 8, 10, 12, 14, 15, 16, 18, 20, 22, 24, 25, 26, 28, 30, 32, 34, 35, 36, 38, 40, 42, 44, 45, 46, 48, 50, 52, 54, 55, 56, 58, 60, 62, 64, 65, 66, 68, 70, 72, 74, 75, 76, 78, 80, 82, 84, 85, 86, 88, 90, 92, 94, 95, 96, 98, 100]\n" 235 | ] 236 | } 237 | ], 238 | "source": [ 239 | "#while loop\n", 240 | "newlist2 =[]\n", 241 | "i =1\n", 242 | "while i <= len(l):\n", 243 | " c = i**3\n", 244 | " if c%4 ==0 or c%5 ==0:\n", 245 | " newlist2.append(i)\n", 246 | " i+=1\n", 247 | "print(newlist2)" 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "id": "e9a7ff36-1d73-449b-95ca-a651eb1f39b3", 253 | "metadata": {}, 254 | "source": [ 255 | "Q5. Write a program to filter count vowels in the below-given string.\n", 256 | "string = \"I want to become a data scientist\"" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 20, 262 | "id": "712d0b81-5db8-4fbd-925c-fdf017a5e5b6", 263 | "metadata": {}, 264 | "outputs": [ 265 | { 266 | "name": "stdin", 267 | "output_type": "stream", 268 | "text": [ 269 | "Enter the input i want to became a data scientist\n" 270 | ] 271 | }, 272 | { 273 | "name": "stdout", 274 | "output_type": "stream", 275 | "text": [ 276 | "12\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "sts = str(input(\"Enter the input\"))\n", 282 | "vowels = 'aeiouAEIOU'\n", 283 | "count=0\n", 284 | "for char in sts:\n", 285 | " if char in vowels:\n", 286 | " count+=1\n", 287 | "print(count)\n", 288 | " " 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "id": "430ba74c-1330-46a6-87d2-51d12034ba86", 295 | "metadata": {}, 296 | "outputs": [], 297 | "source": [] 298 | } 299 | ], 300 | "metadata": { 301 | "kernelspec": { 302 | "display_name": "Python 3 (ipykernel)", 303 | "language": "python", 304 | "name": "python3" 305 | }, 306 | "language_info": { 307 | "codemirror_mode": { 308 | "name": "ipython", 309 | "version": 3 310 | }, 311 | "file_extension": ".py", 312 | "mimetype": "text/x-python", 313 | "name": "python", 314 | "nbconvert_exporter": "python", 315 | "pygments_lexer": "ipython3", 316 | "version": "3.10.8" 317 | } 318 | }, 319 | "nbformat": 4, 320 | "nbformat_minor": 5 321 | } 322 | -------------------------------------------------------------------------------- /Python-Assignment/Python_Assignment_2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "0ce9e1a5-51af-4dd4-b74d-b09e3609db15", 6 | "metadata": {}, 7 | "source": [ 8 | "Q1" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 2, 14 | "id": "d8ab70d8-4837-4b21-b086-574870a8668b", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdin", 19 | "output_type": "stream", 20 | "text": [ 21 | "enter your marks 60\n" 22 | ] 23 | }, 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "Your Grade is C\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "marks = int(input(\"enter your marks\"))\n", 34 | "if marks >90:\n", 35 | " print (\"Your Grade is A\")\n", 36 | "elif marks >80 and marks <=90 :\n", 37 | " print(\"Your Grade is B\")\n", 38 | "elif marks >=60 and marks <=80 :\n", 39 | " print(\"Your Grade is C\")\n", 40 | "else :\n", 41 | " print(\"Your Grade is D\")" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "id": "e487f78e-6407-4fd7-806f-a926cd91acf2", 47 | "metadata": {}, 48 | "source": [ 49 | "Q2" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "id": "0dd7b2fd-e330-42a7-9cbe-e8be2932a516", 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdin", 60 | "output_type": "stream", 61 | "text": [ 62 | "enter the price of bike 1000\n" 63 | ] 64 | }, 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "Your road tax is 5%\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "price = int(input(\"enter the price of bike \"))\n", 75 | "if price >100000 :\n", 76 | " print (\"Your road tax is 15%\")\n", 77 | "elif price >50000 and price <100000 :\n", 78 | " print(\"Your road tax is 10%\")\n", 79 | "else :\n", 80 | " print(\"Your road tax is 5%\")" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "id": "6a09e7f6-0efe-45cb-8fe0-98367610f9bc", 86 | "metadata": {}, 87 | "source": [ 88 | "Q3" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 4, 94 | "id": "bb15da4e-8030-4a31-9a1e-f36a0e0e5aca", 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdin", 99 | "output_type": "stream", 100 | "text": [ 101 | "enter your city Delhi\n" 102 | ] 103 | }, 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "Monument of your city is Red Fort\n" 109 | ] 110 | } 111 | ], 112 | "source": [ 113 | "city = input(\"enter your city\")\n", 114 | "if city == \"Delhi\" :\n", 115 | " print(\"Monument of your city is Red Fort\")\n", 116 | "if city == \"Agra\" :\n", 117 | " print(\"Monument of your city is Tajmahal\")\n", 118 | "if city == \"Jaipur\":\n", 119 | " print(\"Monument of your city is Jal Mahal\")" 120 | ] 121 | }, 122 | { 123 | "cell_type": "markdown", 124 | "id": "d143e1f5-6d12-4c73-8afd-807f14eaf02e", 125 | "metadata": {}, 126 | "source": [ 127 | "Q4" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 8, 133 | "id": "e3c19ea0-f6de-4e79-81e9-172c7b5cabc5", 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "name": "stdin", 138 | "output_type": "stream", 139 | "text": [ 140 | "Enter the numbers 30\n" 141 | ] 142 | }, 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "1\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | " num = int(input(\"Enter the numbers\"))\n", 153 | "count = 0\n", 154 | "while num > 10:\n", 155 | " num = num / 3\n", 156 | " count += 1\n", 157 | "print(count)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "id": "21c37a24-f420-46d7-b08e-f58b49f1ec0a", 163 | "metadata": {}, 164 | "source": [ 165 | "Q5" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 10, 171 | "id": "9126c7c2-1f18-44f9-995c-aed3b29cbfc1", 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "data": { 176 | "text/plain": [ 177 | "'Ans => Python while loop is used to run a block code until a certain condition is met.\\n The syntax of while loop is:\\n while condition:\\n # body of while loop\\n Here,\\n A while loop evaluates the condition\\n If the condition evaluates to True, the code inside the while loop is executed.\\n condition is evaluated again.\\n This process continues until the condition is False.\\n When condition evaluates to False, the loop stops'" 178 | ] 179 | }, 180 | "execution_count": 10, 181 | "metadata": {}, 182 | "output_type": "execute_result" 183 | } 184 | ], 185 | "source": [ 186 | "'''Ans => Python while loop is used to run a block code until a certain condition is met.\n", 187 | " The syntax of while loop is:\n", 188 | " while condition:\n", 189 | " # body of while loop\n", 190 | " Here,\n", 191 | " A while loop evaluates the condition\n", 192 | " If the condition evaluates to True, the code inside the while loop is executed.\n", 193 | " condition is evaluated again.\n", 194 | " This process continues until the condition is False.\n", 195 | " When condition evaluates to False, the loop stops'''" 196 | ] 197 | }, 198 | { 199 | "cell_type": "markdown", 200 | "id": "eaa40b59-a08f-415c-be27-597a9bb4e030", 201 | "metadata": {}, 202 | "source": [ 203 | "Q6\n" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 13, 209 | "id": "39df18dc-7124-4670-8f4b-ab26b6418841", 210 | "metadata": {}, 211 | "outputs": [ 212 | { 213 | "name": "stdout", 214 | "output_type": "stream", 215 | "text": [ 216 | "*\n", 217 | "**\n", 218 | "***\n", 219 | "****\n", 220 | "*****\n", 221 | "-----\n", 222 | "*****\n", 223 | "****\n", 224 | "***\n", 225 | "**\n", 226 | "*\n", 227 | "-----\n", 228 | " *\n", 229 | " **\n", 230 | " ***\n", 231 | " ****\n", 232 | "*****\n" 233 | ] 234 | } 235 | ], 236 | "source": [ 237 | "# Pattern 1:\n", 238 | "i = 1\n", 239 | "while i <= 5:\n", 240 | " j = 1\n", 241 | " while j <= i:\n", 242 | " print(\"*\", end=\"\")\n", 243 | " j += 1\n", 244 | " print(\"\")\n", 245 | " i += 1\n", 246 | " \n", 247 | "print(\"-----\")\n", 248 | "# Pattern 2:\n", 249 | "i = 5\n", 250 | "while i >= 1:\n", 251 | " j = i\n", 252 | " while j >= 1:\n", 253 | " print(\"*\", end=\"\")\n", 254 | " j -= 1\n", 255 | " print(\"\")\n", 256 | " i -= 1\n", 257 | "\n", 258 | "print(\"-----\")\n", 259 | "# Pattern 3:\n", 260 | "i = 5\n", 261 | "while i >= 1:\n", 262 | " j = 1\n", 263 | " while j <= 5:\n", 264 | " if j >= i:\n", 265 | " print(\"*\", end=\"\")\n", 266 | " else:\n", 267 | " print(\" \", end=\"\")\n", 268 | " j += 1\n", 269 | " print(\"\")\n", 270 | " i -= 1" 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "id": "f4a38548-54a8-4fac-9452-ed15529570b9", 276 | "metadata": {}, 277 | "source": [ 278 | "Q7" 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 15, 284 | "id": "4d3e8c08-6bb9-45f2-a2e8-1373c57cf12d", 285 | "metadata": {}, 286 | "outputs": [ 287 | { 288 | "name": "stdout", 289 | "output_type": "stream", 290 | "text": [ 291 | "0\n" 292 | ] 293 | } 294 | ], 295 | "source": [ 296 | "i = 10\n", 297 | "while i > 0:\n", 298 | " i = i - 1\n", 299 | "print(i)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": null, 305 | "id": "5d969056-0d43-431f-a31b-9f8ef7d7f246", 306 | "metadata": {}, 307 | "outputs": [], 308 | "source": [] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": null, 313 | "id": "3c5620e6-90cb-48ee-9eb1-464a84db88af", 314 | "metadata": {}, 315 | "outputs": [], 316 | "source": [] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "execution_count": null, 321 | "id": "1c10e43f-617f-4d0c-892a-b9dcc331f37f", 322 | "metadata": {}, 323 | "outputs": [], 324 | "source": [] 325 | } 326 | ], 327 | "metadata": { 328 | "kernelspec": { 329 | "display_name": "Python 3 (ipykernel)", 330 | "language": "python", 331 | "name": "python3" 332 | }, 333 | "language_info": { 334 | "codemirror_mode": { 335 | "name": "ipython", 336 | "version": 3 337 | }, 338 | "file_extension": ".py", 339 | "mimetype": "text/x-python", 340 | "name": "python", 341 | "nbconvert_exporter": "python", 342 | "pygments_lexer": "ipython3", 343 | "version": "3.10.8" 344 | } 345 | }, 346 | "nbformat": 4, 347 | "nbformat_minor": 5 348 | } 349 | -------------------------------------------------------------------------------- /Pandas/Pandas_Assignment_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "1f71c4d9-51f2-4dca-9c0c-5c88fd791ca1", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "name": "stdout", 11 | "output_type": "stream", 12 | "text": [ 13 | " 0\n", 14 | "0 4\n", 15 | "1 8\n", 16 | "2 15\n", 17 | "3 16\n", 18 | "4 23\n", 19 | "5 42\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | " #Q.1> Create a Pandas Series that contains the following data: 4, 8, 15, 16, 23, and 42. Then, print the series.\n", 25 | "import pandas as pd \n", 26 | "data = [4, 8, 15, 16, 23, 42]\n", 27 | "df = pd.DataFrame(data)\n", 28 | "print(df)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 13, 34 | "id": "d214f492-4279-4c30-ab88-639ffeba5f39", 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | " 0\n", 42 | "0 sarfraj\n", 43 | "1 abshar\n", 44 | "2 abhishek\n", 45 | "3 faizan\n", 46 | "4 hammad\n", 47 | "5 asif\n", 48 | "6 arsalan\n", 49 | "7 faiz\n", 50 | "8 nesat\n", 51 | "9 nemat\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "#Q.2> Create a variable of list type containing 10 elements in it, and apply pandas.Series function on the\n", 57 | "# variable print it.\n", 58 | "import pandas as pd \n", 59 | "data = [\"sarfraj\" , \"abshar\" , \"abhishek\" , \"faizan\" , \"hammad\" , \"asif\" , \"arsalan\" , \"faiz\" , \"nesat\" ,\"nemat\"]\n", 60 | "df = pd.DataFrame(data)\n", 61 | "print(df)" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 9, 67 | "id": "275f893b-3dab-4586-8d90-b84ab439e093", 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | " Name Age Gender\n", 75 | "0 Alice 25 Female\n", 76 | "1 Bob 30 Male\n", 77 | "2 Claire 27 Female\n" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "#Q.3> Create a Pandas DataFrame that contains the following data:\n", 83 | "# Then, print the DataFrame.\n", 84 | "import pandas as pd \n", 85 | "data = {'Name': ['Alice' , 'Bob' , 'Claire'],\n", 86 | "\n", 87 | " 'Age': [25, 30, 27],\n", 88 | "\n", 89 | " 'Gender': ['Female', 'Male', 'Female']}\n", 90 | "df = pd.DataFrame(data)\n", 91 | "print(df)" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 14, 97 | "id": "ee16b8c8-8960-4476-a8a7-782f796e491a", 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | " Name Age Gender\n", 105 | "0 Alice 25 Female\n", 106 | "1 Bob 30 Male\n", 107 | "2 Charlie 35 Male\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "'''Q.4> What is ‘DataFrame’ in pandas and how is it different from pandas.series? Explain with an example.\n", 113 | " Ans> In Pandas, a DataFrame is a two-dimensional labeled data structure with columns of potentially \n", 114 | " different data types. It is similar to a spreadsheet or a SQL table. In a DataFrame , data is \n", 115 | " organized in rows and columns, where each column can be considered as a Pandas Series.\n", 116 | "\n", 117 | " On the other hand, a Series in Pandas is a one-dimensional labeled array that can contain data of any \n", 118 | " data type, including integers, strings, and floating-point numbers. It is similar to a column in a \n", 119 | " spreadsheet or a SQL table.\n", 120 | "\n", 121 | " The main difference between a DataFrame and a Series is that a DataFrame is a two-dimensional data \n", 122 | " structure with rows and columns, while a Series is a one-dimensional data structure with only one column.\n", 123 | " Here's an example to illustrate the difference between a DataFrame and a Series:\n", 124 | "\n", 125 | " Suppose we have the following data:\n", 126 | " Name(Alice , Bob, Charlie) , Age(25, 3, 35) , Gender (Female,Male ,Male)\n", 127 | " We can represent this data using a DataFrame in Pandas as follows:'''\n", 128 | "import pandas as pd\n", 129 | "\n", 130 | "data = {'Name': ['Alice', 'Bob', 'Charlie'],\n", 131 | " 'Age': [25, 30, 35],\n", 132 | " 'Gender': ['Female', 'Male', 'Male']}\n", 133 | "\n", 134 | "df = pd.DataFrame(data)\n", 135 | "\n", 136 | "print(df)\n" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "id": "59f0fd67-4a0c-47e7-a58c-16a5094fed65", 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [ 146 | "#Q.5> What are some common functions you can use to manipulate data in a Pandas DataFrame? Can\n", 147 | " #you give an example of when you might use one of these functions?\n", 148 | "\n", 149 | " # 1. head() and tail(): These functions are used to view the first and last n rows of a DataFrame, \n", 150 | " # respectively. They can be useful for quickly getting a sense of what the data looks like.\n", 151 | " # Example:\n", 152 | "import pandas as pd\n", 153 | "\n", 154 | "df = pd.read_csv('data.csv')\n", 155 | "\n", 156 | "# View the first 5 rows\n", 157 | "print(df.head())\n", 158 | "\n", 159 | "# View the last 5 rows\n", 160 | "print(df.tail())\n", 161 | " \n", 162 | " # 2. describe(): This function provides a statistical summary of the DataFrame, including the count, \n", 163 | " # mean, standard deviation, minimum, and maximum values, among others.\n", 164 | " # Example:\n", 165 | "import pandas as pd\n", 166 | "\n", 167 | "df = pd.read_csv('data.csv')\n", 168 | "\n", 169 | "# Get a statistical summary of the DataFrame\n", 170 | "print(df.describe())\n", 171 | "\n", 172 | " # 3. groupby(): This function is used to group data in a DataFrame based on one or more columns. \n", 173 | " # It can be useful for calculating aggregate statistics for different groups.\n", 174 | " # Example:\n", 175 | "import pandas as pd\n", 176 | "\n", 177 | "df = pd.read_csv('data.csv')\n", 178 | "\n", 179 | "# Group the data by the 'category' column and calculate the mean of the 'value' column\n", 180 | "grouped = df.groupby('category').mean()\n", 181 | "\n", 182 | "print(grouped)\n", 183 | "\n", 184 | " # 4. sort_values(): This function is used to sort the rows of a DataFrame based on one or more columns.\n", 185 | " # Example:\n", 186 | "import pandas as pd\n", 187 | "\n", 188 | "df = pd.read_csv('data.csv')\n", 189 | "\n", 190 | "# Sort the DataFrame by the 'value' column in descending order\n", 191 | "sorted_df = df.sort_values('value', ascending=False)\n", 192 | "\n", 193 | "print(sorted_df)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "id": "55d033d5-febf-4785-b472-e14eb96e62a2", 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [ 203 | "Q.6> Which of the following is mutable in nature Series, DataFrame, Panel?\n", 204 | "Ans> In Pandas, both Series and DataFrame are mutable in nature, while Panel is considered deprecated \n", 205 | " and has been replaced by multi-dimensional arrays, such as numpy.ndarray or xarray.DataArray, \n", 206 | " which are also mutable.\n", 207 | "\n", 208 | " In a mutable object, the contents of the object can be modified after it is created. This means \n", 209 | " that you can add, remove, or update elements in a Series or DataFrame after they are created." 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 16, 215 | "id": "425b536d-2468-457b-a01b-9139a390a8af", 216 | "metadata": {}, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | " Name Age Gender\n", 223 | "0 Alice 25 Female\n", 224 | "1 Bob 30 Male\n", 225 | "2 Charlie 35 Male\n", 226 | "3 David 40 Male\n", 227 | "4 Emily 45 Female\n" 228 | ] 229 | } 230 | ], 231 | "source": [ 232 | "#Q.7> Create a DataFrame using multiple Series. Explain with an example.\n", 233 | "import pandas as pd\n", 234 | "\n", 235 | "# Create a Series for each column of data\n", 236 | "names = pd.Series(['Alice', 'Bob', 'Charlie', 'David', 'Emily'])\n", 237 | "ages = pd.Series([25, 30, 35, 40, 45])\n", 238 | "genders = pd.Series(['Female', 'Male', 'Male', 'Male', 'Female'])\n", 239 | "\n", 240 | "# Combine the Series into a DataFrame\n", 241 | "df = pd.DataFrame({'Name': names, 'Age': ages, 'Gender': genders})\n", 242 | "\n", 243 | "# Print the resulting DataFrame\n", 244 | "print(df)\n" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": null, 250 | "id": "8a43b080-eae1-469e-ae3f-e96ca0daa76c", 251 | "metadata": {}, 252 | "outputs": [], 253 | "source": [] 254 | } 255 | ], 256 | "metadata": { 257 | "kernelspec": { 258 | "display_name": "Python 3 (ipykernel)", 259 | "language": "python", 260 | "name": "python3" 261 | }, 262 | "language_info": { 263 | "codemirror_mode": { 264 | "name": "ipython", 265 | "version": 3 266 | }, 267 | "file_extension": ".py", 268 | "mimetype": "text/x-python", 269 | "name": "python", 270 | "nbconvert_exporter": "python", 271 | "pygments_lexer": "ipython3", 272 | "version": "3.10.8" 273 | } 274 | }, 275 | "nbformat": 4, 276 | "nbformat_minor": 5 277 | } 278 | -------------------------------------------------------------------------------- /Flask_and_WebAPI/webscrapping.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "3abf8517-286a-4422-a88e-521368eb7473", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "Q.1> What is Web Scrapping ? Why is it Used?Give three areas where Web Scrapping is used to get data.\n", 11 | "Ans> Web scraping, also known as web harvesting or web data extraction, refers to the process of automatically \n", 12 | " extracting data from websites using software tools or scripts. Web scraping tools access the website's HTML \n", 13 | " code and extract the data contained within it, typically by parsing the HTML and using regular expressions \n", 14 | " or other algorithms to extract the desired information\n", 15 | " \n", 16 | " Web scraping is used for a variety of purposes, including market research, price monitoring, data analytics, \n", 17 | " and content aggregation. By scraping data from multiple sources, businesses and researchers can gain insights \n", 18 | " into consumer behavior, track competitor pricing and promotions, and build databases of product information and\n", 19 | " customer reviews.\n", 20 | " \n", 21 | " Here are three areas where web scraping is commonly used to get data:\n", 22 | "\n", 23 | " 1. E-commerce: Online retailers can use web scraping to gather pricing and product information from competitors, \n", 24 | " monitor customer reviews, and track changes in inventory levels. This information can help businesses stay \n", 25 | " competitive and make informed pricing and inventory decisions.\n", 26 | "\n", 27 | " 2. Data analytics: Web scraping can be used to collect data on social media activity, online news articles, and \n", 28 | " other web-based sources of information. This data can be analyzed to identify trends, track sentiment, and \n", 29 | " gain insights into consumer behavior.\n", 30 | "\n", 31 | " 3. Research: Researchers in a variety of fields, including social sciences and humanities, can use web scraping \n", 32 | " to gather data for their studies. For example, researchers might scrape data from online forums or social media \n", 33 | " sites to analyze public opinion on a particular issue." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "id": "08a6870a-d061-432c-b4bc-e5f920a89379", 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "Q.2> What are the different methods used for Web Scrapping ? \n", 44 | "Ans> There are several methods used for web scraping, depending on the type of data being extracted and the structure\n", 45 | " of the website being scraped. Here are some of the most common methods:\n", 46 | "\n", 47 | " Parsing HTML: This method involves parsing the HTML code of a website to extract specific data using regular \n", 48 | " expressions or other parsing techniques. It is one of the simplest methods, but it can be limited by changes \n", 49 | " to the website's HTML structure.\n", 50 | "\n", 51 | " Web Scraping Libraries: Developers often use web scraping libraries like Beautiful Soup, Scrapy, or Puppeteer, \n", 52 | " which provide pre-built functions and methods for accessing and scraping data from websites.\n", 53 | "\n", 54 | " APIs: Some websites provide APIs (Application Programming Interfaces) that allow developers to access data in \n", 55 | " a structured format. APIs can be used to extract data in real-time, and they often provide more reliable and \n", 56 | " up-to-date data.\n", 57 | "\n", 58 | " Headless Browsers: This method involves using headless browsers like PhantomJS or Selenium to automate web \n", 59 | " browsing and extract data from dynamic websites that require user interaction.\n", 60 | "\n", 61 | " Machine Learning: Some web scraping tasks can be automated using machine learning algorithms that can \n", 62 | " identify patterns and extract data automatically. This method requires significant data preparation and \n", 63 | " training, but it can be more powerful and flexible than other methods." 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "id": "424cb684-4f21-46d9-aa44-f03ec5ce20a9", 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "Q.3> What is Beautiful Soup ? Why is it used ?\n", 74 | "Ans> Beautiful Soup is a Python library used for web scraping purposes. It provides a simple and efficient \n", 75 | " way to parse HTML and XML documents, extract data, and navigate the document tree. Beautiful Soup can \n", 76 | " handle poorly formatted HTML, making it a popular choice among web developers and data analysts.\n", 77 | "\n", 78 | " Here are some of the reasons why Beautiful Soup is widely used for web scraping:\n", 79 | "\n", 80 | " Easy to learn and use: Beautiful Soup has a user-friendly syntax and a simple API that makes it easy \n", 81 | " for beginners to start scraping data from websites.\n", 82 | "\n", 83 | " Robust parsing capabilities: Beautiful Soup can parse even the most poorly formatted HTML, making it a \n", 84 | " flexible tool for web scraping.\n", 85 | "\n", 86 | " Navigation and search: Beautiful Soup provides a range of search and navigation methods to locate and \n", 87 | " extract data from specific parts of an HTML document.\n", 88 | "\n", 89 | " Integration with other libraries: Beautiful Soup can be integrated with other Python libraries, such as \n", 90 | " requests and pandas, to handle HTTP requests and manage scraped data.\n", 91 | "\n", 92 | " Open source and community-driven: Beautiful Soup is an open-source library, meaning that it is freely \n", 93 | " available and maintained by a large community of developers. This ensures that the library is continuously \n", 94 | " updated and improved with new features and bug fixes." 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "id": "99407551-b9dc-49cd-a411-501df5638cb4", 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "Q.4> Why is flask used in this Web Scraping project?\n", 105 | "Ans> Here are some of the reasons why Flask is commonly used in web scraping projects:\n", 106 | "\n", 107 | " Easy to set up: Flask can be installed quickly and easily using pip, and requires minimal configuration to\n", 108 | " get started.\n", 109 | "\n", 110 | " Flexible routing: Flask provides a simple and intuitive routing system that allows developers to map URLs \n", 111 | " to Python functions, making it easy to build web scrapers that follow a specific pattern or logic.\n", 112 | "\n", 113 | " Lightweight and modular: Flask is a lightweight and modular framework that can be easily extended with third-party \n", 114 | " libraries and plugins, making it easy to add functionality to your web scraper as needed.\n", 115 | "\n", 116 | " Integration with other Python libraries: Flask integrates seamlessly with other popular Python libraries, such as \n", 117 | " Beautiful Soup, Requests, and Pandas, making it easy to build a web scraper that leverages the power of these libraries.\n", 118 | "\n", 119 | " Easy to deploy: Flask applications can be easily deployed to a wide range of hosting services, including Heroku, \n", 120 | " Google Cloud, and Amazon Web Services." 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "id": "dd8fdc8d-a0c8-4780-83a5-6c6871f87bf9", 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "Q.5> Write the names of AWS services used in this project. Also, explain the use of each service.\n", 131 | "Ans> The AWS services used in this project are 1. AWS Codepipeline 2. AWS Elastic Beanstalk\n", 132 | " \n", 133 | " AWS CodePipeline is a fully managed continuous delivery service that helps automate the release process for your \n", 134 | " web applications. It provides a set of tools and services that allow developers to build, test, and deploy their \n", 135 | " code automatically, from source code changes to production deployment. CodePipeline integrates with a wide range \n", 136 | " of AWS services, including Elastic Beanstalk, to provide a seamless and automated software release process.\n", 137 | "\n", 138 | " AWS Elastic Beanstalk is a fully managed service that makes it easy to deploy and run web applications and services \n", 139 | " on AWS. It provides an easy-to-use interface that allows developers to deploy web applications quickly and easily, \n", 140 | " without worrying about the underlying infrastructure. Elastic Beanstalk supports multiple languages, including Python, \n", 141 | " Ruby, Java, and Node.js, and provides a range of pre-configured environments that developers can use to deploy their \n", 142 | " applications.\n", 143 | "\n", 144 | " When used together, AWS CodePipeline and Elastic Beanstalk can simplify the software release process by automating \n", 145 | " the deployment of web applications to the cloud. Developers can use CodePipeline to define a release pipeline that \n", 146 | " includes multiple stages, such as build, test, and deploy, and configure Elastic Beanstalk to deploy the application \n", 147 | " to the appropriate environment based on the pipeline status. This allows developers to release new features and \n", 148 | " updates more quickly and with less risk, as the entire process is automated and standardized." 149 | ] 150 | } 151 | ], 152 | "metadata": { 153 | "kernelspec": { 154 | "display_name": "Python 3 (ipykernel)", 155 | "language": "python", 156 | "name": "python3" 157 | }, 158 | "language_info": { 159 | "codemirror_mode": { 160 | "name": "ipython", 161 | "version": 3 162 | }, 163 | "file_extension": ".py", 164 | "mimetype": "text/x-python", 165 | "name": "python", 166 | "nbconvert_exporter": "python", 167 | "pygments_lexer": "ipython3", 168 | "version": "3.10.8" 169 | } 170 | }, 171 | "nbformat": 4, 172 | "nbformat_minor": 5 173 | } 174 | -------------------------------------------------------------------------------- /Python-Assignment/Python_Assignment_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "db666499-0864-439d-9ce6-c260e7d3cf80", 6 | "metadata": {}, 7 | "source": [ 8 | "1. Who Developed the python programming language?" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "227e7162-633f-4c14-af36-19da10cba39a", 14 | "metadata": {}, 15 | "source": [ 16 | "Guido Van Rossum" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "id": "9726a32e-2102-4309-a60e-e69337d01680", 22 | "metadata": {}, 23 | "source": [ 24 | "2. Which type of programming does python support?" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "id": "f7b04844-d6e0-4fc4-8bee-3b0762751cbd", 30 | "metadata": {}, 31 | "source": [ 32 | "Object oriented programing and structured programing" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "id": "c0c007ce-afb4-42e4-b7b2-9fa68b8e833a", 38 | "metadata": {}, 39 | "source": [ 40 | "3. is python case sensitive when dealing with identifier?" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "id": "cf5e55df-3eea-44c9-b996-0b788e8803bc", 46 | "metadata": {}, 47 | "source": [ 48 | "yes it is a case sensitive" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "id": "b5c73eef-f860-4e8c-b945-ef01286fadb3", 54 | "metadata": {}, 55 | "source": [ 56 | "4. what is the correct extension of python file?" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "id": "6595b591-a4a1-4703-ba85-021ed0a9576c", 62 | "metadata": {}, 63 | "source": [ 64 | " py" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "id": "36f44d43-4544-4409-8bc3-610fd8dc3fb8", 70 | "metadata": {}, 71 | "source": [ 72 | "5. is python code compiled or interpreted ?" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "id": "b460391a-51dc-4c76-8d02-f5cc3d039d3a", 78 | "metadata": {}, 79 | "source": [ 80 | "Python is an interpreted language, which means the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "id": "ba9c7080-eff8-4b1f-abf0-534e38b9c784", 86 | "metadata": {}, 87 | "source": [ 88 | "6. Name a few blocks of code used to define in Python language?\n" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "id": "c1662339-672e-44f9-9abb-23d15141e64c", 94 | "metadata": {}, 95 | "source": [ 96 | "Python uses indentation to define blocks of code. Indentations are simply spaces or tabs used as an indicator that is part of the indent code child.\n", 97 | "\n", 98 | "if (condition):\n", 99 | "\n", 100 | " # Statement 1\n", 101 | " \n", 102 | "else:\n", 103 | "\n", 104 | " # Statement 2\n", 105 | "\n", 106 | "You will find that statements 1 will have the same indentation and therefore is of the same block. And similarly, statements 2 is of a another block." 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "id": "7e645fef-fcf5-4fcc-83ff-ea3f4894d621", 112 | "metadata": {}, 113 | "source": [ 114 | "7. State a character used to give single-line comments in Python?\n" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "id": "5b09914f-e4d1-4ede-8758-abf1f2e7ccd7", 120 | "metadata": {}, 121 | "source": [ 122 | "' # ' symbol to write a single-line comment." 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "id": "3e5179ec-1629-4a70-bcdc-0dc1dd41be33", 128 | "metadata": {}, 129 | "source": [ 130 | "8. Mention functions which can help us to find the version of python that we are currently working on?\n" 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "id": "10682e0c-ba73-4ec4-bb26-1e8ea1e967ea", 136 | "metadata": {}, 137 | "source": [ 138 | "The function sys.version can help us to find the version of python that we are currently working on." 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "id": "cd9ad158-2ec7-4516-9a77-c7ae14228fa9", 144 | "metadata": {}, 145 | "source": [ 146 | "9. Python supports the creation of anonymous functions at runtime, using a construct called\n" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "id": "34ff26c5-b007-4925-b96d-3408d48167fd", 152 | "metadata": {}, 153 | "source": [ 154 | "Lambda" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "id": "1a036748-bda3-4ec7-96d5-31dc3be06226", 160 | "metadata": {}, 161 | "source": [ 162 | "10. What does pip stand for python?\n", 163 | "\n" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "id": "d459ae30-0fcb-4f0c-b572-4250effcbd11", 169 | "metadata": {}, 170 | "source": [ 171 | "Preferred Installer Program" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "id": "f7ec9555-cd14-45d8-b269-9dfef26badf6", 177 | "metadata": {}, 178 | "source": [ 179 | "11. Mention a few built- in functions in python?\n" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "id": "7586843c-ccac-4e73-85ef-15bed53ef936", 185 | "metadata": {}, 186 | "source": [ 187 | " 1. bool()\n", 188 | "This function takes a value as an argument and returns either True or False. It gives True if the value is not empty/zero/False. Or else it returns False. \n", 189 | "\n", 190 | "2. chr()\n", 191 | "This function takes the Unicode value as an input and returns the respective character.\n", 192 | "\n", 193 | "3. dict()\n", 194 | "This function converts the input into the dictionary data type and returns it." 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "id": "fdc295c3-0b32-41ff-9a0a-6c331dab3f01", 200 | "metadata": {}, 201 | "source": [ 202 | "12. What is the maximum possible length of an identifier in Python?\n", 203 | "\n" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "id": "7b7b3324-4101-4b4e-922c-1652a3a09f7d", 209 | "metadata": {}, 210 | "source": [ 211 | "79 characters" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "id": "769db3e6-8a28-4f84-9ead-d5f11145f463", 217 | "metadata": {}, 218 | "source": [ 219 | "13. What are the benefits of using Python?\n" 220 | ] 221 | }, 222 | { 223 | "cell_type": "markdown", 224 | "id": "7015d702-8748-43b2-b2c9-e5423b72c335", 225 | "metadata": {}, 226 | "source": [ 227 | "1. Simple and Easy to learn\n", 228 | "2. Portable and Extensible\n", 229 | "3. Object-Oriented Programming\n", 230 | "4. Artificial Intelligence\n", 231 | "5. Big Data\n", 232 | "\n", 233 | "Python can handle plenty of an immense amount of data. " 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "id": "7a865584-e2cf-4de9-9360-6b696de9ebb5", 239 | "metadata": {}, 240 | "source": [ 241 | "14. How is memory managed in Python?\n", 242 | "\n" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "id": "fbe1545d-8ce1-48a8-81f8-5247d5ca8222", 248 | "metadata": {}, 249 | "source": [ 250 | "Memory in Python is managed by Python private heap space.\n", 251 | "All Python objects and data structures are located in a private heap. This private heap is taken care of by Python Interpreter itself, and a programmer doesn’t have access to this private heap.\n", 252 | "Python memory manager takes care of the allocation of Python private heap space.\n", 253 | "Memory for Python private heap space is made available by Python’s in-built garbage collector, which recycles and frees up all the unused memory.\n", 254 | " The allocation of heap space for Python objects and other internal buffers is performed on demand by the Python memory manager through the Python/C API functions listed in this document.\n", 255 | "To avoid memory corruption, extension writers should never try to operate on Python objects with the functions exported by the C library: malloc(), calloc(), realloc() and free()." 256 | ] 257 | }, 258 | { 259 | "cell_type": "markdown", 260 | "id": "d5e7a38c-6ec4-4bb0-94f7-5ef2c2ae6dd8", 261 | "metadata": {}, 262 | "source": [ 263 | "15. How to install Python on Windows and set path variables?\n" 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "id": "9f3983b7-9216-4a76-8714-2f6b07176111", 269 | "metadata": {}, 270 | "source": [ 271 | "First, we need to locate where the python is being installed after downloading it. Press WINDOWS key and search for “Python”, you will get something like this:Click on open file location and you will be in a location where Python is installed, Copy the location path from the top by clicking over it.\n", 272 | "Now, we have to add the above-copied path as a variable so that windows can recognize. Search for “Environmental Variables”, you will see something like this:\n", 273 | "Click on that(\n", 274 | "Now click the “Environmental Variables” button" 275 | ] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "id": "cb9763ef-bacc-4a04-96d7-4b2ed938baa8", 280 | "metadata": {}, 281 | "source": [ 282 | "16. is indentation is required for the python?" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "id": "097dc72d-2e9b-48b7-89d9-212ec7735c95", 288 | "metadata": {}, 289 | "source": [ 290 | "Indentation refers to the spaces at the beginning of a code line.\n", 291 | "Where in other programming languages the indentation in code is for readability only, for Python is very important.\n", 292 | "Python uses indentation to indicate a block of code" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": null, 298 | "id": "0e9442e7-6de6-48bf-8fdd-c75b0b83d9f1", 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [] 302 | } 303 | ], 304 | "metadata": { 305 | "kernelspec": { 306 | "display_name": "Python 3 (ipykernel)", 307 | "language": "python", 308 | "name": "python3" 309 | }, 310 | "language_info": { 311 | "codemirror_mode": { 312 | "name": "ipython", 313 | "version": 3 314 | }, 315 | "file_extension": ".py", 316 | "mimetype": "text/x-python", 317 | "name": "python", 318 | "nbconvert_exporter": "python", 319 | "pygments_lexer": "ipython3", 320 | "version": "3.10.8" 321 | } 322 | }, 323 | "nbformat": 4, 324 | "nbformat_minor": 5 325 | } 326 | -------------------------------------------------------------------------------- /Machine_Learning_Algorithm/Regression/Regression_Assignment_4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "76829d4f", 6 | "metadata": {}, 7 | "source": [ 8 | "# Question No. 1:\n", 9 | "What is Lasso Regression, and how does it differ from other regression techniques?\n", 10 | "\n", 11 | "## Answer:\n", 12 | "**Lasso Regression** is a type of linear regression technique that is commonly used in machine learning and statistics to model the relationship between a dependent variable and one or more independent variables. \n", 13 | "\n", 14 | "In Lasso Regression, the goal is to identify a subset of independent variables that are most relevant to the dependent variable, while also minimizing the impact of irrelevant or redundant variables. This is achieved by adding a penalty term to the regression equation that penalizes the coefficients of the independent variables.\n", 15 | "\n", 16 | "**Compared to other regression techniques** like Ridge Regression and Ordinary Least Squares Regression, Lasso Regression has the following unique features:\n", 17 | "\n", 18 | "- **Feature selection:** Lasso Regression can perform automatic feature selection, which means it can identify the most relevant independent variables and exclude irrelevant or redundant ones from the model. This is particularly useful when dealing with datasets with many independent variables, as it can help reduce overfitting and improve model accuracy.\n", 19 | "\n", 20 | "- **Shrinking coefficients:** In addition to selecting features, Lasso Regression can also shrink the coefficients of the remaining independent variables towards zero. This helps to prevent overfitting and can improve the model's ability to generalize to new data.\n", 21 | "\n", 22 | "- **Sparsity:** Lasso Regression produces sparse models, which means that the final model will typically include only a subset of the original independent variables. This can make the model easier to interpret and can help reduce the computational complexity of the model." 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "id": "6ceb2faf", 28 | "metadata": {}, 29 | "source": [ 30 | "# Question No. 2:\n", 31 | "What is the main advantage of using Lasso Regression in feature selection?\n", 32 | "\n", 33 | "## Answer:\n", 34 | "The main advantage of using Lasso Regression in feature selection is its ability to automatically identify and select the most relevant independent variables while excluding irrelevant or redundant ones.\n", 35 | "\n", 36 | "When dealing with datasets that contain many independent variables, it can be difficult to determine which variables are most important for predicting the dependent variable. Including irrelevant or redundant variables in the model can lead to overfitting, where the model performs well on the training data but poorly on new, unseen data.\n", 37 | "\n", 38 | "Lasso Regression addresses this problem by adding a penalty term to the regression equation that encourages small or zero coefficients for some of the independent variables. This penalty term causes the coefficients of some of the variables to shrink towards zero, effectively eliminating those variables from the model. " 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "97e33ec6", 44 | "metadata": {}, 45 | "source": [ 46 | "# Question No. 3:\n", 47 | "How do you interpret the coefficients of a Lasso Regression model?\n", 48 | "\n", 49 | "## Answer:\n", 50 | "When interpreting the coefficients of a Lasso Regression model, it is important to take into account both the magnitude and the sign of the coefficients, as well as the variables that have been included in the model.\n", 51 | "\n", 52 | "If a coefficient is positive, it means that as the corresponding independent variable increases, the dependent variable is also expected to increase, holding all other variables constant. If a coefficient is negative, it means that as the corresponding independent variable increases, the dependent variable is expected to decrease.\n", 53 | "\n", 54 | "Additionally, if some coefficients are exactly equal to zero, it means that the corresponding independent variables have been excluded from the model and do not have any impact on the dependent variable. The variables that remain in the model with non-zero coefficients are assumed to be the most important variables for predicting the dependent variable, given the other variables in the model." 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "id": "236bf46d", 60 | "metadata": {}, 61 | "source": [ 62 | "# Question No. 4:\n", 63 | "What are the tuning parameters that can be adjusted in Lasso Regression, and how do they affect the\n", 64 | "model's performance?\n", 65 | "\n", 66 | "## Answer:\n", 67 | "asso Regression has one main tuning parameter, which is the regularization parameter, also known as the lambda parameter. This parameter controls the strength of the penalty term added to the regression equation, which determines the degree of shrinkage of the coefficients.\n", 68 | "\n", 69 | "The higher the value of the lambda parameter, the more the coefficients will be shrunk towards zero, and the more features will be eliminated from the model. Conversely, a lower value of the lambda parameter will result in less shrinkage of the coefficients and more features being included in the model." 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "id": "920508a2", 75 | "metadata": {}, 76 | "source": [ 77 | "# Question No. 5:\n", 78 | "Can Lasso Regression be used for non-linear regression problems? If yes, how?\n", 79 | "\n", 80 | "## Answer:\n", 81 | "It is possible to use Lasso Regression for non-linear regression problems by transforming the independent variables into a non-linear form before fitting the model. This is known as non-linear feature engineering.\n", 82 | "\n", 83 | "Non-linear feature engineering involves transforming the original independent variables using mathematical functions such as logarithmic, exponential, polynomial, or trigonometric functions. This allows the model to capture non-linear relationships between the independent and dependent variables.\n", 84 | "\n", 85 | "After transforming the independent variables, Lasso Regression can be applied as usual to perform feature selection and regularization. However, it is important to note that the transformed features may introduce new collinearity issues, and it may be necessary to apply additional feature selection techniques such as PCA or VIF (Variance Inflation Factor) to remove any redundant or correlated features." 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "id": "f327c5eb", 91 | "metadata": {}, 92 | "source": [ 93 | "# Question No. 6:\n", 94 | "What is the difference between Ridge Regression and Lasso Regression?\n", 95 | "\n", 96 | "## Answer:\n", 97 | "Ridge Regression and Lasso Regression are both linear regression techniques that are used for feature selection and regularization. However, they differ in their approach to regularization and the type of penalty they apply to the regression equation.\n", 98 | "\n", 99 | "The main difference between Ridge Regression and Lasso Regression is in the type of penalty term that is added to the regression equation. Ridge Regression adds a penalty term proportional to the square of the coefficients, while Lasso Regression adds a penalty term proportional to the absolute value of the coefficients." 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "id": "807a3688", 105 | "metadata": {}, 106 | "source": [ 107 | "# Question No. 7:\n", 108 | "Can Lasso Regression handle multicollinearity in the input features? If yes, how?\n", 109 | "\n", 110 | "## Answer:\n", 111 | "Lasso Regression can handle multicollinearity in the input features to some extent, but it may not completely resolve the issue. Multicollinearity occurs when there is a high degree of correlation between two or more independent variables, which can lead to unstable and unreliable estimates of the coefficients.\n", 112 | "\n", 113 | "Lasso Regression can help reduce the impact of multicollinearity by shrinking the coefficients of correlated variables towards zero, which effectively selects one variable from the correlated set and eliminates the others. However, this does not completely remove the multicollinearity issue, as the selected variable may still be highly correlated with other variables that were not selected." 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "id": "bffa4495", 119 | "metadata": {}, 120 | "source": [ 121 | "# Question No. 8:\n", 122 | "How do you choose the optimal value of the regularization parameter (lambda) in Lasso Regression?\n", 123 | "\n", 124 | "## Answer:\n", 125 | "Some common methods to choose the optimal value of lambda:\n", 126 | "\n", 127 | "- **Cross-validation:** Cross-validation is a popular method for selecting the optimal value of lambda. The data is divided into k-folds, and the model is trained on k-1 folds and validated on the remaining fold. This process is repeated for different values of lambda, and the value of lambda that gives the best cross-validation score is chosen as the optimal value.\n", 128 | "\n", 129 | "- **Information criteria:** The Akaike Information Criterion (AIC) and the Bayesian Information Criterion (BIC) are popular methods for selecting the optimal value of lambda. These criteria penalize the model complexity, and the optimal value of lambda is chosen as the value that gives the lowest AIC or BIC score.\n", 130 | "\n", 131 | "- **Grid search:** A grid search is a brute force approach to select the optimal value of lambda. A range of values for lambda is defined, and the model is trained for each value of lambda. The value of lambda that gives the best performance on the validation set is chosen as the optimal value." 132 | ] 133 | } 134 | ], 135 | "metadata": { 136 | "kernelspec": { 137 | "display_name": "Python 3 (ipykernel)", 138 | "language": "python", 139 | "name": "python3" 140 | }, 141 | "language_info": { 142 | "codemirror_mode": { 143 | "name": "ipython", 144 | "version": 3 145 | }, 146 | "file_extension": ".py", 147 | "mimetype": "text/x-python", 148 | "name": "python", 149 | "nbconvert_exporter": "python", 150 | "pygments_lexer": "ipython3", 151 | "version": "3.9.13" 152 | } 153 | }, 154 | "nbformat": 4, 155 | "nbformat_minor": 5 156 | } 157 | -------------------------------------------------------------------------------- /Flask_and_WebAPI/imagescrapping.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "eb9d89c4", 6 | "metadata": {}, 7 | "source": [ 8 | "Go to this given URL and solve the following questions\n", 9 | "URL:https://www.youtube.com/@PW-Foundation/videos" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "id": "fb08fdfa", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "from selenium import webdriver\n", 20 | "from selenium.webdriver.common.by import By\n", 21 | "from webdriver_manager.chrome import ChromeDriverManager" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "id": "7a6f6f52", 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "name": "stderr", 32 | "output_type": "stream", 33 | "text": [ 34 | "c:\\users\\pc\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\ipykernel_launcher.py:1: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n", 35 | " \"\"\"Entry point for launching an IPython kernel.\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "driver = webdriver.Chrome(ChromeDriverManager().install())\n", 41 | "driver.get(\"https://www.youtube.com/@PW-Foundation/videos\")" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "id": "02bd666c", 47 | "metadata": {}, 48 | "source": [ 49 | "#### Q1. Write a python program to extract the video URL of the first five videos.\n" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 3, 55 | "id": "cbacd9fa", 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "links = driver.find_elements(By.TAG_NAME,\"a\")\n", 60 | "urls = []\n", 61 | "for link in links:\n", 62 | " url = link.get_attribute(\"href\")\n", 63 | " urls.append(url)" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 4, 69 | "id": "44cf7341", 70 | "metadata": {}, 71 | "outputs": [ 72 | { 73 | "name": "stdout", 74 | "output_type": "stream", 75 | "text": [ 76 | "['https://www.youtube.com/watch?v=nX5ONgCdLcc', 'https://www.youtube.com/watch?v=nX5ONgCdLcc', 'https://www.youtube.com/watch?v=AM2Dt7cNebw', 'https://www.youtube.com/watch?v=AM2Dt7cNebw', 'https://www.youtube.com/watch?v=7nMJVhey9TM']\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "first_5_links = []\n", 82 | "for url in urls[17:25]:\n", 83 | " if url == None:\n", 84 | " continue\n", 85 | " else:\n", 86 | " first_5_links.append(url)\n", 87 | "print(first_5_links)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "id": "36067f87", 93 | "metadata": {}, 94 | "source": [ 95 | "#### Q2. Write a python program to extract the URL of the video thumbnails of the first five videos.\n" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 39, 101 | "id": "d4f4456a", 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "5\n", 109 | "https://i.ytimg.com/vi/nX5ONgCdLcc/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLBaa_KLfKyYbUYRsb5xf0YCp1YrlQ\n", 110 | "https://i.ytimg.com/vi/AM2Dt7cNebw/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLBJHWHabeJ94dcUDlDDNfvQxnUfJg\n", 111 | "https://i.ytimg.com/vi/7nMJVhey9TM/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLDHikOj7gH4eQNudv7c1qNbwcMWyA\n", 112 | "https://i.ytimg.com/vi/FSVVlcFUCMk/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLAadJS9mLtZptLHtKHEefPgqk3NCg\n", 113 | "https://i.ytimg.com/vi/vKxdTuOirnI/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLBonfyIr0oe5WzFo2o2hoU5Yxbv8w\n" 114 | ] 115 | } 116 | ], 117 | "source": [ 118 | "driver.get(\"https://www.youtube.com/@PW-Foundation/videos\")\n", 119 | "links = driver.find_elements(By.TAG_NAME,\"img\")\n", 120 | "thumbnails = []\n", 121 | "for thumbnail in links[8:18]:\n", 122 | " thumbnail = thumbnail.get_attribute(\"src\")\n", 123 | " if thumbnail == None:\n", 124 | " continue\n", 125 | " else:\n", 126 | " thumbnails.append(thumbnail)\n", 127 | "print(len(thumbnails))\n", 128 | "for i in thumbnails:\n", 129 | " print(i)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "id": "1a73e0ea", 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "id": "5657c6d6", 143 | "metadata": {}, 144 | "source": [ 145 | "#### Q3. Write a python program to extract the title of the first five videos.\n" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 50, 151 | "id": "a1714a5e", 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "['How to Attempt English Board Exam ????', 'Best technique to attempt SST paper in Board exam || Class 10th', 'Last Minute Strategy To Score More Than 98% || ICSE Boards || Class-10th', 'Why You Should Choose Commerce After 10th? || Complete Information💯', \"Follow This Plan To Score More Than 95% in Boards || Topper's Strategy\"]\n" 159 | ] 160 | } 161 | ], 162 | "source": [ 163 | "#title\n", 164 | "driver.get(\"https://www.youtube.com/@PW-Foundation/videos\")\n", 165 | "titles = driver.find_elements(By.TAG_NAME,\"yt-formatted-string\")\n", 166 | "titles_video = []\n", 167 | "# print(links)\n", 168 | "for i in titles[15:35:4]:\n", 169 | " titles_video.append(i.text)\n", 170 | "print(titles_video)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": null, 176 | "id": "46f4ae03", 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "id": "a4f589bb", 184 | "metadata": {}, 185 | "source": [ 186 | "#### Q4. Write a python program to extract the number of views of the first five videos.\n" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "id": "0ea9d16e", 193 | "metadata": {}, 194 | "outputs": [], 195 | "source": [] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 53, 200 | "id": "4aff3ad9", 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "name": "stdout", 205 | "output_type": "stream", 206 | "text": [ 207 | "['120K views', '54K views', '22K views', '28K views', '313K views']\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "#views\n", 213 | "driver.get(\"https://www.youtube.com/@PW-Foundation/videos\")\n", 214 | "views = driver.find_elements(By.TAG_NAME,\"span\")\n", 215 | "video_views = []\n", 216 | "for i in views[23:48]:\n", 217 | " if str(\"views\") in i.text:\n", 218 | " video_views.append(i.text)\n", 219 | "print(video_views)" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": null, 225 | "id": "761139e1", 226 | "metadata": {}, 227 | "outputs": [], 228 | "source": [] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "id": "ef00fb4e", 233 | "metadata": {}, 234 | "source": [ 235 | "#### Q5. Write a python program to extract the time of posting of video for the first five videos.\n" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "id": "2a23146b", 242 | "metadata": {}, 243 | "outputs": [], 244 | "source": [] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 56, 249 | "id": "2b3ac097", 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "name": "stdout", 254 | "output_type": "stream", 255 | "text": [ 256 | "['1 day ago', '2 days ago', '3 days ago', '4 days ago', '9 days ago']\n" 257 | ] 258 | } 259 | ], 260 | "source": [ 261 | "#posting time\n", 262 | "driver.get(\"https://www.youtube.com/@PW-Foundation/videos\")\n", 263 | "times = driver.find_elements(By.TAG_NAME,\"span\")\n", 264 | "video_times = []\n", 265 | "for i in times[24:]:\n", 266 | " if str(\"day\") in i.text:\n", 267 | " video_times.append(i.text)\n", 268 | "print(video_times)" 269 | ] 270 | }, 271 | { 272 | "cell_type": "markdown", 273 | "id": "4a8c1be8", 274 | "metadata": {}, 275 | "source": [ 276 | "Note: Save all the data scraped in the above questions in a CSV file." 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": 57, 282 | "id": "ad1f8148", 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "name": "stdout", 287 | "output_type": "stream", 288 | "text": [ 289 | "['1 day ago', '2 days ago', '3 days ago', '4 days ago', '9 days ago']\n" 290 | ] 291 | } 292 | ], 293 | "source": [ 294 | "# print(first_5_links)\n", 295 | "# print(thumbnails)\n", 296 | "# print(titles_video)\n", 297 | "# print(video_views)\n", 298 | "print(video_times)" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 70, 304 | "id": "c1df737a", 305 | "metadata": {}, 306 | "outputs": [], 307 | "source": [ 308 | "import csv" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 74, 314 | "id": "d5dbd8a5", 315 | "metadata": {}, 316 | "outputs": [ 317 | { 318 | "name": "stdout", 319 | "output_type": "stream", 320 | "text": [ 321 | "done\n" 322 | ] 323 | } 324 | ], 325 | "source": [ 326 | "for i in range(len(first_5_links)-1):\n", 327 | " data_csv = str(first_5_links[i]),str(thumbnails[i]),str(titles_video[i]),str(video_views[i]),str(video_times[i]) \n", 328 | "\n", 329 | " with open('video_details.csv', 'w',encoding='utf-8', newline='') as file:\n", 330 | " writer = csv.writer(file)\n", 331 | " writer.writerow(data_csv)\n", 332 | "print('done')" 333 | ] 334 | } 335 | ], 336 | "metadata": { 337 | "kernelspec": { 338 | "display_name": "Python 3 (ipykernel)", 339 | "language": "python", 340 | "name": "python3" 341 | }, 342 | "language_info": { 343 | "codemirror_mode": { 344 | "name": "ipython", 345 | "version": 3 346 | }, 347 | "file_extension": ".py", 348 | "mimetype": "text/x-python", 349 | "name": "python", 350 | "nbconvert_exporter": "python", 351 | "pygments_lexer": "ipython3", 352 | "version": "3.7.1" 353 | } 354 | }, 355 | "nbformat": 4, 356 | "nbformat_minor": 5 357 | } 358 | -------------------------------------------------------------------------------- /Python-Assignment/Python_Assignment_14.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ae34e657-e621-46b4-96c6-50d72adcd1c7", 6 | "metadata": {}, 7 | "source": [ 8 | "1. what is multithreading in python? hy is it used? Name the module used to handle threads in python" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "a8ed6d74-fe30-4cc0-97e4-5edf766533f0", 14 | "metadata": {}, 15 | "source": [ 16 | "Multithreading in Python is a technique where a program is divided into multiple threads of execution that can run concurrently. Each thread is a separate flow of control that can perform a specific task, and the threads share the same memory space, allowing them to communicate and coordinate with each other.\n", 17 | "\n", 18 | "Multithreading is used in Python to perform multiple tasks simultaneously and make programs more efficient by utilizing the available hardware resources. It is especially useful in applications that involve I/O operations or other tasks that are blocking, as it allows the program to continue executing other tasks while waiting for the blocked task to complete.\n", 19 | "\n", 20 | "In Python, the threading module is used to handle threads. This module provides a simple and easy-to-use interface for creating and managing threads, with support for features such as thread synchronization, thread-safe data structures, and thread communication. The threading module is part of the standard Python library, so it is available on all platforms where Python is supported." 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "id": "ef9749d3-6fba-4e3e-8b89-bab755064eab", 26 | "metadata": {}, 27 | "source": [ 28 | "2. why threading module used? write the use of the following functions\n", 29 | "activeCount()\n", 30 | "currentThread()\n", 31 | "enumerate()" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "id": "a1759380-9fd0-434f-9341-8531e5934c7f", 37 | "metadata": {}, 38 | "source": [ 39 | "The threading module in Python is used to create and manage threads in a program. There are several reasons why you might want to use threading in your Python program:\n", 40 | "\n", 41 | "Concurrency: Threading allows multiple parts of a program to run concurrently, which can improve the performance and efficiency of the program. By running multiple threads at the same time, the program can utilize the available hardware resources more effectively.\n", 42 | "\n", 43 | "Asynchronous I/O: Threading can be used to perform asynchronous I/O operations, where the program can continue executing other tasks while waiting for I/O operations to complete. This can be useful in applications that involve network communication, file I/O, or other types of I/O that are blocking.\n", 44 | "\n", 45 | "Parallelism: Threading can be used to perform tasks in parallel, where multiple threads can work on the same task simultaneously. This can be useful in applications that involve heavy computation, as it can reduce the time required to complete the task.\n", 46 | "\n", 47 | "Synchronization: Threading provides mechanisms for synchronizing access to shared resources, such as data structures or files, to avoid conflicts and ensure thread safety.\n", 48 | "\n", 49 | "\n", 50 | "\n", 51 | "activeCount(): The activeCount() function is used to get the number of thread objects that are currently active in the program. This can be useful for monitoring the status of threads and ensuring that all threads have completed before exiting the program.\n", 52 | "\n", 53 | "currentThread(): The currentThread() function is used to get a reference to the current thread object that is executing the function. This can be useful for identifying the current thread, and for passing the thread object to other functions for manipulation.\n", 54 | "\n", 55 | "enumerate(): The enumerate() function is used to get a list of all thread objects that are currently active in the program. This can be useful for iterating over all active threads and performing operations on them, such as waiting for them to complete or checking their status.\n" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "id": "e801febd-6190-45fe-8c76-2e126914d5b3", 61 | "metadata": {}, 62 | "source": [ 63 | "3. Explain the following functions\n", 64 | "run()\n", 65 | "start()\n", 66 | "join()\n", 67 | "isAlive()" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "id": "094a97b6-cd10-4b43-b9cb-0092de0585e2", 73 | "metadata": {}, 74 | "source": [ 75 | "run(): The run() method is the entry point for the thread. When a thread is started, its run() method is called. This method can be overridden to define the thread's behavior.\n", 76 | "\n", 77 | "start(): The start() method is used to start the execution of a thread. When the start() method is called, the thread's run() method is executed in a separate thread of execution.\n", 78 | "\n", 79 | "join(): The join() method is used to wait for the thread to complete its execution. When the join() method is called, the program waits until the thread completes before continuing execution.\n", 80 | "\n", 81 | "isAlive(): The isAlive() method is used to check whether the thread is currently executing. The method returns True if the thread is currently executing, and False otherwise." 82 | ] 83 | }, 84 | { 85 | "cell_type": "markdown", 86 | "id": "849ee3e8-ea0d-4904-8671-c11cb2cd7ec1", 87 | "metadata": {}, 88 | "source": [ 89 | "4. write a python program to create two threads. Thread one must print the list of squares and thread\n", 90 | "two must print the list of cubes" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 2, 96 | "id": "17deefe9-272b-45fc-855d-862688cf78bc", 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "Square of 1 = 1\n", 104 | "Square of 2 = 4\n", 105 | "Square of 3 = 9\n", 106 | "Square of 4 = 16\n", 107 | "Square of 5 = 25\n", 108 | "Square of 6 = 36\n", 109 | "Square of 7 = 49\n", 110 | "Square of 8 = 64\n", 111 | "Square of 9 = 81\n", 112 | "Square of 10 = 100\n", 113 | "Cube of 1 = 1\n", 114 | "Cube of 2 = 8\n", 115 | "Cube of 3 = 27\n", 116 | "Cube of 4 = 64\n", 117 | "Cube of 5 = 125\n", 118 | "Cube of 6 = 216\n", 119 | "Cube of 7 = 343\n", 120 | "Cube of 8 = 512\n", 121 | "Cube of 9 = 729\n", 122 | "Cube of 10 = 1000\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "import threading\n", 128 | "\n", 129 | "def print_squares():\n", 130 | " for i in range(1, 11):\n", 131 | " print(f\"Square of {i} = {i**2}\")\n", 132 | "\n", 133 | "def print_cubes():\n", 134 | " for i in range(1, 11):\n", 135 | " print(f\"Cube of {i} = {i**3}\")\n", 136 | "\n", 137 | "t1 = threading.Thread(target=print_squares)\n", 138 | "t2 = threading.Thread(target=print_cubes)\n", 139 | "\n", 140 | "t1.start()\n", 141 | "t2.start()\n", 142 | "\n", 143 | "\n", 144 | "t1.join()\n", 145 | "t2.join()" 146 | ] 147 | }, 148 | { 149 | "cell_type": "markdown", 150 | "id": "bbb6fb3e-286d-454d-ba3f-eda5d237dcd9", 151 | "metadata": {}, 152 | "source": [ 153 | "5. State advantages and disadvantages of multithreading" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "id": "ef8e6d41-6931-4c21-87e9-ea048abf90af", 159 | "metadata": {}, 160 | "source": [ 161 | "Advantages of Multithreading:\n", 162 | "\n", 163 | "Improved performance: Multithreading allows a program to perform multiple tasks simultaneously, which can lead to better performance and faster execution times. This is especially useful in applications that involve I/O or other tasks that are blocking.\n", 164 | "\n", 165 | "Resource sharing: Multithreading allows threads to share resources such as memory and CPU time, which can lead to more efficient resource utilization.\n", 166 | "\n", 167 | "Asynchronous execution: Multithreading can be used to perform asynchronous I/O operations, where the program can continue executing other tasks while waiting for I/O operations to complete. This can improve the responsiveness of the program.\n", 168 | "\n", 169 | "Parallel processing: Multithreading can be used to perform tasks in parallel, where multiple threads can work on the same task simultaneously. This can reduce the time required to complete the task.\n", 170 | "\n", 171 | "Disadvantages of Multithreading:\n", 172 | "\n", 173 | "Complexity: Multithreading adds complexity to a program, making it more difficult to design, develop, and debug. It can be challenging to ensure thread safety and avoid race conditions and deadlocks.\n", 174 | "\n", 175 | "Overhead: Multithreading introduces overhead in terms of memory and CPU time, as each thread requires its own stack and context switching between threads requires additional processing time.\n", 176 | "\n", 177 | "Synchronization issues: Multithreading introduces synchronization issues, as threads may access shared resources simultaneously, leading to conflicts and data corruption.\n", 178 | "\n", 179 | "Debugging challenges: Debugging multithreaded programs can be challenging, as the behavior of the program can be non-deterministic and difficult to reproduce." 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "id": "a5991983-50d1-4508-811d-987af4727f3f", 185 | "metadata": {}, 186 | "source": [ 187 | "6. Explain deadlocks and race conditions." 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "id": "4e80f1bf-6209-4dc6-9eea-45685eef77c5", 193 | "metadata": {}, 194 | "source": [ 195 | "Deadlocks occur when two or more threads are waiting for each other to release resources that they need in order to proceed. Deadlocks can occur when a thread holds a resource and then tries to acquire another resource that is held by another thread, which is also waiting for the first thread to release the resource it holds. This can result in a situation where both threads are waiting for each other to release the resources they need, and neither can proceed. Deadlocks can be difficult to detect and resolve, as they can be intermittent and dependent on timing and scheduling.\n", 196 | "\n", 197 | "Race conditions occur when two or more threads access a shared resource simultaneously and the behavior of the program depends on the order in which the threads execute. Race conditions can occur when a shared resource is not properly synchronized, allowing multiple threads to access it simultaneously. This can lead to unexpected and unpredictable behavior, as the behavior of the program depends on the timing and scheduling of the threads. Race conditions can be difficult to detect and reproduce, as they can be dependent on timing and other factors." 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "id": "833f7b40-a5bd-4840-9fe4-161e078b7745", 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [] 207 | } 208 | ], 209 | "metadata": { 210 | "kernelspec": { 211 | "display_name": "Python 3 (ipykernel)", 212 | "language": "python", 213 | "name": "python3" 214 | }, 215 | "language_info": { 216 | "codemirror_mode": { 217 | "name": "ipython", 218 | "version": 3 219 | }, 220 | "file_extension": ".py", 221 | "mimetype": "text/x-python", 222 | "name": "python", 223 | "nbconvert_exporter": "python", 224 | "pygments_lexer": "ipython3", 225 | "version": "3.10.8" 226 | } 227 | }, 228 | "nbformat": 4, 229 | "nbformat_minor": 5 230 | } 231 | -------------------------------------------------------------------------------- /Machine_Learning_Algorithm/Regression/Regression_Assignment_6.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7a6d3709-f1e9-4f1c-bb93-5b7000683fb5", 6 | "metadata": {}, 7 | "source": [ 8 | "**Q1. What are the key steps involved in building an end-to-end web application, from development to deployment on the cloud?**" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "ff6cff45-0728-4c4a-9ba4-7ea21674eb19", 14 | "metadata": {}, 15 | "source": [ 16 | "Building an end-to-end web application involves several steps, from development to deployment on the cloud. Here are the key steps:\n", 17 | "1. **Requirement Gathering**: Gather and define the requirements for your application. Identify the target audience and user needs, and develop a plan for the application's features and functionality.\n", 18 | "2. **Technology Stack Selection**: Decide on the technology stack you'll use for your application, including programming language, database, and web framework.\n", 19 | "3. **Application Design**: Design the application's architecture, including database schema, application layers, and the user interface.\n", 20 | "4. **Development**: Write the code for the application, using a development environment and version control system.\n", 21 | "5. **Testing**: Test the application to ensure that it meets the requirements and functions as expected.\n", 22 | "6. **Deployment**: Deploy the application to a cloud-based infrastructure, such as Amazon Web Services (AWS), Google Cloud Platform (GCP), or Microsoft Azure.\n", 23 | "7. **Configuration and Scaling**: Configure the application, including setting up databases, web servers, load balancers, and firewalls. Ensure that the application can scale to handle increased traffic.\n", 24 | "8. **Continuous Integration and Continuous Deployment (CI/CD)**: Implement a CI/CD pipeline to automate the deployment process, enabling frequent and reliable updates to the application.\n", 25 | "9. **Monitoring and Maintenance**: Monitor the application's performance and address any issues or bugs that arise. Regularly update and maintain the application to ensure its security and stability.\n", 26 | "\n", 27 | "By following these steps, you can build a robust and scalable end-to-end web application and deploy it to the cloud with confidence." 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "id": "3b263468-0101-4a64-a2d4-cae45ccb435e", 33 | "metadata": {}, 34 | "source": [ 35 | "**Q2. Explain the difference between traditional web hosting and cloud hosting.**" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "id": "75f60dd4-e399-4202-884c-09b05d4ccf81", 41 | "metadata": {}, 42 | "source": [ 43 | "**Traditional web hosting** and **cloud hosting** are two different methods of hosting a website. Here's an explanation of the differences between the two:\n", 44 | "1. **Infrastructure**: Traditional web hosting typically involves a single server that hosts one or more websites. Cloud hosting, on the other hand, involves multiple servers that work together to host a website.\n", 45 | "2. **Scalability**: Traditional hosting is typically limited to the resources of a single server, which can result in performance issues if the website experiences a surge in traffic. Cloud hosting is designed to be scalable, with the ability to easily add or remove server resources as needed to handle fluctuations in traffic.\n", 46 | "3. **Cost**: Traditional hosting is generally less expensive than cloud hosting, but it may not offer the same level of scalability and flexibility. Cloud hosting is generally more expensive, but it offers greater scalability and reliability.\n", 47 | "4. **Maintenance**: With traditional hosting, the website owner is responsible for maintaining the server, including security updates and software patches. With cloud hosting, the hosting provider is responsible for maintaining the servers, which can reduce the burden on the website owner.\n", 48 | "5. **Control**: Traditional hosting offers more control over the server environment, as the website owner has direct access to the server. Cloud hosting offers less control, as the hosting provider manages the servers.\n", 49 | "\n", 50 | "In summary, traditional web hosting involves a single server hosting one or more websites, while cloud hosting involves multiple servers working together to host a website. Cloud hosting offers greater scalability and reliability, but at a higher cost, while traditional hosting is less expensive but may not offer the same level of scalability and flexibility." 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "id": "d42d69a4-d861-4517-bfb1-78c8cdf4be4e", 56 | "metadata": {}, 57 | "source": [ 58 | "**Q3. How do you choose the right cloud provider for your application deployment, and what factors should you consider?**" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "id": "bc1ce8d9-f9e8-4d89-bf77-ed3194ca9586", 64 | "metadata": {}, 65 | "source": [ 66 | "Choosing the **right cloud provider** for your **application deployment** is an important decision that can have significant impacts on your business. Here are some factors to consider:\n", 67 | "1. **Requirements**: Consider your application requirements, such as the expected traffic, storage needs, and the programming language used to build it. Ensure that the cloud provider offers the necessary resources and tools to support your application.\n", 68 | "2. **Scalability**: Your application may grow over time, and it's important to choose a cloud provider that can scale with your needs. Consider the provider's ability to scale resources up or down based on demand.\n", 69 | "3. **Security**: The cloud provider you choose should have robust security measures in place to protect your data and infrastructure. Consider the provider's compliance certifications, data encryption, and network security features.\n", 70 | "4. **Performance**: The cloud provider should offer high-performance computing resources, such as fast processing speeds and low latency, to ensure that your application runs smoothly.\n", 71 | "5. **Availability**: Look for a cloud provider with a high level of availability, ensuring that your application is accessible to users at all times. Consider their Service Level Agreement (SLA) and uptime guarantees.\n", 72 | "6. **Cost**: Consider the cost of the cloud provider's services, including their pricing model and any additional fees. Ensure that the provider's pricing aligns with your budget.\n", 73 | "7. **Support**: Choose a cloud provider that offers reliable and responsive customer support. Consider their support channels, response times, and expertise.\n", 74 | "\n", 75 | "By evaluating these factors and comparing the offerings of different cloud providers, you can select the provider that best meets the needs of your application and business." 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "id": "4ca76d0b-1c55-4bbf-8931-02c0fba57bbc", 81 | "metadata": {}, 82 | "source": [ 83 | "**Q4. How do you design and build a responsive user interface for your web application, and what are some best practices to follow?**" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "id": "6f9ad7be-4717-4dda-a42c-a5078ca0358d", 89 | "metadata": {}, 90 | "source": [ 91 | "Designing and building a **responsive user interface** for your **web application** involves creating a **layout** that adapts to **different screen sizes and devices**. Here are some best practices to follow:\n", 92 | "1. **Use a mobile-first approach**: Start designing your UI for mobile devices, and then expand to larger screens. This approach ensures that the UI is optimized for smaller screens, and then adapts to larger screens as needed.\n", 93 | "2. **Keep it simple**: Keep the UI simple and focused, with minimal distractions. Use clear and concise language, and avoid cluttering the screen with too much information.\n", 94 | "3. **Optimize for touch**: Since most mobile devices are touch-based, optimize your UI for touch interactions. Use larger buttons and clickable areas, and ensure that the UI is easy to navigate with a finger.\n", 95 | "4. **Use flexible layouts**: Use flexible layouts that adjust to the screen size and orientation. Use CSS media queries to adjust the layout based on the screen size.\n", 96 | "5. **Prioritize content**: Prioritize the most important content on the screen, and ensure that it is easy to find and interact with. Use white space to separate content, and use contrasting colors to highlight important information.\n", 97 | "6. **Test and iterate**: Test your UI on different devices and screen sizes, and iterate based on user feedback. Ensure that the UI is usable and accessible for users with different abilities.\n", 98 | "7. **Use responsive frameworks**: Consider using responsive frameworks like Bootstrap or Foundation, which provide pre-designed components and layouts that adapt to different screen sizes.\n", 99 | "\n", 100 | "By following these best practices, you can design and build a responsive UI that provides a consistent and user-friendly experience across different devices and screen sizes." 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "id": "ee59a5a1-22b4-46a3-afa4-3cad6e3330d9", 106 | "metadata": {}, 107 | "source": [ 108 | "**Q5. How do you integrate the machine learning model with the user interface for the Algerian Forest Fires project (which we discussed in class), and what APIs or libraries can you use for this purpose?**" 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "id": "3a9ab655-6d63-4cb1-9f0b-2f1f58cf6eda", 114 | "metadata": {}, 115 | "source": [ 116 | "Integrating a **machine learning model** with a **user interface** for the **Algerian Forest Fires project** involves designing and building an interface that allows users to **input data, run the model, and display the results.** Here are some steps and APIs/libraries you can use for this purpose:\n", 117 | "1. **Preprocessing**: The first step is to preprocess the input data before running it through the model. This can involve cleaning, formatting, and scaling the data. You can use libraries like Pandas and NumPy for this purpose.\n", 118 | "2. **Model training**: Train your machine learning model on the preprocessed data. You can use libraries like Scikit-learn, TensorFlow, or PyTorch for model training.\n", 119 | "3. **Model deployment**: Deploy the trained model using a framework like Flask or Django, which allows you to create a web application that can serve the model predictions.\n", 120 | "4. **User interface design**: Design the user interface using HTML, CSS, and JavaScript. You can use frameworks like Bootstrap or React to create a responsive and user-friendly interface.\n", 121 | "5. **API integration**: Integrate the machine learning model API with the user interface using JavaScript or jQuery. You can use libraries like Axios or Fetch to make API calls and retrieve the model predictions.\n", 122 | "6. **Visualization**: Visualize the model predictions using libraries like Plotly or D3.js to provide a clear and intuitive representation of the results.\n", 123 | "\n", 124 | "By following these steps and using appropriate APIs/libraries, you can integrate the machine learning model with the user interface for the Algerian Forest Fires project, allowing users to input data and get predictions in a user-friendly and interactive way." 125 | ] 126 | } 127 | ], 128 | "metadata": { 129 | "kernelspec": { 130 | "display_name": "Python 3 (ipykernel)", 131 | "language": "python", 132 | "name": "python3" 133 | }, 134 | "language_info": { 135 | "codemirror_mode": { 136 | "name": "ipython", 137 | "version": 3 138 | }, 139 | "file_extension": ".py", 140 | "mimetype": "text/x-python", 141 | "name": "python", 142 | "nbconvert_exporter": "python", 143 | "pygments_lexer": "ipython3", 144 | "version": "3.10.8" 145 | } 146 | }, 147 | "nbformat": 4, 148 | "nbformat_minor": 5 149 | } 150 | -------------------------------------------------------------------------------- /Python-Assignment/Python_Assignment_4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ca1cecd5-3139-4e25-88ba-d43b59d7b7b7", 6 | "metadata": {}, 7 | "source": [ 8 | "1. Which keyword is used to create a function? Create a function to return a list of \n", 9 | " odd numbers in the range of 1 to 25.\n", 10 | " Ans> The keyword used to create a function in Python is \"def\".\n", 11 | " Here's the code to create a function that returns a list of odd numbers in the range of 1 to 25\n", 12 | "\n" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 2, 18 | "id": "905c3e4e-90c9-4b68-9a23-98cd63e8a3dd", 19 | "metadata": {}, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "text/plain": [ 24 | "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]" 25 | ] 26 | }, 27 | "execution_count": 2, 28 | "metadata": {}, 29 | "output_type": "execute_result" 30 | } 31 | ], 32 | "source": [ 33 | "def odd_numbers():\n", 34 | " odd_list = []\n", 35 | " for i in range(1, 26):\n", 36 | " if i % 2 != 0:\n", 37 | " odd_list.append(i)\n", 38 | " return odd_list\n", 39 | "odd_numbers()" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "id": "4a87bc3f-8683-4bef-a395-4a32ba63552d", 45 | "metadata": {}, 46 | "source": [ 47 | "2. Why *args and **kwargs is used in some functions? Create a function each for *args and **kwargs \n", 48 | " to demonstrate their use.\n" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "id": "def7438f-c210-4f90-a1f9-ade4257af89c", 54 | "metadata": {}, 55 | "source": [ 56 | "args and **kwargs are used in functions to allow them to accept a varying number of arguments.\n", 57 | "*args is used to send a non-keyworded variable length argument list to the function.\n", 58 | " It allows you to pass a variable number of arguments to a function, which will be received as a tuple.\n", 59 | "**kwargs is used to pass keyworded variable length of arguments to a function,\n", 60 | " which will be received as a dictionary.\n", 61 | "Here's an example of how you can use *args and **kwargs in a function:" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 5, 67 | "id": "8b829b60-5714-4685-a2c9-04f1f780331f", 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "\n", 72 | "def print_args(*args):\n", 73 | " print(\"Arguments passed as *args:\", args)\n", 74 | " \n", 75 | "def print_kwargs(**kwargs):\n", 76 | " print(\"Arguments passed as **kwargs:\", kwargs)\n", 77 | "\n" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "id": "453373c8-c03c-4cfd-9336-20f1a1cf30fc", 83 | "metadata": {}, 84 | "source": [ 85 | "3. What is an iterator in python? Name the method used to initialise the iterator object and the method \n", 86 | " used for iteration. Use these methods to print the first five elements of the given list \n", 87 | " [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]." 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "id": "a20159b5-bf3a-4ca9-af1b-f8225c7610b5", 93 | "metadata": {}, 94 | "source": [ 95 | "An iterator in Python is an object that implements the iterator protocol,\n", 96 | " which consists of two methods: __iter__() and __next__(). \n", 97 | " The __iter__() method is used to initialize the iterator object, \n", 98 | " and the __next__() method is used for iteration." 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 7, 104 | "id": "d22c0c9b-9e58-45ad-8156-91ad619566ea", 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "The first 5 numbers:\n", 112 | "2\n", 113 | "4\n", 114 | "6\n", 115 | "8\n", 116 | "10\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "#example\n", 122 | "numbers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n", 123 | "\n", 124 | "class TopNumbers:\n", 125 | " def __init__(self, limit):\n", 126 | " self.limit = limit\n", 127 | " self.counter = 0\n", 128 | "\n", 129 | " def __iter__(self):\n", 130 | " return self\n", 131 | "\n", 132 | " def __next__(self):\n", 133 | " if self.counter < self.limit:\n", 134 | " number = numbers[self.counter]\n", 135 | " self.counter += 1\n", 136 | " return number\n", 137 | " else:\n", 138 | " raise StopIteration\n", 139 | "\n", 140 | "print(\"The first 5 numbers:\")\n", 141 | "for number in TopNumbers(5):\n", 142 | " print(number)\n" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "id": "7821101a-bdcf-401b-910d-8f6bb5baeb86", 148 | "metadata": {}, 149 | "source": [ 150 | "4. What is a generator function in python? Why yield keyword is used? \n", 151 | " Give an example of a generator function.\n", 152 | " Ans> A generator function in Python is a function that returns a generator iterator.\n", 153 | " The yield keyword is used to return a value from a generator function and pause the \n", 154 | " function's execution. When the generator function is called again, it resumes execution\n", 155 | " from where it left off, using the saved state from the last call to yield." 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 6, 161 | "id": "e9b2c3f7-52b3-4423-9161-5c99e73e6dde", 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "name": "stdout", 166 | "output_type": "stream", 167 | "text": [ 168 | "Fibonacci sequence:\n", 169 | "0\n", 170 | "1\n", 171 | "1\n", 172 | "2\n", 173 | "3\n", 174 | "5\n", 175 | "8\n", 176 | "13\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "\n", 182 | "def fibonacci_sequence(limit):\n", 183 | " a, b = 0, 1\n", 184 | " while a < limit:\n", 185 | " yield a\n", 186 | " a, b = b, a + b\n", 187 | "\n", 188 | "fib = fibonacci_sequence(20)\n", 189 | "\n", 190 | "print(\"Fibonacci sequence:\")\n", 191 | "for number in fib:\n", 192 | " print(number) " 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "id": "f8cca7aa-f571-43fd-b584-18403b55ead1", 198 | "metadata": {}, 199 | "source": [ 200 | "5. Create a generator function for prime numbers less than 1000.\n", 201 | " Use the next() method to print the first 20 prime numbers" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 5, 207 | "id": "72945fdc-e12e-4dce-9ef6-fc046e88597a", 208 | "metadata": { 209 | "tags": [] 210 | }, 211 | "outputs": [ 212 | { 213 | "name": "stdout", 214 | "output_type": "stream", 215 | "text": [ 216 | "The first 20 prime numbers:\n", 217 | "2\n", 218 | "3\n", 219 | "5\n", 220 | "7\n", 221 | "11\n", 222 | "13\n", 223 | "17\n", 224 | "19\n", 225 | "23\n", 226 | "29\n", 227 | "31\n", 228 | "37\n", 229 | "41\n", 230 | "43\n", 231 | "47\n", 232 | "53\n", 233 | "59\n", 234 | "61\n", 235 | "67\n", 236 | "71\n" 237 | ] 238 | } 239 | ], 240 | "source": [ 241 | "\n", 242 | "def prime_numbers():\n", 243 | " yield 2\n", 244 | " primes = [2]\n", 245 | " candidate = 3\n", 246 | " while candidate < 1000:\n", 247 | " is_prime = True\n", 248 | " for prime in primes:\n", 249 | " if candidate % prime == 0:\n", 250 | " is_prime = False\n", 251 | " break\n", 252 | " if is_prime:\n", 253 | " primes.append(candidate)\n", 254 | " yield candidate\n", 255 | " candidate += 2\n", 256 | "\n", 257 | "primes = prime_numbers()\n", 258 | "\n", 259 | "print(\"The first 20 prime numbers:\")\n", 260 | "for i in range(20):\n", 261 | " print(next(primes))" 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "id": "2125196a-be4d-452c-8e40-aec709f95163", 267 | "metadata": {}, 268 | "source": [ 269 | "6.Write a python program to print the first 10 Fibonacci numbers using a while looP" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 4, 275 | "id": "97520bb4-7339-46da-bed3-d7a7ae85ff29", 276 | "metadata": {}, 277 | "outputs": [ 278 | { 279 | "name": "stdout", 280 | "output_type": "stream", 281 | "text": [ 282 | "0\n", 283 | "1\n", 284 | "1\n", 285 | "2\n", 286 | "3\n", 287 | "5\n", 288 | "8\n", 289 | "13\n", 290 | "21\n", 291 | "34\n" 292 | ] 293 | } 294 | ], 295 | "source": [ 296 | "\n", 297 | "a, b = 0, 1\n", 298 | "count = 0\n", 299 | "\n", 300 | "while count < 10:\n", 301 | " print(a)\n", 302 | " a, b = b, a + b\n", 303 | " count += 1\n" 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "id": "6bf52f5b-9bd2-4d6f-8dac-3b5c6fb42f9e", 309 | "metadata": {}, 310 | "source": [ 311 | "7. Write a List Comprehension to iterate through the given string: ‘pwskills’.\n", 312 | " Expected output: ['p', 'w', 's', 'k', 'i', 'l', 'l', 's'] " 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 11, 318 | "id": "52e97cb1-11c4-4493-a261-19fd77023046", 319 | "metadata": {}, 320 | "outputs": [ 321 | { 322 | "name": "stdout", 323 | "output_type": "stream", 324 | "text": [ 325 | "['p', 'w', 's', 'k', 'i', 'l', 'l', 's']\n" 326 | ] 327 | } 328 | ], 329 | "source": [ 330 | "\n", 331 | "string = 'pwskills'\n", 332 | "characters = [char for char in string]\n", 333 | "print(characters)\n" 334 | ] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "id": "b3631819-ebb2-4d37-bc7c-e906843e4607", 339 | "metadata": {}, 340 | "source": [ 341 | "8. Write a python program to check whether a given number is Palindrome or not using a while loop." 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 3, 347 | "id": "c2a8b578-5e11-4e6f-a4c9-2c09fa30b365", 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "name": "stdin", 352 | "output_type": "stream", 353 | "text": [ 354 | "Enter a number: 12321\n" 355 | ] 356 | }, 357 | { 358 | "name": "stdout", 359 | "output_type": "stream", 360 | "text": [ 361 | "12321 is a palindrome.\n" 362 | ] 363 | } 364 | ], 365 | "source": [ 366 | "\n", 367 | "def is_palindrome(num):\n", 368 | " original = num\n", 369 | " reverse = 0\n", 370 | " while num > 0:\n", 371 | " digit = num % 10\n", 372 | " reverse = reverse * 10 + digit\n", 373 | " num = num // 10\n", 374 | " return original == reverse\n", 375 | "\n", 376 | "number = int(input(\"Enter a number: \"))\n", 377 | "if is_palindrome(number):\n", 378 | " print(f\"{number} is a palindrome.\")\n", 379 | "else:\n", 380 | " print(f\"{number} is not a palindrome.\")\n" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "id": "34afa45a-1827-4180-ab09-0bd6715a7858", 387 | "metadata": {}, 388 | "outputs": [], 389 | "source": [] 390 | } 391 | ], 392 | "metadata": { 393 | "kernelspec": { 394 | "display_name": "Python 3 (ipykernel)", 395 | "language": "python", 396 | "name": "python3" 397 | }, 398 | "language_info": { 399 | "codemirror_mode": { 400 | "name": "ipython", 401 | "version": 3 402 | }, 403 | "file_extension": ".py", 404 | "mimetype": "text/x-python", 405 | "name": "python", 406 | "nbconvert_exporter": "python", 407 | "pygments_lexer": "ipython3", 408 | "version": "3.10.8" 409 | } 410 | }, 411 | "nbformat": 4, 412 | "nbformat_minor": 5 413 | } 414 | -------------------------------------------------------------------------------- /Machine_Learning_Algorithm/NaiveBayes/nb_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "6136dea7-4dc2-49b7-98e1-92f319896602", 6 | "metadata": {}, 7 | "source": [ 8 | "**Q1. What is Bayes' theorem?**" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "id": "58766f5f-f471-4253-b685-26bccfe0d578", 14 | "metadata": {}, 15 | "source": [ 16 | "Bayes' theorem, named after Reverend Thomas Bayes, is a mathematical formula that describes the relationship between conditional probabilities.\n", 17 | "\n", 18 | "In its simplest form, Bayes' theorem states that the probability of a hypothesis H given some observed evidence E is proportional to the probability of the evidence E given the hypothesis H, multiplied by the prior probability of the hypothesis H: **P(H|E) = P(E|H) * P(H) / P(E)**\n", 19 | "\n", 20 | "Where:
\n", 21 | "P(H|E) is the posterior probability of H given E (what we want to know)
P(E|H) is the likelihood of observing the evidence E given the hypothesis H (how well the evidence supports the hypothesis)
P(H) is the prior probability of the hypothesis H (our initial belief in the hypothesis before seeing the evidence)
P(E) is the marginal probability of the evidence E (the total probability of observing the evidence, regardless of the hypothesis)\n", 22 | "\n", 23 | "Bayes' theorem is a fundamental concept in probability & has many applications in fields such as statistics, machine learning, and artificial intelligence. It provides a way to update our beliefs about the world in light of new evidence and is essential for making informed decisions in uncertain situations." 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "id": "70b5579a-8497-45f3-851a-a240e28c685f", 29 | "metadata": {}, 30 | "source": [ 31 | "**Q2. What is the formula for Bayes' theorem?**" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "id": "b9cb4ecb-44e6-4568-8f66-6d6002c40585", 37 | "metadata": {}, 38 | "source": [ 39 | "The formula for Bayes' theorem is:
**P(H|E) = P(E|H) * P(H) / P(E)**\n", 40 | "\n", 41 | "Where:
\n", 42 | "P(H|E) is the posterior probability of H given E (what we want to know)
P(E|H) is the likelihood of observing the evidence E given the hypothesis H (how well the evidence supports the hypothesis)
P(H) is the prior probability of the hypothesis H (our initial belief in the hypothesis before seeing the evidence)
P(E) is the marginal probability of the evidence E (the total probability of observing the evidence, regardless of the hypothesis)\n", 43 | "\n", 44 | "This formula is used to update our beliefs about the probability of a hypothesis H being true based on new evidence E. It is a powerful tool in Bayesian statistics and machine learning, allowing us to make more accurate predictions and decisions based on data." 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "id": "6b289975-01b8-4f73-900b-56b55beb0d6f", 50 | "metadata": {}, 51 | "source": [ 52 | "**Q3. How is Bayes' theorem used in practice?**" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "id": "0b233ed6-b291-4229-9e85-99f2eff7c202", 58 | "metadata": {}, 59 | "source": [ 60 | "Bayes' theorem is used in practice in a wide range of fields, including statistics, machine learning, artificial intelligence, and decision-making. Also, Bayes' theorem is used in some other applications, which are given below:\n", 61 | "1. **Medical diagnosis**: In medicine, Bayes' theorem can be used to calculate the probability of a patient having a disease given their symptoms and other medical information. Doctors can use this information to make more informed decisions about diagnosis and treatment.\n", 62 | "2. **Spam filtering**: Bayes' theorem is used in spam filtering algorithms to calculate the probability that an email is spam given its contents. This helps email providers identify and filter out unwanted messages.\n", 63 | "3. **Predictive modeling**: In machine learning, Bayes' theorem is used to build predictive models that can make accurate predictions based on past data. This can be useful in a wide range of applications, from stock market forecasting to weather prediction.\n", 64 | "4. **A/B testing**: Bayes' theorem is used in A/B testing to determine the probability that one version of a product or service is better than another based on user behavior. This helps businesses optimize their offerings and improve customer satisfaction.\n", 65 | "5. **Decision-making**: Bayes' theorem can be used to make decisions in uncertain situations by calculating the expected value of different options. This approach is often used in game theory, finance, and other fields where decision-making under uncertainty is important.\n", 66 | "\n", 67 | "Overall, Bayes' theorem provides a powerful framework for making predictions and decisions based on data and has numerous practical applications in a wide range of fields." 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "id": "ce27e3a8-cb67-484c-af7a-6dea4b002aca", 73 | "metadata": {}, 74 | "source": [ 75 | "**Q4. What is the relationship between Bayes' theorem and conditional probability?**" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "id": "8da61ac6-69c1-4629-bb8c-55b3006a7f06", 81 | "metadata": {}, 82 | "source": [ 83 | "Bayes' theorem is a mathematical formula that describes the relationship between conditional probabilities. In fact, Bayes' theorem can be derived from the definition of conditional probability.\n", 84 | "\n", 85 | "Conditional probability is the probability of an event states that another event has already occurred. For example, the probability of rolling a six on a die given that the die has already been rolled is a conditional probability. Denoted as P(A|B), where A and B are events, it's stated as \"Probability of A given B.\"\n", 86 | "\n", 87 | "Bayes' theorem provides a way to calculate the conditional probability of one event given another event and some prior information. It allows us to update our prior beliefs about the probability of an event occurring based on new evidence.\n", 88 | "\n", 89 | "The formula for Bayes' theorem involves multiplying the likelihood of the observed evidence given the hypothesis by the prior probability of the hypothesis and dividing by the marginal probability of the evidence. In this way, Bayes' theorem combines prior beliefs with new evidence to calculate a revised probability of a hypothesis being true.\n", 90 | "\n", 91 | "So, in summary, Bayes' theorem and conditional probability are related because Bayes' theorem provides a way to calculate conditional probabilities based on prior information and new evidence." 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "id": "1db07c65-f2cd-4a43-9c0c-7a3a889b1e4e", 97 | "metadata": {}, 98 | "source": [ 99 | "**Q5. How do you choose which type of Naive Bayes classifier to use for any given problem?**" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "id": "aaa7c01d-6cc2-4541-9795-d3bc3bf03062", 105 | "metadata": {}, 106 | "source": [ 107 | "The Naive Bayes classifier is a simple and effective machine learning algorithm that is used for a wide range of applications such as text classification, spam filtering, sentiment analysis, and more. There are three types of Naive Bayes classifiers: Gaussian Naive Bayes, Multinomial Naive Bayes, and Bernoulli Naive Bayes. The choice of which Naive Bayes classifier to use for a given problem depends on the nature of the data and the problem at hand. Here are some guidelines to help you choose the appropriate type of Naive Bayes classifier:\n", 108 | "1. **Gaussian Naive Bayes**: This type of classifier is used when the features are continuous variables that follow a Gaussian distribution. It is often used for problems such as image classification and spam filtering, where the features are numeric values.\n", 109 | "2. **Multinomial Naive Bayes**: This type of classifier is used when the features are discrete variables such as word counts in text classification problems. It is often used in natural language processing applications, such as sentiment analysis and text classification.\n", 110 | "3. **Bernoulli Naive Bayes**: This type of classifier is similar to the Multinomial Naive Bayes classifier, but it is used for solving some text classification problems when the features are binary variables such as the presence or absence of a particular word in a document.\n", 111 | "\n", 112 | "In summary, the choice of Naive Bayes classifier depends on the nature of the data and the problem at hand. Gaussian Naive Bayes is used for continuous variables, Multinomial Naive Bayes for discrete variables, and Bernoulli Naive Bayes for binary variables." 113 | ] 114 | }, 115 | { 116 | "cell_type": "markdown", 117 | "id": "d9574295-c895-478d-a09e-387a4c4482cf", 118 | "metadata": {}, 119 | "source": [ 120 | "**Q6. Assignment:**
\n", 121 | "**You have a dataset with two features, X1 and X2, and two possible classes, A and B. You want to use Naive Bayes to classify a new instance with features X1 = 3 and X2 = 4. The following table shows the frequency of each feature value for each class:**
\n", 122 | "**A=[3, 3, 4, 4, 3, 3, 3]**
\n", 123 | "**B=[2, 2, 1, 2, 2, 2, 3]**
\n", 124 | "**Assuming equal prior probabilities for each class, which class would Naive Bayes predict the new instance to belong to?**" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "id": "adaeaa24-ee7f-44b3-bd3c-fd0d04134cac", 130 | "metadata": {}, 131 | "source": [ 132 | "To use Naive Bayes to classify the new instance with features X1 = 3 and X2 = 4, we need to calculate the posterior probability of the instance belonging to each class, given the values of X1 and X2. The formula for Naive Bayes is given below:\n", 133 | "\n", 134 | "**P(class|X1,X2) = P(X1,X2|class) * P(class) / P(X1,X2)**\n", 135 | "\n", 136 | "where class is the class label A or B, and P(X1,X2|class) denotes observing the feature values given the class label. P(class) is the prior probability of the class, and P(X1,X2) is the marginal probability of observing the feature values.\n", 137 | "\n", 138 | "To calculate the likelihood of observing the feature values for each class, we can use the frequency table provided in the problem:\n", 139 | "\n", 140 | "P(X1=3,X2=4|A) = 3/7 * 4/7 = 0.1224
\n", 141 | "P(X1=3,X2=4|B) = 0/7 * 1/7 = 0\n", 142 | "\n", 143 | "To calculate the prior probability of each class, we assume that they are equal, i.e., P(A) = P(B) = 0.5.\n", 144 | "\n", 145 | "To calculate the marginal probability of observing the feature values, we can use the law of total probability:\n", 146 | "\n", 147 | "P(X1=3,X2=4) = P(X1=3,X2=4|A) * P(A) + P(X1=3,X2=4|B) * P(B)
\n", 148 | "= 0.1224 * 0.5 + 0 * 0.5
\n", 149 | "= 0.0612\n", 150 | "\n", 151 | "Finally, we can calculate the posterior probability of the instance belonging to each class:\n", 152 | "\n", 153 | "P(A|X1=3,X2=4) = P(X1=3,X2=4|A) * P(A) / P(X1=3,X2=4)
\n", 154 | "= 0.1224 * 0.5 / 0.0612
\n", 155 | "= 1\n", 156 | "\n", 157 | "P(B|X1=3,X2=4) = P(X1=3,X2=4|B) * P(B) / P(X1=3,X2=4)
\n", 158 | "= 0 * 0.5 / 0.0612
\n", 159 | "= 0\n", 160 | "\n", 161 | "Therefore, Naive Bayes would predict that the new instance belongs to class A.\n", 162 | "\n", 163 | "**ALTERNATIVE METHOD**\n", 164 | "\n", 165 | "To predict the class of a new instance with features X1 = 3 and X2 = 4 using Naive Bayes, we need to calculate the posterior probabilities of the instance belonging to each class, given the observed feature values. We can use the Naive Bayes formula:\n", 166 | "\n", 167 | "**P(class|X1=3,X2=4) = P(X1=3,X2=4|class) * P(class) / P(X1=3,X2=4)**\n", 168 | "\n", 169 | "where P(X1=3,X2=4|class) is the likelihood of observing the feature values given the class, P(class) is the prior probability of the class, and P(X1=3,X2=4) is the marginal probability of observing the feature values.\n", 170 | "\n", 171 | "To calculate the likelihood of observing the feature values for each class, we can count the number of times each feature value occurs in the training data for each class and compute the corresponding probabilities:\n", 172 | "\n", 173 | "P(X1=3|A) = 4/7, P(X2=4|A) = 3/7, P(X1=3|B) = 0/7, P(X2=4|B) = 1/7\n", 174 | "\n", 175 | "Using these values, we can calculate the posterior probabilities of the new instance belonging to each class, given the observed feature values:\n", 176 | "\n", 177 | "P(A|X1=3,X2=4) = P(X1=3,X2=4|A) * P(A) / P(X1=3,X2=4) = (4/7) * (1/2) / ((4/7)(1/2) + (0/7)(1/2)) = 1
\n", 178 | "P(B|X1=3,X2=4) = P(X1=3,X2=4|B) * P(B) / P(X1=3,X2=4) = (0/7) * (1/2) / ((4/7)(1/2) + (0/7)(1/2)) = 0\n", 179 | "\n", 180 | "Therefore, Naive Bayes would predict that the new instance belongs to class A." 181 | ] 182 | } 183 | ], 184 | "metadata": { 185 | "kernelspec": { 186 | "display_name": "Python 3 (ipykernel)", 187 | "language": "python", 188 | "name": "python3" 189 | }, 190 | "language_info": { 191 | "codemirror_mode": { 192 | "name": "ipython", 193 | "version": 3 194 | }, 195 | "file_extension": ".py", 196 | "mimetype": "text/x-python", 197 | "name": "python", 198 | "nbconvert_exporter": "python", 199 | "pygments_lexer": "ipython3", 200 | "version": "3.10.8" 201 | } 202 | }, 203 | "nbformat": 4, 204 | "nbformat_minor": 5 205 | } 206 | -------------------------------------------------------------------------------- /Machine_Learning_Algorithm/Regression/Regression_Assignment_5.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "30c18ddb", 6 | "metadata": {}, 7 | "source": [ 8 | "# Question No. 1:\n", 9 | "What is Elastic Net Regression and how does it differ from other regression techniques?\n", 10 | "\n", 11 | "## Answer:\n", 12 | "Elastic Net regression is a type of linear regression that combines the L1 and L2 regularization methods to overcome some of the limitations of each individual method. The L1 regularization method, also known as Lasso, helps in feature selection by shrinking the coefficients of less important variables to zero. On the other hand, the L2 regularization method, also known as Ridge, helps in handling multicollinearity by shrinking the coefficients of highly correlated variables.\n", 13 | "\n", 14 | "Compared to other regression techniques, such as linear regression, Lasso, and Ridge regression, Elastic Net regression has the advantage of being able to handle situations where there are many correlated predictor variables. It also provides a better balance between feature selection and handling multicollinearity, making it more suitable for high-dimensional datasets with many variables. However, Elastic Net regression is computationally more intensive than other regression techniques and requires careful tuning of its hyperparameters." 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "id": "f462ce44", 20 | "metadata": {}, 21 | "source": [ 22 | "# Question No. 2:\n", 23 | "How do you choose the optimal values of the regularization parameters for Elastic Net Regression?\n", 24 | "\n", 25 | "## Answer:\n", 26 | "Choosing the optimal values of the regularization parameters for Elastic Net Regression requires a two-step process:\n", 27 | "\n", 28 | "Cross-Validation:\n", 29 | "The first step involves using cross-validation to estimate the predictive performance of the model for different values of the regularization parameters. Cross-validation involves splitting the data into training and validation sets multiple times and training the model on the training set while evaluating its performance on the validation set. This process is repeated for different values of the regularization parameters, and the values that result in the best predictive performance are chosen.\n", 30 | "\n", 31 | "Grid Search:\n", 32 | "The second step involves performing a grid search over a range of values for the regularization parameters. Grid search involves specifying a range of values for each of the regularization parameters and testing all possible combinations of these values. The combination of values that results in the best performance during cross-validation is chosen as the optimal regularization parameters for the Elastic Net Regression model." 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "id": "9ba6ac04", 38 | "metadata": {}, 39 | "source": [ 40 | "# Question No. 3:\n", 41 | "What are the advantages and disadvantages of Elastic Net Regression?\n", 42 | "\n", 43 | "## Answer:\n", 44 | "**Advantages:**\n", 45 | "\n", 46 | "1. **Feature Selection:** Elastic Net Regression can perform feature selection by shrinking the coefficients of less important variables to zero, resulting in a more interpretable and efficient model.\n", 47 | "2. **Handles multicollinearity:** Elastic Net Regression can handle multicollinearity, which occurs when predictor variables are highly correlated with each other, by shrinking the coefficients of highly correlated variables.\n", 48 | "3. **Works well with high-dimensional data:** Elastic Net Regression is particularly useful when working with high-dimensional data where the number of predictors is much larger than the number of observations.\n", 49 | "4. **Flexibility:** Elastic Net Regression offers a balance between the L1 and L2 regularization methods, which makes it more flexible than the individual regularization methods.\n", 50 | "\n", 51 | "**Disadvantages:**\n", 52 | "\n", 53 | "1. **Computational Intensity:** Elastic Net Regression can be computationally intensive, especially when dealing with large datasets, due to the cross-validation and grid search process required to select the optimal regularization parameters.\n", 54 | "2. **May require tuning of hyperparameters:** Elastic Net Regression requires tuning of hyperparameters such as the regularization parameters to achieve optimal performance, which may be difficult to do in practice.\n", 55 | "3. **Assumes linear relationships:** Like other linear regression models, Elastic Net Regression assumes a linear relationship between the predictor variables and the response variable, which may not always be the case.\n", 56 | "4. **Limited to linear models:** Elastic Net Regression is limited to linear models and may not be suitable for more complex non-linear relationships between the predictor variables and the response variable." 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "id": "6c358c90", 62 | "metadata": {}, 63 | "source": [ 64 | "# Question No. 4:\n", 65 | "What are some common use cases for Elastic Net Regression?\n", 66 | "\n", 67 | "## Answer:\n", 68 | "Some common use cases for Elastic Net Regression include:\n", 69 | "\n", 70 | "- **Gene expression analysis:** Elastic Net Regression can be used to identify genes that are associated with a particular disease or condition by analyzing gene expression data. In this case, Elastic Net Regression can be used to perform feature selection and identify the most important genes associated with the disease.\n", 71 | "\n", 72 | "- **Finance:** Elastic Net Regression can be used in finance to analyze the relationship between different economic factors and stock prices, bond yields, or other financial metrics. In this case, Elastic Net Regression can be used to select the most relevant factors that are driving the financial metrics and build a predictive model.\n", 73 | "\n", 74 | "- **Marketing:** Elastic Net Regression can be used in marketing to identify the factors that influence consumer behavior and buying patterns. In this case, Elastic Net Regression can be used to analyze customer data and identify the most important factors that influence buying decisions." 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "id": "d97b4222", 80 | "metadata": {}, 81 | "source": [ 82 | "# Question No. 5:\n", 83 | "How do you interpret the coefficients in Elastic Net Regression?\n", 84 | "\n", 85 | "## Answer:\n", 86 | "The coefficients can be interpreted as follows:\n", 87 | "\n", 88 | "- **Sign:** The sign of the coefficient indicates the direction of the relationship between the predictor variable and the response variable. A positive coefficient indicates a positive relationship, while a negative coefficient indicates a negative relationship.\n", 89 | "\n", 90 | "- **Magnitude:** The magnitude of the coefficient indicates the strength of the relationship between the predictor variable and the response variable. Larger magnitude coefficients indicate stronger relationships, while smaller magnitude coefficients indicate weaker relationships.\n", 91 | "\n", 92 | "- **Regularization Penalty:** The coefficients in Elastic Net Regression are subject to a regularization penalty, which means that the magnitude of the coefficients is shrunk towards zero. This regularization penalty can result in some coefficients being exactly zero, which indicates that the corresponding predictor variable has been excluded from the model." 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "id": "71dce4d8", 98 | "metadata": {}, 99 | "source": [ 100 | "# Question No. 6:\n", 101 | "How do you handle missing values when using Elastic Net Regression?\n", 102 | "\n", 103 | "## Answer:\n", 104 | "There are several ways to handle missing values when using Elastic Net Regression:\n", 105 | "\n", 106 | "1. **Complete Case Analysis:** One approach is to simply remove any observations that have missing values. This approach is known as complete case analysis or listwise deletion. While this method is simple to implement, it may result in a loss of valuable data, especially if there are a large number of missing values.\n", 107 | "\n", 108 | "2. **Imputation:** Another approach is to impute missing values with an estimate of their value. There are several methods of imputation, including mean imputation, median imputation, and regression imputation. Mean and median imputation involve replacing missing values with the mean or median of the observed values for that variable. Regression imputation involves using other predictor variables to estimate the missing value.\n", 109 | "\n", 110 | "3. **Modeling Missingness:** A third approach is to model the missingness of the data as a function of the observed data, and include the missingness model as an additional predictor variable in the Elastic Net Regression model. This approach can be useful when missingness is not completely random and may contain some information that is useful for prediction." 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "id": "7aa3ab59", 116 | "metadata": {}, 117 | "source": [ 118 | "# Question No. 7:\n", 119 | "How do you use Elastic Net Regression for feature selection?\n", 120 | "\n", 121 | "## Answer:\n", 122 | "To use Elastic Net Regression for feature selection, one typically follows these steps:\n", 123 | "\n", 124 | "1. **Prepare the Data:** Prepare the data by standardizing the predictor variables to have mean zero and standard deviation one. This is important for the regularization penalty in Elastic Net Regression to work properly.\n", 125 | "\n", 126 | "2. **Select a Range of Regularization Parameters:** Elastic Net Regression has two tuning parameters, alpha and lambda, which control the strength of the regularization penalty. Choose a range of alpha and lambda values to perform a grid search over. Alpha controls the mix of L1 and L2 regularization, with a value of 0 representing L2 regularization only and a value of 1 representing L1 regularization only. Lambda controls the strength of the regularization penalty, with larger values of lambda resulting in more coefficients being exactly zero.\n", 127 | "\n", 128 | "3. **Fit the Model:** Fit an Elastic Net Regression model to the data using each combination of alpha and lambda values in the grid search. This will result in a set of models with different coefficients and different numbers of non-zero coefficients.\n", 129 | "\n", 130 | "4. **Evaluate Model Performance:** Evaluate the performance of each model using cross-validation or another appropriate method. One common method is to use k-fold cross-validation to estimate the mean squared error of each model.\n", 131 | "\n", 132 | "5. **Select the Optimal Model:** Select the model with the best performance, which will typically have the right balance of model complexity and predictive accuracy. The coefficients in this model can be used to identify the most important predictor variables in the data." 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "id": "eb3afe48", 138 | "metadata": {}, 139 | "source": [ 140 | "# Question No. 8:\n", 141 | "How do you pickle and unpickle a trained Elastic Net Regression model in Python?\n", 142 | "\n", 143 | "## Answer:" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "id": "4b954c3c", 150 | "metadata": {}, 151 | "outputs": [], 152 | "source": [ 153 | "import pickle\n", 154 | "from sklearn.linear_model import ElasticNet\n", 155 | "\n", 156 | "enet = ElasticNet(alpha=0.1, l1_ratio=0.5)\n", 157 | "enet.fit(X_train, y_train)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": null, 163 | "id": "795e1084", 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [ 167 | "# pickle \n", 168 | "with open('enet_model.pkl', 'wb') as f:\n", 169 | " pickle.dump(enet, f)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "id": "de5a6f9e", 176 | "metadata": {}, 177 | "outputs": [], 178 | "source": [ 179 | "# unpickle\n", 180 | "with open('enet_model.pkl', 'rb') as f:\n", 181 | " enet = pickle.load(f)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "id": "904c5577", 187 | "metadata": {}, 188 | "source": [ 189 | "# Question No. 9:\n", 190 | "What is the purpose of pickling a model in machine learning?\n", 191 | "\n", 192 | "## Answer:\n", 193 | "There are several benefits of pickling a model:\n", 194 | "\n", 195 | "- **Efficient Storage:** Serialized models take up much less disk space than the original Python objects, making them easier to store and transfer.\n", 196 | "\n", 197 | "- **Faster Deployment:** Since the serialized model can be loaded quickly from a file, it can be deployed much faster than retraining the model every time it needs to be used.\n", 198 | "\n", 199 | "- **Reproducibility:** By pickling a model, you can ensure that you are using the exact same model for future predictions, even if the training data or environment has changed.\n", 200 | "\n", 201 | "- **Sharing Models:** Serialized models can be easily shared with others, allowing them to use the same model for their own purposes." 202 | ] 203 | } 204 | ], 205 | "metadata": { 206 | "kernelspec": { 207 | "display_name": "Python 3 (ipykernel)", 208 | "language": "python", 209 | "name": "python3" 210 | }, 211 | "language_info": { 212 | "codemirror_mode": { 213 | "name": "ipython", 214 | "version": 3 215 | }, 216 | "file_extension": ".py", 217 | "mimetype": "text/x-python", 218 | "name": "python", 219 | "nbconvert_exporter": "python", 220 | "pygments_lexer": "ipython3", 221 | "version": "3.9.13" 222 | } 223 | }, 224 | "nbformat": 4, 225 | "nbformat_minor": 5 226 | } 227 | -------------------------------------------------------------------------------- /Introduction To Machine learning/Assignment_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "6ec8a809", 6 | "metadata": {}, 7 | "source": [ 8 | "**Q1. Explain the following with an example:**\n", 9 | "\n", 10 | "**I. Artificial Intelligence
\n", 11 | "II. Machine Learning
\n", 12 | "III. Deep Learning**\n", 13 | "\n", 14 | "1. **Artificial Intelligence (AI):**\n", 15 | "Artificial Intelligence (AI) is the simulation of human intelligence processes by computer systems. AI can perform tasks that typically require human intelligence, such as understanding natural language, recognizing speech and images, and making decisions. AI can be divided into two main categories:\n", 16 | "\n", 17 | "i) Narrow or Weak AI
ii) General or Strong AI\n", 18 | "\n", 19 | "**Example:** One example of AI is self-driving cars. These cars use computer vision and machine learning algorithms to analyze their surroundings and make decisions based on that analysis. They can navigate traffic, avoid obstacles, and make decisions based on traffic laws and road conditions.\n", 20 | "\n", 21 | "2. **Machine Learning (ML):**\n", 22 | "Machine Learning (ML) is a subset of AI that uses statistical techniques to enable computer systems to improve their performance on a specific task by learning from data without being explicitly programmed. Machine learning algorithms can recognize patterns in data and use those patterns to make predictions or decisions. Machine learning algorithms can be classified into three main categories:\n", 23 | "\n", 24 | "i) Supervised Learning
ii) Unsupervised Learning
iii) Reinforcement Learning\n", 25 | "\n", 26 | "**Example:** An example of machine learning is email filtering. Email providers like Gmail use machine learning algorithms to classify incoming emails as spam or not spam. The algorithm learns from patterns in the text and other features of the email to make a prediction about whether the email is spam or not.\n", 27 | "\n", 28 | "3. **Deep Learning (DL):**\n", 29 | "Deep Learning (DL) is a subset of machine learning that uses neural networks to model and solve complex problems. Deep learning algorithms use multiple layers of interconnected nodes to process and learn from data. Deep learning is capable of learning from unstructured data such as images, videos, and audio.\n", 30 | "\n", 31 | "**Example:** An example of deep learning is image recognition. Deep learning models can be trained to identify objects within images, such as cars or people. These models use convolutional neural networks (CNNs) to analyze the features of an image and classify it into different categories based on those features. For example, a deep learning model could be trained to identify different breeds of dogs within a photo." 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "id": "4e0e9f23", 37 | "metadata": {}, 38 | "source": [ 39 | "**Q2. What is supervised learning? List some examples of supervised learning.**\n", 40 | "\n", 41 | "**Supervised learning** is a type of machine learning in which a model is trained on a labeled dataset. In supervised learning, the model learns to map input features to the correct output by using the labeled examples provided in the training set. The objective of the model is to predict the correct output for new, unseen input examples.\n", 42 | "\n", 43 | "**Examples of supervised learning**
\n", 44 | "1. **Image classification:** The task of identifying the content of an image, such as distinguishing between images of cats and dogs.\n", 45 | "2. **Spam detection:** The task of classifying emails as either spam or not spam based on their content.\n", 46 | "3. **Sentiment analysis:** The task of classifying text as either positive, negative, or neutral based on the sentiment expressed in the text.\n", 47 | "4. **Fraud detection:** The task of identifying fraudulent transactions based on historical data." 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "id": "1ce5a5b8", 53 | "metadata": {}, 54 | "source": [ 55 | "**Q3. What is unsupervised learning? List some examples of unsupervised learning.**\n", 56 | "\n", 57 | "**Unsupervised learning** is a type of machine learning in which a model is trained on an unlabeled dataset. In unsupervised learning, the model is not provided with any specific output or label to predict, but rather learns to identify patterns and relationships in the input data. The objective of unsupervised learning is often to discover the underlying structure or clusters in the data.\n", 58 | "\n", 59 | "**Examples of unsupervised learning:**
\n", 60 | "1. **Clustering:** The task of grouping similar examples together based on their characteristics. This can be used for customer segmentation, image segmentation, or anomaly detection.\n", 61 | "2. **Dimensionality reduction:** The task of reducing the number of features in a dataset while retaining as much of the relevant information as possible. This can be used for visualizing high-dimensional data, speeding up training of supervised learning models, or data compression.\n", 62 | "3. **Association rule mining:** The task of discovering interesting relationships between items in a dataset. This can be used for market basket analysis or recommendation systems.\n", 63 | "4. **Generative modeling:** The task of learning the underlying distribution of a dataset and generating new examples that are similar to the original data. This can be used for generating realistic images or music." 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "id": "7ef5af6f", 69 | "metadata": {}, 70 | "source": [ 71 | "**Q4. What is the difference between AI, ML, DL, and DS?**\n", 72 | "\n", 73 | "**Artificial Intelligence (AI)** refers to the development of computer systems that can perform tasks that typically require human intelligence, such as natural language processing, computer vision, and decision making. AI can be achieved through various approaches, including rule-based systems, expert systems, and machine learning.\n", 74 | "\n", 75 | "**Machine Learning (ML)** is a subset of AI that involves the development of algorithms and statistical models that enable computer systems to improve their performance on a specific task by learning from data without being explicitly programmed. ML can be supervised, unsupervised, or semi-supervised.\n", 76 | "\n", 77 | "**Deep Learning (DL)** is a subfield of ML that involves the use of artificial neural networks, which are modeled after the structure and function of the human brain. DL is particularly suited for tasks such as image and speech recognition, natural language processing, and autonomous driving.\n", 78 | "\n", 79 | "**Data Science (DS)** is a multidisciplinary field that involves the use of statistical and computational methods to extract insights and knowledge from data. DS combines elements of statistics, mathematics, computer science, and domain expertise to address complex data-driven problems and make data-driven decisions." 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "id": "bb402688", 85 | "metadata": {}, 86 | "source": [ 87 | "**Q5. What are the main differences between supervised learning, unsupervised learning, and semi-supervised learning?**\n", 88 | "\n", 89 | "**Supervised learning:** In supervised learning, the model is trained on labeled data, where the input and output pairs are known. The objective of the model is to learn a mapping between input features and the corresponding output. The labeled data is used to train the model, and the model is then tested on new, unseen data. The goal of supervised learning is to make accurate predictions on new, unseen data.\n", 90 | "\n", 91 | "**Unsupervised learning:** In unsupervised learning, the model is trained on unlabeled data, where the input data has no corresponding output. The objective of the model is to identify patterns or structure in the input data. Unsupervised learning can be used for tasks such as clustering, dimensionality reduction, and anomaly detection. The goal of unsupervised learning is to gain insights into the data and discover interesting relationships or anomalies that can be further explored or used for decision-making.\n", 92 | "\n", 93 | "**Semi-supervised learning:** In semi-supervised learning, the model is trained on a combination of labeled and unlabeled data. The labeled data is used to guide the learning process, while the unlabeled data is used to improve the generalization performance of the model. Semi-supervised learning can be useful when labeled data is scarce or expensive to obtain. The goal of semi-supervised learning is to make accurate predictions on new, unseen data while using as much of the available data as possible." 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "id": "8449a96a", 99 | "metadata": {}, 100 | "source": [ 101 | "**Q6. What is train, test and validation split? Explain the importance of each term.**\n", 102 | "\n", 103 | "1. **Train set**: This is the part of the dataset used to train the model. The model learns the patterns and relationships in the data through this set. Typically, the train set consists of 60-80% of the entire dataset. The train set is important because it is used to teach the model how to make accurate predictions on new, unseen data. The model learns the patterns and relationships in the data through this set.\n", 104 | "\n", 105 | "2. **Test set:** This is the part of the dataset used to evaluate the performance of the trained model on unseen data. The model is applied to the test set, and the resulting predictions are compared to the true values to evaluate the accuracy of the model. Typically, the test set consists of 20-40% of the entire dataset. The test set is important because it is used to evaluate the performance of the trained model on unseen data. This helps to ensure that the model can make accurate predictions on new data and is not overfitting the training data.\n", 106 | "\n", 107 | "3. **Validation set:** This is an optional part of the dataset used to fine-tune the model hyperparameters and prevent overfitting. The validation set is used to evaluate the performance of the model on data that is not used for training or testing. This helps to prevent the model from becoming too complex and overfitting the training data. Typically, the validation set consists of 10-20% of the entire dataset. The validation set is important because it is used to fine-tune the model hyperparameters and prevent overfitting. By evaluating the performance of the model on data that is not used for training or testing, we can ensure that the model is not becoming too complex and overfitting the training data." 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "id": "91de46fc", 113 | "metadata": {}, 114 | "source": [ 115 | "**Q7. How can unsupervised learning be used for anomaly detection?**\n", 116 | "\n", 117 | "Unsupervised learning can be used for anomaly detection by identifying patterns and structures in the data that deviate from the norm. Anomaly detection is the process of identifying data points that are significantly different from the majority of the data, and it is an important problem in many fields, including finance, healthcare, and cybersecurity.\n", 118 | "\n", 119 | "Unsupervised learning algorithms do not rely on labeled data, so they can be used to detect anomalies in datasets where there are no pre-defined classes or labels for the anomalies. Clustering, density estimation, dimensionality reduction, and one-class classification are common unsupervised learning techniques used for anomaly detection. By identifying anomalies in the data, we can detect abnormal behavior or events that require further investigation.\n", 120 | "\n", 121 | "For example, clustering algorithms can be used to group similar data points together and identify anomalies as data points that do not belong to any cluster or belong to a very small cluster. Density estimation algorithms can be used to estimate the probability distribution of the data and identify anomalies as data points with low probability. Dimensionality reduction algorithms can be used to reduce the number of features in the data while preserving the most important information, and identify anomalies as data points that are far away from the majority of the data in the reduced space. By detecting anomalies in the data, we can identify abnormal behavior or events that require further investigation." 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "id": "70fe1e2c", 127 | "metadata": {}, 128 | "source": [ 129 | "**Q8. List down some commonly used supervised learning algorithms and unsupervised learning algorithms.**\n", 130 | "\n", 131 | "**Supervised Learning Algorithms:**\n", 132 | "\n", 133 | ">Linear Regression
Logistic Regression
Decision Trees
Random Forest
Support Vector Machines (SVM)
K-Nearest Neighbors (KNN)
Naive Bayes
Artificial Neural Networks (ANN)
Gradient Boosting Machines (GBM)
XGBoost
\n", 134 | "\n", 135 | "**Unsupervised Learning Algorithms:**\n", 136 | "\n", 137 | ">K-Means Clustering
Hierarchical Clustering
Density-Based Spatial Clustering of Applications with Noise (DBSCAN)
Principal Component Analysis (PCA)
Independent Component Analysis (ICA)
t-Distributed Stochastic Neighbor Embedding (t-SNE)
Self-Organizing Maps (SOM)
Apriori Algorithm for Association Rule Mining
Isolation Forest for Anomaly Detection
Gaussian Mixture Models (GMM)
" 138 | ] 139 | } 140 | ], 141 | "metadata": { 142 | "kernelspec": { 143 | "display_name": "Python 3 (ipykernel)", 144 | "language": "python", 145 | "name": "python3" 146 | }, 147 | "language_info": { 148 | "codemirror_mode": { 149 | "name": "ipython", 150 | "version": 3 151 | }, 152 | "file_extension": ".py", 153 | "mimetype": "text/x-python", 154 | "name": "python", 155 | "nbconvert_exporter": "python", 156 | "pygments_lexer": "ipython3", 157 | "version": "3.10.8" 158 | } 159 | }, 160 | "nbformat": 4, 161 | "nbformat_minor": 5 162 | } 163 | -------------------------------------------------------------------------------- /Introduction To Machine learning/Assignment_2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c7afb718", 6 | "metadata": {}, 7 | "source": [ 8 | "**Q1. Define overfitting and underfitting in machine learning. What are the consequences of each, and how can they be mitigated?**\n", 9 | "\n", 10 | "**Overfitting:** Overfitting occurs when a model learns the noise in the training data, rather than the underlying pattern. This leads to a model that performs very well on the training data but poorly on new, unseen data. The consequences of overfitting are reduced accuracy and poor generalization. It is often caused by models that are too complex, too flexible or too sensitive to small variations in the data.\n", 11 | "\n", 12 | "**To mitigate overfitting**, one can:\n", 13 | "1. Use regularization techniques such as L1/L2 regularization or dropout to add a penalty term to the loss function, which will discourage the model from overfitting the data.\n", 14 | "2. Use early stopping technique to stop training the model when the performance on a validation set starts to degrade.\n", 15 | "3. Use techniques like cross-validation to tune hyperparameters and choose the best model that generalizes well on unseen data.\n", 16 | "\n", 17 | "**Underfitting:** Underfitting occurs when a model is too simple to capture the underlying pattern in the data. This leads to poor performance on both the training data and new, unseen data. The consequences of underfitting are reduced accuracy and poor model performance. It is often caused by models that are too simple or are not trained for long enough.\n", 18 | "\n", 19 | "**To mitigate underfitting**, one can:\n", 20 | "1. Increase the complexity of the model by adding more layers or neurons.\n", 21 | "2. Increase the number of training epochs or the batch size.\n", 22 | "3. Use a different model architecture that is better suited to the data. 4. Use more training data to capture a wider range of patterns." 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "id": "7acc7282", 28 | "metadata": {}, 29 | "source": [ 30 | "**Q2. How can we reduce overfitting? Explain in brief.**\n", 31 | "\n", 32 | "Overfitting is a common problem in machine learning where a model learns the noise in the training data, rather than the underlying pattern. This leads to a model that performs well on the training data but poorly on new, unseen data.\n", 33 | "\n", 34 | "**Overfitting can be reduced using the following techniques:**\n", 35 | "\n", 36 | "1. **Regularization:** Regularization is a technique that adds a penalty term to the loss function to discourage the model from overfitting the data. Two commonly used regularization techniques are L1/L2 regularization, where the penalty is the sum of the absolute/ square values of the model weights. This forces the model to keep the weights small, reducing the complexity of the model and preventing overfitting.\n", 37 | "\n", 38 | "2. **Early stopping:** Early stopping is a technique that stops the training of the model when the performance on a validation set starts to degrade. It prevents the model from memorizing the training data and helps to generalize well on new, unseen data.\n", 39 | "\n", 40 | "3. **Dropout:** Dropout is a regularization technique that randomly drops out some neurons during training, which helps to prevent overfitting. This technique encourages the model to learn multiple independent features and reduces the dependency between neurons.\n", 41 | "\n", 42 | "4. **Data augmentation:** Data augmentation is a technique that artificially increases the size of the training data by applying various transformations to the data, such as rotation, scaling, and flipping. This helps the model to learn a wider range of patterns and reduces overfitting.\n", 43 | "\n", 44 | "5. **Cross-validation:** Cross-validation is a technique that splits the data into training and validation sets, trains the model on the training set and evaluates its performance on the validation set. This helps to identify overfitting and select the best model that generalizes well on new, unseen data." 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "id": "c89cc575", 50 | "metadata": {}, 51 | "source": [ 52 | "**Q3. Explain underfitting. List scenarios where underfitting can occur in Machine Learning (ML).**\n", 53 | "\n", 54 | "Underfitting is a common problem in machine learning where a model is too simple to capture the underlying pattern in the data. This leads to poor performance on both the training data and new, unseen data.\n", 55 | "\n", 56 | "**Underfitting can occur in the following scenarios:**\n", 57 | "\n", 58 | "1. **Insufficient training data:** When the size of the training data is small, the model may not have enough information to learn the underlying pattern and generalize well on new, unseen data. This can lead to underfitting.\n", 59 | "\n", 60 | "2. **Model complexity:** When the model is too simple or has too few parameters to capture the underlying pattern in the data, it may lead to underfitting.\n", 61 | "\n", 62 | "3. **Incorrect model architecture:** When the model architecture is not suitable for the data, it may lead to underfitting. For example, using a linear model to fit a non-linear dataset may lead to underfitting.\n", 63 | "\n", 64 | "4. **Inappropriate feature selection:** When the model is trained on a subset of features that are not representative of the underlying pattern in the data, it may lead to underfitting.\n", 65 | "\n", 66 | "5. **High bias:** When the model has a high bias, it may not be able to capture the complexity of the underlying pattern in the data, leading to underfitting." 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "id": "769d48aa", 72 | "metadata": {}, 73 | "source": [ 74 | "**Q4. Explain the bias-variance tradeoff in machine learning. What is the relationship between bias and variance, and how do they affect model performance?**\n", 75 | "\n", 76 | "**The bias-variance tradeoff** is a fundamental concept in machine learning that refers to the tradeoff between a model's ability to fit the training data well (low bias) and its ability to generalize well to new, unseen data (low variance).\n", 77 | "\n", 78 | "**Bias** refers to the difference between the true value and the predicted value of the model. A model with high bias tends to be too simple and unable to capture the underlying pattern in the data. This can lead to underfitting and poor performance on both the training and test data.\n", 79 | "\n", 80 | "**Variance** refers to the sensitivity of the model to the noise in the training data. A model with high variance tends to overfit the training data and capture the noise in the data, leading to poor performance on new, unseen data.\n", 81 | "\n", 82 | "**The relationship between bias and variance** can be visualized as a U-shaped curve. As the complexity of the model increases, the bias decreases, and the variance increases. On the other hand, as the complexity of the model decreases, the bias increases, and the variance decreases.\n", 83 | "\n", 84 | "The optimal model is the one that strikes a balance between bias and variance, i.e., a model that is complex enough to capture the underlying pattern in the data but not so complex that it overfits the training data." 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "id": "17667303", 90 | "metadata": {}, 91 | "source": [ 92 | "**Q5. Discuss some common methods for detecting overfitting and underfitting in machine learning models. How can you determine whether your model is overfitting or underfitting?**\n", 93 | "\n", 94 | "1. **Using training and validation curves:** Plotting the training and validation curves of a model can help detect overfitting and underfitting. If the training error is much lower than the validation error, it indicates that the model is overfitting. If both the training and validation errors are high, it indicates that the model is underfitting.\n", 95 | "\n", 96 | "2. **Using learning curves:** Learning curves show how the model's performance improves as the size of the training data increases. If the learning curve plateaus, it indicates that the model is unable to learn from additional training data, and the model may be underfitting. On the other hand, if the gap between the training and validation curves is large, it indicates that the model may be overfitting.\n", 97 | "\n", 98 | "3. **Using cross-validation:** Cross-validation is a technique for evaluating the performance of a model on multiple subsets of the training data. If the model performs well on all the subsets, it indicates that the model is not overfitting. If the performance is poor on all subsets, it indicates that the model is underfitting.\n", 99 | "\n", 100 | "4. **Using regularization:** Regularization techniques such as L1 and L2 regularization can help reduce overfitting by adding a penalty term to the loss function. If the regularization parameter is too high, it can lead to underfitting.\n", 101 | "\n", 102 | "**To determine whether a model is overfitting or underfitting,** we can use the above methods to analyze the model's performance. If the training error is low, but the validation error is high, it indicates that the model is overfitting. If both the training and validation errors are high, it indicates that the model is underfitting." 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "id": "2dc6552f", 108 | "metadata": {}, 109 | "source": [ 110 | "**Q6. Compare and contrast bias and variance in machine learning. What are some examples of high bias and high variance models, and how do they differ in terms of their performance?**\n", 111 | "\n", 112 | "Bias and variance are two sources of error in machine learning models. Bias refers to the difference between the expected output and the true output of the model, while variance refers to the variability of the model's output for different inputs.\n", 113 | "\n", 114 | "**High bias models** are typically too simple and unable to capture the underlying patterns in the data. They tend to underfit the data, leading to high training and test errors. High bias models have low complexity and often have fewer parameters than the data requires.\n", 115 | "\n", 116 | "**Examples of high bias models** include linear regression models, which assume that the relationship between the inputs and the output is linear, even when it is not.\n", 117 | "\n", 118 | "**High variance models** are typically too complex and able to fit the training data too closely, including noise in the data. They tend to overfit the data, leading to low training error but high test error. High variance models have high complexity and often have more parameters than necessary.\n", 119 | "\n", 120 | "**Examples of high variance models** include decision trees with deep and complex branches, which can fit the training data too closely.\n", 121 | "\n", 122 | "**The main difference between high bias and high variance models** is their performance. High bias models perform poorly on both training and test data, while high variance models perform well on training data but poorly on test data." 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "id": "0789b19c", 128 | "metadata": {}, 129 | "source": [ 130 | "**Q7. What is regularization in machine learning, and how can it be used to prevent overfitting? Describe some common regularization techniques and how they work.**\n", 131 | "\n", 132 | "Regularization is a technique used in machine learning to prevent overfitting, which occurs when a model is too complex and performs well on the training data but poorly on new, unseen data. Regularization works by adding a penalty term to the loss function that encourages the model to have smaller weights, making it less complex and more likely to generalize well to new data.\n", 133 | "\n", 134 | "**Some common regularization techniques used in machine learning are:**\n", 135 | "\n", 136 | "1. **L1 regularization (Lasso):** L1 regularization adds a penalty term proportional to the absolute value of the weights to the loss function. This encourages the model to have sparse weights, i.e., many weights are zero. L1 regularization can be used for feature selection, where only the most important features are used in the model.\n", 137 | "\n", 138 | "2. **L2 regularization (Ridge):** L2 regularization adds a penalty term proportional to the square of the weights to the loss function. This encourages the model to have smaller weights, but it does not lead to sparse weights like L1 regularization. L2 regularization is commonly used in linear regression models.\n", 139 | "\n", 140 | "3. **Elastic Net:** Elastic Net combines L1 and L2 regularization by adding a penalty term proportional to the sum of the absolute and square of the weights to the loss function. This provides a balance between L1 and L2 regularization and can be useful when there are many correlated features in the data.\n", 141 | "\n", 142 | "4. **Dropout:** Dropout is a technique used in deep neural networks that randomly drops out some of the neurons during training. This encourages the model to learn more robust features and reduces overfitting.\n", 143 | "\n", 144 | "5. **Early stopping:** Early stopping is a technique that stops training the model when the performance on the validation set starts to degrade. This helps to prevent the model from overfitting by stopping the training before the model starts to memorize the training data." 145 | ] 146 | } 147 | ], 148 | "metadata": { 149 | "kernelspec": { 150 | "display_name": "Python 3 (ipykernel)", 151 | "language": "python", 152 | "name": "python3" 153 | }, 154 | "language_info": { 155 | "codemirror_mode": { 156 | "name": "ipython", 157 | "version": 3 158 | }, 159 | "file_extension": ".py", 160 | "mimetype": "text/x-python", 161 | "name": "python", 162 | "nbconvert_exporter": "python", 163 | "pygments_lexer": "ipython3", 164 | "version": "3.10.8" 165 | } 166 | }, 167 | "nbformat": 4, 168 | "nbformat_minor": 5 169 | } 170 | --------------------------------------------------------------------------------