├── 8puzzle.py ├── 8puzzle ├── 8puzzle.py ├── gbfs.py ├── main.py └── state.py ├── ML └── ML notes │ └── Unit-4MLl.pptx ├── Python Programming Intro.ipynb ├── README.md ├── bfs.py ├── classifierss └── programs │ ├── MLPClassifier.ipynb │ └── decisiontreesmall.ipynb ├── decisiontree ├── diabetes │ ├── .ipynb_checkpoints │ │ ├── diabetesclassf-checkpoint.ipynb │ │ └── diabetesentropyclassf-checkpoint.ipynb │ ├── diabetes.csv │ ├── diabetesclassf.ipynb │ └── diabetesentropyclassf.ipynb ├── iris │ ├── decisiontreesmall.ipynb │ └── iris.csv ├── studentt │ ├── decisiontreesmallstudent.ipynb │ └── student_dataset.csv └── weightheight │ ├── decisiontreesmallweight.ipynb │ └── weight_height_dataset.csv ├── gbfs.py ├── hangman game └── manual │ ├── __pycache__ │ ├── hangman_art.cpython-312.pyc │ └── hangman_words.cpython-312.pyc │ ├── hangman_art.py │ ├── hangman_words.py │ └── hangmanv5.py ├── hangman_1.py ├── hangmanv5.py ├── nqueens └── queen.py ├── numpy ├── .ipynb_checkpoints │ └── numpyml-checkpoint.ipynb └── numpyml.ipynb ├── pandas └── pandas_d │ ├── dataset │ ├── car-sales.csv │ └── export_cars.csv │ └── pandasnotebook │ └── pandasfk.ipynb ├── prolog ├── cartypes.pl ├── cartypes.pl~ ├── familytree.pl ├── familytree.pl~ ├── girlstudents.pl ├── girlstudents.pl~ ├── outputcartypes.png ├── outputgirlstudents.png ├── prolog.pptx ├── simplefacts.pl └── simplefacts.pl~ ├── pythonbasics ├── List.ipynb └── ai lab basics.ipynb ├── students_contributions ├── AI_NUMPY.ipynb ├── Waterjug_auto.py ├── numpy.py └── prolog_familytree.py ├── uninformed search ├── bfs.py └── dfs.py └── waterjug code ├── waterjbfs.py ├── waterjug.pptx └── waterjugmanual.py /8puzzle.py: -------------------------------------------------------------------------------- 1 | from queue import PriorityQueue 2 | 3 | # Define the goal state for the 8-puzzle 4 | #GOAL_STATE = (1, 2, 3, 4, 5, 6, 7, 8, 0) 5 | GOAL_STATE = (1, 2, 3, 8,0,4,7,6,5) 6 | 7 | class PuzzleState: 8 | def __init__(self, board, empty_index, path_cost=0, parent=None): 9 | self.board = board 10 | self.empty_index = empty_index # Index of the empty space (0) 11 | self.path_cost = path_cost 12 | self.parent = parent 13 | 14 | def is_goal(self): 15 | return self.board == GOAL_STATE 16 | 17 | def generate_children(self): 18 | children = [] 19 | row, col = divmod(self.empty_index, 3) 20 | directions = { 21 | "up": (-1, 0), 22 | "down": (1, 0), 23 | "left": (0, -1), 24 | "right": (0, 1) 25 | } 26 | 27 | for direction, (dr, dc) in directions.items(): 28 | new_row, new_col = row + dr, col + dc 29 | if 0 <= new_row < 3 and 0 <= new_col < 3: 30 | new_index = new_row * 3 + new_col 31 | new_board = list(self.board) 32 | new_board[self.empty_index], new_board[new_index] = new_board[new_index], new_board[self.empty_index] 33 | children.append(PuzzleState(tuple(new_board), new_index, self.path_cost + 1, self)) 34 | 35 | return children 36 | 37 | def heuristic(self): 38 | distance = 0 39 | for index, value in enumerate(self.board): 40 | if value != 0: 41 | goal_index = value - 1 42 | goal_row, goal_col = divmod(goal_index, 3) 43 | current_row, current_col = divmod(index, 3) 44 | distance += abs(goal_row - current_row) + abs(goal_col - current_col) 45 | return distance 46 | 47 | def __lt__(self, other): 48 | return self.heuristic() < other.heuristic() 49 | 50 | def greedy_best_first_search(initial_board): 51 | initial_index = initial_board.index(0) 52 | initial_state = PuzzleState(tuple(initial_board), initial_index) 53 | 54 | if initial_state.is_goal(): 55 | return initial_state 56 | 57 | frontier = PriorityQueue() 58 | frontier.put(initial_state) 59 | explored = set() 60 | 61 | while not frontier.empty(): 62 | current_state = frontier.get() 63 | 64 | if current_state.is_goal(): 65 | return current_state 66 | 67 | explored.add(current_state.board) 68 | 69 | for child in current_state.generate_children(): 70 | if child.board not in explored: 71 | frontier.put(child) 72 | 73 | return None # No solution found 74 | 75 | def print_solution(solution): 76 | path = [] 77 | current_state = solution 78 | while current_state: 79 | path.append(current_state.board) 80 | current_state = current_state.parent 81 | for state in reversed(path): 82 | print(state) 83 | 84 | # Example usage 85 | initial_board = [2,8,3,1,6,4,7,0,5] 86 | # Initial configuration initial_board = [1, 2, 3, 4, 5, 6, 0, 7, 8] 87 | solution = greedy_best_first_search(initial_board) 88 | 89 | if solution: 90 | print("Solution found:") 91 | print_solution(solution) 92 | else: 93 | print("No solution found.") 94 | -------------------------------------------------------------------------------- /8puzzle/8puzzle.py: -------------------------------------------------------------------------------- 1 | from queue import PriorityQueue 2 | 3 | # Define the goal state for the 8-puzzle 4 | #GOAL_STATE = (1, 2, 3, 4, 5, 6, 7, 8, 0) 5 | GOAL_STATE = (1, 2, 3, 8,0,4,7,6,5) 6 | 7 | class PuzzleState: 8 | def __init__(self, board, empty_index, path_cost=0, parent=None): 9 | self.board = board 10 | self.empty_index = empty_index # Index of the empty space (0) 11 | self.path_cost = path_cost 12 | self.parent = parent 13 | 14 | def is_goal(self): 15 | return self.board == GOAL_STATE 16 | 17 | def generate_children(self): 18 | children = [] 19 | row, col = divmod(self.empty_index, 3) 20 | directions = { 21 | "up": (-1, 0), 22 | "down": (1, 0), 23 | "left": (0, -1), 24 | "right": (0, 1) 25 | } 26 | 27 | for direction, (dr, dc) in directions.items(): 28 | new_row, new_col = row + dr, col + dc 29 | if 0 <= new_row < 3 and 0 <= new_col < 3: 30 | new_index = new_row * 3 + new_col 31 | new_board = list(self.board) 32 | new_board[self.empty_index], new_board[new_index] = new_board[new_index], new_board[self.empty_index] 33 | children.append(PuzzleState(tuple(new_board), new_index, self.path_cost + 1, self)) 34 | 35 | return children 36 | 37 | def heuristic(self): 38 | distance = 0 39 | for index, value in enumerate(self.board): 40 | if value != 0: 41 | goal_index = value - 1 42 | goal_row, goal_col = divmod(goal_index, 3) 43 | current_row, current_col = divmod(index, 3) 44 | distance += abs(goal_row - current_row) + abs(goal_col - current_col) 45 | return distance 46 | 47 | def __lt__(self, other): 48 | return self.heuristic() < other.heuristic() 49 | 50 | def greedy_best_first_search(initial_board): 51 | initial_index = initial_board.index(0) 52 | initial_state = PuzzleState(tuple(initial_board), initial_index) 53 | 54 | if initial_state.is_goal(): 55 | return initial_state 56 | 57 | frontier = PriorityQueue() 58 | frontier.put(initial_state) 59 | explored = set() 60 | 61 | while not frontier.empty(): 62 | current_state = frontier.get() 63 | 64 | if current_state.is_goal(): 65 | return current_state 66 | 67 | explored.add(current_state.board) 68 | 69 | for child in current_state.generate_children(): 70 | if child.board not in explored: 71 | frontier.put(child) 72 | 73 | return None # No solution found 74 | 75 | def print_solution(solution): 76 | path = [] 77 | current_state = solution 78 | while current_state: 79 | path.append(current_state.board) 80 | current_state = current_state.parent 81 | for state in reversed(path): 82 | print(state) 83 | 84 | # Example usage 85 | initial_board = [2,8,3,1,6,4,7,0,5] 86 | # Initial configuration initial_board = [1, 2, 3, 4, 5, 6, 0, 7, 8] 87 | solution = greedy_best_first_search(initial_board) 88 | 89 | if solution: 90 | print("Solution found:") 91 | print_solution(solution) 92 | else: 93 | print("No solution found.") 94 | -------------------------------------------------------------------------------- /8puzzle/gbfs.py: -------------------------------------------------------------------------------- 1 | from state import State 2 | from queue import PriorityQueue 3 | def Greedy(given_state , n): 4 | frontier = PriorityQueue() 5 | explored = [] 6 | counter = 0 7 | root = State(given_state, None, None, 0, 0) 8 | #root.evaluation() 9 | evaluation = root.Misplaced_Tiles(n) #we can use Misplaced_Tiles() instead. 10 | frontier.put((evaluation, counter, root)) #based on greedy evaluation 11 | 12 | while not frontier.empty(): 13 | current_node = frontier.get() 14 | current_node = current_node[2] 15 | explored.append(current_node.state) 16 | 17 | if current_node.test(): 18 | return current_node.solution(), len(explored) 19 | 20 | children = current_node.expand(n) 21 | for child in children: 22 | if child.state not in explored: 23 | counter += 1 24 | evaluation = child.Misplaced_Tiles(n) #we can use Misplaced_Tiles() instead. 25 | frontier.put((evaluation, counter, child)) #based on greedy evaluation 26 | return 27 | -------------------------------------------------------------------------------- /8puzzle/main.py: -------------------------------------------------------------------------------- 1 | from gbfs import Greedy 2 | 3 | #initial state 4 | n = int(input("Enter n\n")) 5 | print("Enter your" ,n,"*",n, "puzzle") 6 | root = [] 7 | for i in range(0,n*n): 8 | p = int(input()) 9 | root.append(p) 10 | 11 | print("The given state is:", root) 12 | 13 | 14 | #count the number of inversions 15 | def inv_num(puzzle): 16 | inv = 0 17 | for i in range(len(puzzle)-1): 18 | for j in range(i+1 , len(puzzle)): 19 | if (( puzzle[i] > puzzle[j]) and puzzle[i] and puzzle[j]): 20 | inv += 1 21 | return inv 22 | 23 | def solvable(puzzle): #check if initial state puzzle is solvable: number of inversions should be even. 24 | inv_counter = inv_num(puzzle) 25 | if (inv_counter %2 ==0): 26 | return True 27 | return False 28 | 29 | 30 | #1,8,2,0,4,3,7,6,5 is solvable 31 | #2,1,3,4,5,6,7,8,0 is not solvable 32 | 33 | from time import time 34 | 35 | if solvable(root): 36 | print("Solvable, please wait. \n") 37 | 38 | 39 | time3 = time() 40 | Greedy_solution = Greedy(root, n) 41 | Greedy_time = time() - time3 42 | print('Greedy Solution is ', Greedy_solution[0]) 43 | print('Number of explored nodes is ', Greedy_solution[1]) 44 | print('Greedy Time:', Greedy_time , "\n") 45 | 46 | 47 | 48 | 49 | else: 50 | print("Not solvable") 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /8puzzle/state.py: -------------------------------------------------------------------------------- 1 | class State: 2 | goal = [1, 2, 3, 4, 5, 6, 7, 8, 0] 3 | #this should be changed manually based on n 4 | #e.g. it should be [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0] if n is 4. 5 | 6 | greedy_evaluation = None 7 | 8 | heuristic = None 9 | def __init__(self, state, parent, direction, depth, cost): 10 | self.state = state 11 | self.parent = parent 12 | self.direction = direction 13 | self.depth = depth 14 | 15 | if parent: 16 | self.cost = parent.cost + cost 17 | 18 | else: 19 | self.cost = cost 20 | 21 | 22 | 23 | def test(self): #check if the given state is goal 24 | if self.state == self.goal: 25 | return True 26 | return False 27 | 28 | 29 | 30 | 31 | #heuristic function based on number of misplaced tiles 32 | def Misplaced_Tiles(self,n): 33 | counter = 0; 34 | self.heuristic = 0 35 | for i in range(n*n): 36 | for j in range(n*n): 37 | if (self.state[i] != self.goal[j]): 38 | counter += 1 39 | self.heuristic = self.heuristic + counter 40 | 41 | self.greedy_evaluation = self.heuristic 42 | 43 | 44 | return( self.greedy_evaluation) 45 | 46 | 47 | 48 | @staticmethod 49 | 50 | #this would remove illegal moves for a given state 51 | def available_moves(x,n): 52 | moves = ['Left', 'Right', 'Up', 'Down'] 53 | if x % n == 0: 54 | moves.remove('Left') 55 | if x % n == n-1: 56 | moves.remove('Right') 57 | if x - n < 0: 58 | moves.remove('Up') 59 | if x + n > n*n - 1: 60 | moves.remove('Down') 61 | 62 | return moves 63 | 64 | #produces children of a given state 65 | def expand(self , n): 66 | x = self.state.index(0) 67 | moves = self.available_moves(x,n) 68 | 69 | children = [] 70 | for direction in moves: 71 | temp = self.state.copy() 72 | if direction == 'Left': 73 | temp[x], temp[x - 1] = temp[x - 1], temp[x] 74 | elif direction == 'Right': 75 | temp[x], temp[x + 1] = temp[x + 1], temp[x] 76 | elif direction == 'Up': 77 | temp[x], temp[x - n] = temp[x - n], temp[x] 78 | elif direction == 'Down': 79 | temp[x], temp[x + n] = temp[x + n], temp[x] 80 | 81 | 82 | children.append(State(temp, self, direction, self.depth + 1, 1)) #depth should be changed as children are produced 83 | return children 84 | 85 | 86 | #gets the given state and returns it's direction + it's parent's direction till there is no parent 87 | def solution(self): 88 | solution = [] 89 | solution.append(self.direction) 90 | path = self 91 | while path.parent != None: 92 | path = path.parent 93 | solution.append(path.direction) 94 | solution = solution[:-1] 95 | solution.reverse() 96 | return solution 97 | -------------------------------------------------------------------------------- /ML/ML notes/Unit-4MLl.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sinifive/Artificial-Intelligence-Lab/c8cdb829b9a9fa9d9d8401e81898aa85cac34290/ML/ML notes/Unit-4MLl.pptx -------------------------------------------------------------------------------- /Python Programming Intro.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "db1e2eaa-3fd2-4f2e-8aed-527654771836", 6 | "metadata": {}, 7 | "source": [ 8 | "**Basics of Python**" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "81369bbb-b0da-4860-bcb2-9a50c6a5ed4e", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdin", 19 | "output_type": "stream", 20 | "text": [ 21 | "enter your message: hello\n" 22 | ] 23 | }, 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "type of message is: \n", 29 | "your message is hello\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "message=input(\"enter your message:\")\n", 35 | "print(\"type of message is:\",type(message))\n", 36 | "print(\"your message is\", message)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "id": "47fc61b8-9a12-4ed2-95fc-e7c6157fa1a8", 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "hi-->there\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "print(\"hi\",\"there\",sep=\"-->\")" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "id": "1d44f07a-b7e9-4209-aed9-de63fe4a2e68", 60 | "metadata": {}, 61 | "source": [ 62 | "**Arithmetic Operations**" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 4, 68 | "id": "8b658735-343c-47b7-a574-d4be8e3fe3d7", 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdin", 73 | "output_type": "stream", 74 | "text": [ 75 | "enter the value of a: 3\n", 76 | "enter the value for b: 4\n" 77 | ] 78 | }, 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "3+4 is: 7\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "a=int(input(\"enter the value of a:\"))\n", 89 | "b=int(input(\"enter the value for b:\"))\n", 90 | "print(f\"{a}+{b} is:\",(a+b))" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "id": "2ec5a8b0-ec60-4af3-8308-37acbae9577f", 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdin", 101 | "output_type": "stream", 102 | "text": [ 103 | "enter the value of a: 4\n", 104 | "enter the value for b: 3\n" 105 | ] 106 | }, 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "4-3 is: 1\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "a=int(input(\"enter the value of a:\"))\n", 117 | "b=int(input(\"enter the value for b:\"))\n", 118 | "print(f\"{a}-{b} is:\",(a-b))" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 6, 124 | "id": "63158c6c-c81e-4b08-a50d-0408ed83c036", 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdin", 129 | "output_type": "stream", 130 | "text": [ 131 | "enter the value of a: 2\n", 132 | "enter the value for b: 3\n" 133 | ] 134 | }, 135 | { 136 | "name": "stdout", 137 | "output_type": "stream", 138 | "text": [ 139 | "2*3 is: 6\n" 140 | ] 141 | } 142 | ], 143 | "source": [ 144 | "a=int(input(\"enter the value of a:\"))\n", 145 | "b=int(input(\"enter the value for b:\"))\n", 146 | "print(f\"{a}*{b} is:\",(a*b))" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 7, 152 | "id": "6cc21422-ce83-4ce0-abd2-b73a03fa4616", 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdin", 157 | "output_type": "stream", 158 | "text": [ 159 | "enter the value of a: 4\n", 160 | "enter the value for b: 2\n" 161 | ] 162 | }, 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | "4/2 is: 2.0\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "a=int(input(\"enter the value of a:\"))\n", 173 | "b=int(input(\"enter the value for b:\"))\n", 174 | "print(f\"{a}/{b} is:\",(a/b))" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "id": "18d36ea0-2c14-43d8-9909-209e9a89db78", 180 | "metadata": {}, 181 | "source": [ 182 | "**type conversion example**" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 8, 188 | "id": "60a5cfef-b58c-41b1-a822-f811aedcb157", 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "ename": "TypeError", 193 | "evalue": "unsupported operand type(s) for +: 'int' and 'str'", 194 | "output_type": "error", 195 | "traceback": [ 196 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 197 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 198 | "Cell \u001b[1;32mIn[8], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m a\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m\n\u001b[0;32m 2\u001b[0m b\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msam\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(a\u001b[38;5;241m+\u001b[39mb)\n", 199 | "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 200 | ] 201 | } 202 | ], 203 | "source": [ 204 | "a=3\n", 205 | "b=\"sam\"\n", 206 | "print(a+b)" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 10, 212 | "id": "9a9edff9-4fa6-48aa-bbe1-d71f3a9144ae", 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "sam3\n" 220 | ] 221 | } 222 | ], 223 | "source": [ 224 | "a=3\n", 225 | "b=\"sam\"\n", 226 | "#str() converts specified value to a string\n", 227 | "print(b+str(a))" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 11, 233 | "id": "48d65bb8-5899-4db5-a21a-51b01203a8db", 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "samsamsam\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "print(b*a)" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 12, 251 | "id": "741999e9-84bb-4b21-b916-8c87c8d9dbd9", 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "name": "stdout", 256 | "output_type": "stream", 257 | "text": [ 258 | "9\n" 259 | ] 260 | } 261 | ], 262 | "source": [ 263 | "print(a**2)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 13, 269 | "id": "9e141493-baff-4a54-8106-7ae68e191b66", 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "name": "stdout", 274 | "output_type": "stream", 275 | "text": [ 276 | "2\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "print(a-1)" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 14, 287 | "id": "81e9bd6d-75b6-4e76-923b-daa8922d1516", 288 | "metadata": {}, 289 | "outputs": [ 290 | { 291 | "name": "stdout", 292 | "output_type": "stream", 293 | "text": [ 294 | "1.0\n" 295 | ] 296 | } 297 | ], 298 | "source": [ 299 | "print(a/3)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 15, 305 | "id": "170ff70c-2fd1-4d61-8adc-05df2aba3d32", 306 | "metadata": {}, 307 | "outputs": [ 308 | { 309 | "name": "stdout", 310 | "output_type": "stream", 311 | "text": [ 312 | "1\n" 313 | ] 314 | } 315 | ], 316 | "source": [ 317 | "print(a//3)" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 16, 323 | "id": "bdcbdabf-45c5-44a8-af43-6748bdf6705f", 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "0\n" 331 | ] 332 | } 333 | ], 334 | "source": [ 335 | "print(a%3)" 336 | ] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "id": "c2238538-ecda-4b8a-a9b2-cd697816b5b4", 341 | "metadata": {}, 342 | "source": [ 343 | "**Loops**" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 18, 349 | "id": "58a61293-d9eb-45b7-b749-e6d67275dbfe", 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "name": "stdout", 354 | "output_type": "stream", 355 | "text": [ 356 | "0\n", 357 | "1\n", 358 | "2\n", 359 | "3\n", 360 | "4\n" 361 | ] 362 | } 363 | ], 364 | "source": [ 365 | "#range function returns a sequence of numbers starting from 0 which is default value and increments by 1\n", 366 | "for i in range(0,5):\n", 367 | " print(i)" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 19, 373 | "id": "4520d259-1429-4f02-848a-9485814d15ea", 374 | "metadata": {}, 375 | "outputs": [ 376 | { 377 | "name": "stdout", 378 | "output_type": "stream", 379 | "text": [ 380 | "1 2 3 4 5 " 381 | ] 382 | } 383 | ], 384 | "source": [ 385 | "#while loop\n", 386 | "i=1\n", 387 | "while i<=5:\n", 388 | " print(i,end=\" \")\n", 389 | " i+=1" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": 20, 395 | "id": "271e2b5a-8d28-426a-b32e-a97358804f85", 396 | "metadata": {}, 397 | "outputs": [ 398 | { 399 | "name": "stdout", 400 | "output_type": "stream", 401 | "text": [ 402 | "1\n", 403 | "2\n", 404 | "3\n", 405 | "4\n", 406 | "5\n" 407 | ] 408 | } 409 | ], 410 | "source": [ 411 | "i=1\n", 412 | "while True:\n", 413 | " print(i)\n", 414 | " i=i+1\n", 415 | " if(i>5):\n", 416 | " break" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 21, 422 | "id": "a5edafb6-0100-4945-b9b0-b3bea162d624", 423 | "metadata": {}, 424 | "outputs": [ 425 | { 426 | "name": "stdout", 427 | "output_type": "stream", 428 | "text": [ 429 | "0\n", 430 | "1\n", 431 | "2\n", 432 | "3\n", 433 | "4\n", 434 | "for loop\n" 435 | ] 436 | } 437 | ], 438 | "source": [ 439 | "#for with else\n", 440 | "n=5\n", 441 | "for i in range(n):\n", 442 | " print(i)\n", 443 | "else:\n", 444 | " print(\"for loop\")" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 22, 450 | "id": "9b0329e8-84ad-4c5e-b5bf-df1f2faf361b", 451 | "metadata": {}, 452 | "outputs": [ 453 | { 454 | "name": "stdout", 455 | "output_type": "stream", 456 | "text": [ 457 | "Inside Loop\n", 458 | "Inside Loop\n", 459 | "Inside Loop\n", 460 | "Inside else\n" 461 | ] 462 | } 463 | ], 464 | "source": [ 465 | "#while loop with else\n", 466 | "counter=0\n", 467 | "while counter<3:\n", 468 | " print('Inside Loop')\n", 469 | " counter=counter+1\n", 470 | "else:\n", 471 | " print('Inside else')\n", 472 | " " 473 | ] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "execution_count": 23, 478 | "id": "3c78870c-07f8-4478-81c5-e2022802000a", 479 | "metadata": {}, 480 | "outputs": [ 481 | { 482 | "name": "stdout", 483 | "output_type": "stream", 484 | "text": [ 485 | "it is positive\n" 486 | ] 487 | } 488 | ], 489 | "source": [ 490 | "#if constructs\n", 491 | "num=10\n", 492 | "if num>0:\n", 493 | " print('it is positive')\n", 494 | "else:\n", 495 | " print('it is negative')" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 24, 501 | "id": "c22c7fa7-ec5b-4848-baae-4be77e19d68d", 502 | "metadata": {}, 503 | "outputs": [ 504 | { 505 | "name": "stdout", 506 | "output_type": "stream", 507 | "text": [ 508 | "number x= 4\n", 509 | "y= 5\n" 510 | ] 511 | } 512 | ], 513 | "source": [ 514 | "#swap two numbers\n", 515 | "x=5\n", 516 | "y=4\n", 517 | "x,y=y,x\n", 518 | "print(\"number x=\",x)\n", 519 | "print(\"y=\",y)" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 26, 525 | "id": "c5495aec-256f-4cef-8e0e-6c98363e3226", 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "name": "stdin", 530 | "output_type": "stream", 531 | "text": [ 532 | "enter the number: 153\n" 533 | ] 534 | }, 535 | { 536 | "name": "stdout", 537 | "output_type": "stream", 538 | "text": [ 539 | "153 is armstrong\n" 540 | ] 541 | } 542 | ], 543 | "source": [ 544 | "#armstrong numbers\n", 545 | "num=int(input(\"enter the number:\"))\n", 546 | "sum=0\n", 547 | "temp=num\n", 548 | "while temp>0:\n", 549 | " digit=temp%10\n", 550 | " sum+=digit**3\n", 551 | " temp//=10\n", 552 | "if num==sum:\n", 553 | " print(num,\"is armstrong\")\n", 554 | "else:\n", 555 | " print(num,\"is not an armstrong\")" 556 | ] 557 | }, 558 | { 559 | "cell_type": "code", 560 | "execution_count": 27, 561 | "id": "ae9060b0-7d5d-4f46-b55d-15372b2706c8", 562 | "metadata": {}, 563 | "outputs": [ 564 | { 565 | "name": "stdin", 566 | "output_type": "stream", 567 | "text": [ 568 | "enter the number: 4\n" 569 | ] 570 | }, 571 | { 572 | "name": "stdout", 573 | "output_type": "stream", 574 | "text": [ 575 | "24\n" 576 | ] 577 | } 578 | ], 579 | "source": [ 580 | "#find the factorial of a number\n", 581 | "num=int(input(\"enter the number: \"))\n", 582 | "fact=1\n", 583 | "if num < 0:\n", 584 | " print(\"factorial of -ve number does not exist\")\n", 585 | "elif num==0:\n", 586 | " print(\"factorial of 0 is\",fact)\n", 587 | "else:\n", 588 | " for i in range(1,num+1):\n", 589 | " fact=fact*i\n", 590 | " print(fact)" 591 | ] 592 | } 593 | ], 594 | "metadata": { 595 | "kernelspec": { 596 | "display_name": "Python 3 (ipykernel)", 597 | "language": "python", 598 | "name": "python3" 599 | }, 600 | "language_info": { 601 | "codemirror_mode": { 602 | "name": "ipython", 603 | "version": 3 604 | }, 605 | "file_extension": ".py", 606 | "mimetype": "text/x-python", 607 | "name": "python", 608 | "nbconvert_exporter": "python", 609 | "pygments_lexer": "ipython3", 610 | "version": "3.11.7" 611 | } 612 | }, 613 | "nbformat": 4, 614 | "nbformat_minor": 5 615 | } 616 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # AI Algorithms 3 | This repository is focused on Artificial Intelligence Searching strategies,Machine Learning algorithm such as Decision Tree, MultiLayer perceptron along with Natural Language Processing. 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | ## Contributing 12 | 13 | Contributions are always welcome! 14 | 15 | - Create a personal fork of the project on Github.This will create a copy of this repository in your account. 16 | 17 | - Clone the fork on your local machine. Your remote repo on Github is called origin. 18 | 19 | ``` 20 | git clone "url u just copied" 21 | ``` 22 | 23 | - Make necessary changes and commit using 24 | ``` 25 | git add filename 26 | ``` 27 | ``` 28 | git commit -m "msg" 29 | ``` 30 | - Push your changes to github forked repository. 31 | ``` 32 | git push -u origin your-branch-name 33 | ``` 34 | - Submit your changes for a review 35 | ``` 36 | If you go to your repository on GitHub, you'll see a Compare & pull request button. Click on that button. 37 | ``` 38 | - Open a pull request 39 | -Soon I'll be merging all your changes into the main branch of this project. You will get a notification email once the changes have been merged. 40 | 41 | 42 | 43 | 44 | ## Authors 45 | - [@Sana Mateen](https://github.com/mssanamateen) 46 | 47 | ## Special Contribution by Students 48 | - [@Robin Thomas Antony](https://github.com/Rt1818) 49 | 50 | ## Student Contributions 51 | -[@Anushka088](https://github.com/Anushka088) 52 | -[@AshwinDivakaruni](https://github.com/Divk-Ashwin) 53 | -[@Sainath Ch](https://github.com/sainathch45) 54 | 55 | -------------------------------------------------------------------------------- /bfs.py: -------------------------------------------------------------------------------- 1 | graph={ 2 | 'P':['Q','R','S'], 3 | 'Q':['P','R'], 4 | 'R':['P','Q','T'], 5 | 'T':['R'], 6 | 'S':['P'] 7 | } 8 | visited=[] 9 | queue=[] 10 | def bfs(visited,graph,node): 11 | visited.append(node) 12 | queue.append(node) 13 | while queue: 14 | m=queue.pop(0) 15 | print(m,end=" ") 16 | for neighbour in graph[m]: 17 | if neighbour not in visited: 18 | visited.append(neighbour) 19 | queue.append(neighbour) 20 | print("following is the breadth first search") 21 | bfs(visited,graph,'P') -------------------------------------------------------------------------------- /decisiontree/diabetes/diabetes.csv: -------------------------------------------------------------------------------- 1 | Glucose,BMI,Outcome 2 | 148,33.6,1 3 | 85,26.6,0 4 | 183,23.3,1 5 | 89,28.1,0 6 | 137,43.1,1 7 | 116,25.6,0 8 | 78,31,1 9 | 115,35.3,0 10 | 197,30.5,1 11 | 125,0,1 12 | 110,37.6,0 13 | 168,38,1 14 | 139,27.1,0 15 | 189,30.1,1 16 | 166,25.8,1 17 | 100,30,1 18 | 118,45.8,1 19 | 107,29.6,1 20 | 103,43.3,0 21 | 115,34.6,1 22 | 126,39.3,0 23 | 99,35.4,0 24 | 196,39.8,1 25 | 119,29,1 26 | 143,36.6,1 27 | 125,31.1,1 28 | 147,39.4,1 29 | 97,23.2,0 30 | 145,22.2,0 31 | 117,34.1,0 32 | 109,36,0 33 | 158,31.6,1 34 | 88,24.8,0 35 | 92,19.9,0 36 | 122,27.6,0 37 | 103,24,0 38 | 138,33.2,0 39 | 102,32.9,1 40 | 90,38.2,1 41 | 111,37.1,1 42 | 180,34,0 43 | 133,40.2,0 44 | 106,22.7,0 45 | 171,45.4,1 46 | 159,27.4,0 47 | 180,42,1 48 | 146,29.7,0 49 | 71,28,0 50 | 103,39.1,1 51 | 105,0,0 52 | 103,19.4,0 53 | 101,24.2,0 54 | 88,24.4,0 55 | 176,33.7,1 56 | 150,34.7,0 57 | 73,23,0 58 | 187,37.7,1 59 | 100,46.8,0 60 | 146,40.5,0 61 | 105,41.5,0 62 | 84,0,0 63 | 133,32.9,1 64 | 44,25,0 65 | 141,25.4,0 66 | 114,32.8,1 67 | 99,29,0 68 | 109,32.5,1 69 | 109,42.7,0 70 | 95,19.6,0 71 | 146,28.9,0 72 | 100,32.9,1 73 | 139,28.6,0 74 | 126,43.4,1 75 | 129,35.1,0 76 | 79,32,0 77 | 0,24.7,0 78 | 62,32.6,0 79 | 95,37.7,0 80 | 131,43.2,1 81 | 112,25,0 82 | 113,22.4,0 83 | 74,0,0 84 | 83,29.3,0 85 | 101,24.6,0 86 | 137,48.8,1 87 | 110,32.4,0 88 | 106,36.6,0 89 | 100,38.5,0 90 | 136,37.1,1 91 | 107,26.5,0 92 | 80,19.1,0 93 | 123,32,0 94 | 81,46.7,0 95 | 134,23.8,1 96 | 142,24.7,0 97 | 144,33.9,0 98 | 92,31.6,0 99 | 71,20.4,0 100 | 93,28.7,0 101 | 122,49.7,1 102 | 163,39,1 103 | 151,26.1,0 104 | 125,22.5,0 105 | 81,26.6,0 106 | 85,39.6,0 107 | 126,28.7,0 108 | 96,22.4,0 109 | 144,29.5,0 110 | 83,34.3,0 111 | 95,37.4,1 112 | 171,33.3,1 113 | 155,34,1 114 | 89,31.2,0 115 | 76,34,0 116 | 160,30.5,1 117 | 146,31.2,1 118 | 124,34,1 119 | 78,33.7,0 120 | 97,28.2,0 121 | 99,23.2,0 122 | 162,53.2,1 123 | 111,34.2,0 124 | 107,33.6,0 125 | 132,26.8,0 126 | 113,33.3,1 127 | 88,55,1 128 | 120,42.9,0 129 | 118,33.3,0 130 | 117,34.5,1 131 | 105,27.9,1 132 | 173,29.7,1 133 | 122,33.3,1 134 | 170,34.5,1 135 | 84,38.3,0 136 | 96,21.1,0 137 | 125,33.8,0 138 | 100,30.8,0 139 | 93,28.7,0 140 | 129,31.2,0 141 | 105,36.9,0 142 | 128,21.1,0 143 | 106,39.5,0 144 | 108,32.5,0 145 | 108,32.4,1 146 | 154,32.8,0 147 | 102,0,0 148 | 57,32.8,0 149 | 106,30.5,0 150 | 147,33.7,0 151 | 90,27.3,0 152 | 136,37.4,0 153 | 114,21.9,0 154 | 156,34.3,1 155 | 153,40.6,0 156 | 188,47.9,1 157 | 152,50,1 158 | 99,24.6,0 159 | 109,25.2,0 160 | 88,29,0 161 | 163,40.9,1 162 | 151,29.7,0 163 | 102,37.2,0 164 | 114,44.2,0 165 | 100,29.7,0 166 | 131,31.6,1 167 | 104,29.9,1 168 | 148,32.5,0 169 | 120,29.6,0 170 | 110,31.9,0 171 | 111,28.4,0 172 | 102,30.8,1 173 | 134,35.4,1 174 | 87,28.9,0 175 | 79,43.5,0 176 | 75,29.7,0 177 | 179,32.7,1 178 | 85,31.2,0 179 | 129,67.1,1 180 | 143,45,0 181 | 130,39.1,1 182 | 87,23.2,0 183 | 119,34.9,0 184 | 0,27.7,0 185 | 73,26.8,0 186 | 141,27.6,0 187 | 194,35.9,1 188 | 181,30.1,1 189 | 128,32,1 190 | 109,27.9,1 191 | 139,31.6,1 192 | 111,22.6,0 193 | 123,33.1,0 194 | 159,30.4,1 195 | 135,52.3,1 196 | 85,24.4,0 197 | 158,39.4,1 198 | 105,24.3,0 199 | 107,22.9,1 200 | 109,34.8,1 201 | 148,30.9,1 202 | 113,31,0 203 | 138,40.1,0 204 | 108,27.3,0 205 | 99,20.4,0 206 | 103,37.7,0 207 | 111,23.9,0 208 | 196,37.5,1 209 | 162,37.7,1 210 | 96,33.2,0 211 | 184,35.5,1 212 | 81,27.7,0 213 | 147,42.8,0 214 | 179,34.2,0 215 | 140,42.6,1 216 | 112,34.2,1 217 | 151,41.8,1 218 | 109,35.8,1 219 | 125,30,0 220 | 85,29,1 221 | 112,37.8,1 222 | 177,34.6,1 223 | 158,31.6,1 224 | 119,25.2,0 225 | 142,28.8,0 226 | 100,23.6,0 227 | 87,34.6,0 228 | 101,35.7,0 229 | 162,37.2,1 230 | 197,36.7,0 231 | 117,45.2,0 232 | 142,44,1 233 | 134,46.2,1 234 | 79,25.4,0 235 | 122,35,0 236 | 74,29.7,0 237 | 171,43.6,1 238 | 181,35.9,1 239 | 179,44.1,1 240 | 164,30.8,1 241 | 104,18.4,0 242 | 91,29.2,0 243 | 91,33.1,0 244 | 139,25.6,1 245 | 119,27.1,1 246 | 146,38.2,0 247 | 184,30,1 248 | 122,31.2,0 249 | 165,52.3,0 250 | 124,35.4,0 251 | 111,30.1,0 252 | 106,31.2,0 253 | 129,28,0 254 | 90,24.4,0 255 | 86,35.8,0 256 | 92,27.6,1 257 | 113,33.6,1 258 | 111,30.1,0 259 | 114,28.7,0 260 | 193,25.9,0 261 | 155,33.3,1 262 | 191,30.9,0 263 | 141,30,1 264 | 95,32.1,0 265 | 142,32.4,0 266 | 123,32,1 267 | 96,33.6,0 268 | 138,36.3,1 269 | 128,40,0 270 | 102,25.1,0 271 | 146,27.5,1 272 | 101,45.6,1 273 | 108,25.2,0 274 | 122,23,0 275 | 71,33.2,0 276 | 106,34.2,0 277 | 100,40.5,0 278 | 106,26.5,1 279 | 104,27.8,0 280 | 114,24.9,0 281 | 108,25.3,0 282 | 146,37.9,1 283 | 129,35.9,0 284 | 133,32.4,0 285 | 161,30.4,1 286 | 108,27,1 287 | 136,26,0 288 | 155,38.7,0 289 | 119,45.6,1 290 | 96,20.8,0 291 | 108,36.1,0 292 | 78,36.9,0 293 | 107,36.6,1 294 | 128,43.3,1 295 | 128,40.5,1 296 | 161,21.9,0 297 | 151,35.5,0 298 | 146,28,1 299 | 126,30.7,0 300 | 100,36.6,1 301 | 112,23.6,0 302 | 167,32.3,1 303 | 144,31.6,1 304 | 77,35.8,0 305 | 115,52.9,1 306 | 150,21,0 307 | 120,39.7,0 308 | 161,25.5,1 309 | 137,24.8,0 310 | 128,30.5,1 311 | 124,32.9,1 312 | 80,26.2,0 313 | 106,39.4,0 314 | 155,26.6,1 315 | 113,29.5,0 316 | 109,35.9,1 317 | 112,34.1,0 318 | 99,19.3,0 319 | 182,30.5,1 320 | 115,38.1,0 321 | 194,23.5,1 322 | 129,27.5,0 323 | 112,31.6,1 324 | 124,27.4,1 325 | 152,26.8,1 326 | 112,35.7,0 327 | 157,25.6,0 328 | 122,35.1,1 329 | 179,35.1,0 330 | 102,45.5,1 331 | 105,30.8,0 332 | 118,23.1,0 333 | 87,32.7,0 334 | 180,43.3,1 335 | 106,23.6,0 336 | 95,23.9,0 337 | 165,47.9,0 338 | 117,33.8,0 339 | 115,31.2,1 340 | 152,34.2,1 341 | 178,39.9,1 342 | 130,25.9,0 343 | 95,25.9,0 344 | 0,32,0 345 | 122,34.7,0 346 | 95,36.8,0 347 | 126,38.5,0 348 | 139,28.7,0 349 | 116,23.5,0 350 | 99,21.8,0 351 | 0,41,1 352 | 92,42.2,0 353 | 137,31.2,0 354 | 61,34.4,0 355 | 90,27.2,0 356 | 90,42.7,0 357 | 165,30.4,1 358 | 125,33.3,1 359 | 129,39.9,1 360 | 88,35.3,0 361 | 196,36.5,1 362 | 189,31.2,1 363 | 158,29.8,0 364 | 103,39.2,0 365 | 146,38.5,1 366 | 147,34.9,0 367 | 99,34,0 368 | 124,27.6,1 369 | 101,21,0 370 | 81,27.5,0 371 | 133,32.8,1 372 | 173,38.4,1 373 | 118,0,0 374 | 84,35.8,0 375 | 105,34.9,0 376 | 122,36.2,0 377 | 140,39.2,1 378 | 98,25.2,0 379 | 87,37.2,0 380 | 156,48.3,1 381 | 93,43.4,0 382 | 107,30.8,0 383 | 105,20,0 384 | 109,25.4,0 385 | 90,25.1,0 386 | 125,24.3,0 387 | 119,22.3,0 388 | 116,32.3,1 389 | 105,43.3,1 390 | 144,32,1 391 | 100,31.6,0 392 | 100,32,0 393 | 166,45.7,1 394 | 131,23.7,0 395 | 116,22.1,0 396 | 158,32.9,1 397 | 127,27.7,0 398 | 96,24.7,0 399 | 131,34.3,1 400 | 82,21.1,0 401 | 193,34.9,1 402 | 95,32,1 403 | 137,24.2,0 404 | 136,35,1 405 | 72,31.6,0 406 | 168,32.9,1 407 | 123,42.1,0 408 | 115,28.9,1 409 | 101,21.9,0 410 | 197,25.9,1 411 | 172,42.4,1 412 | 102,35.7,0 413 | 112,34.4,0 414 | 143,42.4,0 415 | 143,26.2,0 416 | 138,34.6,1 417 | 173,35.7,1 418 | 97,27.2,0 419 | 144,38.5,1 420 | 83,18.2,0 421 | 129,26.4,1 422 | 119,45.3,0 423 | 94,26,0 424 | 102,40.6,0 425 | 115,30.8,0 426 | 151,42.9,1 427 | 184,37,1 428 | 94,0,0 429 | 181,34.1,1 430 | 135,40.6,0 431 | 95,35,1 432 | 99,22.2,0 433 | 89,30.4,0 434 | 80,30,0 435 | 139,25.6,0 436 | 90,24.5,0 437 | 141,42.4,1 438 | 140,37.4,0 439 | 147,29.9,0 440 | 97,18.2,0 441 | 107,36.8,0 442 | 189,34.3,1 443 | 83,32.2,0 444 | 117,33.2,0 445 | 108,30.5,1 446 | 117,29.7,1 447 | 180,59.4,1 448 | 100,25.3,0 449 | 95,36.5,0 450 | 104,33.6,1 451 | 120,30.5,0 452 | 82,21.2,0 453 | 134,28.9,1 454 | 91,39.9,0 455 | 119,19.6,0 456 | 100,37.8,0 457 | 175,33.6,1 458 | 135,26.7,0 459 | 86,30.2,0 460 | 148,37.6,1 461 | 134,25.9,0 462 | 120,20.8,0 463 | 71,21.8,0 464 | 74,35.3,0 465 | 88,27.6,0 466 | 115,24,0 467 | 124,21.8,0 468 | 74,27.8,0 469 | 97,36.8,0 470 | 120,30,1 471 | 154,46.1,0 472 | 144,41.3,0 473 | 137,33.2,0 474 | 119,38.8,0 475 | 136,29.9,0 476 | 114,28.9,0 477 | 137,27.3,0 478 | 105,33.7,1 479 | 114,23.8,0 480 | 126,25.9,0 481 | 132,28,0 482 | 158,35.5,1 483 | 123,35.2,0 484 | 85,27.8,0 485 | 84,38.2,0 486 | 145,44.2,1 487 | 135,42.3,1 488 | 139,40.7,0 489 | 173,46.5,0 490 | 99,25.6,0 491 | 194,26.1,0 492 | 83,36.8,0 493 | 89,33.5,0 494 | 99,32.8,0 495 | 125,28.9,1 496 | 80,0,0 497 | 166,26.6,0 498 | 110,26,0 499 | 81,30.1,0 500 | 195,25.1,1 501 | 154,29.3,0 502 | 117,25.2,0 503 | 84,37.2,0 504 | 0,39,1 505 | 94,33.3,0 506 | 96,37.3,0 507 | 75,33.3,0 508 | 180,36.5,1 509 | 130,28.6,0 510 | 84,30.4,0 511 | 120,25,0 512 | 84,29.7,1 513 | 139,22.1,0 514 | 91,24.2,0 515 | 91,27.3,0 516 | 99,25.6,0 517 | 163,31.6,1 518 | 145,30.3,1 519 | 125,37.6,0 520 | 76,32.8,0 521 | 129,19.6,0 522 | 68,25,0 523 | 124,33.2,0 524 | 114,0,0 525 | 130,34.2,1 526 | 125,31.6,0 527 | 87,21.8,0 528 | 97,18.2,0 529 | 116,26.3,0 530 | 117,30.8,0 531 | 111,24.6,0 532 | 122,29.8,0 533 | 107,45.3,0 534 | 86,41.3,0 535 | 91,29.8,0 536 | 77,33.3,0 537 | 132,32.9,1 538 | 105,29.6,0 539 | 57,21.7,0 540 | 127,36.3,0 541 | 129,36.4,1 542 | 100,39.4,1 543 | 128,32.4,1 544 | 90,34.9,1 545 | 84,39.5,0 546 | 88,32,0 547 | 186,34.5,1 548 | 187,43.6,1 549 | 131,33.1,0 550 | 164,32.8,0 551 | 189,28.5,0 552 | 116,27.4,0 553 | 84,31.9,0 554 | 114,27.8,0 555 | 88,29.9,0 556 | 84,36.9,0 557 | 124,25.5,0 558 | 97,38.1,0 559 | 110,27.8,0 560 | 103,46.2,0 561 | 85,30.1,0 562 | 125,33.8,1 563 | 198,41.3,1 564 | 87,37.6,0 565 | 99,26.9,0 566 | 91,32.4,0 567 | 95,26.1,0 568 | 99,38.6,0 569 | 92,32,0 570 | 154,31.3,0 571 | 121,34.3,1 572 | 78,32.5,0 573 | 130,22.6,0 574 | 111,29.5,0 575 | 98,34.7,0 576 | 143,30.1,0 577 | 119,35.5,0 578 | 108,24,0 579 | 118,42.9,1 580 | 133,27,0 581 | 197,34.7,1 582 | 151,42.1,1 583 | 109,25,0 584 | 121,26.5,0 585 | 100,38.7,0 586 | 124,28.7,1 587 | 93,22.5,0 588 | 143,34.9,1 589 | 103,24.3,0 590 | 176,33.3,1 591 | 73,21.1,0 592 | 111,46.8,1 593 | 112,39.4,0 594 | 132,34.4,1 595 | 82,28.5,0 596 | 123,33.6,0 597 | 188,32,1 598 | 67,45.3,0 599 | 89,27.8,0 600 | 173,36.8,1 601 | 109,23.1,0 602 | 108,27.1,0 603 | 96,23.7,0 604 | 124,27.8,0 605 | 150,35.2,1 606 | 183,28.4,1 607 | 124,35.8,0 608 | 181,40,1 609 | 92,19.5,0 610 | 152,41.5,0 611 | 111,24,0 612 | 106,30.9,0 613 | 174,32.9,1 614 | 168,38.2,1 615 | 105,32.5,0 616 | 138,36.1,1 617 | 106,25.8,0 618 | 117,28.7,0 619 | 68,20.1,0 620 | 112,28.2,1 621 | 119,32.4,1 622 | 112,38.4,0 623 | 92,24.2,0 624 | 183,40.8,0 625 | 94,43.5,0 626 | 108,30.8,0 627 | 90,37.7,0 628 | 125,24.7,0 629 | 132,32.4,0 630 | 128,34.6,0 631 | 94,24.7,0 632 | 114,27.4,1 633 | 102,34.5,0 634 | 111,26.2,0 635 | 128,27.5,0 636 | 92,25.9,0 637 | 104,31.2,1 638 | 104,28.8,0 639 | 94,31.6,0 640 | 97,40.9,1 641 | 100,19.5,0 642 | 102,29.3,0 643 | 128,34.3,0 644 | 147,29.5,1 645 | 90,28,0 646 | 103,27.6,0 647 | 157,39.4,0 648 | 167,23.4,1 649 | 179,37.8,1 650 | 136,28.3,1 651 | 107,26.4,0 652 | 91,25.2,0 653 | 117,33.8,0 654 | 123,34.1,0 655 | 120,26.8,0 656 | 106,34.2,0 657 | 155,38.7,1 658 | 101,21.8,0 659 | 120,38.9,0 660 | 127,39,0 661 | 80,34.2,1 662 | 162,27.7,0 663 | 199,42.9,1 664 | 167,37.6,1 665 | 145,37.9,1 666 | 115,33.7,1 667 | 112,34.8,0 668 | 145,32.5,1 669 | 111,27.5,1 670 | 98,34,0 671 | 154,30.9,0 672 | 165,33.6,0 673 | 99,25.4,0 674 | 68,35.5,0 675 | 123,57.3,0 676 | 91,35.6,0 677 | 195,30.9,1 678 | 156,24.8,1 679 | 93,35.3,0 680 | 121,36,1 681 | 101,24.2,0 682 | 56,24.2,0 683 | 162,49.6,1 684 | 95,44.6,0 685 | 125,32.3,1 686 | 136,0,0 687 | 129,33.2,0 688 | 130,23.1,0 689 | 107,28.3,0 690 | 140,24.1,0 691 | 144,46.1,1 692 | 107,24.6,0 693 | 158,42.3,1 694 | 121,39.1,0 695 | 129,38.5,1 696 | 90,23.5,0 697 | 142,30.4,1 698 | 169,29.9,1 699 | 99,25,0 700 | 127,34.5,0 701 | 118,44.5,0 702 | 122,35.9,0 703 | 125,27.6,1 704 | 168,35,1 705 | 129,38.5,0 706 | 110,28.4,0 707 | 80,39.8,0 708 | 115,0,1 709 | 127,34.4,0 710 | 164,32.8,1 711 | 93,38,1 712 | 158,31.2,0 713 | 126,29.6,0 714 | 129,41.2,1 715 | 134,26.4,0 716 | 102,29.5,0 717 | 187,33.9,1 718 | 173,33.8,1 719 | 94,23.1,0 720 | 108,35.5,0 721 | 97,35.6,1 722 | 83,29.3,0 723 | 114,38.1,0 724 | 149,29.3,1 725 | 117,39.1,0 726 | 111,32.8,0 727 | 112,39.4,0 728 | 116,36.1,0 729 | 141,32.4,0 730 | 175,22.9,0 731 | 92,30.1,0 732 | 130,28.4,1 733 | 120,28.4,1 734 | 174,44.5,1 735 | 106,29,0 736 | 105,23.3,0 737 | 95,35.4,0 738 | 126,27.4,0 739 | 65,32,0 740 | 99,36.6,0 741 | 102,39.5,1 742 | 120,42.3,1 743 | 102,30.8,0 744 | 109,28.5,0 745 | 140,32.7,1 746 | 153,40.6,0 747 | 100,30,0 748 | 147,49.3,1 749 | 81,46.3,0 750 | 187,36.4,1 751 | 162,24.3,1 752 | 136,31.2,1 753 | 121,39,0 754 | 108,26,0 755 | 181,43.3,1 756 | 154,32.4,1 757 | 128,36.5,1 758 | 137,32,0 759 | 123,36.3,1 760 | 106,37.5,0 761 | 190,35.5,1 762 | 88,28.4,0 763 | 170,44,1 764 | 89,22.5,0 765 | 101,32.9,0 766 | 122,36.8,0 767 | 121,26.2,0 768 | 126,30.1,1 769 | 93,30.4,0 770 | -------------------------------------------------------------------------------- /decisiontree/iris/iris.csv: -------------------------------------------------------------------------------- 1 | sepal_length,sepal_width,petal_length,petal_width,species 2 | 5.1,3.5,1.4,0.2,setosa 3 | 4.9,3,1.4,0.2,setosa 4 | 4.7,3.2,1.3,0.2,setosa 5 | 4.6,3.1,1.5,0.2,setosa 6 | 5,3.6,1.4,0.2,setosa 7 | 5.4,3.9,1.7,0.4,setosa 8 | 4.6,3.4,1.4,0.3,setosa 9 | 5,3.4,1.5,0.2,setosa 10 | 4.4,2.9,1.4,0.2,setosa 11 | 4.9,3.1,1.5,0.1,setosa 12 | 5.4,3.7,1.5,0.2,setosa 13 | 4.8,3.4,1.6,0.2,setosa 14 | 4.8,3,1.4,0.1,setosa 15 | 4.3,3,1.1,0.1,setosa 16 | 5.8,4,1.2,0.2,setosa 17 | 5.7,4.4,1.5,0.4,setosa 18 | 5.4,3.9,1.3,0.4,setosa 19 | 5.1,3.5,1.4,0.3,setosa 20 | 5.7,3.8,1.7,0.3,setosa 21 | 5.1,3.8,1.5,0.3,setosa 22 | 5.4,3.4,1.7,0.2,setosa 23 | 5.1,3.7,1.5,0.4,setosa 24 | 4.6,3.6,1,0.2,setosa 25 | 5.1,3.3,1.7,0.5,setosa 26 | 4.8,3.4,1.9,0.2,setosa 27 | 5,3,1.6,0.2,setosa 28 | 5,3.4,1.6,0.4,setosa 29 | 5.2,3.5,1.5,0.2,setosa 30 | 5.2,3.4,1.4,0.2,setosa 31 | 4.7,3.2,1.6,0.2,setosa 32 | 4.8,3.1,1.6,0.2,setosa 33 | 5.4,3.4,1.5,0.4,setosa 34 | 5.2,4.1,1.5,0.1,setosa 35 | 5.5,4.2,1.4,0.2,setosa 36 | 4.9,3.1,1.5,0.1,setosa 37 | 5,3.2,1.2,0.2,setosa 38 | 5.5,3.5,1.3,0.2,setosa 39 | 4.9,3.1,1.5,0.1,setosa 40 | 4.4,3,1.3,0.2,setosa 41 | 5.1,3.4,1.5,0.2,setosa 42 | 5,3.5,1.3,0.3,setosa 43 | 4.5,2.3,1.3,0.3,setosa 44 | 4.4,3.2,1.3,0.2,setosa 45 | 5,3.5,1.6,0.6,setosa 46 | 5.1,3.8,1.9,0.4,setosa 47 | 4.8,3,1.4,0.3,setosa 48 | 5.1,3.8,1.6,0.2,setosa 49 | 4.6,3.2,1.4,0.2,setosa 50 | 5.3,3.7,1.5,0.2,setosa 51 | 5,3.3,1.4,0.2,setosa 52 | 7,3.2,4.7,1.4,versicolor 53 | 6.4,3.2,4.5,1.5,versicolor 54 | 6.9,3.1,4.9,1.5,versicolor 55 | 5.5,2.3,4,1.3,versicolor 56 | 6.5,2.8,4.6,1.5,versicolor 57 | 5.7,2.8,4.5,1.3,versicolor 58 | 6.3,3.3,4.7,1.6,versicolor 59 | 4.9,2.4,3.3,1,versicolor 60 | 6.6,2.9,4.6,1.3,versicolor 61 | 5.2,2.7,3.9,1.4,versicolor 62 | 5,2,3.5,1,versicolor 63 | 5.9,3,4.2,1.5,versicolor 64 | 6,2.2,4,1,versicolor 65 | 6.1,2.9,4.7,1.4,versicolor 66 | 5.6,2.9,3.6,1.3,versicolor 67 | 6.7,3.1,4.4,1.4,versicolor 68 | 5.6,3,4.5,1.5,versicolor 69 | 5.8,2.7,4.1,1,versicolor 70 | 6.2,2.2,4.5,1.5,versicolor 71 | 5.6,2.5,3.9,1.1,versicolor 72 | 5.9,3.2,4.8,1.8,versicolor 73 | 6.1,2.8,4,1.3,versicolor 74 | 6.3,2.5,4.9,1.5,versicolor 75 | 6.1,2.8,4.7,1.2,versicolor 76 | 6.4,2.9,4.3,1.3,versicolor 77 | 6.6,3,4.4,1.4,versicolor 78 | 6.8,2.8,4.8,1.4,versicolor 79 | 6.7,3,5,1.7,versicolor 80 | 6,2.9,4.5,1.5,versicolor 81 | 5.7,2.6,3.5,1,versicolor 82 | 5.5,2.4,3.8,1.1,versicolor 83 | 5.5,2.4,3.7,1,versicolor 84 | 5.8,2.7,3.9,1.2,versicolor 85 | 6,2.7,5.1,1.6,versicolor 86 | 5.4,3,4.5,1.5,versicolor 87 | 6,3.4,4.5,1.6,versicolor 88 | 6.7,3.1,4.7,1.5,versicolor 89 | 6.3,2.3,4.4,1.3,versicolor 90 | 5.6,3,4.1,1.3,versicolor 91 | 5.5,2.5,4,1.3,versicolor 92 | 5.5,2.6,4.4,1.2,versicolor 93 | 6.1,3,4.6,1.4,versicolor 94 | 5.8,2.6,4,1.2,versicolor 95 | 5,2.3,3.3,1,versicolor 96 | 5.6,2.7,4.2,1.3,versicolor 97 | 5.7,3,4.2,1.2,versicolor 98 | 5.7,2.9,4.2,1.3,versicolor 99 | 6.2,2.9,4.3,1.3,versicolor 100 | 5.1,2.5,3,1.1,versicolor 101 | 5.7,2.8,4.1,1.3,versicolor 102 | 6.3,3.3,6,2.5,virginica 103 | 5.8,2.7,5.1,1.9,virginica 104 | 7.1,3,5.9,2.1,virginica 105 | 6.3,2.9,5.6,1.8,virginica 106 | 6.5,3,5.8,2.2,virginica 107 | 7.6,3,6.6,2.1,virginica 108 | 4.9,2.5,4.5,1.7,virginica 109 | 7.3,2.9,6.3,1.8,virginica 110 | 6.7,2.5,5.8,1.8,virginica 111 | 7.2,3.6,6.1,2.5,virginica 112 | 6.5,3.2,5.1,2,virginica 113 | 6.4,2.7,5.3,1.9,virginica 114 | 6.8,3,5.5,2.1,virginica 115 | 5.7,2.5,5,2,virginica 116 | 5.8,2.8,5.1,2.4,virginica 117 | 6.4,3.2,5.3,2.3,virginica 118 | 6.5,3,5.5,1.8,virginica 119 | 7.7,3.8,6.7,2.2,virginica 120 | 7.7,2.6,6.9,2.3,virginica 121 | 6,2.2,5,1.5,virginica 122 | 6.9,3.2,5.7,2.3,virginica 123 | 5.6,2.8,4.9,2,virginica 124 | 7.7,2.8,6.7,2,virginica 125 | 6.3,2.7,4.9,1.8,virginica 126 | 6.7,3.3,5.7,2.1,virginica 127 | 7.2,3.2,6,1.8,virginica 128 | 6.2,2.8,4.8,1.8,virginica 129 | 6.1,3,4.9,1.8,virginica 130 | 6.4,2.8,5.6,2.1,virginica 131 | 7.2,3,5.8,1.6,virginica 132 | 7.4,2.8,6.1,1.9,virginica 133 | 7.9,3.8,6.4,2,virginica 134 | 6.4,2.8,5.6,2.2,virginica 135 | 6.3,2.8,5.1,1.5,virginica 136 | 6.1,2.6,5.6,1.4,virginica 137 | 7.7,3,6.1,2.3,virginica 138 | 6.3,3.4,5.6,2.4,virginica 139 | 6.4,3.1,5.5,1.8,virginica 140 | 6,3,4.8,1.8,virginica 141 | 6.9,3.1,5.4,2.1,virginica 142 | 6.7,3.1,5.6,2.4,virginica 143 | 6.9,3.1,5.1,2.3,virginica 144 | 5.8,2.7,5.1,1.9,virginica 145 | 6.8,3.2,5.9,2.3,virginica 146 | 6.7,3.3,5.7,2.5,virginica 147 | 6.7,3,5.2,2.3,virginica 148 | 6.3,2.5,5,1.9,virginica 149 | 6.5,3,5.2,2,virginica 150 | 6.2,3.4,5.4,2.3,virginica 151 | 5.9,3,5.1,1.8,virginica 152 | -------------------------------------------------------------------------------- /decisiontree/studentt/student_dataset.csv: -------------------------------------------------------------------------------- 1 | Grades,Attendance,Study Hours,Label 2 | 46,0.62,2,Weak 3 | 59,0.71,3,Weak 4 | 54,0.67,3,Weak 5 | 50,0.62,3,Weak 6 | 47,0.6,4,Weak 7 | 46,0.62,4,Weak 8 | 58,0.64,3,Weak 9 | 50,0.74,4,Weak 10 | 50,0.73,1,Weak 11 | 43,0.62,1,Weak 12 | 47,0.6,4,Weak 13 | 42,0.64,4,Weak 14 | 41,0.73,3,Weak 15 | 51,0.66,2,Weak 16 | 45,0.68,4,Weak 17 | 41,0.74,1,Weak 18 | 40,0.74,3,Weak 19 | 51,0.69,4,Weak 20 | 51,0.72,1,Weak 21 | 56,0.78,1,Weak 22 | 49,0.66,2,Weak 23 | 55,0.76,3,Weak 24 | 54,0.79,3,Weak 25 | 54,0.63,2,Weak 26 | 58,0.64,2,Weak 27 | 51,0.66,3,Weak 28 | 59,0.72,3,Weak 29 | 42,0.74,4,Weak 30 | 44,0.7,4,Weak 31 | 58,0.63,2,Weak 32 | 46,0.72,4,Weak 33 | 48,0.66,1,Weak 34 | 46,0.78,4,Weak 35 | 57,0.61,4,Weak 36 | 43,0.69,1,Weak 37 | 53,0.72,2,Weak 38 | 57,0.65,1,Weak 39 | 48,0.71,4,Weak 40 | 41,0.71,2,Weak 41 | 59,0.79,4,Weak 42 | 54,0.7,1,Weak 43 | 46,0.66,1,Weak 44 | 51,0.6,4,Weak 45 | 47,0.6,3,Weak 46 | 54,0.79,3,Weak 47 | 42,0.72,1,Weak 48 | 53,0.68,1,Weak 49 | 56,0.62,3,Weak 50 | 43,0.66,3,Weak 51 | 57,0.65,4,Weak 52 | 47,0.67,4,Weak 53 | 43,0.68,3,Weak 54 | 41,0.64,4,Weak 55 | 45,0.6,4,Weak 56 | 49,0.78,2,Weak 57 | 43,0.69,4,Weak 58 | 57,0.71,1,Weak 59 | 51,0.74,4,Weak 60 | 41,0.68,4,Weak 61 | 49,0.79,3,Weak 62 | 43,0.76,1,Weak 63 | 53,0.76,4,Weak 64 | 55,0.79,4,Weak 65 | 54,0.71,2,Weak 66 | 47,0.66,4,Weak 67 | 53,0.61,4,Weak 68 | 47,0.62,2,Weak 69 | 55,0.76,3,Weak 70 | 52,0.64,4,Weak 71 | 57,0.76,2,Weak 72 | 54,0.76,1,Weak 73 | 52,0.76,3,Weak 74 | 48,0.61,1,Weak 75 | 54,0.61,1,Weak 76 | 52,0.64,2,Weak 77 | 40,0.6,4,Weak 78 | 46,0.6,2,Weak 79 | 48,0.78,2,Weak 80 | 40,0.61,2,Weak 81 | 51,0.71,2,Weak 82 | 47,0.65,3,Weak 83 | 50,0.63,4,Weak 84 | 58,0.7,1,Weak 85 | 56,0.76,1,Weak 86 | 47,0.65,4,Weak 87 | 42,0.64,1,Weak 88 | 42,0.79,4,Weak 89 | 40,0.61,1,Weak 90 | 44,0.65,2,Weak 91 | 49,0.7,1,Weak 92 | 46,0.75,4,Weak 93 | 48,0.75,4,Weak 94 | 46,0.6,4,Weak 95 | 48,0.68,4,Weak 96 | 47,0.65,4,Weak 97 | 51,0.75,3,Weak 98 | 41,0.62,3,Weak 99 | 40,0.79,1,Weak 100 | 55,0.63,4,Weak 101 | 44,0.78,1,Weak 102 | 95,0.8,5,Advanced 103 | 91,0.8,8,Advanced 104 | 98,0.94,5,Advanced 105 | 93,0.81,9,Advanced 106 | 85,0.95,7,Advanced 107 | 85,0.87,7,Advanced 108 | 92,0.92,5,Advanced 109 | 98,0.8,8,Advanced 110 | 87,0.95,8,Advanced 111 | 81,0.86,9,Advanced 112 | 80,0.84,5,Advanced 113 | 94,0.82,7,Advanced 114 | 80,0.91,8,Advanced 115 | 84,0.95,5,Advanced 116 | 95,0.98,8,Advanced 117 | 98,0.84,8,Advanced 118 | 83,0.93,7,Advanced 119 | 82,0.84,6,Advanced 120 | 96,0.94,9,Advanced 121 | 96,0.96,9,Advanced 122 | 91,0.93,7,Advanced 123 | 93,0.99,8,Advanced 124 | 85,0.84,5,Advanced 125 | 82,0.91,8,Advanced 126 | 88,0.95,7,Advanced 127 | 84,0.95,9,Advanced 128 | 96,0.86,8,Advanced 129 | 93,0.83,9,Advanced 130 | 82,0.8,5,Advanced 131 | 80,0.84,9,Advanced 132 | 99,0.89,9,Advanced 133 | 80,0.84,6,Advanced 134 | 82,0.83,6,Advanced 135 | 97,0.81,6,Advanced 136 | 89,0.99,9,Advanced 137 | 82,0.89,7,Advanced 138 | 87,0.98,9,Advanced 139 | 93,0.8,7,Advanced 140 | 97,0.84,7,Advanced 141 | 94,0.92,6,Advanced 142 | 81,0.83,8,Advanced 143 | 89,0.95,5,Advanced 144 | 81,0.95,6,Advanced 145 | 96,0.81,6,Advanced 146 | 87,0.96,8,Advanced 147 | 80,0.99,5,Advanced 148 | 88,0.91,9,Advanced 149 | 90,0.97,9,Advanced 150 | 95,0.82,6,Advanced 151 | 86,0.8,5,Advanced 152 | 89,0.8,6,Advanced 153 | 82,0.98,7,Advanced 154 | 97,0.9,6,Advanced 155 | 92,0.84,6,Advanced 156 | 86,0.91,9,Advanced 157 | 83,0.82,9,Advanced 158 | 92,0.8,9,Advanced 159 | 99,0.8,7,Advanced 160 | 80,0.87,9,Advanced 161 | 87,0.89,5,Advanced 162 | 93,0.9,8,Advanced 163 | 95,0.91,5,Advanced 164 | 93,0.92,5,Advanced 165 | 91,0.91,9,Advanced 166 | 98,0.93,8,Advanced 167 | 94,0.81,8,Advanced 168 | 81,0.98,8,Advanced 169 | 81,0.97,7,Advanced 170 | 98,0.82,9,Advanced 171 | 96,0.96,8,Advanced 172 | 99,0.87,7,Advanced 173 | 89,0.89,6,Advanced 174 | 85,0.81,6,Advanced 175 | 94,0.98,7,Advanced 176 | 90,0.88,7,Advanced 177 | 84,0.86,9,Advanced 178 | 80,0.83,9,Advanced 179 | 87,0.97,6,Advanced 180 | 91,0.92,8,Advanced 181 | 91,0.9,6,Advanced 182 | 84,0.83,8,Advanced 183 | 86,0.83,8,Advanced 184 | 83,0.89,9,Advanced 185 | 85,0.84,5,Advanced 186 | 92,0.88,5,Advanced 187 | 99,0.82,7,Advanced 188 | 94,0.96,9,Advanced 189 | 82,0.82,8,Advanced 190 | 87,0.95,5,Advanced 191 | 99,0.83,8,Advanced 192 | 95,0.97,5,Advanced 193 | 92,0.96,5,Advanced 194 | 97,0.86,5,Advanced 195 | 89,0.84,9,Advanced 196 | 98,0.91,6,Advanced 197 | 96,0.96,8,Advanced 198 | 98,0.92,9,Advanced 199 | 84,0.82,9,Advanced 200 | 88,0.88,9,Advanced 201 | 91,0.96,9,Advanced 202 | 75,0.86,5,Average 203 | 70,0.76,6,Average 204 | 64,0.81,4,Average 205 | 63,0.86,7,Average 206 | 62,0.82,4,Average 207 | 78,0.89,3,Average 208 | 79,0.73,3,Average 209 | 77,0.79,6,Average 210 | 74,0.72,5,Average 211 | 68,0.78,6,Average 212 | 76,0.82,6,Average 213 | 73,0.87,6,Average 214 | 74,0.84,6,Average 215 | 60,0.73,7,Average 216 | 62,0.84,7,Average 217 | 75,0.71,4,Average 218 | 70,0.84,3,Average 219 | 71,0.77,6,Average 220 | 69,0.77,4,Average 221 | 75,0.86,3,Average 222 | 67,0.81,5,Average 223 | 65,0.88,7,Average 224 | 71,0.79,3,Average 225 | 67,0.78,5,Average 226 | 63,0.74,3,Average 227 | 67,0.75,7,Average 228 | 77,0.89,7,Average 229 | 64,0.71,4,Average 230 | 68,0.82,6,Average 231 | 63,0.75,3,Average 232 | 76,0.74,3,Average 233 | 68,0.7,5,Average 234 | 60,0.75,7,Average 235 | 79,0.75,3,Average 236 | 72,0.85,7,Average 237 | 75,0.73,3,Average 238 | 72,0.8,4,Average 239 | 73,0.78,6,Average 240 | 62,0.72,3,Average 241 | 65,0.88,7,Average 242 | 77,0.77,4,Average 243 | 78,0.74,3,Average 244 | 64,0.73,5,Average 245 | 74,0.75,6,Average 246 | 61,0.71,3,Average 247 | 69,0.89,4,Average 248 | 77,0.8,5,Average 249 | 72,0.73,4,Average 250 | 64,0.84,4,Average 251 | 60,0.75,5,Average 252 | 60,0.86,4,Average 253 | 77,0.75,5,Average 254 | 74,0.88,3,Average 255 | 76,0.71,5,Average 256 | 70,0.84,5,Average 257 | 76,0.8,6,Average 258 | 72,0.77,5,Average 259 | 60,0.88,5,Average 260 | 61,0.82,3,Average 261 | 68,0.81,6,Average 262 | 62,0.74,7,Average 263 | 60,0.75,3,Average 264 | 75,0.73,3,Average 265 | 65,0.88,7,Average 266 | 76,0.89,5,Average 267 | 64,0.7,4,Average 268 | 64,0.89,6,Average 269 | 65,0.86,4,Average 270 | 62,0.81,7,Average 271 | 64,0.84,3,Average 272 | 64,0.7,3,Average 273 | 69,0.83,6,Average 274 | 69,0.75,3,Average 275 | 78,0.74,3,Average 276 | 76,0.8,6,Average 277 | 73,0.73,6,Average 278 | 68,0.82,7,Average 279 | 73,0.8,3,Average 280 | 60,0.72,3,Average 281 | 78,0.7,7,Average 282 | 72,0.75,5,Average 283 | 72,0.87,5,Average 284 | 63,0.79,5,Average 285 | 60,0.74,7,Average 286 | 76,0.79,3,Average 287 | 67,0.81,7,Average 288 | 61,0.71,7,Average 289 | 67,0.82,5,Average 290 | 66,0.77,4,Average 291 | 61,0.71,3,Average 292 | 62,0.89,5,Average 293 | 77,0.7,4,Average 294 | 71,0.74,4,Average 295 | 60,0.78,6,Average 296 | 71,0.86,3,Average 297 | 64,0.78,4,Average 298 | 76,0.8,5,Average 299 | 75,0.84,6,Average 300 | 74,0.75,3,Average 301 | 74,0.72,7,Average 302 | -------------------------------------------------------------------------------- /decisiontree/weightheight/weight_height_dataset.csv: -------------------------------------------------------------------------------- 1 | Height(cm),Weight(kg),Class 2 | 171.40842141405886,69.03793522418515,Normal 3 | 153.9356883748205,47.79750816218935,Underweight 4 | 176.5739605465234,78.87143757172358,Overweight 5 | 170.66309293452898,70.26371396688934,Normal 6 | 164.00991158352414,68.73092188134976,Normal 7 | 173.44241859624879,88.91073884868632,Overweight 8 | 163.74920615999537,50.765359854718305,Underweight 9 | 168.18508809416036,86.63466041920044,Overweight 10 | 176.46837222371582,94.48210900033776,Overweight 11 | 179.19893243003716,89.62551210259333,Overweight 12 | 160.6763726935085,54.375691156050706,Underweight 13 | 167.42811141946825,73.0143671453471,Overweight 14 | 176.29959819572287,89.70929285260712,Overweight 15 | 178.0929214774563,84.0105972470539,Overweight 16 | 179.17950293765037,73.92747510295575,Overweight 17 | 174.7015908552763,65.51753332837743,Normal 18 | 169.70103175214368,67.85839774940663,Normal 19 | 149.8235823314591,47.54574199801973,Underweight 20 | 156.64726061790495,51.33501568836588,Underweight 21 | 163.49009177813255,56.483631585023204,Underweight 22 | 175.92672590462342,90.02912227177642,Overweight 23 | 176.78619940643966,80.25518294981622,Overweight 24 | 174.74124701776603,76.43102519692924,Normal 25 | 155.75788390844104,58.2110953172147,Underweight 26 | 169.81707679208472,63.566726640049815,Normal 27 | 155.91766500624024,57.52736788905408,Underweight 28 | 164.02112789722804,73.49798234498755,Normal 29 | 161.62187889141128,61.06206644411448,Underweight 30 | 171.6508610367146,80.31854012189005,Normal 31 | 164.58206476005057,52.487562701302075,Underweight 32 | 172.34181224674202,78.37365643227365,Overweight 33 | 177.74052724115106,83.35217598359992,Overweight 34 | 176.02951157887244,87.21837051555906,Overweight 35 | 172.47630504897015,72.15234661733227,Normal 36 | 157.5739499235026,54.05477557647265,Underweight 37 | 165.7001368763758,75.4966454580681,Overweight 38 | 159.92421056492907,56.15337775131157,Underweight 39 | 158.3755095370378,58.46730067547198,Underweight 40 | 171.2405715879198,78.05587339394478,Overweight 41 | 178.80352145871564,78.96728306999789,Overweight 42 | 183.4530104304701,91.59540889848857,Overweight 43 | 176.98449931394606,73.46520436877996,Normal 44 | 172.1363972120299,74.38036976900261,Normal 45 | 178.7289549543317,88.05476013622345,Overweight 46 | 152.90595999074503,47.15876631240174,Underweight 47 | 164.18063100657278,76.22278601054832,Normal 48 | 164.41430531040547,67.0210196557641,Normal 49 | 162.39629054580354,58.6704856758808,Underweight 50 | 165.7153396398932,75.05365396874456,Normal 51 | 161.73707851333305,56.62133263077524,Underweight 52 | 162.4783342243884,56.19855741097601,Underweight 53 | 181.25867938737062,84.57916928295525,Overweight 54 | 168.40826349190468,85.27504182513367,Overweight 55 | 162.42543374354153,84.40788409013139,Overweight 56 | 149.28707126718763,52.88996268896349,Underweight 57 | 163.7418877784345,70.11308016321095,Normal 58 | 165.33654012395525,58.20604741195168,Underweight 59 | 168.22146971088148,64.1902401741075,Normal 60 | 170.91665483841646,78.38732340700841,Normal 61 | 163.1381875637279,68.49356681391482,Normal 62 | 183.99713192663788,81.74955428024067,Overweight 63 | 165.37378374913433,67.43270182644882,Normal 64 | 178.9174774717409,78.60497952058643,Overweight 65 | 181.0864988404095,81.01912456863327,Overweight 66 | 179.09816867647893,82.62936509548688,Overweight 67 | 178.81655777647816,74.59926247055799,Normal 68 | 174.90234347916285,80.13741853753817,Overweight 69 | 175.3194460992977,71.8105403566319,Normal 70 | 160.64871894379638,66.32700498026745,Normal 71 | 166.18355931955745,63.980636673706236,Normal 72 | 161.55655300228906,60.59962083454077,Underweight 73 | 167.68680538183213,70.32412408680035,Normal 74 | 166.24561600348142,75.30138703958129,Normal 75 | 167.99419229364617,76.50515360322176,Normal 76 | 160.87906907486632,50.71429490094916,Underweight 77 | 160.40065101688714,56.59619556222087,Underweight 78 | 171.38971981084688,68.77971957677816,Normal 79 | 166.25814786910712,71.75188510258884,Normal 80 | 169.96848569003492,73.90236290544661,Normal 81 | 167.04634566025086,72.40199190141377,Normal 82 | 172.88401677722388,77.3035986047819,Normal 83 | 174.1053837254244,82.96229336096873,Overweight 84 | 161.58688697358733,58.771700262086355,Underweight 85 | 174.7327601064552,62.695648105808345,Normal 86 | 179.74756995910064,68.89605142019884,Normal 87 | 161.5004104393108,62.33863895978806,Underweight 88 | 166.56614458049836,85.57250887255644,Overweight 89 | 153.53851565157154,53.333701700191845,Underweight 90 | 175.67835366117563,71.28954573632464,Normal 91 | 168.4648865959548,69.79340080873259,Normal 92 | 169.93166011006738,72.36111854091838,Normal 93 | 174.08679986430178,82.0233250757889,Overweight 94 | 175.3821698955031,90.03312901616759,Overweight 95 | 167.57751184035004,58.65516975322794,Underweight 96 | 167.93170718953814,76.30925142832601,Normal 97 | 155.28324309371436,60.973368862891796,Underweight 98 | 181.74454726140377,84.01863192504192,Overweight 99 | 154.5648666054243,65.0061305023216,Underweight 100 | 154.0387857658006,54.26362888352178,Underweight 101 | 159.6063870272737,44.449245519610926,Underweight 102 | 161.69991119571355,49.07026886428267,Underweight 103 | 164.53793065739848,52.75970261723595,Underweight 104 | 172.34733824158172,87.54532583981764,Overweight 105 | 164.08472448062165,50.906790746104335,Underweight 106 | 162.299133535788,69.27038479262654,Normal 107 | 166.03280394580827,89.23587444265618,Overweight 108 | 175.88442218064435,67.42049018636372,Normal 109 | 176.8141631470423,68.85005824965872,Normal 110 | 180.5386244684511,84.0702031583978,Overweight 111 | 157.74502078720343,46.9242848091674,Underweight 112 | 168.89097960522628,70.71671972914699,Normal 113 | 154.21804125243304,55.956514480764994,Underweight 114 | 167.10446855782547,63.629718436898564,Normal 115 | 171.81167728121352,82.2186145903011,Overweight 116 | 161.01720662786445,52.08543573171027,Underweight 117 | 167.53295343934533,90.41900545030555,Overweight 118 | 161.21521756716118,57.499162685980735,Underweight 119 | 151.98335797846877,59.47717192367854,Underweight 120 | 171.33392000943758,70.91290092577128,Normal 121 | 157.68010045334614,58.489886926603866,Underweight 122 | 167.18825788440108,72.8132552172219,Normal 123 | 157.3421056968414,60.0236875374849,Underweight 124 | 167.7454004486107,69.29138107363387,Normal 125 | 174.67324671640188,84.70585292341968,Overweight 126 | 159.94610555687603,62.654241725284024,Underweight 127 | 176.001961514746,96.25086403818688,Overweight 128 | 168.35713870094315,58.12581544359125,Underweight 129 | 165.9241124205252,66.63227682241427,Normal 130 | 161.80682965094587,62.9636574363919,Underweight 131 | 173.98034708456058,84.16231779237023,Overweight 132 | 153.86461657254378,58.24355606771714,Underweight 133 | 175.38251161545287,84.69894490155798,Overweight 134 | 179.3220714108819,96.83958410216694,Overweight 135 | 159.48586304133528,66.91255902878228,Normal 136 | 188.47592541859666,84.2193548248987,Overweight 137 | 172.43748965070412,85.87054460857944,Overweight 138 | 160.94944913133097,54.4995072368211,Underweight 139 | 171.45488728452023,73.0542144777422,Normal 140 | 155.1654130851605,60.403649548848946,Underweight 141 | 165.78244707012678,48.324324110844564,Underweight 142 | 158.46514326560495,56.11078569998022,Underweight 143 | 174.60406952405657,92.5926327724097,Overweight 144 | 179.5491292785509,81.26289004363545,Overweight 145 | 156.2018786546893,49.35552652311076,Underweight 146 | 178.53579583991774,83.1486430072168,Overweight 147 | 181.93316061060858,85.66030562293648,Overweight 148 | 166.0077582720072,73.99769918950743,Normal 149 | 158.38339633337836,55.46406471569268,Underweight 150 | 174.59690116346712,86.13027611862047,Overweight 151 | 176.32344043245718,89.02096199174483,Overweight 152 | -------------------------------------------------------------------------------- /gbfs.py: -------------------------------------------------------------------------------- 1 | from state import State 2 | from queue import PriorityQueue 3 | def Greedy(given_state , n): 4 | frontier = PriorityQueue() 5 | explored = [] 6 | counter = 0 7 | root = State(given_state, None, None, 0, 0) 8 | #root.evaluation() 9 | evaluation = root.Misplaced_Tiles(n) #we can use Misplaced_Tiles() instead. 10 | frontier.put((evaluation, counter, root)) #based on greedy evaluation 11 | 12 | while not frontier.empty(): 13 | current_node = frontier.get() 14 | current_node = current_node[2] 15 | explored.append(current_node.state) 16 | 17 | if current_node.test(): 18 | return current_node.solution(), len(explored) 19 | 20 | children = current_node.expand(n) 21 | for child in children: 22 | if child.state not in explored: 23 | counter += 1 24 | evaluation = child.Misplaced_Tiles(n) #we can use Misplaced_Tiles() instead. 25 | frontier.put((evaluation, counter, child)) #based on greedy evaluation 26 | return 27 | -------------------------------------------------------------------------------- /hangman game/manual/__pycache__/hangman_art.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sinifive/Artificial-Intelligence-Lab/c8cdb829b9a9fa9d9d8401e81898aa85cac34290/hangman game/manual/__pycache__/hangman_art.cpython-312.pyc -------------------------------------------------------------------------------- /hangman game/manual/__pycache__/hangman_words.cpython-312.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sinifive/Artificial-Intelligence-Lab/c8cdb829b9a9fa9d9d8401e81898aa85cac34290/hangman game/manual/__pycache__/hangman_words.cpython-312.pyc -------------------------------------------------------------------------------- /hangman game/manual/hangman_art.py: -------------------------------------------------------------------------------- 1 | stages = [''' 2 | +---+ 3 | | | 4 | O | 5 | /|\ | 6 | / \ | 7 | | 8 | ========= 9 | ''', ''' 10 | +---+ 11 | | | 12 | O | 13 | /|\ | 14 | / | 15 | | 16 | ========= 17 | ''', ''' 18 | +---+ 19 | | | 20 | O | 21 | /|\ | 22 | | 23 | | 24 | ========= 25 | ''', ''' 26 | +---+ 27 | | | 28 | O | 29 | /| | 30 | | 31 | | 32 | =========''', ''' 33 | +---+ 34 | | | 35 | O | 36 | | | 37 | | 38 | | 39 | ========= 40 | ''', ''' 41 | +---+ 42 | | | 43 | O | 44 | | 45 | | 46 | | 47 | ========= 48 | ''', ''' 49 | +---+ 50 | | | 51 | | 52 | | 53 | | 54 | | 55 | ========= 56 | '''] 57 | 58 | logo = ''' 59 | _ 60 | | | 61 | | |__ __ _ _ __ __ _ _ __ ___ __ _ _ __ 62 | | '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \ 63 | | | | | (_| | | | | (_| | | | | | | (_| | | | | 64 | |_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_| 65 | __/ | 66 | |___/ ''' 67 | 68 | 69 | -------------------------------------------------------------------------------- /hangman game/manual/hangman_words.py: -------------------------------------------------------------------------------- 1 | word_list = [ 2 | 'abruptly', 3 | 'absurd', 4 | 'abyss', 5 | 'affix', 6 | 'askew', 7 | 'avenue', 8 | 'awkward', 9 | 'axiom', 10 | 'azure', 11 | 'bagpipes', 12 | 'bandwagon', 13 | 'banjo', 14 | 'bayou', 15 | 'beekeeper', 16 | 'blitz', 17 | 'blizzard', 18 | 'boggle', 19 | 'bookworm', 20 | 'boxcar', 21 | 'boxful', 22 | 'buckaroo', 23 | 'buffalo', 24 | 'buffoon', 25 | 'buxom', 26 | 'buzzard', 27 | 'buzzing', 28 | 'buzzwords', 29 | 'caliph', 30 | 'cobweb', 31 | 'cockiness', 32 | 'croquet', 33 | 'crypt', 34 | 'curacao', 35 | 'cycle', 36 | 'daiquiri', 37 | 'dirndl', 38 | 'disavow', 39 | 'dizzying', 40 | 'duplex', 41 | 'dwarves', 42 | 'embezzle', 43 | 'equip', 44 | 'espionage', 45 | 'euouae', 46 | 'exodus', 47 | 'faking', 48 | 'fishhook', 49 | 'fixable', 50 | 'fjord', 51 | 'flapjack', 52 | 'flopping', 53 | 'fluffiness', 54 | 'flyby', 55 | 'foxglove', 56 | 'frazzled', 57 | 'frizzled', 58 | 'fuchsia', 59 | 'funny', 60 | 'gabby', 61 | 'galaxy', 62 | 'galvanize', 63 | 'gazebo', 64 | 'giaour', 65 | 'gizmo', 66 | 'glowworm', 67 | 'glyph', 68 | 'gnarly', 69 | 'gnostic', 70 | 'gossip', 71 | 'grogginess', 72 | 'haiku', 73 | 'haphazard', 74 | 'hyphen', 75 | 'iatrogenic', 76 | 'icebox', 77 | 'injury', 78 | 'ivory', 79 | 'ivy', 80 | 'jackpot', 81 | 'jaundice', 82 | 'jawbreaker', 83 | 'jaywalk', 84 | 'jazziest', 85 | 'jazzy', 86 | 'jelly', 87 | 'jigsaw', 88 | 'jinx', 89 | 'jiujitsu', 90 | 'jockey', 91 | 'jogging', 92 | 'joking', 93 | 'jovial', 94 | 'joyful', 95 | 'juicy', 96 | 'jukebox', 97 | 'jumbo', 98 | 'kayak', 99 | 'kazoo', 100 | 'keyhole', 101 | 'khaki', 102 | 'kilobyte', 103 | 'kiosk', 104 | 'kitsch', 105 | 'kiwifruit', 106 | 'klutz', 107 | 'knapsack', 108 | 'larynx', 109 | 'lengths', 110 | 'lucky', 111 | 'luxury', 112 | 'lymph', 113 | 'marquis', 114 | 'matrix', 115 | 'megahertz', 116 | 'microwave', 117 | 'mnemonic', 118 | 'mystify', 119 | 'naphtha', 120 | 'nightclub', 121 | 'nowadays', 122 | 'numbskull', 123 | 'nymph', 124 | 'onyx', 125 | 'ovary', 126 | 'oxidize', 127 | 'oxygen', 128 | 'pajama', 129 | 'peekaboo', 130 | 'phlegm', 131 | 'pixel', 132 | 'pizazz', 133 | 'pneumonia', 134 | 'polka', 135 | 'pshaw', 136 | 'psyche', 137 | 'puppy', 138 | 'puzzling', 139 | 'quartz', 140 | 'queue', 141 | 'quips', 142 | 'quixotic', 143 | 'quiz', 144 | 'quizzes', 145 | 'quorum', 146 | 'razzmatazz', 147 | 'rhubarb', 148 | 'rhythm', 149 | 'rickshaw', 150 | 'schnapps', 151 | 'scratch', 152 | 'shiv', 153 | 'snazzy', 154 | 'sphinx', 155 | 'spritz', 156 | 'squawk', 157 | 'staff', 158 | 'strength', 159 | 'strengths', 160 | 'stretch', 161 | 'stronghold', 162 | 'stymied', 163 | 'subway', 164 | 'swivel', 165 | 'syndrome', 166 | 'thriftless', 167 | 'thumbscrew', 168 | 'topaz', 169 | 'transcript', 170 | 'transgress', 171 | 'transplant', 172 | 'triphthong', 173 | 'twelfth', 174 | 'twelfths', 175 | 'unknown', 176 | 'unworthy', 177 | 'unzip', 178 | 'uptown', 179 | 'vaporize', 180 | 'vixen', 181 | 'vodka', 182 | 'voodoo', 183 | 'vortex', 184 | 'voyeurism', 185 | 'walkway', 186 | 'waltz', 187 | 'wave', 188 | 'wavy', 189 | 'waxy', 190 | 'wellspring', 191 | 'wheezy', 192 | 'whiskey', 193 | 'whizzing', 194 | 'whomever', 195 | 'wimpy', 196 | 'witchcraft', 197 | 'wizard', 198 | 'woozy', 199 | 'wristwatch', 200 | 'wyvern', 201 | 'xylophone', 202 | 'yachtsman', 203 | 'yippee', 204 | 'yoked', 205 | 'youthful', 206 | 'yummy', 207 | 'zephyr', 208 | 'zigzag', 209 | 'zigzagging', 210 | 'zilch', 211 | 'zipper', 212 | 'zodiac', 213 | 'zombie', 214 | ] 215 | -------------------------------------------------------------------------------- /hangman game/manual/hangmanv5.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | from hangman_words import word_list 4 | from hangman_art import stages,logo 5 | chosen_word=random.choice(word_list) 6 | #lives=6 if len(chosen_word) >= 6 else 6 7 | lives=6 8 | print(f'Pssst, the solution is {chosen_word}.') 9 | print(logo) 10 | print(len(stages)) 11 | display=[] 12 | guessed=[] 13 | for i in range(len(chosen_word)): 14 | 15 | display.append("_") 16 | while "_" in display and lives >0: 17 | guess=input("Guess a letter:").lower() 18 | if len(guess)==1 and guess.isalpha(): 19 | if guess in guessed: 20 | print("letter already present") 21 | continue 22 | guessed.append(guess) 23 | for i in range(len(chosen_word)): 24 | 25 | letter=chosen_word[i] 26 | 27 | if letter==guess: 28 | display[i]=letter 29 | if guess not in chosen_word: 30 | if lives > 0: 31 | lives=lives-1 32 | if lives==0: 33 | print(stages[lives]) 34 | print(f'the solution is {chosen_word}.') 35 | print("You loose") 36 | exit(1) 37 | print(f"{' '.join(display)}") 38 | print(lives) 39 | print(stages[lives]) 40 | else: 41 | print("guess should be a character rather than a word") 42 | 43 | else: 44 | print("You have won") 45 | 46 | -------------------------------------------------------------------------------- /hangman_1.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | from hangman_words import word_list 4 | from hangman_art import stages,logo 5 | chosen_word=random.choice(word_list) 6 | #lives=6 if len(chosen_word) >= 6 else 6 7 | lives=6 8 | 9 | #print(f'pssst, the solution is {chosen_word}.') 10 | print(logo) 11 | print(len(stages)) 12 | display=[] 13 | guessed=[] 14 | for i in range(len(chosen_word)): 15 | display.append("_") 16 | while "_" in display and lives>0: 17 | guess=input("guess a letter:").lower() 18 | if len(guess)==1 and guess.isalpha(): 19 | if guess in guessed: 20 | print("letter already present") 21 | continue 22 | guessed.append(guess) 23 | for i in range(len(chosen_word)): 24 | letter=chosen_word[i] 25 | if letter==guess: 26 | display[i]=letter 27 | if guess not in chosen_word: 28 | if lives>0: 29 | lives=lives-1 30 | if lives==0: 31 | print(stages[lives]) 32 | print(f'the solution is {chosen_word}.') 33 | print("You loose") 34 | exit(1) 35 | print(f"{' '.join(display)}") 36 | print(lives) 37 | print(stages[lives]) 38 | else: 39 | print("guess should be a charater rather than a word") 40 | else: 41 | print("you have won") 42 | -------------------------------------------------------------------------------- /hangmanv5.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | from hangman_words import word_list 4 | from hangman_art import stages,logo 5 | chosen_word=random.choice(word_list) 6 | #lives=6 if len(chosen_word) >= 6 else 6 7 | lives=6 8 | print(f'Pssst, the solution is {chosen_word}.') 9 | print(logo) 10 | print(len(stages)) 11 | display=[] 12 | guessed=[] 13 | for i in range(len(chosen_word)): 14 | 15 | display.append("_") 16 | while "_" in display and lives >0: 17 | guess=input("Guess a letter:").lower() 18 | if len(guess)==1 and guess.isalpha(): 19 | if guess in guessed: 20 | print("letter already present") 21 | continue 22 | guessed.append(guess) 23 | for i in range(len(chosen_word)): 24 | 25 | letter=chosen_word[i] 26 | 27 | if letter==guess: 28 | display[i]=letter 29 | if guess not in chosen_word: 30 | if lives > 0: 31 | lives=lives-1 32 | if lives==0: 33 | print(stages[lives]) 34 | print(f'the solution is {chosen_word}.') 35 | print("You loose") 36 | exit(1) 37 | print(f"{' '.join(display)}") 38 | print(lives) 39 | print(stages[lives]) 40 | else: 41 | print("guess should be a character rather than a word") 42 | 43 | else: 44 | print("You have won") 45 | 46 | -------------------------------------------------------------------------------- /nqueens/queen.py: -------------------------------------------------------------------------------- 1 | class solution: 2 | def __init__(self): 3 | self.MAX = 20 # size of array 4 | self.A = [0]*self.MAX 5 | 6 | def placement(self,i,j): # to check if queen can be placed 7 | for k in range(1,i): 8 | if (self.A[k] == j) or abs(self.A[k] - j) == abs(k - i): 9 | return False 10 | print(self.A) 11 | return True 12 | 13 | def printplacedqueen(self,N): # method for print the placed Queen 14 | print('Arrangment--->') 15 | print() 16 | 17 | for i in range(1,N+1): 18 | for j in range(1,N+1): 19 | if self.A[i] != j: 20 | print('\t_',end =' ') 21 | else: 22 | print('\tQ',end =' ') 23 | print() 24 | print() 25 | 26 | def N_Queens(self,i,j): 27 | for k in range(1,N+1): 28 | if self.placement(i,k): 29 | self.A[i] = k 30 | if i == N: 31 | self.printplacedqueen(N) 32 | else: 33 | self.N_Queens(i+1,N) 34 | 35 | 36 | N = int(input("enter the queens value")) 37 | obj = solution() 38 | obj.N_Queens(1,N) -------------------------------------------------------------------------------- /numpy/.ipynb_checkpoints/numpyml-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "82244738-1a5c-46be-b91d-9bd60056f54e", 6 | "metadata": {}, 7 | "source": [ 8 | "#NumPy\n", 9 | "1. **NumPy is a Python library used for working with arrays**\n", 10 | "\n", 11 | "2. *It also has functions for working in domain of linear algebra and matrices.*\n", 12 | "\n", 13 | "3. NumPy was created in 2005 by **Travis Oliphant.** It is an open source project and you can use it freely.\n", 14 | "\n", 15 | "4. **NumPy stands for Numerical Python.**" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "id": "ddb9fba1-f94a-4ca9-b9f7-674720b2f80c", 21 | "metadata": {}, 22 | "source": [ 23 | "#**Why Use NumPy?**\n", 24 | "-In Python we have ***lists that serve the purpose of arrays, but they are slow to process***.\n", 25 | "\n", 26 | "-NumPy aims to provide an array object that is up to **50x faster than traditional Python lists**.\n", 27 | "\n", 28 | "-***The array object in NumPy is called ndarray***, it provides a lot of supporting functions that make working with ndarray very easy." 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 4, 34 | "id": "fd9c83a9-de72-473b-9c35-03e6377b5349", 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "import numpy as np" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "id": "087da0e5-159e-4a21-9201-163537ef818a", 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "pip install numpy" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 5, 54 | "id": "8aa837fa-0eb1-403b-9d7c-bcbedff6bd8e", 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "'1.26.4'" 61 | ] 62 | }, 63 | "execution_count": 5, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "np.__version__" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "id": "58ee6064-5b13-4494-84ea-70453d9901be", 75 | "metadata": {}, 76 | "source": [ 77 | "**NumPy is used to work with arrays. The array object in NumPy is called ndarray.**\n", 78 | "\n", 79 | "We can ***create a NumPy ndarray object by using the array() function.***" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "id": "97136468-df7a-4834-8fae-f4880945e47d", 85 | "metadata": {}, 86 | "source": [ 87 | "**Dimensions in Arrays**\n", 88 | "\n", 89 | "1.A dimension in arrays is one level of array depth (nested arrays).\n", 90 | "\n", 91 | "2.dimensions refer to the number of axes or directions along which data is organized in an array.\n", 92 | "\n", 93 | "3.**nested array: are arrays that have arrays as their elements.**" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 6, 99 | "id": "adc8891c-0680-460e-b868-0d1ccac3efb5", 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "array([1, 2, 3])" 106 | ] 107 | }, 108 | "execution_count": 6, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "#1-D Array\n", 115 | "array1=np.array([1,2,3])\n", 116 | "array1" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "id": "2a8bd04d-e539-4c83-9e9c-000bd134b16d", 122 | "metadata": {}, 123 | "source": [ 124 | "This creates the array we can see on the right here:\r\n", 125 | "\r\n", 126 | "![](http://jalammar.github.io/images/numpy/create-numpy-array-1.png)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 7, 132 | "id": "c7c8983c-acd7-4ea1-82ef-30e196a937f9", 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "data": { 137 | "text/plain": [ 138 | "numpy.ndarray" 139 | ] 140 | }, 141 | "execution_count": 7, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "type(array1)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "id": "02d58852-02c4-42e2-a468-5aad89cb54f0", 153 | "metadata": {}, 154 | "source": [ 155 | "**To create a `numpy` array with more dimensions, we can pass nested lists, like this:**\r\n", 156 | "\r\n", 157 | "![](http://jalammar.github.io/images/numpy/numpy-array-create-2d.png)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 8, 163 | "id": "0893c232-8efd-4948-958f-7235a1d5a6e3", 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "data": { 168 | "text/plain": [ 169 | "array([[1. , 2. , 3.3],\n", 170 | " [4. , 5. , 6.5]])" 171 | ] 172 | }, 173 | "execution_count": 8, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "#2-D Array\n", 180 | "'''An array that has 1-D arrays as its elements is called a 2-D array.'''\n", 181 | "array2=np.array([[1,2,3.3]\n", 182 | " ,[4,5,6.5]])\n", 183 | "array2" 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "id": "c58e5a28-9a91-4a09-a698-f8e0c0583720", 189 | "metadata": {}, 190 | "source": [ 191 | "To create a `numpy` array with more dimensions, we can pass nested lists, like this:g)\r\n", 192 | "\r\n", 193 | "![](http://jalammar.github.io/images/numpy/numpy-3d-array.png)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 9, 199 | "id": "f29b1da9-0e8e-42c0-8123-f2094db86ad4", 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [ 203 | "arrex=np.array([\n", 204 | " [\n", 205 | " [1,2],\n", 206 | " [3,4]\n", 207 | " ],\n", 208 | " [\n", 209 | " [5,6],\n", 210 | " [7,8]\n", 211 | " ]\n", 212 | "])" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 10, 218 | "id": "d4ff3618-8f2f-46cc-965b-aed04a1b958d", 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "data": { 223 | "text/plain": [ 224 | "array([[[1, 2],\n", 225 | " [3, 4]],\n", 226 | "\n", 227 | " [[5, 6],\n", 228 | " [7, 8]]])" 229 | ] 230 | }, 231 | "execution_count": 10, 232 | "metadata": {}, 233 | "output_type": "execute_result" 234 | } 235 | ], 236 | "source": [ 237 | "arrex" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 11, 243 | "id": "6a43add2-a5a8-43ea-908b-3fff1e01ceb8", 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "data": { 248 | "text/plain": [ 249 | "array([[[ 1, 2, 3],\n", 250 | " [ 4, 5, 6],\n", 251 | " [ 7, 8, 9]],\n", 252 | "\n", 253 | " [[10, 11, 12],\n", 254 | " [13, 14, 15],\n", 255 | " [16, 17, 18]]])" 256 | ] 257 | }, 258 | "execution_count": 11, 259 | "metadata": {}, 260 | "output_type": "execute_result" 261 | } 262 | ], 263 | "source": [ 264 | "#3-D Array has depth,row andd column\n", 265 | "'''An array that has 2-D arrays (matrices) as its elements is called 3-D array.'''\n", 266 | "array3=np.array([[[1,2,3],\n", 267 | " [4,5,6],\n", 268 | " [7,8,9]],\n", 269 | " [[10,11,12],\n", 270 | " [13,14,15],\n", 271 | " [16,17,18]]])\n", 272 | "array3" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "id": "c9506605-d0f7-499a-9329-ddda66e53622", 278 | "metadata": {}, 279 | "source": [ 280 | "**To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray:**" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 14, 286 | "id": "0bc26184-1de1-4849-8896-338a73b7757f", 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "data": { 291 | "text/plain": [ 292 | "array([12, 13, 14])" 293 | ] 294 | }, 295 | "execution_count": 14, 296 | "metadata": {}, 297 | "output_type": "execute_result" 298 | } 299 | ], 300 | "source": [ 301 | "#passing list of elements as tuple\n", 302 | "array4=np.array((12,13,14))\n", 303 | "array4" 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "id": "e6fe7a38-c16b-42f2-b594-a52e3d96a7d5", 309 | "metadata": {}, 310 | "source": [ 311 | "NumPy Arrays provides the **ndim** attribute that **returns an integer that tells us how many dimensions the array have**." 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 15, 317 | "id": "df9b28e0-adf7-4ed0-84ae-5e17cb0e93f6", 318 | "metadata": {}, 319 | "outputs": [ 320 | { 321 | "name": "stdout", 322 | "output_type": "stream", 323 | "text": [ 324 | "1\n" 325 | ] 326 | } 327 | ], 328 | "source": [ 329 | "print(array1.ndim)" 330 | ] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": 16, 335 | "id": "fcbafc4d-33da-4f99-b866-e3222892d725", 336 | "metadata": {}, 337 | "outputs": [ 338 | { 339 | "name": "stdout", 340 | "output_type": "stream", 341 | "text": [ 342 | "2\n" 343 | ] 344 | } 345 | ], 346 | "source": [ 347 | "print(array2.ndim)" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 17, 353 | "id": "80cd7acf-b29f-4823-97f8-b51b340e2d0c", 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "name": "stdout", 358 | "output_type": "stream", 359 | "text": [ 360 | "3\n" 361 | ] 362 | } 363 | ], 364 | "source": [ 365 | "print(array3.ndim)" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 18, 371 | "id": "08b61559-f225-4ae3-b9b8-a4f1a6604d5d", 372 | "metadata": {}, 373 | "outputs": [ 374 | { 375 | "name": "stdout", 376 | "output_type": "stream", 377 | "text": [ 378 | "1\n" 379 | ] 380 | } 381 | ], 382 | "source": [ 383 | "print(array4.ndim)" 384 | ] 385 | }, 386 | { 387 | "cell_type": "markdown", 388 | "id": "0671134c-6c24-4e9d-bbe4-5f056b722bbc", 389 | "metadata": {}, 390 | "source": [ 391 | "An array can have any number of dimensions.\n", 392 | "\n", 393 | "When the array is created, you can **define the number of dimensions** by using the **ndmin argument**." 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 19, 399 | "id": "3a4c2b20-b3e8-4f48-bb7d-e4b042f1c513", 400 | "metadata": {}, 401 | "outputs": [ 402 | { 403 | "data": { 404 | "text/plain": [ 405 | "array([[[[1, 2, 3, 5]]]])" 406 | ] 407 | }, 408 | "execution_count": 19, 409 | "metadata": {}, 410 | "output_type": "execute_result" 411 | } 412 | ], 413 | "source": [ 414 | "array5=np.array([1,2,3,5],ndmin=4)\n", 415 | "array5" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 13, 421 | "id": "74f19377-d962-4dc0-9160-4825918194e9", 422 | "metadata": {}, 423 | "outputs": [ 424 | { 425 | "name": "stdout", 426 | "output_type": "stream", 427 | "text": [ 428 | "4\n" 429 | ] 430 | } 431 | ], 432 | "source": [ 433 | "print(array5.ndim)" 434 | ] 435 | }, 436 | { 437 | "cell_type": "markdown", 438 | "id": "6530e45b-2679-4997-8c98-cb43044b67cf", 439 | "metadata": {}, 440 | "source": [ 441 | "**Access Array Elements**\n", 442 | "1.Array indexing is the same as accessing an array element.\n", 443 | "\n", 444 | "2.You can access an array element by referring to its **index number.**\n", 445 | "\n", 446 | "3. ***The indexes in NumPy arrays start with 0***, meaning that the first element has index 0, and the second has index 1 etc." 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": 14, 452 | "id": "2dca1b16-a35d-400e-9eab-30e7ffefddb0", 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "data": { 457 | "text/plain": [ 458 | "array([[1. , 2. , 3.3],\n", 459 | " [4. , 5. , 6.5]])" 460 | ] 461 | }, 462 | "execution_count": 14, 463 | "metadata": {}, 464 | "output_type": "execute_result" 465 | } 466 | ], 467 | "source": [ 468 | "array2" 469 | ] 470 | }, 471 | { 472 | "cell_type": "code", 473 | "execution_count": 15, 474 | "id": "c0c3cc0d-56be-4233-a413-5197b59d9e28", 475 | "metadata": {}, 476 | "outputs": [ 477 | { 478 | "data": { 479 | "text/plain": [ 480 | "2.0" 481 | ] 482 | }, 483 | "execution_count": 15, 484 | "metadata": {}, 485 | "output_type": "execute_result" 486 | } 487 | ], 488 | "source": [ 489 | "array2[0][1]" 490 | ] 491 | }, 492 | { 493 | "cell_type": "markdown", 494 | "id": "e128b11a-a546-4670-8ac5-e6cf309f935a", 495 | "metadata": {}, 496 | "source": [ 497 | "**Shape of an Array**\n", 498 | "The shape of an array is the **number of elements in each dimension**." 499 | ] 500 | }, 501 | { 502 | "cell_type": "code", 503 | "execution_count": 23, 504 | "id": "161786e3-8797-4171-9dde-8352345531d0", 505 | "metadata": {}, 506 | "outputs": [ 507 | { 508 | "data": { 509 | "text/plain": [ 510 | "(2, 3)" 511 | ] 512 | }, 513 | "execution_count": 23, 514 | "metadata": {}, 515 | "output_type": "execute_result" 516 | } 517 | ], 518 | "source": [ 519 | "array2.shape" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 20, 525 | "id": "77a0e433-228c-47d2-a631-f12a46ee1946", 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "name": "stdout", 530 | "output_type": "stream", 531 | "text": [ 532 | "1.0\n", 533 | "2.0\n", 534 | "3.3\n", 535 | "4.0\n", 536 | "5.0\n", 537 | "6.5\n" 538 | ] 539 | } 540 | ], 541 | "source": [ 542 | "for x in range(0, 2):\n", 543 | " for y in range(0, 3):\n", 544 | " print(array2[x][y])" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": 31, 550 | "id": "d332ade1-1500-4c3e-af2a-d8f15a460b35", 551 | "metadata": {}, 552 | "outputs": [ 553 | { 554 | "data": { 555 | "text/plain": [ 556 | "array([1, 2, 3, 4, 5])" 557 | ] 558 | }, 559 | "execution_count": 31, 560 | "metadata": {}, 561 | "output_type": "execute_result" 562 | } 563 | ], 564 | "source": [ 565 | "array1" 566 | ] 567 | }, 568 | { 569 | "cell_type": "code", 570 | "execution_count": 28, 571 | "id": "332637d2-fd2f-418d-bc91-10ec2d3aa265", 572 | "metadata": {}, 573 | "outputs": [ 574 | { 575 | "data": { 576 | "text/plain": [ 577 | "(5,)" 578 | ] 579 | }, 580 | "execution_count": 28, 581 | "metadata": {}, 582 | "output_type": "execute_result" 583 | } 584 | ], 585 | "source": [ 586 | "array1.shape" 587 | ] 588 | }, 589 | { 590 | "cell_type": "code", 591 | "execution_count": 32, 592 | "id": "9a568aa1-9fb0-495c-acd0-123397021a54", 593 | "metadata": {}, 594 | "outputs": [ 595 | { 596 | "data": { 597 | "text/plain": [ 598 | "array([[1. , 2. , 3.3],\n", 599 | " [4. , 5. , 6.5]])" 600 | ] 601 | }, 602 | "execution_count": 32, 603 | "metadata": {}, 604 | "output_type": "execute_result" 605 | } 606 | ], 607 | "source": [ 608 | "array2" 609 | ] 610 | }, 611 | { 612 | "cell_type": "code", 613 | "execution_count": 29, 614 | "id": "ad3069bd-79f3-4ee1-886b-6613bb8184b3", 615 | "metadata": {}, 616 | "outputs": [ 617 | { 618 | "data": { 619 | "text/plain": [ 620 | "(2, 3)" 621 | ] 622 | }, 623 | "execution_count": 29, 624 | "metadata": {}, 625 | "output_type": "execute_result" 626 | } 627 | ], 628 | "source": [ 629 | "#2 partitions with 3 elements in each\n", 630 | "#2 rows and 3 columns\n", 631 | "array2.shape" 632 | ] 633 | }, 634 | { 635 | "cell_type": "code", 636 | "execution_count": 33, 637 | "id": "fde7cb2e-e805-47b5-8906-778613dcab07", 638 | "metadata": {}, 639 | "outputs": [ 640 | { 641 | "data": { 642 | "text/plain": [ 643 | "array([[[ 1, 2, 3],\n", 644 | " [ 4, 5, 6],\n", 645 | " [ 7, 8, 9]],\n", 646 | "\n", 647 | " [[10, 11, 12],\n", 648 | " [13, 14, 15],\n", 649 | " [16, 17, 18]]])" 650 | ] 651 | }, 652 | "execution_count": 33, 653 | "metadata": {}, 654 | "output_type": "execute_result" 655 | } 656 | ], 657 | "source": [ 658 | "array3" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 30, 664 | "id": "65742cff-939e-4389-a51f-0c2361e79bdc", 665 | "metadata": {}, 666 | "outputs": [ 667 | { 668 | "data": { 669 | "text/plain": [ 670 | "(2, 3, 3)" 671 | ] 672 | }, 673 | "execution_count": 30, 674 | "metadata": {}, 675 | "output_type": "execute_result" 676 | } 677 | ], 678 | "source": [ 679 | "#2 partitions with 3 rows and 3 columns in each\n", 680 | "\n", 681 | "array3.shape" 682 | ] 683 | }, 684 | { 685 | "cell_type": "markdown", 686 | "id": "177d6306-3c5b-4b5e-8869-6419c6d311c4", 687 | "metadata": {}, 688 | "source": [ 689 | "The NumPy array object has a property called **dtype that returns the data type of the array:**" 690 | ] 691 | }, 692 | { 693 | "cell_type": "code", 694 | "execution_count": 34, 695 | "id": "8d834d28-ee6d-4759-93e9-a4c2caa575ec", 696 | "metadata": {}, 697 | "outputs": [ 698 | { 699 | "data": { 700 | "text/plain": [ 701 | "dtype('int32')" 702 | ] 703 | }, 704 | "execution_count": 34, 705 | "metadata": {}, 706 | "output_type": "execute_result" 707 | } 708 | ], 709 | "source": [ 710 | "array1.dtype" 711 | ] 712 | }, 713 | { 714 | "cell_type": "code", 715 | "execution_count": 35, 716 | "id": "c8d5d800-a0c0-443c-b869-6fe5e3fc5e74", 717 | "metadata": {}, 718 | "outputs": [ 719 | { 720 | "data": { 721 | "text/plain": [ 722 | "dtype('float64')" 723 | ] 724 | }, 725 | "execution_count": 35, 726 | "metadata": {}, 727 | "output_type": "execute_result" 728 | } 729 | ], 730 | "source": [ 731 | "array2.dtype" 732 | ] 733 | }, 734 | { 735 | "cell_type": "code", 736 | "execution_count": 36, 737 | "id": "154a9b00-b857-4d0c-92e5-fe605869ef08", 738 | "metadata": {}, 739 | "outputs": [ 740 | { 741 | "data": { 742 | "text/plain": [ 743 | "dtype('int32')" 744 | ] 745 | }, 746 | "execution_count": 36, 747 | "metadata": {}, 748 | "output_type": "execute_result" 749 | } 750 | ], 751 | "source": [ 752 | "array3.dtype" 753 | ] 754 | }, 755 | { 756 | "cell_type": "code", 757 | "execution_count": 37, 758 | "id": "256aef52-f2fa-4818-8e98-ba875aa483f0", 759 | "metadata": {}, 760 | "outputs": [ 761 | { 762 | "data": { 763 | "text/plain": [ 764 | "dtype('int32')" 765 | ] 766 | }, 767 | "execution_count": 37, 768 | "metadata": {}, 769 | "output_type": "execute_result" 770 | } 771 | ], 772 | "source": [ 773 | "array4.dtype" 774 | ] 775 | }, 776 | { 777 | "cell_type": "code", 778 | "execution_count": 38, 779 | "id": "0681f72d-bc88-44c8-a096-82e443ca1151", 780 | "metadata": {}, 781 | "outputs": [ 782 | { 783 | "data": { 784 | "text/plain": [ 785 | "(dtype('int32'), dtype('int32'))" 786 | ] 787 | }, 788 | "execution_count": 38, 789 | "metadata": {}, 790 | "output_type": "execute_result" 791 | } 792 | ], 793 | "source": [ 794 | "array1.dtype,array4.dtype" 795 | ] 796 | }, 797 | { 798 | "cell_type": "code", 799 | "execution_count": 39, 800 | "id": "2a7f2bb3-57d8-4d85-9db9-4faaa386a65d", 801 | "metadata": {}, 802 | "outputs": [], 803 | "source": [ 804 | "array5=np.array([1,2,3,4,5],dtype='S')" 805 | ] 806 | }, 807 | { 808 | "cell_type": "code", 809 | "execution_count": 40, 810 | "id": "d7d0f917-4c58-4567-8a61-2a8a289fb06f", 811 | "metadata": {}, 812 | "outputs": [ 813 | { 814 | "data": { 815 | "text/plain": [ 816 | "array([b'1', b'2', b'3', b'4', b'5'], dtype='|S1')" 817 | ] 818 | }, 819 | "execution_count": 40, 820 | "metadata": {}, 821 | "output_type": "execute_result" 822 | } 823 | ], 824 | "source": [ 825 | "array5" 826 | ] 827 | }, 828 | { 829 | "cell_type": "raw", 830 | "id": "16c19f9a-9a8f-4c65-9d88-b81318828b4f", 831 | "metadata": {}, 832 | "source": [ 833 | "#b'1' These are byte strings representing individual elements of the array\n", 834 | "#|S1 string of length 1" 835 | ] 836 | }, 837 | { 838 | "cell_type": "code", 839 | "execution_count": 42, 840 | "id": "b249d234-397b-4d44-8a09-3bedbaa0f9d9", 841 | "metadata": {}, 842 | "outputs": [ 843 | { 844 | "data": { 845 | "text/plain": [ 846 | "array([[1, 2, 3],\n", 847 | " [4, 5, 6]], dtype=int32)" 848 | ] 849 | }, 850 | "execution_count": 42, 851 | "metadata": {}, 852 | "output_type": "execute_result" 853 | } 854 | ], 855 | "source": [ 856 | "#Change data type from float to integer by using 'i' as parameter value:\n", 857 | "array2conv=array2.astype('i')\n", 858 | "array2conv" 859 | ] 860 | }, 861 | { 862 | "cell_type": "code", 863 | "execution_count": 43, 864 | "id": "450bd114-e2c7-45a0-9741-224f15711a16", 865 | "metadata": {}, 866 | "outputs": [ 867 | { 868 | "data": { 869 | "text/plain": [ 870 | "array([[1., 2., 3.],\n", 871 | " [4., 5., 6.]], dtype=float32)" 872 | ] 873 | }, 874 | "execution_count": 43, 875 | "metadata": {}, 876 | "output_type": "execute_result" 877 | } 878 | ], 879 | "source": [ 880 | "array2float=array2conv.astype('f')\n", 881 | "array2float" 882 | ] 883 | }, 884 | { 885 | "cell_type": "markdown", 886 | "id": "59f6df28-02ee-4e22-955f-3a0be2054cc5", 887 | "metadata": {}, 888 | "source": [ 889 | "**Python numpy.ones() function returns a new array of given shape and data type, where the element’s value is set to 1.**" 890 | ] 891 | }, 892 | { 893 | "cell_type": "markdown", 894 | "id": "a2c5ffee-0847-4064-bdf5-0a67a716b4f1", 895 | "metadata": {}, 896 | "source": [ 897 | "There are often cases when we want numpy to initialize the values of the array for us. numpy provides methods like `ones()`, `zeros()`, and `random.random()` for these cases. We just pass them the number of elements we want it to generate:\r\n", 898 | "\r\n", 899 | "![](http://jalammar.github.io/images/numpy/create-numpy-array-ones-zeros-random.png)" 900 | ] 901 | }, 902 | { 903 | "cell_type": "code", 904 | "execution_count": 44, 905 | "id": "bcf7adf4-6164-4a79-be65-5e758b757dd2", 906 | "metadata": {}, 907 | "outputs": [ 908 | { 909 | "data": { 910 | "text/plain": [ 911 | "array([[1., 1.],\n", 912 | " [1., 1.]])" 913 | ] 914 | }, 915 | "execution_count": 44, 916 | "metadata": {}, 917 | "output_type": "execute_result" 918 | } 919 | ], 920 | "source": [ 921 | "numone=np.ones((2,2))\n", 922 | "numone" 923 | ] 924 | }, 925 | { 926 | "cell_type": "code", 927 | "execution_count": 22, 928 | "id": "5b612d07-7c47-445d-956b-f5297d3af3d1", 929 | "metadata": {}, 930 | "outputs": [ 931 | { 932 | "data": { 933 | "text/plain": [ 934 | "array([[1., 1.],\n", 935 | " [1., 1.],\n", 936 | " [1., 1.]])" 937 | ] 938 | }, 939 | "execution_count": 22, 940 | "metadata": {}, 941 | "output_type": "execute_result" 942 | } 943 | ], 944 | "source": [ 945 | "numsone=np.ones((3,2))\n", 946 | "numsone" 947 | ] 948 | }, 949 | { 950 | "cell_type": "markdown", 951 | "id": "c14686f6-89ed-43f5-8698-02a38c62f039", 952 | "metadata": {}, 953 | "source": [ 954 | "We can also use these methods to produce multi-dimensional arrays, as long as we pass them a tuple describing the dimensions of the matrix we want to create:\n", 955 | "\n", 956 | "![](http://jalammar.github.io/images/numpy/numpy-matrix-ones-zeros-random.png)\n", 957 | "\n", 958 | "![](http://jalammar.github.io/images/numpy/numpy-3d-array-creation.png)" 959 | ] 960 | }, 961 | { 962 | "cell_type": "raw", 963 | "id": "00c8976a-6a51-46e5-9ba9-f975f02f6992", 964 | "metadata": {}, 965 | "source": [ 966 | "Python numpy.zeros() function returns a new array of given shape and type, where the element’s value as 0." 967 | ] 968 | }, 969 | { 970 | "cell_type": "code", 971 | "execution_count": 24, 972 | "id": "c4909095-f26a-458b-b5ed-6e87684eec96", 973 | "metadata": {}, 974 | "outputs": [ 975 | { 976 | "data": { 977 | "text/plain": [ 978 | "array([[0., 0.],\n", 979 | " [0., 0.],\n", 980 | " [0., 0.]])" 981 | ] 982 | }, 983 | "execution_count": 24, 984 | "metadata": {}, 985 | "output_type": "execute_result" 986 | } 987 | ], 988 | "source": [ 989 | "numszero=np.zeros((3,2))\n", 990 | "numszero" 991 | ] 992 | }, 993 | { 994 | "cell_type": "code", 995 | "execution_count": 23, 996 | "id": "5d38056a-8756-444b-baeb-c503e2377ca2", 997 | "metadata": {}, 998 | "outputs": [ 999 | { 1000 | "data": { 1001 | "text/plain": [ 1002 | "array([[0., 0., 0.],\n", 1003 | " [0., 0., 0.]])" 1004 | ] 1005 | }, 1006 | "execution_count": 23, 1007 | "metadata": {}, 1008 | "output_type": "execute_result" 1009 | } 1010 | ], 1011 | "source": [ 1012 | "numzero=np.zeros((2,3))\n", 1013 | "numzero" 1014 | ] 1015 | }, 1016 | { 1017 | "cell_type": "raw", 1018 | "id": "8c22f0a1-5979-46c9-be84-5f30ca64a67e", 1019 | "metadata": {}, 1020 | "source": [ 1021 | "Syntax\n", 1022 | "import numpy as np\n", 1023 | "np.arange(start, stop, step, dtype=None)\n", 1024 | "Arguments\n", 1025 | "start: The starting value of the sequence (inclusive). If omitted, it defaults to 0.\n", 1026 | "stop: The end value of the sequence (exclusive).\n", 1027 | "step: The spacing between values in the sequence (default is 1). It can be positive or negative.\n", 1028 | "dtype: Optional data type for the elements of the array (default is float)." 1029 | ] 1030 | }, 1031 | { 1032 | "cell_type": "code", 1033 | "execution_count": 46, 1034 | "id": "1e0eccc1-112b-4775-8588-84038f0e9a76", 1035 | "metadata": {}, 1036 | "outputs": [ 1037 | { 1038 | "data": { 1039 | "text/plain": [ 1040 | "array([0, 3, 6, 9])" 1041 | ] 1042 | }, 1043 | "execution_count": 46, 1044 | "metadata": {}, 1045 | "output_type": "execute_result" 1046 | } 1047 | ], 1048 | "source": [ 1049 | "range_array=np.arange(0,10,3)\n", 1050 | "range_array" 1051 | ] 1052 | }, 1053 | { 1054 | "cell_type": "markdown", 1055 | "id": "3e1badba-8c34-4093-bd1d-71934aa999d8", 1056 | "metadata": {}, 1057 | "source": [ 1058 | "np.random.randint(low, high=None, size=None, dtype=int)\n", 1059 | "low: int or array-like of ints: Lowest integers are drawn from the random values.\n", 1060 | "high: int or array-like of ints, optional: Larger or highest integers are drawn from the random values.\n", 1061 | "size: int or tuple of ints, optional: The shape of the array you want to create.\n", 1062 | "dtype: dtype, optional The type of the output you want.\n" 1063 | ] 1064 | }, 1065 | { 1066 | "cell_type": "code", 1067 | "execution_count": 47, 1068 | "id": "165dbf95-ea96-443e-9b80-60f1a22f3449", 1069 | "metadata": {}, 1070 | "outputs": [ 1071 | { 1072 | "data": { 1073 | "text/plain": [ 1074 | "array([0, 0, 0, 0, 0])" 1075 | ] 1076 | }, 1077 | "execution_count": 47, 1078 | "metadata": {}, 1079 | "output_type": "execute_result" 1080 | } 1081 | ], 1082 | "source": [ 1083 | "# create a 1-D array\n", 1084 | "a = np.random.randint(low = 1, size = 5)\n", 1085 | "a" 1086 | ] 1087 | }, 1088 | { 1089 | "cell_type": "code", 1090 | "execution_count": 48, 1091 | "id": "4f422162-4680-401e-bfe1-d25e22462221", 1092 | "metadata": {}, 1093 | "outputs": [ 1094 | { 1095 | "data": { 1096 | "text/plain": [ 1097 | "array([[3, 1, 2],\n", 1098 | " [1, 4, 4]])" 1099 | ] 1100 | }, 1101 | "execution_count": 48, 1102 | "metadata": {}, 1103 | "output_type": "execute_result" 1104 | } 1105 | ], 1106 | "source": [ 1107 | "b=np.random.randint(low=1,high=6,size=(2,3))\n", 1108 | "b" 1109 | ] 1110 | }, 1111 | { 1112 | "cell_type": "code", 1113 | "execution_count": 50, 1114 | "id": "19d8173b-8af4-41fb-9882-6876e530c1bf", 1115 | "metadata": {}, 1116 | "outputs": [ 1117 | { 1118 | "data": { 1119 | "text/plain": [ 1120 | "array([1, 2, 3, 4, 5])" 1121 | ] 1122 | }, 1123 | "execution_count": 50, 1124 | "metadata": {}, 1125 | "output_type": "execute_result" 1126 | } 1127 | ], 1128 | "source": [ 1129 | "array1" 1130 | ] 1131 | }, 1132 | { 1133 | "cell_type": "code", 1134 | "execution_count": 52, 1135 | "id": "47238247-652a-45f0-8878-e451e40a8643", 1136 | "metadata": {}, 1137 | "outputs": [ 1138 | { 1139 | "data": { 1140 | "text/plain": [ 1141 | "array([12, 13, 14])" 1142 | ] 1143 | }, 1144 | "execution_count": 52, 1145 | "metadata": {}, 1146 | "output_type": "execute_result" 1147 | } 1148 | ], 1149 | "source": [ 1150 | "array4" 1151 | ] 1152 | }, 1153 | { 1154 | "cell_type": "code", 1155 | "execution_count": 49, 1156 | "id": "60192f37-cfcd-45cf-89ae-f46e6b0badd6", 1157 | "metadata": {}, 1158 | "outputs": [ 1159 | { 1160 | "data": { 1161 | "text/plain": [ 1162 | "array([ 1, 2, 3, 4, 5, 12, 13, 14])" 1163 | ] 1164 | }, 1165 | "execution_count": 49, 1166 | "metadata": {}, 1167 | "output_type": "execute_result" 1168 | } 1169 | ], 1170 | "source": [ 1171 | "#concatenate two or more arrays at once\n", 1172 | "np.concatenate([array1,array4])" 1173 | ] 1174 | }, 1175 | { 1176 | "cell_type": "code", 1177 | "execution_count": 53, 1178 | "id": "5c47ae1a-9a80-44f9-a32c-363df006d8a0", 1179 | "metadata": {}, 1180 | "outputs": [ 1181 | { 1182 | "ename": "ValueError", 1183 | "evalue": "all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 3 dimension(s)", 1184 | "output_type": "error", 1185 | "traceback": [ 1186 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1187 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 1188 | "Cell \u001b[1;32mIn[53], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m np\u001b[38;5;241m.\u001b[39mconcatenate([array2,array3])\n", 1189 | "\u001b[1;31mValueError\u001b[0m: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 3 dimension(s)" 1190 | ] 1191 | } 1192 | ], 1193 | "source": [ 1194 | "np.concatenate([array2,array3])" 1195 | ] 1196 | }, 1197 | { 1198 | "cell_type": "code", 1199 | "execution_count": 57, 1200 | "id": "03b40f82-0d77-4356-bab7-55e05bf83328", 1201 | "metadata": {}, 1202 | "outputs": [ 1203 | { 1204 | "data": { 1205 | "text/plain": [ 1206 | "array([[[ 1, 2, 3],\n", 1207 | " [ 4, 5, 6],\n", 1208 | " [ 7, 8, 9]],\n", 1209 | "\n", 1210 | " [[10, 11, 12],\n", 1211 | " [13, 14, 15],\n", 1212 | " [16, 17, 18]]])" 1213 | ] 1214 | }, 1215 | "execution_count": 57, 1216 | "metadata": {}, 1217 | "output_type": "execute_result" 1218 | } 1219 | ], 1220 | "source": [ 1221 | "array3" 1222 | ] 1223 | }, 1224 | { 1225 | "cell_type": "code", 1226 | "execution_count": 70, 1227 | "id": "054f39f2-f80a-4652-8866-a2c64f21bc1a", 1228 | "metadata": {}, 1229 | "outputs": [ 1230 | { 1231 | "data": { 1232 | "text/plain": [ 1233 | "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,\n", 1234 | " 18])" 1235 | ] 1236 | }, 1237 | "execution_count": 70, 1238 | "metadata": {}, 1239 | "output_type": "execute_result" 1240 | } 1241 | ], 1242 | "source": [ 1243 | "#function returns a copy of the array collapsed into one dimension.\n", 1244 | "flatarr=array3.flatten()\n", 1245 | "flatarr" 1246 | ] 1247 | }, 1248 | { 1249 | "cell_type": "code", 1250 | "execution_count": 72, 1251 | "id": "39674469-1e39-4598-a50e-1a88f9354326", 1252 | "metadata": {}, 1253 | "outputs": [ 1254 | { 1255 | "data": { 1256 | "text/plain": [ 1257 | "array([[1, 2],\n", 1258 | " [3, 4]])" 1259 | ] 1260 | }, 1261 | "execution_count": 72, 1262 | "metadata": {}, 1263 | "output_type": "execute_result" 1264 | } 1265 | ], 1266 | "source": [ 1267 | "np.reshape(flatarr[:4],(2,2))" 1268 | ] 1269 | }, 1270 | { 1271 | "cell_type": "code", 1272 | "execution_count": 73, 1273 | "id": "eca4dcc4-022c-448d-b99b-244bbd16a182", 1274 | "metadata": {}, 1275 | "outputs": [ 1276 | { 1277 | "data": { 1278 | "text/plain": [ 1279 | "array([[1., 0.],\n", 1280 | " [0., 1.]])" 1281 | ] 1282 | }, 1283 | "execution_count": 73, 1284 | "metadata": {}, 1285 | "output_type": "execute_result" 1286 | } 1287 | ], 1288 | "source": [ 1289 | "#create an identity matrix\n", 1290 | "#used to create a 2D array with ones on the diagonal and zeros elsewhere\n", 1291 | "np.eye(2)" 1292 | ] 1293 | }, 1294 | { 1295 | "cell_type": "code", 1296 | "execution_count": 74, 1297 | "id": "6408812f-a68c-4d45-b66e-ce1a2bd214af", 1298 | "metadata": {}, 1299 | "outputs": [ 1300 | { 1301 | "data": { 1302 | "text/plain": [ 1303 | "array([[1., 0., 0.],\n", 1304 | " [0., 1., 0.],\n", 1305 | " [0., 0., 1.]])" 1306 | ] 1307 | }, 1308 | "execution_count": 74, 1309 | "metadata": {}, 1310 | "output_type": "execute_result" 1311 | } 1312 | ], 1313 | "source": [ 1314 | "np.eye(3)" 1315 | ] 1316 | }, 1317 | { 1318 | "cell_type": "code", 1319 | "execution_count": 75, 1320 | "id": "d7e5131f-862b-457f-9d7f-7fe1f8a9eea1", 1321 | "metadata": {}, 1322 | "outputs": [ 1323 | { 1324 | "data": { 1325 | "text/plain": [ 1326 | "array([[1., 0., 0., 0.],\n", 1327 | " [0., 1., 0., 0.],\n", 1328 | " [0., 0., 1., 0.],\n", 1329 | " [0., 0., 0., 1.]])" 1330 | ] 1331 | }, 1332 | "execution_count": 75, 1333 | "metadata": {}, 1334 | "output_type": "execute_result" 1335 | } 1336 | ], 1337 | "source": [ 1338 | "np.eye(4)" 1339 | ] 1340 | }, 1341 | { 1342 | "cell_type": "code", 1343 | "execution_count": 76, 1344 | "id": "cc261998-a97c-4a65-ba37-77a9d7a7cc8d", 1345 | "metadata": {}, 1346 | "outputs": [ 1347 | { 1348 | "data": { 1349 | "text/html": [ 1350 | "
\n", 1351 | "\n", 1364 | "\n", 1365 | " \n", 1366 | " \n", 1367 | " \n", 1368 | " \n", 1369 | " \n", 1370 | " \n", 1371 | " \n", 1372 | " \n", 1373 | " \n", 1374 | " \n", 1375 | " \n", 1376 | " \n", 1377 | " \n", 1378 | " \n", 1379 | " \n", 1380 | " \n", 1381 | " \n", 1382 | " \n", 1383 | " \n", 1384 | " \n", 1385 | " \n", 1386 | " \n", 1387 | "
012
01.02.03.3
14.05.06.5
\n", 1388 | "
" 1389 | ], 1390 | "text/plain": [ 1391 | " 0 1 2\n", 1392 | "0 1.0 2.0 3.3\n", 1393 | "1 4.0 5.0 6.5" 1394 | ] 1395 | }, 1396 | "execution_count": 76, 1397 | "metadata": {}, 1398 | "output_type": "execute_result" 1399 | } 1400 | ], 1401 | "source": [ 1402 | "#create a dataframe from numpy array\n", 1403 | "import pandas as pd\n", 1404 | "df=pd.DataFrame(array2)\n", 1405 | "df" 1406 | ] 1407 | }, 1408 | { 1409 | "cell_type": "code", 1410 | "execution_count": 79, 1411 | "id": "931653b6-4292-4753-8321-32aefa5ab0b3", 1412 | "metadata": {}, 1413 | "outputs": [ 1414 | { 1415 | "name": "stdout", 1416 | "output_type": "stream", 1417 | "text": [ 1418 | "Number of array dimensions.\n", 1419 | "\n", 1420 | "Examples\n", 1421 | "--------\n", 1422 | ">>> x = np.array([1, 2, 3])\n", 1423 | ">>> x.ndim\n", 1424 | "1\n", 1425 | ">>> y = np.zeros((2, 3, 4))\n", 1426 | ">>> y.ndim\n", 1427 | "3\n" 1428 | ] 1429 | } 1430 | ], 1431 | "source": [ 1432 | "np.info(np.ndarray.ndim)" 1433 | ] 1434 | }, 1435 | { 1436 | "cell_type": "code", 1437 | "execution_count": 80, 1438 | "id": "4e4bc0a3-3dee-4c78-93da-1ccabf0f1a18", 1439 | "metadata": {}, 1440 | "outputs": [ 1441 | { 1442 | "name": "stdout", 1443 | "output_type": "stream", 1444 | "text": [ 1445 | " ndarray()\n", 1446 | "\n", 1447 | "ndarray(shape, dtype=float, buffer=None, offset=0,\n", 1448 | " strides=None, order=None)\n", 1449 | "\n", 1450 | "An array object represents a multidimensional, homogeneous array\n", 1451 | "of fixed-size items. An associated data-type object describes the\n", 1452 | "format of each element in the array (its byte-order, how many bytes it\n", 1453 | "occupies in memory, whether it is an integer, a floating point number,\n", 1454 | "or something else, etc.)\n", 1455 | "\n", 1456 | "Arrays should be constructed using `array`, `zeros` or `empty` (refer\n", 1457 | "to the See Also section below). The parameters given here refer to\n", 1458 | "a low-level method (`ndarray(...)`) for instantiating an array.\n", 1459 | "\n", 1460 | "For more information, refer to the `numpy` module and examine the\n", 1461 | "methods and attributes of an array.\n", 1462 | "\n", 1463 | "Parameters\n", 1464 | "----------\n", 1465 | "(for the __new__ method; see Notes below)\n", 1466 | "\n", 1467 | "shape : tuple of ints\n", 1468 | " Shape of created array.\n", 1469 | "dtype : data-type, optional\n", 1470 | " Any object that can be interpreted as a numpy data type.\n", 1471 | "buffer : object exposing buffer interface, optional\n", 1472 | " Used to fill the array with data.\n", 1473 | "offset : int, optional\n", 1474 | " Offset of array data in buffer.\n", 1475 | "strides : tuple of ints, optional\n", 1476 | " Strides of data in memory.\n", 1477 | "order : {'C', 'F'}, optional\n", 1478 | " Row-major (C-style) or column-major (Fortran-style) order.\n", 1479 | "\n", 1480 | "Attributes\n", 1481 | "----------\n", 1482 | "T : ndarray\n", 1483 | " Transpose of the array.\n", 1484 | "data : buffer\n", 1485 | " The array's elements, in memory.\n", 1486 | "dtype : dtype object\n", 1487 | " Describes the format of the elements in the array.\n", 1488 | "flags : dict\n", 1489 | " Dictionary containing information related to memory use, e.g.,\n", 1490 | " 'C_CONTIGUOUS', 'OWNDATA', 'WRITEABLE', etc.\n", 1491 | "flat : numpy.flatiter object\n", 1492 | " Flattened version of the array as an iterator. The iterator\n", 1493 | " allows assignments, e.g., ``x.flat = 3`` (See `ndarray.flat` for\n", 1494 | " assignment examples; TODO).\n", 1495 | "imag : ndarray\n", 1496 | " Imaginary part of the array.\n", 1497 | "real : ndarray\n", 1498 | " Real part of the array.\n", 1499 | "size : int\n", 1500 | " Number of elements in the array.\n", 1501 | "itemsize : int\n", 1502 | " The memory use of each array element in bytes.\n", 1503 | "nbytes : int\n", 1504 | " The total number of bytes required to store the array data,\n", 1505 | " i.e., ``itemsize * size``.\n", 1506 | "ndim : int\n", 1507 | " The array's number of dimensions.\n", 1508 | "shape : tuple of ints\n", 1509 | " Shape of the array.\n", 1510 | "strides : tuple of ints\n", 1511 | " The step-size required to move from one element to the next in\n", 1512 | " memory. For example, a contiguous ``(3, 4)`` array of type\n", 1513 | " ``int16`` in C-order has strides ``(8, 2)``. This implies that\n", 1514 | " to move from element to element in memory requires jumps of 2 bytes.\n", 1515 | " To move from row-to-row, one needs to jump 8 bytes at a time\n", 1516 | " (``2 * 4``).\n", 1517 | "ctypes : ctypes object\n", 1518 | " Class containing properties of the array needed for interaction\n", 1519 | " with ctypes.\n", 1520 | "base : ndarray\n", 1521 | " If the array is a view into another array, that array is its `base`\n", 1522 | " (unless that array is also a view). The `base` array is where the\n", 1523 | " array data is actually stored.\n", 1524 | "\n", 1525 | "See Also\n", 1526 | "--------\n", 1527 | "array : Construct an array.\n", 1528 | "zeros : Create an array, each element of which is zero.\n", 1529 | "empty : Create an array, but leave its allocated memory unchanged (i.e.,\n", 1530 | " it contains \"garbage\").\n", 1531 | "dtype : Create a data-type.\n", 1532 | "numpy.typing.NDArray : An ndarray alias :term:`generic `\n", 1533 | " w.r.t. its `dtype.type `.\n", 1534 | "\n", 1535 | "Notes\n", 1536 | "-----\n", 1537 | "There are two modes of creating an array using ``__new__``:\n", 1538 | "\n", 1539 | "1. If `buffer` is None, then only `shape`, `dtype`, and `order`\n", 1540 | " are used.\n", 1541 | "2. If `buffer` is an object exposing the buffer interface, then\n", 1542 | " all keywords are interpreted.\n", 1543 | "\n", 1544 | "No ``__init__`` method is needed because the array is fully initialized\n", 1545 | "after the ``__new__`` method.\n", 1546 | "\n", 1547 | "Examples\n", 1548 | "--------\n", 1549 | "These examples illustrate the low-level `ndarray` constructor. Refer\n", 1550 | "to the `See Also` section above for easier ways of constructing an\n", 1551 | "ndarray.\n", 1552 | "\n", 1553 | "First mode, `buffer` is None:\n", 1554 | "\n", 1555 | ">>> np.ndarray(shape=(2,2), dtype=float, order='F')\n", 1556 | "array([[0.0e+000, 0.0e+000], # random\n", 1557 | " [ nan, 2.5e-323]])\n", 1558 | "\n", 1559 | "Second mode:\n", 1560 | "\n", 1561 | ">>> np.ndarray((2,), buffer=np.array([1,2,3]),\n", 1562 | "... offset=np.int_().itemsize,\n", 1563 | "... dtype=int) # offset = 1*itemsize, i.e. skip first element\n", 1564 | "array([2, 3])\n", 1565 | "\n", 1566 | "\n", 1567 | "Methods:\n", 1568 | "\n", 1569 | " all -- a.all(axis=None, out=None, keepdims=False, *, where=True)\n", 1570 | " any -- a.any(axis=None, out=None, keepdims=False, *, where=True)\n", 1571 | " argmax -- a.argmax(axis=None, out=None, *, keepdims=False)\n", 1572 | " argmin -- a.argmin(axis=None, out=None, *, keepdims=False)\n", 1573 | " argpartition -- a.argpartition(kth, axis=-1, kind='introselect', order=None)\n", 1574 | " argsort -- a.argsort(axis=-1, kind=None, order=None)\n", 1575 | " astype -- a.astype(dtype, order='K', casting='unsafe', subok=True, copy=True)\n", 1576 | " byteswap -- a.byteswap(inplace=False)\n", 1577 | " choose -- a.choose(choices, out=None, mode='raise')\n", 1578 | " clip -- a.clip(min=None, max=None, out=None, **kwargs)\n", 1579 | " compress -- a.compress(condition, axis=None, out=None)\n", 1580 | " conj -- a.conj()\n", 1581 | " conjugate -- a.conjugate()\n", 1582 | " copy -- a.copy(order='C')\n", 1583 | " cumprod -- a.cumprod(axis=None, dtype=None, out=None)\n", 1584 | " cumsum -- a.cumsum(axis=None, dtype=None, out=None)\n", 1585 | " diagonal -- a.diagonal(offset=0, axis1=0, axis2=1)\n", 1586 | " dot -- None\n", 1587 | " dump -- a.dump(file)\n", 1588 | " dumps -- a.dumps()\n", 1589 | " fill -- a.fill(value)\n", 1590 | " flatten -- a.flatten(order='C')\n", 1591 | " getfield -- a.getfield(dtype, offset=0)\n", 1592 | " item -- a.item(*args)\n", 1593 | " itemset -- a.itemset(*args)\n", 1594 | " max -- a.max(axis=None, out=None, keepdims=False, initial=, where=True)\n", 1595 | " mean -- a.mean(axis=None, dtype=None, out=None, keepdims=False, *, where=True)\n", 1596 | " min -- a.min(axis=None, out=None, keepdims=False, initial=, where=True)\n", 1597 | " newbyteorder -- arr.newbyteorder(new_order='S', /)\n", 1598 | " nonzero -- a.nonzero()\n", 1599 | " partition -- a.partition(kth, axis=-1, kind='introselect', order=None)\n", 1600 | " prod -- a.prod(axis=None, dtype=None, out=None, keepdims=False, initial=1, where=True)\n", 1601 | " ptp -- a.ptp(axis=None, out=None, keepdims=False)\n", 1602 | " put -- a.put(indices, values, mode='raise')\n", 1603 | " ravel -- a.ravel([order])\n", 1604 | " repeat -- a.repeat(repeats, axis=None)\n", 1605 | " reshape -- a.reshape(shape, order='C')\n", 1606 | " resize -- a.resize(new_shape, refcheck=True)\n", 1607 | " round -- a.round(decimals=0, out=None)\n", 1608 | " searchsorted -- a.searchsorted(v, side='left', sorter=None)\n", 1609 | " setfield -- a.setfield(val, dtype, offset=0)\n", 1610 | " setflags -- a.setflags(write=None, align=None, uic=None)\n", 1611 | " sort -- a.sort(axis=-1, kind=None, order=None)\n", 1612 | " squeeze -- a.squeeze(axis=None)\n", 1613 | " std -- a.std(axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True)\n", 1614 | " sum -- a.sum(axis=None, dtype=None, out=None, keepdims=False, initial=0, where=True)\n", 1615 | " swapaxes -- a.swapaxes(axis1, axis2)\n", 1616 | " take -- a.take(indices, axis=None, out=None, mode='raise')\n", 1617 | " tobytes -- a.tobytes(order='C')\n", 1618 | " tofile -- a.tofile(fid, sep=\"\", format=\"%s\")\n", 1619 | " tolist -- a.tolist()\n", 1620 | " tostring -- a.tostring(order='C')\n", 1621 | " trace -- a.trace(offset=0, axis1=0, axis2=1, dtype=None, out=None)\n", 1622 | " transpose -- a.transpose(*axes)\n", 1623 | " var -- a.var(axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True)\n", 1624 | " view -- a.view([dtype][, type])\n" 1625 | ] 1626 | } 1627 | ], 1628 | "source": [ 1629 | "np.info(np.ndarray)" 1630 | ] 1631 | }, 1632 | { 1633 | "cell_type": "code", 1634 | "execution_count": null, 1635 | "id": "90b2935f-5caa-4742-9a62-126407e07156", 1636 | "metadata": {}, 1637 | "outputs": [], 1638 | "source": [] 1639 | } 1640 | ], 1641 | "metadata": { 1642 | "kernelspec": { 1643 | "display_name": "Python 3 (ipykernel)", 1644 | "language": "python", 1645 | "name": "python3" 1646 | }, 1647 | "language_info": { 1648 | "codemirror_mode": { 1649 | "name": "ipython", 1650 | "version": 3 1651 | }, 1652 | "file_extension": ".py", 1653 | "mimetype": "text/x-python", 1654 | "name": "python", 1655 | "nbconvert_exporter": "python", 1656 | "pygments_lexer": "ipython3", 1657 | "version": "3.11.7" 1658 | } 1659 | }, 1660 | "nbformat": 4, 1661 | "nbformat_minor": 5 1662 | } 1663 | -------------------------------------------------------------------------------- /numpy/numpyml.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "82244738-1a5c-46be-b91d-9bd60056f54e", 6 | "metadata": {}, 7 | "source": [ 8 | "#NumPy\n", 9 | "1. **NumPy is a Python library used for working with arrays**\n", 10 | "\n", 11 | "2. *It also has functions for working in domain of linear algebra and matrices.*\n", 12 | "\n", 13 | "3. NumPy was created in 2005 by **Travis Oliphant.** It is an open source project and you can use it freely.\n", 14 | "\n", 15 | "4. **NumPy stands for Numerical Python.**" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "id": "ddb9fba1-f94a-4ca9-b9f7-674720b2f80c", 21 | "metadata": {}, 22 | "source": [ 23 | "#**Why Use NumPy?**\n", 24 | "\n", 25 | "-In Python we have ***lists that serve the purpose of arrays, but they are slow to process***.\n", 26 | "\n", 27 | "-NumPy aims to provide an array object that is up to **50x faster than traditional Python lists**.\n", 28 | "\n", 29 | "-***The array object in NumPy is called ndarray***, it provides a lot of supporting functions that make working with ndarray very easy." 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 3, 35 | "id": "fd9c83a9-de72-473b-9c35-03e6377b5349", 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "import numpy as np" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 4, 45 | "id": "087da0e5-159e-4a21-9201-163537ef818a", 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "name": "stdout", 50 | "output_type": "stream", 51 | "text": [ 52 | "Requirement already satisfied: numpy in c:\\users\\hp\\anaconda3\\lib\\site-packages (1.26.4)\n", 53 | "Note: you may need to restart the kernel to use updated packages.\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "pip install numpy" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": 5, 64 | "id": "8aa837fa-0eb1-403b-9d7c-bcbedff6bd8e", 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "'1.26.4'" 71 | ] 72 | }, 73 | "execution_count": 5, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "np.__version__" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "id": "58ee6064-5b13-4494-84ea-70453d9901be", 85 | "metadata": {}, 86 | "source": [ 87 | "**NumPy is used to work with arrays. The array object in NumPy is called ndarray.**\n", 88 | "\n", 89 | "We can ***create a NumPy ndarray object by using the array() function.***" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "id": "97136468-df7a-4834-8fae-f4880945e47d", 95 | "metadata": {}, 96 | "source": [ 97 | "**Dimensions in Arrays**\n", 98 | "\n", 99 | "1.A dimension in arrays is one level of array depth (nested arrays).\n", 100 | "\n", 101 | "2.dimensions refer to the **number of axes or directions along which data is organized** in an array.\n", 102 | "\n", 103 | "3.**nested array: are arrays that have arrays as their elements.**" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 6, 109 | "id": "adc8891c-0680-460e-b868-0d1ccac3efb5", 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/plain": [ 115 | "array([1, 2, 3])" 116 | ] 117 | }, 118 | "execution_count": 6, 119 | "metadata": {}, 120 | "output_type": "execute_result" 121 | } 122 | ], 123 | "source": [ 124 | "#1-D Array\n", 125 | "array1=np.array([1,2,3])\n", 126 | "array1" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "id": "2a8bd04d-e539-4c83-9e9c-000bd134b16d", 132 | "metadata": {}, 133 | "source": [ 134 | "This creates the array we can see on the right here:\r\n", 135 | "\r\n", 136 | "![](http://jalammar.github.io/images/numpy/create-numpy-array-1.png)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 7, 142 | "id": "c7c8983c-acd7-4ea1-82ef-30e196a937f9", 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "data": { 147 | "text/plain": [ 148 | "numpy.ndarray" 149 | ] 150 | }, 151 | "execution_count": 7, 152 | "metadata": {}, 153 | "output_type": "execute_result" 154 | } 155 | ], 156 | "source": [ 157 | "type(array1)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "id": "02d58852-02c4-42e2-a468-5aad89cb54f0", 163 | "metadata": {}, 164 | "source": [ 165 | "**To create a `numpy` array with more dimensions, we can pass nested lists, like this:**\r\n", 166 | "\r\n", 167 | "![](http://jalammar.github.io/images/numpy/numpy-array-create-2d.png)" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 8, 173 | "id": "0893c232-8efd-4948-958f-7235a1d5a6e3", 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "array([[1. , 2. , 3.3],\n", 180 | " [4. , 5. , 6.5]])" 181 | ] 182 | }, 183 | "execution_count": 8, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "#2-D Array\n", 190 | "'''An array that has 1-D arrays as its elements is called a 2-D array.'''\n", 191 | "array2=np.array([[1,2,3.3]\n", 192 | " ,[4,5,6.5]])\n", 193 | "array2" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "id": "c58e5a28-9a91-4a09-a698-f8e0c0583720", 199 | "metadata": {}, 200 | "source": [ 201 | "To create a `numpy` array with more dimensions, we can pass nested lists, like this:\r\n", 202 | "\r\n", 203 | "![](http://jalammar.github.io/images/numpy/numpy-3d-array.png)" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 9, 209 | "id": "f29b1da9-0e8e-42c0-8123-f2094db86ad4", 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "arrex=np.array([\n", 214 | " [\n", 215 | " [1,2],\n", 216 | " [3,4]\n", 217 | " ],\n", 218 | " [\n", 219 | " [5,6],\n", 220 | " [7,8]\n", 221 | " ]\n", 222 | "])" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 10, 228 | "id": "d4ff3618-8f2f-46cc-965b-aed04a1b958d", 229 | "metadata": {}, 230 | "outputs": [ 231 | { 232 | "data": { 233 | "text/plain": [ 234 | "array([[[1, 2],\n", 235 | " [3, 4]],\n", 236 | "\n", 237 | " [[5, 6],\n", 238 | " [7, 8]]])" 239 | ] 240 | }, 241 | "execution_count": 10, 242 | "metadata": {}, 243 | "output_type": "execute_result" 244 | } 245 | ], 246 | "source": [ 247 | "arrex" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 11, 253 | "id": "6a43add2-a5a8-43ea-908b-3fff1e01ceb8", 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "data": { 258 | "text/plain": [ 259 | "array([[[ 1, 2, 3],\n", 260 | " [ 4, 5, 6],\n", 261 | " [ 7, 8, 9]],\n", 262 | "\n", 263 | " [[10, 11, 12],\n", 264 | " [13, 14, 15],\n", 265 | " [16, 17, 18]]])" 266 | ] 267 | }, 268 | "execution_count": 11, 269 | "metadata": {}, 270 | "output_type": "execute_result" 271 | } 272 | ], 273 | "source": [ 274 | "#3-D Array has depth,row and column\n", 275 | "'''An array that has 2-D arrays (matrices) as its elements is called 3-D array.'''\n", 276 | "array3=np.array([[[1,2,3],\n", 277 | " [4,5,6],\n", 278 | " [7,8,9]],\n", 279 | " [[10,11,12],\n", 280 | " [13,14,15],\n", 281 | " [16,17,18]]])\n", 282 | "array3" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "id": "c9506605-d0f7-499a-9329-ddda66e53622", 288 | "metadata": {}, 289 | "source": [ 290 | "**To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray:**" 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 12, 296 | "id": "0bc26184-1de1-4849-8896-338a73b7757f", 297 | "metadata": {}, 298 | "outputs": [ 299 | { 300 | "data": { 301 | "text/plain": [ 302 | "array([12, 13, 14])" 303 | ] 304 | }, 305 | "execution_count": 12, 306 | "metadata": {}, 307 | "output_type": "execute_result" 308 | } 309 | ], 310 | "source": [ 311 | "#passing list of elements as tuple\n", 312 | "array4=np.array((12,13,14))\n", 313 | "array4" 314 | ] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "id": "e6fe7a38-c16b-42f2-b594-a52e3d96a7d5", 319 | "metadata": {}, 320 | "source": [ 321 | "NumPy Arrays provides the **ndim** attribute that **returns an integer that tells us how many dimensions the array have**." 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 13, 327 | "id": "df9b28e0-adf7-4ed0-84ae-5e17cb0e93f6", 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "name": "stdout", 332 | "output_type": "stream", 333 | "text": [ 334 | "1\n" 335 | ] 336 | } 337 | ], 338 | "source": [ 339 | "print(array1.ndim)" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 14, 345 | "id": "fcbafc4d-33da-4f99-b866-e3222892d725", 346 | "metadata": {}, 347 | "outputs": [ 348 | { 349 | "name": "stdout", 350 | "output_type": "stream", 351 | "text": [ 352 | "2\n" 353 | ] 354 | } 355 | ], 356 | "source": [ 357 | "print(array2.ndim)" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 15, 363 | "id": "80cd7acf-b29f-4823-97f8-b51b340e2d0c", 364 | "metadata": {}, 365 | "outputs": [ 366 | { 367 | "name": "stdout", 368 | "output_type": "stream", 369 | "text": [ 370 | "3\n" 371 | ] 372 | } 373 | ], 374 | "source": [ 375 | "print(array3.ndim)" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": 16, 381 | "id": "08b61559-f225-4ae3-b9b8-a4f1a6604d5d", 382 | "metadata": {}, 383 | "outputs": [ 384 | { 385 | "name": "stdout", 386 | "output_type": "stream", 387 | "text": [ 388 | "1\n" 389 | ] 390 | } 391 | ], 392 | "source": [ 393 | "print(array4.ndim)" 394 | ] 395 | }, 396 | { 397 | "cell_type": "markdown", 398 | "id": "0671134c-6c24-4e9d-bbe4-5f056b722bbc", 399 | "metadata": {}, 400 | "source": [ 401 | "An array can have any number of dimensions.\n", 402 | "\n", 403 | "When the array is created, you can **define the number of dimensions** by using the **ndmin argument**." 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 17, 409 | "id": "3a4c2b20-b3e8-4f48-bb7d-e4b042f1c513", 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "data": { 414 | "text/plain": [ 415 | "array([[[[1, 2, 3, 5]]]])" 416 | ] 417 | }, 418 | "execution_count": 17, 419 | "metadata": {}, 420 | "output_type": "execute_result" 421 | } 422 | ], 423 | "source": [ 424 | "array5=np.array([1,2,3,5],ndmin=4)\n", 425 | "array5" 426 | ] 427 | }, 428 | { 429 | "cell_type": "markdown", 430 | "id": "4edb74e0-593c-491c-bcad-6e67fe21873b", 431 | "metadata": {}, 432 | "source": [ 433 | "**Since the input list has 4 elements, NumPy adds extra dimensions to meet the minimum requirement, resulting in a shape of (1, 1, 1, 4).**" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": 18, 439 | "id": "74f19377-d962-4dc0-9160-4825918194e9", 440 | "metadata": {}, 441 | "outputs": [ 442 | { 443 | "name": "stdout", 444 | "output_type": "stream", 445 | "text": [ 446 | "4\n" 447 | ] 448 | } 449 | ], 450 | "source": [ 451 | "print(array5.ndim)" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": 19, 457 | "id": "405dd0ad-60f8-402c-8b76-264c68adc92a", 458 | "metadata": {}, 459 | "outputs": [ 460 | { 461 | "data": { 462 | "text/plain": [ 463 | "(1, 1, 1, 4)" 464 | ] 465 | }, 466 | "execution_count": 19, 467 | "metadata": {}, 468 | "output_type": "execute_result" 469 | } 470 | ], 471 | "source": [ 472 | "array5.shape" 473 | ] 474 | }, 475 | { 476 | "cell_type": "markdown", 477 | "id": "6530e45b-2679-4997-8c98-cb43044b67cf", 478 | "metadata": {}, 479 | "source": [ 480 | "**Access Array Elements**\n", 481 | "\n", 482 | "1.Array indexing is the same as accessing an array element.\n", 483 | "\n", 484 | "2.You can access an array element by referring to its **index number.**\n", 485 | "\n", 486 | "3.***The indexes in NumPy arrays start with 0***, meaning that the first element has index 0, and the second has index 1 etc." 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 20, 492 | "id": "2dca1b16-a35d-400e-9eab-30e7ffefddb0", 493 | "metadata": {}, 494 | "outputs": [ 495 | { 496 | "data": { 497 | "text/plain": [ 498 | "array([[1. , 2. , 3.3],\n", 499 | " [4. , 5. , 6.5]])" 500 | ] 501 | }, 502 | "execution_count": 20, 503 | "metadata": {}, 504 | "output_type": "execute_result" 505 | } 506 | ], 507 | "source": [ 508 | "array2" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": 21, 514 | "id": "c0c3cc0d-56be-4233-a413-5197b59d9e28", 515 | "metadata": {}, 516 | "outputs": [ 517 | { 518 | "data": { 519 | "text/plain": [ 520 | "2.0" 521 | ] 522 | }, 523 | "execution_count": 21, 524 | "metadata": {}, 525 | "output_type": "execute_result" 526 | } 527 | ], 528 | "source": [ 529 | "array2[0][1]" 530 | ] 531 | }, 532 | { 533 | "cell_type": "markdown", 534 | "id": "e128b11a-a546-4670-8ac5-e6cf309f935a", 535 | "metadata": {}, 536 | "source": [ 537 | "**Shape of an Array**\n", 538 | "The shape of an array is the **number of elements in each dimension**." 539 | ] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "execution_count": 22, 544 | "id": "161786e3-8797-4171-9dde-8352345531d0", 545 | "metadata": {}, 546 | "outputs": [ 547 | { 548 | "data": { 549 | "text/plain": [ 550 | "(2, 3)" 551 | ] 552 | }, 553 | "execution_count": 22, 554 | "metadata": {}, 555 | "output_type": "execute_result" 556 | } 557 | ], 558 | "source": [ 559 | "array2.shape" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 23, 565 | "id": "77a0e433-228c-47d2-a631-f12a46ee1946", 566 | "metadata": {}, 567 | "outputs": [ 568 | { 569 | "name": "stdout", 570 | "output_type": "stream", 571 | "text": [ 572 | "1.0\n", 573 | "2.0\n", 574 | "3.3\n", 575 | "4.0\n", 576 | "5.0\n", 577 | "6.5\n" 578 | ] 579 | } 580 | ], 581 | "source": [ 582 | "for x in range(0, 2):\n", 583 | " for y in range(0, 3):\n", 584 | " print(array2[x][y])" 585 | ] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": 24, 590 | "id": "d332ade1-1500-4c3e-af2a-d8f15a460b35", 591 | "metadata": {}, 592 | "outputs": [ 593 | { 594 | "data": { 595 | "text/plain": [ 596 | "array([1, 2, 3])" 597 | ] 598 | }, 599 | "execution_count": 24, 600 | "metadata": {}, 601 | "output_type": "execute_result" 602 | } 603 | ], 604 | "source": [ 605 | "array1" 606 | ] 607 | }, 608 | { 609 | "cell_type": "code", 610 | "execution_count": 25, 611 | "id": "332637d2-fd2f-418d-bc91-10ec2d3aa265", 612 | "metadata": {}, 613 | "outputs": [ 614 | { 615 | "data": { 616 | "text/plain": [ 617 | "(3,)" 618 | ] 619 | }, 620 | "execution_count": 25, 621 | "metadata": {}, 622 | "output_type": "execute_result" 623 | } 624 | ], 625 | "source": [ 626 | "array1.shape" 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "execution_count": 26, 632 | "id": "9a568aa1-9fb0-495c-acd0-123397021a54", 633 | "metadata": {}, 634 | "outputs": [ 635 | { 636 | "data": { 637 | "text/plain": [ 638 | "array([[1. , 2. , 3.3],\n", 639 | " [4. , 5. , 6.5]])" 640 | ] 641 | }, 642 | "execution_count": 26, 643 | "metadata": {}, 644 | "output_type": "execute_result" 645 | } 646 | ], 647 | "source": [ 648 | "array2" 649 | ] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "execution_count": 27, 654 | "id": "ad3069bd-79f3-4ee1-886b-6613bb8184b3", 655 | "metadata": {}, 656 | "outputs": [ 657 | { 658 | "data": { 659 | "text/plain": [ 660 | "(2, 3)" 661 | ] 662 | }, 663 | "execution_count": 27, 664 | "metadata": {}, 665 | "output_type": "execute_result" 666 | } 667 | ], 668 | "source": [ 669 | "#2 partitions with 3 elements in each\n", 670 | "#2 rows and 3 columns\n", 671 | "array2.shape" 672 | ] 673 | }, 674 | { 675 | "cell_type": "code", 676 | "execution_count": 28, 677 | "id": "fde7cb2e-e805-47b5-8906-778613dcab07", 678 | "metadata": {}, 679 | "outputs": [ 680 | { 681 | "data": { 682 | "text/plain": [ 683 | "array([[[ 1, 2, 3],\n", 684 | " [ 4, 5, 6],\n", 685 | " [ 7, 8, 9]],\n", 686 | "\n", 687 | " [[10, 11, 12],\n", 688 | " [13, 14, 15],\n", 689 | " [16, 17, 18]]])" 690 | ] 691 | }, 692 | "execution_count": 28, 693 | "metadata": {}, 694 | "output_type": "execute_result" 695 | } 696 | ], 697 | "source": [ 698 | "array3" 699 | ] 700 | }, 701 | { 702 | "cell_type": "code", 703 | "execution_count": 29, 704 | "id": "65742cff-939e-4389-a51f-0c2361e79bdc", 705 | "metadata": {}, 706 | "outputs": [ 707 | { 708 | "data": { 709 | "text/plain": [ 710 | "(2, 3, 3)" 711 | ] 712 | }, 713 | "execution_count": 29, 714 | "metadata": {}, 715 | "output_type": "execute_result" 716 | } 717 | ], 718 | "source": [ 719 | "#2 partitions with 3 rows and 3 columns in each\n", 720 | "\n", 721 | "array3.shape" 722 | ] 723 | }, 724 | { 725 | "cell_type": "markdown", 726 | "id": "177d6306-3c5b-4b5e-8869-6419c6d311c4", 727 | "metadata": {}, 728 | "source": [ 729 | "The NumPy array object has a property called **dtype that returns the data type of the array:**" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": 30, 735 | "id": "8d834d28-ee6d-4759-93e9-a4c2caa575ec", 736 | "metadata": {}, 737 | "outputs": [ 738 | { 739 | "data": { 740 | "text/plain": [ 741 | "dtype('int32')" 742 | ] 743 | }, 744 | "execution_count": 30, 745 | "metadata": {}, 746 | "output_type": "execute_result" 747 | } 748 | ], 749 | "source": [ 750 | "array1.dtype" 751 | ] 752 | }, 753 | { 754 | "cell_type": "code", 755 | "execution_count": 31, 756 | "id": "c8d5d800-a0c0-443c-b869-6fe5e3fc5e74", 757 | "metadata": {}, 758 | "outputs": [ 759 | { 760 | "data": { 761 | "text/plain": [ 762 | "dtype('float64')" 763 | ] 764 | }, 765 | "execution_count": 31, 766 | "metadata": {}, 767 | "output_type": "execute_result" 768 | } 769 | ], 770 | "source": [ 771 | "array2.dtype" 772 | ] 773 | }, 774 | { 775 | "cell_type": "code", 776 | "execution_count": 32, 777 | "id": "154a9b00-b857-4d0c-92e5-fe605869ef08", 778 | "metadata": {}, 779 | "outputs": [ 780 | { 781 | "data": { 782 | "text/plain": [ 783 | "dtype('int32')" 784 | ] 785 | }, 786 | "execution_count": 32, 787 | "metadata": {}, 788 | "output_type": "execute_result" 789 | } 790 | ], 791 | "source": [ 792 | "array3.dtype" 793 | ] 794 | }, 795 | { 796 | "cell_type": "code", 797 | "execution_count": 33, 798 | "id": "256aef52-f2fa-4818-8e98-ba875aa483f0", 799 | "metadata": {}, 800 | "outputs": [ 801 | { 802 | "data": { 803 | "text/plain": [ 804 | "dtype('int32')" 805 | ] 806 | }, 807 | "execution_count": 33, 808 | "metadata": {}, 809 | "output_type": "execute_result" 810 | } 811 | ], 812 | "source": [ 813 | "array4.dtype" 814 | ] 815 | }, 816 | { 817 | "cell_type": "code", 818 | "execution_count": 34, 819 | "id": "0681f72d-bc88-44c8-a096-82e443ca1151", 820 | "metadata": {}, 821 | "outputs": [ 822 | { 823 | "data": { 824 | "text/plain": [ 825 | "(dtype('int32'), dtype('int32'))" 826 | ] 827 | }, 828 | "execution_count": 34, 829 | "metadata": {}, 830 | "output_type": "execute_result" 831 | } 832 | ], 833 | "source": [ 834 | "array1.dtype,array4.dtype" 835 | ] 836 | }, 837 | { 838 | "cell_type": "markdown", 839 | "id": "b995fc23-252a-4cd6-a8f4-406ff3268c79", 840 | "metadata": {}, 841 | "source": [ 842 | "**dtype='S': This specifies that the data type of the array elements is a byte string. The default byte length is 1, meaning each integer is converted into its byte representation.**" 843 | ] 844 | }, 845 | { 846 | "cell_type": "code", 847 | "execution_count": 35, 848 | "id": "2a7f2bb3-57d8-4d85-9db9-4faaa386a65d", 849 | "metadata": {}, 850 | "outputs": [], 851 | "source": [ 852 | "array5=np.array([1,2,3,4,5],dtype='S')" 853 | ] 854 | }, 855 | { 856 | "cell_type": "code", 857 | "execution_count": 36, 858 | "id": "d7d0f917-4c58-4567-8a61-2a8a289fb06f", 859 | "metadata": {}, 860 | "outputs": [ 861 | { 862 | "data": { 863 | "text/plain": [ 864 | "array([b'1', b'2', b'3', b'4', b'5'], dtype='|S1')" 865 | ] 866 | }, 867 | "execution_count": 36, 868 | "metadata": {}, 869 | "output_type": "execute_result" 870 | } 871 | ], 872 | "source": [ 873 | "array5" 874 | ] 875 | }, 876 | { 877 | "cell_type": "markdown", 878 | "id": "e35b9957-73c4-4ecf-abef-35748373c6a8", 879 | "metadata": {}, 880 | "source": [ 881 | "**b'1' These are byte strings representing individual elements of the array**\n", 882 | "\n", 883 | "**|S1 string of length 1**" 884 | ] 885 | }, 886 | { 887 | "cell_type": "code", 888 | "execution_count": 50, 889 | "id": "216d5f0d-c058-432a-a71a-8cecbb4f03c7", 890 | "metadata": {}, 891 | "outputs": [ 892 | { 893 | "data": { 894 | "text/plain": [ 895 | "array([[1. , 2. , 3.3],\n", 896 | " [4. , 5. , 6.5]])" 897 | ] 898 | }, 899 | "execution_count": 50, 900 | "metadata": {}, 901 | "output_type": "execute_result" 902 | } 903 | ], 904 | "source": [ 905 | "array2" 906 | ] 907 | }, 908 | { 909 | "cell_type": "code", 910 | "execution_count": 37, 911 | "id": "b249d234-397b-4d44-8a09-3bedbaa0f9d9", 912 | "metadata": {}, 913 | "outputs": [ 914 | { 915 | "data": { 916 | "text/plain": [ 917 | "array([[1, 2, 3],\n", 918 | " [4, 5, 6]], dtype=int32)" 919 | ] 920 | }, 921 | "execution_count": 37, 922 | "metadata": {}, 923 | "output_type": "execute_result" 924 | } 925 | ], 926 | "source": [ 927 | "#Change data type from float to integer by using 'i' as parameter value:\n", 928 | "array2conv=array2.astype('i')\n", 929 | "array2conv" 930 | ] 931 | }, 932 | { 933 | "cell_type": "code", 934 | "execution_count": 38, 935 | "id": "450bd114-e2c7-45a0-9741-224f15711a16", 936 | "metadata": {}, 937 | "outputs": [ 938 | { 939 | "data": { 940 | "text/plain": [ 941 | "array([[1., 2., 3.],\n", 942 | " [4., 5., 6.]], dtype=float32)" 943 | ] 944 | }, 945 | "execution_count": 38, 946 | "metadata": {}, 947 | "output_type": "execute_result" 948 | } 949 | ], 950 | "source": [ 951 | "array2float=array2conv.astype('f')\n", 952 | "array2float" 953 | ] 954 | }, 955 | { 956 | "cell_type": "markdown", 957 | "id": "59f6df28-02ee-4e22-955f-3a0be2054cc5", 958 | "metadata": {}, 959 | "source": [ 960 | "**Python numpy.ones() function returns a new array of given shape and data type, where the element’s value is set to 1.**" 961 | ] 962 | }, 963 | { 964 | "cell_type": "markdown", 965 | "id": "a2c5ffee-0847-4064-bdf5-0a67a716b4f1", 966 | "metadata": {}, 967 | "source": [ 968 | "There are often cases when we want numpy to initialize the values of the array for us. numpy provides methods like `ones()`, `zeros()`, and `random.random()` for these cases. We just pass them the number of elements we want it to generate:\r\n", 969 | "\r\n", 970 | "![](http://jalammar.github.io/images/numpy/create-numpy-array-ones-zeros-random.png)" 971 | ] 972 | }, 973 | { 974 | "cell_type": "code", 975 | "execution_count": 51, 976 | "id": "bcf7adf4-6164-4a79-be65-5e758b757dd2", 977 | "metadata": {}, 978 | "outputs": [ 979 | { 980 | "data": { 981 | "text/plain": [ 982 | "array([[1, 1],\n", 983 | " [1, 1]])" 984 | ] 985 | }, 986 | "execution_count": 51, 987 | "metadata": {}, 988 | "output_type": "execute_result" 989 | } 990 | ], 991 | "source": [ 992 | "numone=np.ones((2,2),dtype=int)\n", 993 | "numone" 994 | ] 995 | }, 996 | { 997 | "cell_type": "code", 998 | "execution_count": 40, 999 | "id": "5b612d07-7c47-445d-956b-f5297d3af3d1", 1000 | "metadata": {}, 1001 | "outputs": [ 1002 | { 1003 | "data": { 1004 | "text/plain": [ 1005 | "array([[1., 1.],\n", 1006 | " [1., 1.],\n", 1007 | " [1., 1.]])" 1008 | ] 1009 | }, 1010 | "execution_count": 40, 1011 | "metadata": {}, 1012 | "output_type": "execute_result" 1013 | } 1014 | ], 1015 | "source": [ 1016 | "numsone=np.ones((3,2))\n", 1017 | "numsone" 1018 | ] 1019 | }, 1020 | { 1021 | "cell_type": "markdown", 1022 | "id": "c14686f6-89ed-43f5-8698-02a38c62f039", 1023 | "metadata": {}, 1024 | "source": [ 1025 | "We can also use these methods to produce multi-dimensional arrays, as long as we pass them a tuple describing the dimensions of the matrix we want to create:\n", 1026 | "\n", 1027 | "![](http://jalammar.github.io/images/numpy/numpy-matrix-ones-zeros-random.png)\n", 1028 | "\n", 1029 | "![](http://jalammar.github.io/images/numpy/numpy-3d-array-creation.png)" 1030 | ] 1031 | }, 1032 | { 1033 | "cell_type": "markdown", 1034 | "id": "9e512055-bff2-4dcd-b358-3c847e5c11a1", 1035 | "metadata": {}, 1036 | "source": [ 1037 | "**Python numpy.zeros() function returns a new array of given shape and type, where the element’s value as 0.**" 1038 | ] 1039 | }, 1040 | { 1041 | "cell_type": "code", 1042 | "execution_count": 41, 1043 | "id": "c4909095-f26a-458b-b5ed-6e87684eec96", 1044 | "metadata": {}, 1045 | "outputs": [ 1046 | { 1047 | "data": { 1048 | "text/plain": [ 1049 | "array([[0., 0.],\n", 1050 | " [0., 0.],\n", 1051 | " [0., 0.]])" 1052 | ] 1053 | }, 1054 | "execution_count": 41, 1055 | "metadata": {}, 1056 | "output_type": "execute_result" 1057 | } 1058 | ], 1059 | "source": [ 1060 | "numszero=np.zeros((3,2))\n", 1061 | "numszero" 1062 | ] 1063 | }, 1064 | { 1065 | "cell_type": "code", 1066 | "execution_count": 42, 1067 | "id": "5d38056a-8756-444b-baeb-c503e2377ca2", 1068 | "metadata": {}, 1069 | "outputs": [ 1070 | { 1071 | "data": { 1072 | "text/plain": [ 1073 | "array([[0., 0., 0.],\n", 1074 | " [0., 0., 0.]])" 1075 | ] 1076 | }, 1077 | "execution_count": 42, 1078 | "metadata": {}, 1079 | "output_type": "execute_result" 1080 | } 1081 | ], 1082 | "source": [ 1083 | "numzero=np.zeros((2,3))\n", 1084 | "numzero" 1085 | ] 1086 | }, 1087 | { 1088 | "cell_type": "markdown", 1089 | "id": "093c7c73-8331-48b9-9996-f0ffeb9147b4", 1090 | "metadata": {}, 1091 | "source": [ 1092 | "**Syntax**\n", 1093 | "\n", 1094 | "import numpy as np\n", 1095 | "\n", 1096 | "**np.arange(start, stop, step, dtype=None)**\n", 1097 | "\n", 1098 | "**Arguments**\n", 1099 | "\n", 1100 | "1.start: The starting value of the sequence (inclusive). If omitted, it defaults to 0.\n", 1101 | "\n", 1102 | "2.stop: The end value of the sequence (exclusive).\n", 1103 | "\n", 1104 | "3.step: The spacing between values in the sequence (default is 1). It can be positive or negative.\n", 1105 | "\n", 1106 | "4.dtype: Optional data type for the elements of the array (default is float)." 1107 | ] 1108 | }, 1109 | { 1110 | "cell_type": "code", 1111 | "execution_count": 43, 1112 | "id": "1e0eccc1-112b-4775-8588-84038f0e9a76", 1113 | "metadata": {}, 1114 | "outputs": [ 1115 | { 1116 | "data": { 1117 | "text/plain": [ 1118 | "array([0, 3, 6, 9])" 1119 | ] 1120 | }, 1121 | "execution_count": 43, 1122 | "metadata": {}, 1123 | "output_type": "execute_result" 1124 | } 1125 | ], 1126 | "source": [ 1127 | "range_array=np.arange(0,10,3)\n", 1128 | "range_array" 1129 | ] 1130 | }, 1131 | { 1132 | "cell_type": "markdown", 1133 | "id": "3e1badba-8c34-4093-bd1d-71934aa999d8", 1134 | "metadata": {}, 1135 | "source": [ 1136 | "**np.random.randint(low, high=None, size=None, dtype=int)**\n", 1137 | "\n", 1138 | "1.low: int or array-like of ints: Lowest integers are drawn from the random values.\n", 1139 | "\n", 1140 | "2.high: int or array-like of ints, optional: Larger or highest integers are drawn from the random values.\n", 1141 | "\n", 1142 | "3.size: int or tuple of ints, optional: The shape of the array you want to create.\n", 1143 | "\n", 1144 | "4.dtype: dtype, optional The type of the output you want.\n" 1145 | ] 1146 | }, 1147 | { 1148 | "cell_type": "code", 1149 | "execution_count": 56, 1150 | "id": "165dbf95-ea96-443e-9b80-60f1a22f3449", 1151 | "metadata": {}, 1152 | "outputs": [ 1153 | { 1154 | "data": { 1155 | "text/plain": [ 1156 | "array([0, 0, 0, 0, 0])" 1157 | ] 1158 | }, 1159 | "execution_count": 56, 1160 | "metadata": {}, 1161 | "output_type": "execute_result" 1162 | } 1163 | ], 1164 | "source": [ 1165 | "# create a 1-D array\n", 1166 | "a = np.random.randint(low = 1, size = 5)\n", 1167 | "a" 1168 | ] 1169 | }, 1170 | { 1171 | "cell_type": "code", 1172 | "execution_count": 45, 1173 | "id": "4f422162-4680-401e-bfe1-d25e22462221", 1174 | "metadata": {}, 1175 | "outputs": [ 1176 | { 1177 | "data": { 1178 | "text/plain": [ 1179 | "array([[3, 5, 4],\n", 1180 | " [4, 4, 2]])" 1181 | ] 1182 | }, 1183 | "execution_count": 45, 1184 | "metadata": {}, 1185 | "output_type": "execute_result" 1186 | } 1187 | ], 1188 | "source": [ 1189 | "b=np.random.randint(low=1,high=6,size=(2,3))\n", 1190 | "b" 1191 | ] 1192 | }, 1193 | { 1194 | "cell_type": "code", 1195 | "execution_count": 46, 1196 | "id": "19d8173b-8af4-41fb-9882-6876e530c1bf", 1197 | "metadata": {}, 1198 | "outputs": [ 1199 | { 1200 | "data": { 1201 | "text/plain": [ 1202 | "array([1, 2, 3])" 1203 | ] 1204 | }, 1205 | "execution_count": 46, 1206 | "metadata": {}, 1207 | "output_type": "execute_result" 1208 | } 1209 | ], 1210 | "source": [ 1211 | "array1" 1212 | ] 1213 | }, 1214 | { 1215 | "cell_type": "code", 1216 | "execution_count": 47, 1217 | "id": "47238247-652a-45f0-8878-e451e40a8643", 1218 | "metadata": {}, 1219 | "outputs": [ 1220 | { 1221 | "data": { 1222 | "text/plain": [ 1223 | "array([12, 13, 14])" 1224 | ] 1225 | }, 1226 | "execution_count": 47, 1227 | "metadata": {}, 1228 | "output_type": "execute_result" 1229 | } 1230 | ], 1231 | "source": [ 1232 | "array4" 1233 | ] 1234 | }, 1235 | { 1236 | "cell_type": "code", 1237 | "execution_count": 48, 1238 | "id": "60192f37-cfcd-45cf-89ae-f46e6b0badd6", 1239 | "metadata": {}, 1240 | "outputs": [ 1241 | { 1242 | "data": { 1243 | "text/plain": [ 1244 | "array([ 1, 2, 3, 12, 13, 14])" 1245 | ] 1246 | }, 1247 | "execution_count": 48, 1248 | "metadata": {}, 1249 | "output_type": "execute_result" 1250 | } 1251 | ], 1252 | "source": [ 1253 | "#concatenate two or more arrays at once\n", 1254 | "np.concatenate([array1,array4])" 1255 | ] 1256 | }, 1257 | { 1258 | "cell_type": "code", 1259 | "execution_count": 49, 1260 | "id": "5c47ae1a-9a80-44f9-a32c-363df006d8a0", 1261 | "metadata": {}, 1262 | "outputs": [ 1263 | { 1264 | "ename": "ValueError", 1265 | "evalue": "all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 3 dimension(s)", 1266 | "output_type": "error", 1267 | "traceback": [ 1268 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1269 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 1270 | "Cell \u001b[1;32mIn[49], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m np\u001b[38;5;241m.\u001b[39mconcatenate([array2,array3])\n", 1271 | "\u001b[1;31mValueError\u001b[0m: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 3 dimension(s)" 1272 | ] 1273 | } 1274 | ], 1275 | "source": [ 1276 | "np.concatenate([array2,array3])" 1277 | ] 1278 | }, 1279 | { 1280 | "cell_type": "code", 1281 | "execution_count": 57, 1282 | "id": "03b40f82-0d77-4356-bab7-55e05bf83328", 1283 | "metadata": {}, 1284 | "outputs": [ 1285 | { 1286 | "data": { 1287 | "text/plain": [ 1288 | "array([[[ 1, 2, 3],\n", 1289 | " [ 4, 5, 6],\n", 1290 | " [ 7, 8, 9]],\n", 1291 | "\n", 1292 | " [[10, 11, 12],\n", 1293 | " [13, 14, 15],\n", 1294 | " [16, 17, 18]]])" 1295 | ] 1296 | }, 1297 | "execution_count": 57, 1298 | "metadata": {}, 1299 | "output_type": "execute_result" 1300 | } 1301 | ], 1302 | "source": [ 1303 | "array3" 1304 | ] 1305 | }, 1306 | { 1307 | "cell_type": "code", 1308 | "execution_count": 58, 1309 | "id": "054f39f2-f80a-4652-8866-a2c64f21bc1a", 1310 | "metadata": {}, 1311 | "outputs": [ 1312 | { 1313 | "data": { 1314 | "text/plain": [ 1315 | "array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,\n", 1316 | " 18])" 1317 | ] 1318 | }, 1319 | "execution_count": 58, 1320 | "metadata": {}, 1321 | "output_type": "execute_result" 1322 | } 1323 | ], 1324 | "source": [ 1325 | "#function returns a copy of the array collapsed into one dimension.\n", 1326 | "flatarr=array3.flatten()\n", 1327 | "flatarr" 1328 | ] 1329 | }, 1330 | { 1331 | "cell_type": "code", 1332 | "execution_count": 59, 1333 | "id": "39674469-1e39-4598-a50e-1a88f9354326", 1334 | "metadata": {}, 1335 | "outputs": [ 1336 | { 1337 | "data": { 1338 | "text/plain": [ 1339 | "array([[1, 2],\n", 1340 | " [3, 4]])" 1341 | ] 1342 | }, 1343 | "execution_count": 59, 1344 | "metadata": {}, 1345 | "output_type": "execute_result" 1346 | } 1347 | ], 1348 | "source": [ 1349 | "np.reshape(flatarr[:4],(2,2))" 1350 | ] 1351 | }, 1352 | { 1353 | "cell_type": "code", 1354 | "execution_count": 60, 1355 | "id": "eca4dcc4-022c-448d-b99b-244bbd16a182", 1356 | "metadata": {}, 1357 | "outputs": [ 1358 | { 1359 | "data": { 1360 | "text/plain": [ 1361 | "array([[1., 0.],\n", 1362 | " [0., 1.]])" 1363 | ] 1364 | }, 1365 | "execution_count": 60, 1366 | "metadata": {}, 1367 | "output_type": "execute_result" 1368 | } 1369 | ], 1370 | "source": [ 1371 | "#create an identity matrix\n", 1372 | "#used to create a 2D array with ones on the diagonal and zeros elsewhere\n", 1373 | "np.eye(2)" 1374 | ] 1375 | }, 1376 | { 1377 | "cell_type": "code", 1378 | "execution_count": 61, 1379 | "id": "6408812f-a68c-4d45-b66e-ce1a2bd214af", 1380 | "metadata": {}, 1381 | "outputs": [ 1382 | { 1383 | "data": { 1384 | "text/plain": [ 1385 | "array([[1., 0., 0.],\n", 1386 | " [0., 1., 0.],\n", 1387 | " [0., 0., 1.]])" 1388 | ] 1389 | }, 1390 | "execution_count": 61, 1391 | "metadata": {}, 1392 | "output_type": "execute_result" 1393 | } 1394 | ], 1395 | "source": [ 1396 | "np.eye(3)" 1397 | ] 1398 | }, 1399 | { 1400 | "cell_type": "code", 1401 | "execution_count": 62, 1402 | "id": "d7e5131f-862b-457f-9d7f-7fe1f8a9eea1", 1403 | "metadata": {}, 1404 | "outputs": [ 1405 | { 1406 | "data": { 1407 | "text/plain": [ 1408 | "array([[1., 0., 0., 0.],\n", 1409 | " [0., 1., 0., 0.],\n", 1410 | " [0., 0., 1., 0.],\n", 1411 | " [0., 0., 0., 1.]])" 1412 | ] 1413 | }, 1414 | "execution_count": 62, 1415 | "metadata": {}, 1416 | "output_type": "execute_result" 1417 | } 1418 | ], 1419 | "source": [ 1420 | "np.eye(4)" 1421 | ] 1422 | }, 1423 | { 1424 | "cell_type": "code", 1425 | "execution_count": 63, 1426 | "id": "cc261998-a97c-4a65-ba37-77a9d7a7cc8d", 1427 | "metadata": {}, 1428 | "outputs": [ 1429 | { 1430 | "data": { 1431 | "text/html": [ 1432 | "
\n", 1433 | "\n", 1446 | "\n", 1447 | " \n", 1448 | " \n", 1449 | " \n", 1450 | " \n", 1451 | " \n", 1452 | " \n", 1453 | " \n", 1454 | " \n", 1455 | " \n", 1456 | " \n", 1457 | " \n", 1458 | " \n", 1459 | " \n", 1460 | " \n", 1461 | " \n", 1462 | " \n", 1463 | " \n", 1464 | " \n", 1465 | " \n", 1466 | " \n", 1467 | " \n", 1468 | " \n", 1469 | "
012
01.02.03.3
14.05.06.5
\n", 1470 | "
" 1471 | ], 1472 | "text/plain": [ 1473 | " 0 1 2\n", 1474 | "0 1.0 2.0 3.3\n", 1475 | "1 4.0 5.0 6.5" 1476 | ] 1477 | }, 1478 | "execution_count": 63, 1479 | "metadata": {}, 1480 | "output_type": "execute_result" 1481 | } 1482 | ], 1483 | "source": [ 1484 | "#create a dataframe from numpy array\n", 1485 | "import pandas as pd\n", 1486 | "df=pd.DataFrame(array2)\n", 1487 | "df" 1488 | ] 1489 | }, 1490 | { 1491 | "cell_type": "code", 1492 | "execution_count": 64, 1493 | "id": "931653b6-4292-4753-8321-32aefa5ab0b3", 1494 | "metadata": {}, 1495 | "outputs": [ 1496 | { 1497 | "name": "stdout", 1498 | "output_type": "stream", 1499 | "text": [ 1500 | "Number of array dimensions.\n", 1501 | "\n", 1502 | "Examples\n", 1503 | "--------\n", 1504 | ">>> x = np.array([1, 2, 3])\n", 1505 | ">>> x.ndim\n", 1506 | "1\n", 1507 | ">>> y = np.zeros((2, 3, 4))\n", 1508 | ">>> y.ndim\n", 1509 | "3\n" 1510 | ] 1511 | } 1512 | ], 1513 | "source": [ 1514 | "np.info(np.ndarray.ndim)" 1515 | ] 1516 | }, 1517 | { 1518 | "cell_type": "code", 1519 | "execution_count": null, 1520 | "id": "4e4bc0a3-3dee-4c78-93da-1ccabf0f1a18", 1521 | "metadata": {}, 1522 | "outputs": [], 1523 | "source": [ 1524 | "np.info(np.ndarray)" 1525 | ] 1526 | }, 1527 | { 1528 | "cell_type": "code", 1529 | "execution_count": null, 1530 | "id": "90b2935f-5caa-4742-9a62-126407e07156", 1531 | "metadata": {}, 1532 | "outputs": [], 1533 | "source": [] 1534 | } 1535 | ], 1536 | "metadata": { 1537 | "kernelspec": { 1538 | "display_name": "Python 3 (ipykernel)", 1539 | "language": "python", 1540 | "name": "python3" 1541 | }, 1542 | "language_info": { 1543 | "codemirror_mode": { 1544 | "name": "ipython", 1545 | "version": 3 1546 | }, 1547 | "file_extension": ".py", 1548 | "mimetype": "text/x-python", 1549 | "name": "python", 1550 | "nbconvert_exporter": "python", 1551 | "pygments_lexer": "ipython3", 1552 | "version": "3.11.7" 1553 | } 1554 | }, 1555 | "nbformat": 4, 1556 | "nbformat_minor": 5 1557 | } 1558 | -------------------------------------------------------------------------------- /pandas/pandas_d/dataset/car-sales.csv: -------------------------------------------------------------------------------- 1 | Make,Colour,Odometer (KM),Doors,Price 2 | Toyota,White,150043,4,"$4,000.00" 3 | Honda,Red,87899,4,"$5,000.00" 4 | Toyota,Blue,32549,3,"$7,000.00" 5 | BMW,Black,11179,5,"$22,000.00" 6 | Nissan,White,213095,4,"$3,500.00" 7 | Toyota,Green,99213,4,"$4,500.00" 8 | Honda,Blue,45698,4,"$7,500.00" 9 | Honda,Blue,54738,4,"$7,000.00" 10 | Toyota,White,60000,4,"$6,250.00" 11 | Nissan,White,31600,4,"$9,700.00" -------------------------------------------------------------------------------- /pandas/pandas_d/dataset/export_cars.csv: -------------------------------------------------------------------------------- 1 | Make,Colour,Odometer (KM),Doors,Price 2 | Toyota,White,150043,4,"$4,000.00" 3 | Honda,Red,87899,4,"$5,000.00" 4 | Toyota,Blue,32549,3,"$7,000.00" 5 | BMW,Black,11179,5,"$22,000.00" 6 | Nissan,White,213095,4,"$3,500.00" 7 | Toyota,Green,99213,4,"$4,500.00" 8 | Honda,Blue,45698,4,"$7,500.00" 9 | Honda,Blue,54738,4,"$7,000.00" 10 | Toyota,White,60000,4,"$6,250.00" 11 | Nissan,White,31600,4,"$9,700.00" 12 | -------------------------------------------------------------------------------- /prolog/cartypes.pl: -------------------------------------------------------------------------------- 1 | %facts 2 | owns(ashwin,car(bmw)). 3 | owns(hasan,car(toyota_camry)). 4 | owns(varshith,car(honda_civic)). 5 | owns(sainath,car(ford_focus)). 6 | owns(trishal,car(chevrolet_cruze)). 7 | owns(aryan,car(tesla_model_s)). 8 | 9 | 10 | %car types 11 | sedan(car(toyota_camry)). 12 | sedan(car(honda_civic)). 13 | sedan(car(chevrolet_cruze)). 14 | hatchback(car(ford_focus)). 15 | electric(car(tesla_model_s)). 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /prolog/cartypes.pl~: -------------------------------------------------------------------------------- 1 | %facts 2 | owns(ashwin,car(bmw)). 3 | owns(hasan,car(toyota_camry)). 4 | owns(varshith,car(honda_civic)). 5 | owns(sainath,car(ford_focus)). 6 | owns(trishal,car(chevrolet_cruze)). 7 | owns(aryan,car(tesla_model_s)). 8 | 9 | 10 | %car types 11 | sedan(car(toyota_camry)). 12 | sedan(car(honda_civic)). 13 | hatchback(car(ford_focus)). 14 | sedan(car(chevrolet_cruze)). 15 | electric(car(tesla_model_s)). 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /prolog/familytree.pl: -------------------------------------------------------------------------------- 1 | male(harivansh). 2 | male(amitabh). 3 | male(abhishek). 4 | female(teji). 5 | female(jaya). 6 | female(shweta). 7 | female(aishwarya). 8 | female(aradhya). 9 | 10 | parent(harivansh,amitabh). 11 | parent(teji,amitabh). 12 | parent(amitabh,abhishek). 13 | parent(amitabh,shweta). 14 | parent(jaya,abhishek). 15 | parent(jaya,shweta). 16 | parent(abhishek,aradhya). 17 | parent(aishwarya,aradhya). 18 | 19 | mother(M,C):-female(M),parent(M,C). 20 | father(F,C):-male(F),parent(F,C). 21 | son(S,P):-male(S),parent(P,S). 22 | daughter(D,P):-female(D), parent(P,D). 23 | brother(B,S):-male(B),parent(P,B),parent(P,S). 24 | sister(S,B):-female(S), parent(P,S), parent(P,B). 25 | grandfather(G,C):-male(G),parent(G,M),parent(M,C). 26 | grandmother(GM,C):-female(GM),parent(GM,M),parent(M,C). 27 | 28 | 29 | -------------------------------------------------------------------------------- /prolog/familytree.pl~: -------------------------------------------------------------------------------- 1 | male(harivansh). 2 | male(amitabh). 3 | male(abhishek). 4 | female(teji). 5 | female(jaya). 6 | female(shweta). 7 | female(aishwarya). 8 | female(aradhya). 9 | 10 | parent(harivansh,amitabh). 11 | parent(teji,amitabh). 12 | parent(amitabh,abhishek). 13 | parent(amitabh,shweta). 14 | parent(jaya,abhishek). 15 | parent(jaya,shweta). 16 | parent(abhishek,aradhya). 17 | parent(aishwarya,aradhya). 18 | 19 | mother(M,C):-female(M),parent(M,C). 20 | father(F,C):-male(F),parent(F,C). 21 | son(S,P):-male(S),parent(P,S). 22 | daughter(D,P):-female(D), parent(P,D). 23 | brother(B,S):-male(B),parent(P,B),parent(P,S). 24 | sister(S,B):-female(S), parent(P,S), parent(P,B). 25 | 26 | 27 | -------------------------------------------------------------------------------- /prolog/girlstudents.pl: -------------------------------------------------------------------------------- 1 | female(afreen). 2 | female(pranati). 3 | female(srivalli). 4 | 5 | student(afreen). 6 | student(pranati). 7 | student(srivalli). 8 | 9 | 10 | friend(afreen,pranati). 11 | friend(pranati,srivalli). 12 | friend(srivalli,afreen). 13 | friend(srivalli,pranati). 14 | 15 | 16 | friendly(Student):-female(Student),friend(Student,_). 17 | 18 | 19 | social(Student):- 20 | female(Student), 21 | findall(Friend,friend(Student, Friend),Friends), 22 | length(Friends,Count), 23 | Count>1. 24 | -------------------------------------------------------------------------------- /prolog/girlstudents.pl~: -------------------------------------------------------------------------------- 1 | female(afreen). 2 | female(pranati). 3 | female(srivalli). 4 | 5 | student(afreen). 6 | student(pranati). 7 | student(srivalli). 8 | 9 | 10 | friend(afreen,pranati). 11 | friend(pranati,srivalli). 12 | friend(srivalli,afreen). 13 | 14 | 15 | friendly(Student):-female(Student),friend(Student,_). 16 | 17 | 18 | social(Student):- 19 | female(Student), 20 | findall(Friend,friend(Student, Friend),Friends), 21 | length(Friends,Count), 22 | Count>1. 23 | -------------------------------------------------------------------------------- /prolog/outputcartypes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sinifive/Artificial-Intelligence-Lab/c8cdb829b9a9fa9d9d8401e81898aa85cac34290/prolog/outputcartypes.png -------------------------------------------------------------------------------- /prolog/outputgirlstudents.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sinifive/Artificial-Intelligence-Lab/c8cdb829b9a9fa9d9d8401e81898aa85cac34290/prolog/outputgirlstudents.png -------------------------------------------------------------------------------- /prolog/prolog.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sinifive/Artificial-Intelligence-Lab/c8cdb829b9a9fa9d9d8401e81898aa85cac34290/prolog/prolog.pptx -------------------------------------------------------------------------------- /prolog/simplefacts.pl: -------------------------------------------------------------------------------- 1 | owns(hasan,car(bmw)). 2 | owns(sameer,car(chevy)). 3 | owns(mujeeb,car(civic)). 4 | owns(afzal,car(chevy)). 5 | sedan(car(bmw)). 6 | sedan(car(civic)). 7 | truck(car(chevy)). 8 | -------------------------------------------------------------------------------- /prolog/simplefacts.pl~: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sinifive/Artificial-Intelligence-Lab/c8cdb829b9a9fa9d9d8401e81898aa85cac34290/prolog/simplefacts.pl~ -------------------------------------------------------------------------------- /pythonbasics/List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "65bd71fd-ad1f-45cc-8137-4502756a841a", 6 | "metadata": {}, 7 | "source": [ 8 | "**list can store the data of any type. Lists are ordered and mutable(can be changed), which means you can change their contents (add, remove, or modify elements) after they are created.\n", 9 | "Python lists are defined using square brackets [] and can contain items separated by commas.**" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "id": "8dd913bb-4a8b-46a0-848c-2792c7f8f65b", 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [ 19 | "#mutable- can bechanged\n", 20 | "#immutable -- cant be changed" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 1, 26 | "id": "fb9520e1-9c0c-408a-bcc6-c6bd9bf003e6", 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "marks=[23,24,22,21]" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 2, 36 | "id": "bfe5b8dc-87ea-4039-a637-c6313b2b9d92", 37 | "metadata": {}, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "print(type(marks))" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 3, 54 | "id": "944785bd-51ad-48b3-a997-cb224c777c4b", 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "data": { 59 | "text/plain": [ 60 | "24" 61 | ] 62 | }, 63 | "execution_count": 3, 64 | "metadata": {}, 65 | "output_type": "execute_result" 66 | } 67 | ], 68 | "source": [ 69 | "marks[1]" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "id": "8bf4f192-785a-4a3d-85b6-acef1416e4c1", 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "23" 82 | ] 83 | }, 84 | "execution_count": 4, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "marks[0]" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "id": "91922da1-0a6d-4844-a57d-a5ef10cdea17", 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "23\n", 104 | "24\n", 105 | "22\n", 106 | "21\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "for i in range(len(marks)):\n", 112 | " print(marks[i])" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 6, 118 | "id": "bc322851-8eab-49cd-bd73-48a6c7d22473", 119 | "metadata": {}, 120 | "outputs": [ 121 | { 122 | "ename": "IndexError", 123 | "evalue": "list assignment index out of range", 124 | "output_type": "error", 125 | "traceback": [ 126 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 127 | "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", 128 | "Cell \u001b[1;32mIn[6], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m marks[\u001b[38;5;241m5\u001b[39m]\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m24\u001b[39m\n", 129 | "\u001b[1;31mIndexError\u001b[0m: list assignment index out of range" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "marks[5]=24" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 8, 140 | "id": "0b5d2252-3ef6-4721-942c-0d841cdffdb2", 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "marks[3]=20" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 9, 150 | "id": "486885ec-5047-42e5-94dd-56a793a0d47b", 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "[23, 24, 22, 20]" 157 | ] 158 | }, 159 | "execution_count": 9, 160 | "metadata": {}, 161 | "output_type": "execute_result" 162 | } 163 | ], 164 | "source": [ 165 | "marks" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 10, 171 | "id": "b4ac86be-0458-4e93-a4aa-cbda14a6cf0a", 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [ 175 | "marks[2]='AB'" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 11, 181 | "id": "293d1119-3022-4bbf-8b74-c702e1923370", 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "data": { 186 | "text/plain": [ 187 | "[23, 24, 'AB', 20]" 188 | ] 189 | }, 190 | "execution_count": 11, 191 | "metadata": {}, 192 | "output_type": "execute_result" 193 | } 194 | ], 195 | "source": [ 196 | "marks" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "id": "8b81d867-258c-49ee-becf-b8e77493734f", 202 | "metadata": {}, 203 | "source": [ 204 | "**In Python you can use list slicing to extract a section of a list by specifying a range of indices. The basic format, for list slicing is as follows;\n", 205 | "new_list = my_list[start:stop:step]\n", 206 | "start; This indicates the index at which the slicing begins (inclusive). If not provided it will start from the beginning of the list. stop; This indicates the index at which the slicing ends (exclusive). If not provided it will go until the end of the list. step; This specifies how much to increment or step through during slicing. If not provided it defaults to 1.\n", 207 | "This way you can easily extract subsets of data from a list using Pythons list slicing feature.**" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 13, 213 | "id": "bd554c6f-482b-443e-a5ef-f00b2341f645", 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "name": "stdout", 218 | "output_type": "stream", 219 | "text": [ 220 | "[23, 24, 'AB', 20]\n", 221 | "[24, 'AB']\n" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "#Slicing\n", 227 | "print(marks)\n", 228 | "print(marks[1:3])" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 16, 234 | "id": "ce6abcf4-88d9-458b-8daf-1a4535b149f0", 235 | "metadata": {}, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | "[24, 'AB']\n" 242 | ] 243 | } 244 | ], 245 | "source": [ 246 | "print(marks[-3:-1])" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 17, 252 | "id": "18aacd9c-aa73-403d-bf35-0f62aa974705", 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "name": "stdout", 257 | "output_type": "stream", 258 | "text": [ 259 | "\n" 260 | ] 261 | } 262 | ], 263 | "source": [ 264 | "print(type(marks))" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 18, 270 | "id": "83bebe54-6884-4ff1-8271-e280524279ee", 271 | "metadata": {}, 272 | "outputs": [], 273 | "source": [ 274 | "subjects=[\"AI\",\"SE\",\"IRS\",\"DIP\"]" 275 | ] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "id": "8d4ecdd5-ce0f-4df6-ab9c-48f558710407", 280 | "metadata": {}, 281 | "source": [ 282 | "**append() adds the list at the end of first list**" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 19, 288 | "id": "4959c835-5deb-4919-8d8f-dbb75ba80b84", 289 | "metadata": {}, 290 | "outputs": [], 291 | "source": [ 292 | "subjects.append(marks)" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 20, 298 | "id": "5a8303e7-4384-4c52-9880-3f8d0d7d5d34", 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "data": { 303 | "text/plain": [ 304 | "['AI', 'SE', 'IRS', 'DIP', [23, 24, 'AB', 20]]" 305 | ] 306 | }, 307 | "execution_count": 20, 308 | "metadata": {}, 309 | "output_type": "execute_result" 310 | } 311 | ], 312 | "source": [ 313 | "subjects" 314 | ] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "id": "7284937b-8bc5-44ff-b68c-fc188b0f5939", 319 | "metadata": {}, 320 | "source": [ 321 | "**index() displays the index of the given element in the list**" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 21, 327 | "id": "6f0a949b-a278-4868-8b15-28ff71294597", 328 | "metadata": {}, 329 | "outputs": [ 330 | { 331 | "name": "stdout", 332 | "output_type": "stream", 333 | "text": [ 334 | "[23, 24, 'AB', 20]\n" 335 | ] 336 | } 337 | ], 338 | "source": [ 339 | "print(subjects[4])" 340 | ] 341 | }, 342 | { 343 | "cell_type": "markdown", 344 | "id": "8254bc46-58e0-41c9-b91f-6dbce7c239b9", 345 | "metadata": {}, 346 | "source": [ 347 | "**pop() removes the lasst element that has been added to the list**" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 22, 353 | "id": "ad8685be-bfad-4431-a884-7a0ad5e54472", 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "name": "stdout", 358 | "output_type": "stream", 359 | "text": [ 360 | "[23, 24, 'AB', 20]\n" 361 | ] 362 | } 363 | ], 364 | "source": [ 365 | "print(subjects.pop())" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 23, 371 | "id": "57b692e7-9aa7-45bf-a0ae-da86b0535a6d", 372 | "metadata": {}, 373 | "outputs": [ 374 | { 375 | "data": { 376 | "text/plain": [ 377 | "['AI', 'SE', 'IRS', 'DIP']" 378 | ] 379 | }, 380 | "execution_count": 23, 381 | "metadata": {}, 382 | "output_type": "execute_result" 383 | } 384 | ], 385 | "source": [ 386 | "subjects" 387 | ] 388 | }, 389 | { 390 | "cell_type": "markdown", 391 | "id": "0ef1cc4d-a7bd-4e08-8b8b-49c403419ff7", 392 | "metadata": {}, 393 | "source": [ 394 | "**count() counts the number of occurrence of the given element**" 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": 24, 400 | "id": "272a964b-1887-4943-80f8-1bb0c39dc043", 401 | "metadata": {}, 402 | "outputs": [ 403 | { 404 | "name": "stdout", 405 | "output_type": "stream", 406 | "text": [ 407 | "1\n" 408 | ] 409 | } 410 | ], 411 | "source": [ 412 | "print(subjects.count('SE'))" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": null, 418 | "id": "afa0715a-d1f3-44f2-b9c1-01deaafae80d", 419 | "metadata": {}, 420 | "outputs": [], 421 | "source": [] 422 | } 423 | ], 424 | "metadata": { 425 | "kernelspec": { 426 | "display_name": "Python 3 (ipykernel)", 427 | "language": "python", 428 | "name": "python3" 429 | }, 430 | "language_info": { 431 | "codemirror_mode": { 432 | "name": "ipython", 433 | "version": 3 434 | }, 435 | "file_extension": ".py", 436 | "mimetype": "text/x-python", 437 | "name": "python", 438 | "nbconvert_exporter": "python", 439 | "pygments_lexer": "ipython3", 440 | "version": "3.11.7" 441 | } 442 | }, 443 | "nbformat": 4, 444 | "nbformat_minor": 5 445 | } 446 | -------------------------------------------------------------------------------- /pythonbasics/ai lab basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "db1e2eaa-3fd2-4f2e-8aed-527654771836", 6 | "metadata": {}, 7 | "source": [ 8 | "**Basics of Python**" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "id": "81369bbb-b0da-4860-bcb2-9a50c6a5ed4e", 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdin", 19 | "output_type": "stream", 20 | "text": [ 21 | "enter your message: hello\n" 22 | ] 23 | }, 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "type of message is: \n", 29 | "your message is hello\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "message=input(\"enter your message:\")\n", 35 | "print(\"type of message is:\",type(message))\n", 36 | "print(\"your message is\", message)" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 2, 42 | "id": "47fc61b8-9a12-4ed2-95fc-e7c6157fa1a8", 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "hi-->there\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "print(\"hi\",\"there\",sep=\"-->\")" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "id": "1d44f07a-b7e9-4209-aed9-de63fe4a2e68", 60 | "metadata": {}, 61 | "source": [ 62 | "**Arithmetic Operations**" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 4, 68 | "id": "8b658735-343c-47b7-a574-d4be8e3fe3d7", 69 | "metadata": {}, 70 | "outputs": [ 71 | { 72 | "name": "stdin", 73 | "output_type": "stream", 74 | "text": [ 75 | "enter the value of a: 3\n", 76 | "enter the value for b: 4\n" 77 | ] 78 | }, 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "3+4 is: 7\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "a=int(input(\"enter the value of a:\"))\n", 89 | "b=int(input(\"enter the value for b:\"))\n", 90 | "print(f\"{a}+{b} is:\",(a+b))" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "id": "2ec5a8b0-ec60-4af3-8308-37acbae9577f", 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdin", 101 | "output_type": "stream", 102 | "text": [ 103 | "enter the value of a: 4\n", 104 | "enter the value for b: 3\n" 105 | ] 106 | }, 107 | { 108 | "name": "stdout", 109 | "output_type": "stream", 110 | "text": [ 111 | "4-3 is: 1\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "a=int(input(\"enter the value of a:\"))\n", 117 | "b=int(input(\"enter the value for b:\"))\n", 118 | "print(f\"{a}-{b} is:\",(a-b))" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 6, 124 | "id": "63158c6c-c81e-4b08-a50d-0408ed83c036", 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdin", 129 | "output_type": "stream", 130 | "text": [ 131 | "enter the value of a: 2\n", 132 | "enter the value for b: 3\n" 133 | ] 134 | }, 135 | { 136 | "name": "stdout", 137 | "output_type": "stream", 138 | "text": [ 139 | "2*3 is: 6\n" 140 | ] 141 | } 142 | ], 143 | "source": [ 144 | "a=int(input(\"enter the value of a:\"))\n", 145 | "b=int(input(\"enter the value for b:\"))\n", 146 | "print(f\"{a}*{b} is:\",(a*b))" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 7, 152 | "id": "6cc21422-ce83-4ce0-abd2-b73a03fa4616", 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdin", 157 | "output_type": "stream", 158 | "text": [ 159 | "enter the value of a: 4\n", 160 | "enter the value for b: 2\n" 161 | ] 162 | }, 163 | { 164 | "name": "stdout", 165 | "output_type": "stream", 166 | "text": [ 167 | "4/2 is: 2.0\n" 168 | ] 169 | } 170 | ], 171 | "source": [ 172 | "a=int(input(\"enter the value of a:\"))\n", 173 | "b=int(input(\"enter the value for b:\"))\n", 174 | "print(f\"{a}/{b} is:\",(a/b))" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "id": "18d36ea0-2c14-43d8-9909-209e9a89db78", 180 | "metadata": {}, 181 | "source": [ 182 | "**type conversion example**" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 8, 188 | "id": "60a5cfef-b58c-41b1-a822-f811aedcb157", 189 | "metadata": {}, 190 | "outputs": [ 191 | { 192 | "ename": "TypeError", 193 | "evalue": "unsupported operand type(s) for +: 'int' and 'str'", 194 | "output_type": "error", 195 | "traceback": [ 196 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 197 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 198 | "Cell \u001b[1;32mIn[8], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m a\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m\n\u001b[0;32m 2\u001b[0m b\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msam\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(a\u001b[38;5;241m+\u001b[39mb)\n", 199 | "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 200 | ] 201 | } 202 | ], 203 | "source": [ 204 | "a=3\n", 205 | "b=\"sam\"\n", 206 | "print(a+b)" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 10, 212 | "id": "9a9edff9-4fa6-48aa-bbe1-d71f3a9144ae", 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "sam3\n" 220 | ] 221 | } 222 | ], 223 | "source": [ 224 | "a=3\n", 225 | "b=\"sam\"\n", 226 | "#str() converts specified value to a string\n", 227 | "print(b+str(a))" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 11, 233 | "id": "48d65bb8-5899-4db5-a21a-51b01203a8db", 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "samsamsam\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "print(b*a)" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 12, 251 | "id": "741999e9-84bb-4b21-b916-8c87c8d9dbd9", 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "name": "stdout", 256 | "output_type": "stream", 257 | "text": [ 258 | "9\n" 259 | ] 260 | } 261 | ], 262 | "source": [ 263 | "print(a**2)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 13, 269 | "id": "9e141493-baff-4a54-8106-7ae68e191b66", 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "name": "stdout", 274 | "output_type": "stream", 275 | "text": [ 276 | "2\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "print(a-1)" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 14, 287 | "id": "81e9bd6d-75b6-4e76-923b-daa8922d1516", 288 | "metadata": {}, 289 | "outputs": [ 290 | { 291 | "name": "stdout", 292 | "output_type": "stream", 293 | "text": [ 294 | "1.0\n" 295 | ] 296 | } 297 | ], 298 | "source": [ 299 | "print(a/3)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 15, 305 | "id": "170ff70c-2fd1-4d61-8adc-05df2aba3d32", 306 | "metadata": {}, 307 | "outputs": [ 308 | { 309 | "name": "stdout", 310 | "output_type": "stream", 311 | "text": [ 312 | "1\n" 313 | ] 314 | } 315 | ], 316 | "source": [ 317 | "print(a//3)" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 16, 323 | "id": "bdcbdabf-45c5-44a8-af43-6748bdf6705f", 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "0\n" 331 | ] 332 | } 333 | ], 334 | "source": [ 335 | "print(a%3)" 336 | ] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "id": "c2238538-ecda-4b8a-a9b2-cd697816b5b4", 341 | "metadata": {}, 342 | "source": [ 343 | "**Loops**" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 18, 349 | "id": "58a61293-d9eb-45b7-b749-e6d67275dbfe", 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "name": "stdout", 354 | "output_type": "stream", 355 | "text": [ 356 | "0\n", 357 | "1\n", 358 | "2\n", 359 | "3\n", 360 | "4\n" 361 | ] 362 | } 363 | ], 364 | "source": [ 365 | "#range function returns a sequence of numbers starting from 0 which is default value and increments by 1\n", 366 | "for i in range(0,5):\n", 367 | " print(i)" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 19, 373 | "id": "4520d259-1429-4f02-848a-9485814d15ea", 374 | "metadata": {}, 375 | "outputs": [ 376 | { 377 | "name": "stdout", 378 | "output_type": "stream", 379 | "text": [ 380 | "1 2 3 4 5 " 381 | ] 382 | } 383 | ], 384 | "source": [ 385 | "#while loop\n", 386 | "i=1\n", 387 | "while i<=5:\n", 388 | " print(i,end=\" \")\n", 389 | " i+=1" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": 20, 395 | "id": "271e2b5a-8d28-426a-b32e-a97358804f85", 396 | "metadata": {}, 397 | "outputs": [ 398 | { 399 | "name": "stdout", 400 | "output_type": "stream", 401 | "text": [ 402 | "1\n", 403 | "2\n", 404 | "3\n", 405 | "4\n", 406 | "5\n" 407 | ] 408 | } 409 | ], 410 | "source": [ 411 | "i=1\n", 412 | "while True:\n", 413 | " print(i)\n", 414 | " i=i+1\n", 415 | " if(i>5):\n", 416 | " break" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 21, 422 | "id": "a5edafb6-0100-4945-b9b0-b3bea162d624", 423 | "metadata": {}, 424 | "outputs": [ 425 | { 426 | "name": "stdout", 427 | "output_type": "stream", 428 | "text": [ 429 | "0\n", 430 | "1\n", 431 | "2\n", 432 | "3\n", 433 | "4\n", 434 | "for loop\n" 435 | ] 436 | } 437 | ], 438 | "source": [ 439 | "#for with else\n", 440 | "n=5\n", 441 | "for i in range(n):\n", 442 | " print(i)\n", 443 | "else:\n", 444 | " print(\"for loop\")" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 22, 450 | "id": "9b0329e8-84ad-4c5e-b5bf-df1f2faf361b", 451 | "metadata": {}, 452 | "outputs": [ 453 | { 454 | "name": "stdout", 455 | "output_type": "stream", 456 | "text": [ 457 | "Inside Loop\n", 458 | "Inside Loop\n", 459 | "Inside Loop\n", 460 | "Inside else\n" 461 | ] 462 | } 463 | ], 464 | "source": [ 465 | "#while loop with else\n", 466 | "counter=0\n", 467 | "while counter<3:\n", 468 | " print('Inside Loop')\n", 469 | " counter=counter+1\n", 470 | "else:\n", 471 | " print('Inside else')\n", 472 | " " 473 | ] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "execution_count": 23, 478 | "id": "3c78870c-07f8-4478-81c5-e2022802000a", 479 | "metadata": {}, 480 | "outputs": [ 481 | { 482 | "name": "stdout", 483 | "output_type": "stream", 484 | "text": [ 485 | "it is positive\n" 486 | ] 487 | } 488 | ], 489 | "source": [ 490 | "#if constructs\n", 491 | "num=10\n", 492 | "if num>0:\n", 493 | " print('it is positive')\n", 494 | "else:\n", 495 | " print('it is negative')" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 24, 501 | "id": "c22c7fa7-ec5b-4848-baae-4be77e19d68d", 502 | "metadata": {}, 503 | "outputs": [ 504 | { 505 | "name": "stdout", 506 | "output_type": "stream", 507 | "text": [ 508 | "number x= 4\n", 509 | "y= 5\n" 510 | ] 511 | } 512 | ], 513 | "source": [ 514 | "#swap two numbers\n", 515 | "x=5\n", 516 | "y=4\n", 517 | "x,y=y,x\n", 518 | "print(\"number x=\",x)\n", 519 | "print(\"y=\",y)" 520 | ] 521 | }, 522 | { 523 | "cell_type": "code", 524 | "execution_count": 26, 525 | "id": "c5495aec-256f-4cef-8e0e-6c98363e3226", 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "name": "stdin", 530 | "output_type": "stream", 531 | "text": [ 532 | "enter the number: 153\n" 533 | ] 534 | }, 535 | { 536 | "name": "stdout", 537 | "output_type": "stream", 538 | "text": [ 539 | "153 is armstrong\n" 540 | ] 541 | } 542 | ], 543 | "source": [ 544 | "#armstrong numbers\n", 545 | "num=int(input(\"enter the number:\"))\n", 546 | "sum=0\n", 547 | "temp=num\n", 548 | "while temp>0:\n", 549 | " digit=temp%10\n", 550 | " sum+=digit**3\n", 551 | " temp//=10\n", 552 | "if num==sum:\n", 553 | " print(num,\"is armstrong\")\n", 554 | "else:\n", 555 | " print(num,\"is not an armstrong\")" 556 | ] 557 | }, 558 | { 559 | "cell_type": "code", 560 | "execution_count": 27, 561 | "id": "ae9060b0-7d5d-4f46-b55d-15372b2706c8", 562 | "metadata": {}, 563 | "outputs": [ 564 | { 565 | "name": "stdin", 566 | "output_type": "stream", 567 | "text": [ 568 | "enter the number: 4\n" 569 | ] 570 | }, 571 | { 572 | "name": "stdout", 573 | "output_type": "stream", 574 | "text": [ 575 | "24\n" 576 | ] 577 | } 578 | ], 579 | "source": [ 580 | "#find the factorial of a number\n", 581 | "num=int(input(\"enter the number: \"))\n", 582 | "fact=1\n", 583 | "if num < 0:\n", 584 | " print(\"factorial of -ve number does not exist\")\n", 585 | "elif num==0:\n", 586 | " print(\"factorial of 0 is\",fact)\n", 587 | "else:\n", 588 | " for i in range(1,num+1):\n", 589 | " fact=fact*i\n", 590 | " print(fact)" 591 | ] 592 | } 593 | ], 594 | "metadata": { 595 | "kernelspec": { 596 | "display_name": "Python 3 (ipykernel)", 597 | "language": "python", 598 | "name": "python3" 599 | }, 600 | "language_info": { 601 | "codemirror_mode": { 602 | "name": "ipython", 603 | "version": 3 604 | }, 605 | "file_extension": ".py", 606 | "mimetype": "text/x-python", 607 | "name": "python", 608 | "nbconvert_exporter": "python", 609 | "pygments_lexer": "ipython3", 610 | "version": "3.11.7" 611 | } 612 | }, 613 | "nbformat": 4, 614 | "nbformat_minor": 5 615 | } 616 | -------------------------------------------------------------------------------- /students_contributions/AI_NUMPY.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "id": "cc077e6e", 7 | "metadata": {}, 8 | "outputs": [ 9 | { 10 | "data": { 11 | "text/plain": [ 12 | "array([1, 2, 3])" 13 | ] 14 | }, 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "output_type": "execute_result" 18 | } 19 | ], 20 | "source": [ 21 | "import numpy as np\n", 22 | "array1=np.array([1,2,3])\n", 23 | "array1" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 3, 29 | "id": "7a284bf0", 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | "numpy.ndarray" 36 | ] 37 | }, 38 | "execution_count": 3, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | "type(array1)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 6, 50 | "id": "8c63b944", 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "array([[1. , 2. , 3.3],\n", 57 | " [4. , 5. , 6.5]])" 58 | ] 59 | }, 60 | "execution_count": 6, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | "#2-D Array\n", 67 | "arr2=np.array([[1,2,3.3],\n", 68 | " [4,5,6.5]])\n", 69 | "arr2" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 9, 75 | "id": "4f447054", 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "array([[[1, 2],\n", 82 | " [3, 4]],\n", 83 | "\n", 84 | " [[5, 6],\n", 85 | " [7, 8]]])" 86 | ] 87 | }, 88 | "execution_count": 9, 89 | "metadata": {}, 90 | "output_type": "execute_result" 91 | } 92 | ], 93 | "source": [ 94 | "arrex=np.array([\n", 95 | " [\n", 96 | " [1,2],\n", 97 | " [3,4]\n", 98 | " ],\n", 99 | " [\n", 100 | " [5,6],\n", 101 | " [7,8]\n", 102 | " ]\n", 103 | "])\n", 104 | "arrex" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 12, 110 | "id": "ed219d2c", 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "data": { 115 | "text/plain": [ 116 | "array([[[ 1, 2, 3],\n", 117 | " [ 4, 5, 6],\n", 118 | " [ 6, 7, 8]],\n", 119 | "\n", 120 | " [[10, 11, 12],\n", 121 | " [14, 15, 16],\n", 122 | " [17, 18, 19]]])" 123 | ] 124 | }, 125 | "execution_count": 12, 126 | "metadata": {}, 127 | "output_type": "execute_result" 128 | } 129 | ], 130 | "source": [ 131 | "arr4=np.array([[[1,2,3],\n", 132 | " [4,5,6],\n", 133 | " [6,7,8]],\n", 134 | " [[10,11,12],\n", 135 | " [14,15,16],\n", 136 | " [17,18,19]]])\n", 137 | "arr4" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 14, 143 | "id": "095d742a", 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "data": { 148 | "text/plain": [ 149 | "array([12, 13, 14])" 150 | ] 151 | }, 152 | "execution_count": 14, 153 | "metadata": {}, 154 | "output_type": "execute_result" 155 | } 156 | ], 157 | "source": [ 158 | "#passing elements as tuple\n", 159 | "arr5=np.array((12,13,14))\n", 160 | "arr5" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 17, 166 | "id": "3597a375", 167 | "metadata": {}, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "3\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "#No of dimensions\n", 179 | "print(arr4.ndim)" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 24, 185 | "id": "f89dddf4", 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "name": "stdout", 190 | "output_type": "stream", 191 | "text": [ 192 | "6\n" 193 | ] 194 | } 195 | ], 196 | "source": [ 197 | "#ndimin defines the no of dimesnsions you want \n", 198 | "arr10=np.array([1,2,3,9,12],ndmin=6)\n", 199 | "arr10\n", 200 | "print(arr10.ndim)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 26, 206 | "id": "0edcad0a", 207 | "metadata": {}, 208 | "outputs": [ 209 | { 210 | "data": { 211 | "text/plain": [ 212 | "(1, 1, 1, 1, 1, 5)" 213 | ] 214 | }, 215 | "execution_count": 26, 216 | "metadata": {}, 217 | "output_type": "execute_result" 218 | } 219 | ], 220 | "source": [ 221 | "arr10.shape" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 31, 227 | "id": "6863f21d", 228 | "metadata": {}, 229 | "outputs": [ 230 | { 231 | "name": "stdout", 232 | "output_type": "stream", 233 | "text": [ 234 | "[[1. 2. 3.3]\n", 235 | " [4. 5. 6.5]]\n", 236 | "5.0\n" 237 | ] 238 | } 239 | ], 240 | "source": [ 241 | "#Indexing in array\n", 242 | "print(arr2)\n", 243 | "print(arr2[1][1])" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 33, 249 | "id": "012f24a2", 250 | "metadata": {}, 251 | "outputs": [ 252 | { 253 | "name": "stdout", 254 | "output_type": "stream", 255 | "text": [ 256 | "1.0\n", 257 | "2.0\n", 258 | "3.3\n", 259 | "4.0\n", 260 | "5.0\n", 261 | "6.5\n" 262 | ] 263 | } 264 | ], 265 | "source": [ 266 | "for x in range(0,2):\n", 267 | " for y in range(0,3):\n", 268 | " print(arr2[x][y])" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 35, 274 | "id": "00137d2d", 275 | "metadata": {}, 276 | "outputs": [ 277 | { 278 | "data": { 279 | "text/plain": [ 280 | "(3,)" 281 | ] 282 | }, 283 | "execution_count": 35, 284 | "metadata": {}, 285 | "output_type": "execute_result" 286 | } 287 | ], 288 | "source": [ 289 | "array1.shape" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 37, 295 | "id": "aa757c5e", 296 | "metadata": {}, 297 | "outputs": [ 298 | { 299 | "data": { 300 | "text/plain": [ 301 | "(2, 3)" 302 | ] 303 | }, 304 | "execution_count": 37, 305 | "metadata": {}, 306 | "output_type": "execute_result" 307 | } 308 | ], 309 | "source": [ 310 | "arr2.shape" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": 41, 316 | "id": "0d4dc416", 317 | "metadata": {}, 318 | "outputs": [ 319 | { 320 | "data": { 321 | "text/plain": [ 322 | "(dtype('int32'), dtype('int32'))" 323 | ] 324 | }, 325 | "execution_count": 41, 326 | "metadata": {}, 327 | "output_type": "execute_result" 328 | } 329 | ], 330 | "source": [ 331 | "arr4.dtype,arr5.dtype" 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "execution_count": 43, 337 | "id": "8528d04c", 338 | "metadata": {}, 339 | "outputs": [ 340 | { 341 | "data": { 342 | "text/plain": [ 343 | "array([[1, 2, 3],\n", 344 | " [4, 5, 6]], dtype=int32)" 345 | ] 346 | }, 347 | "execution_count": 43, 348 | "metadata": {}, 349 | "output_type": "execute_result" 350 | } 351 | ], 352 | "source": [ 353 | "array2conv=arr2.astype('i')\n", 354 | "array2conv" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 46, 360 | "id": "b6413537", 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "data": { 365 | "text/plain": [ 366 | "array([b'1', b'2', b'3', b'4', b'5'], dtype='|S1')" 367 | ] 368 | }, 369 | "execution_count": 46, 370 | "metadata": {}, 371 | "output_type": "execute_result" 372 | } 373 | ], 374 | "source": [ 375 | "arr11=np.array([1,2,3,4,5],dtype='S')\n", 376 | "arr11" 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": 48, 382 | "id": "b26a55c4", 383 | "metadata": {}, 384 | "outputs": [ 385 | { 386 | "data": { 387 | "text/plain": [ 388 | "array([[1. , 2. , 3.3],\n", 389 | " [4. , 5. , 6.5]], dtype=float32)" 390 | ] 391 | }, 392 | "execution_count": 48, 393 | "metadata": {}, 394 | "output_type": "execute_result" 395 | } 396 | ], 397 | "source": [ 398 | "arr2float=arr2.astype('f')\n", 399 | "arr2float" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": 52, 405 | "id": "301e1bb9", 406 | "metadata": {}, 407 | "outputs": [ 408 | { 409 | "data": { 410 | "text/plain": [ 411 | "array([[0, 0],\n", 412 | " [0, 0]])" 413 | ] 414 | }, 415 | "execution_count": 52, 416 | "metadata": {}, 417 | "output_type": "execute_result" 418 | } 419 | ], 420 | "source": [ 421 | "numone=np.zeros((2,2),dtype=int)\n", 422 | "numone" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 55, 428 | "id": "467dbb7c", 429 | "metadata": {}, 430 | "outputs": [ 431 | { 432 | "data": { 433 | "text/plain": [ 434 | "array([[1, 1],\n", 435 | " [1, 1],\n", 436 | " [1, 1]])" 437 | ] 438 | }, 439 | "execution_count": 55, 440 | "metadata": {}, 441 | "output_type": "execute_result" 442 | } 443 | ], 444 | "source": [ 445 | "num2=np.ones((3,2),dtype=int)\n", 446 | "num2" 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": 61, 452 | "id": "1cef661f", 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "data": { 457 | "text/plain": [ 458 | "array([[1, 0, 0],\n", 459 | " [0, 1, 0],\n", 460 | " [0, 0, 1]])" 461 | ] 462 | }, 463 | "execution_count": 61, 464 | "metadata": {}, 465 | "output_type": "execute_result" 466 | } 467 | ], 468 | "source": [ 469 | "num4=np.eye(3,dtype=int)\n", 470 | "num4" 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 66, 476 | "id": "d907b53f", 477 | "metadata": {}, 478 | "outputs": [ 479 | { 480 | "data": { 481 | "text/plain": [ 482 | "array([6, 9])" 483 | ] 484 | }, 485 | "execution_count": 66, 486 | "metadata": {}, 487 | "output_type": "execute_result" 488 | } 489 | ], 490 | "source": [ 491 | "range_array=np.arange(6,10,3)\n", 492 | "range_array" 493 | ] 494 | }, 495 | { 496 | "cell_type": "code", 497 | "execution_count": 75, 498 | "id": "a7145fba", 499 | "metadata": {}, 500 | "outputs": [ 501 | { 502 | "data": { 503 | "text/plain": [ 504 | "array([3, 0, 1, 3])" 505 | ] 506 | }, 507 | "execution_count": 75, 508 | "metadata": {}, 509 | "output_type": "execute_result" 510 | } 511 | ], 512 | "source": [ 513 | "a=np.random.randint(low=4,size=(4))\n", 514 | "a" 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "execution_count": 77, 520 | "id": "1ff82a7f", 521 | "metadata": {}, 522 | "outputs": [ 523 | { 524 | "data": { 525 | "text/plain": [ 526 | "array([ 1, 2, 3, 4, 5, 6, 6, 7, 8, 10, 11, 12, 14, 15, 16, 17, 18,\n", 527 | " 19])" 528 | ] 529 | }, 530 | "execution_count": 77, 531 | "metadata": {}, 532 | "output_type": "execute_result" 533 | } 534 | ], 535 | "source": [ 536 | "flatarr=arr4.flatten()\n", 537 | "flatarr" 538 | ] 539 | } 540 | ], 541 | "metadata": { 542 | "kernelspec": { 543 | "display_name": "Python 3 (ipykernel)", 544 | "language": "python", 545 | "name": "python3" 546 | }, 547 | "language_info": { 548 | "codemirror_mode": { 549 | "name": "ipython", 550 | "version": 3 551 | }, 552 | "file_extension": ".py", 553 | "mimetype": "text/x-python", 554 | "name": "python", 555 | "nbconvert_exporter": "python", 556 | "pygments_lexer": "ipython3", 557 | "version": "3.11.4" 558 | } 559 | }, 560 | "nbformat": 4, 561 | "nbformat_minor": 5 562 | } 563 | -------------------------------------------------------------------------------- /students_contributions/Waterjug_auto.py: -------------------------------------------------------------------------------- 1 | import math 2 | from collections import deque 3 | a = int(input("Enter Jug A Capacity: ")) 4 | b = int(input("Enter Jug B Capacity: ")) 5 | ai = int(input("Initially Water in Jug A: ")) 6 | bi = int(input("Initially Water in Jug B: ")) 7 | af = int(input("Final State of Jug A: ")) 8 | bf = int(input("Final State of Jug B: ")) 9 | if a <= 0 or b <= 0: 10 | print("Jug capacities must be positive.") 11 | exit(1) 12 | if ai < 0 or bi < 0 or af < 0 or bf < 0: 13 | print("Negative values are not allowed.") 14 | exit(1) 15 | if ai==af and bi==bf: 16 | print(f"initial state is already the final state: juga{ai} and jugb={bi}") 17 | exit() 18 | def bfs_wjug(a, b, ai, bi, af, bf): 19 | visited = set() 20 | queue = deque([(ai, bi, [])]) 21 | while queue: 22 | curr_ai, curr_bi, operations = queue.popleft() 23 | if (curr_ai, curr_bi) in visited: 24 | continue 25 | visited.add((curr_ai, curr_bi)) 26 | if curr_ai == af and curr_bi == bf: 27 | for i, op in enumerate(operations): 28 | print(f"Step {i + 1}: {op}") 29 | print(f"Final State Reached: Jug A = {curr_ai}, Jug B = {curr_bi}") 30 | return 31 | possible_operations = [ 32 | (a, curr_bi, "Fill Jug A"), 33 | (curr_ai, b, "Fill Jug B"), 34 | (0, curr_bi, "Empty Jug A"), 35 | (curr_ai, 0, "Empty Jug B"), 36 | (curr_ai - min(curr_ai, b - curr_bi), curr_bi + min(curr_ai, b - curr_bi), "Pour from A to B"), 37 | (curr_ai + min(curr_bi, a - curr_ai), curr_bi - min(curr_bi, a - curr_ai), "Pour from B to A"), 38 | ] 39 | for next_ai, next_bi, op in possible_operations: 40 | if (next_ai, next_bi) not in visited: 41 | queue.append((next_ai, next_bi, operations + [op])) 42 | print("No solution found.") 43 | return 44 | gcd = math.gcd(a, b) 45 | if (af <= a and bf <= b) and (af % gcd == bf % gcd == 0): 46 | bfs_wjug(a, b, ai, bi, af, bf) 47 | else: 48 | print("The final state is not achievable with the given capacities.") 49 | exit() 50 | -------------------------------------------------------------------------------- /students_contributions/numpy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | import numpy as np 8 | 9 | 10 | # In[2]: 11 | 12 | 13 | arr1=np.array([1,2,3]) 14 | arr1 15 | 16 | 17 | # In[3]: 18 | 19 | 20 | type(arr1) 21 | 22 | 23 | # In[6]: 24 | 25 | 26 | arr2=np.array([[1,2,3.3],[4,5,6.5,]]) 27 | arr2 28 | 29 | 30 | # In[13]: 31 | 32 | 33 | arr3=np.array([ 34 | [ 35 | [1,2], 36 | [3,4] 37 | ], 38 | [ 39 | [5,6], 40 | [7,8] 41 | ] 42 | ]) 43 | print(arr3) 44 | 45 | 46 | # In[14]: 47 | 48 | 49 | arr4=np.array((12,13,15)) 50 | arr4 51 | 52 | 53 | # In[15]: 54 | 55 | 56 | print(arr1.ndim) 57 | 58 | 59 | # In[16]: 60 | 61 | 62 | print(arr2.ndim) 63 | 64 | 65 | # In[17]: 66 | 67 | 68 | print(arr3.ndim) 69 | 70 | 71 | # In[18]: 72 | 73 | 74 | print(arr4.ndim) 75 | 76 | 77 | # In[19]: 78 | 79 | 80 | array5=np.array([1,2,3,5],ndmin=4) 81 | array5 82 | 83 | 84 | # In[20]: 85 | 86 | 87 | print(array5.ndim) 88 | 89 | 90 | # In[22]: 91 | 92 | 93 | array5.shape 94 | 95 | 96 | # In[23]: 97 | 98 | 99 | arr2 100 | 101 | 102 | # In[25]: 103 | 104 | 105 | arr2[0][1] 106 | 107 | 108 | # arr2.shape(2,3) 109 | 110 | # In[26]: 111 | 112 | 113 | arr2.shape 114 | 115 | 116 | # In[27]: 117 | 118 | 119 | for x in range(0,2): 120 | for y in range(0,3): 121 | print(arr2[x][y]) 122 | 123 | 124 | # In[29]: 125 | 126 | 127 | arr1.dtype 128 | 129 | 130 | # In[30]: 131 | 132 | 133 | arr2.dtype 134 | 135 | 136 | # In[32]: 137 | 138 | 139 | arr5=np.array([1,2,3,4,5],dtype='S') 140 | arr5 141 | 142 | 143 | # In[34]: 144 | 145 | 146 | array2conv=arr2.astype('i') 147 | array2conv 148 | 149 | 150 | # 151 | 152 | # In[35]: 153 | 154 | 155 | array2float=arr2.astype('f') 156 | array2float 157 | 158 | 159 | # In[41]: 160 | 161 | 162 | num1=np.zeros((2,2),dtype=int) 163 | num1 164 | 165 | 166 | # In[47]: 167 | 168 | 169 | range_array=np.arange(0,10,3) 170 | range_array 171 | 172 | 173 | # In[61]: 174 | 175 | 176 | a=np.random.randint(low=4,high=10,size=5) 177 | a 178 | 179 | 180 | # In[64]: 181 | 182 | 183 | np.concatenate([arr1,arr4]) 184 | 185 | 186 | # In[65]: 187 | 188 | 189 | flatarr=arr3.flatten() 190 | flatarr 191 | 192 | 193 | # In[ ]: 194 | 195 | 196 | 197 | 198 | -------------------------------------------------------------------------------- /students_contributions/prolog_familytree.py: -------------------------------------------------------------------------------- 1 | male(harivansh). 2 | male(amitabh). 3 | male(abhishek). 4 | female(teji). 5 | female(jaya). 6 | female(shweta). 7 | female(aishwarya). 8 | female(aradhya). 9 | 10 | parent(harivansh,amitabh). 11 | parent(teji,amitabh). 12 | parent(amitabh,abhishek). 13 | parent(amitabh,shweta). 14 | parent(jaya,abhishek). 15 | parent(jaya,shweta). 16 | parent(abhishek,aradhya). 17 | parent(aishwarya,aradhya). 18 | 19 | mother(M,C):-female(M),parent(M,C). 20 | father(F,C):-male(F),parent(F,C). 21 | son(S,P):-male(S),parent(P,S). 22 | daughter(D,P):-female(D), parent(P,D). 23 | brother(B,S):-male(B),parent(P,B),parent(P,S). 24 | sister(S,B):-female(S), parent(P,S), parent(P,B). 25 | grandfather(G,C):-male(G),parent(G,M),parent(M,C). 26 | grandmother(GM,C):-female(GM),parent(GM,M),parent(M,C). 27 | 28 | -------------------------------------------------------------------------------- /uninformed search/bfs.py: -------------------------------------------------------------------------------- 1 | graph={ 2 | 'P':['Q','R','S'], 3 | 'Q':['P','R'], 4 | 'R':['P','Q','T'], 5 | 'T':['R'], 6 | 'S':['P'] 7 | } 8 | visited=[] 9 | queue=[] 10 | def bfs(visited,graph,node): 11 | visited.append(node) 12 | queue.append(node) 13 | while queue: 14 | m=queue.pop(0) 15 | print(m,end=" ") 16 | for neighbour in graph[m]: 17 | if neighbour not in visited: 18 | visited.append(neighbour) 19 | queue.append(neighbour) 20 | print("following is the breadth first search") 21 | bfs(visited,graph,'P') -------------------------------------------------------------------------------- /uninformed search/dfs.py: -------------------------------------------------------------------------------- 1 | #ADJACENCY LIST 2 | graph = { 3 | 'A' : ['B','C'], 4 | 'B' : ['D', 'E'], 5 | 'C' : ['F'], 6 | 'D' : [], 7 | 'E':['F'], 8 | 'F' : [], 9 | } 10 | visited = set() # Set to keep track of visited nodes of graph. 11 | def dfs(visited, graph, node): 12 | #print(visited) #function for dfs 13 | if node not in visited: 14 | print (node) 15 | visited.add(node) 16 | for neighbour in graph[node]: 17 | dfs(visited, graph, neighbour) 18 | print("Following is the Depth-First Search") 19 | dfs(visited, graph, 'A') -------------------------------------------------------------------------------- /waterjug code/waterjbfs.py: -------------------------------------------------------------------------------- 1 | import math 2 | from collections import deque 3 | 4 | ''' Input capacities and initial/final states for jugs''' 5 | a = int(input("Enter Jug A Capacity: ")) 6 | b = int(input("Enter Jug B Capacity: ")) 7 | ai = int(input("Initially Water in Jug A: ")) 8 | bi = int(input("Initially Water in Jug B: ")) 9 | af = int(input("Final State of Jug A: ")) 10 | bf = int(input("Final State of Jug B: ")) 11 | 12 | # Check for negative values and whether initial state is equal to final state 13 | if a <= 0 or b <= 0: 14 | print("Jug capacities must be positive.") 15 | exit(1) 16 | if ai < 0 or bi < 0 or af < 0 or bf < 0: 17 | print("Negative values are not allowed.") 18 | exit(1) 19 | if ai==af and bi==bf: 20 | print(f"initial state is already the final state: juga{ai} and jugb={bi}") 21 | exit() 22 | # Define the water jug solver function using BFS 23 | def bfs_wjug(a, b, ai, bi, af, bf): 24 | visited = set() 25 | queue = deque([(ai, bi, [])]) # (Jug A state, Jug B state, List of operations) 26 | 27 | while queue: 28 | curr_ai, curr_bi, operations = queue.popleft() 29 | 30 | if (curr_ai, curr_bi) in visited: 31 | continue 32 | visited.add((curr_ai, curr_bi)) 33 | 34 | # Check if the final state is reached 35 | if curr_ai == af and curr_bi == bf: 36 | for i, op in enumerate(operations): 37 | print(f"Step {i + 1}: {op}") 38 | print(f"Final State Reached: Jug A = {curr_ai}, Jug B = {curr_bi}") 39 | return 40 | 41 | # List of possible operations 42 | possible_operations = [ 43 | (a, curr_bi, "Fill Jug A"), # Fill Jug A 44 | (curr_ai, b, "Fill Jug B"), # Fill Jug B 45 | (0, curr_bi, "Empty Jug A"), # Empty Jug A 46 | (curr_ai, 0, "Empty Jug B"), # Empty Jug B 47 | (curr_ai - min(curr_ai, b - curr_bi), curr_bi + min(curr_ai, b - curr_bi), "Pour from A to B"), # Pour A to B 48 | (curr_ai + min(curr_bi, a - curr_ai), curr_bi - min(curr_bi, a - curr_ai), "Pour from B to A"), # Pour B to A 49 | ] 50 | 51 | # Add each possible operation to the queue 52 | for next_ai, next_bi, op in possible_operations: 53 | if (next_ai, next_bi) not in visited: 54 | queue.append((next_ai, next_bi, operations + [op])) 55 | 56 | print("No solution found.") 57 | return 58 | 59 | # Check if the final state can be achievable using GCD 60 | gcd = math.gcd(a, b) 61 | 62 | if (af <= a and bf <= b) and (af % gcd == bf % gcd == 0): 63 | bfs_wjug(a, b, ai, bi, af, bf) 64 | else: 65 | print("The final state is not achievable with the given capacities.") 66 | exit() 67 | -------------------------------------------------------------------------------- /waterjug code/waterjug.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sinifive/Artificial-Intelligence-Lab/c8cdb829b9a9fa9d9d8401e81898aa85cac34290/waterjug code/waterjug.pptx -------------------------------------------------------------------------------- /waterjug code/waterjugmanual.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | # Input capacities and initial/final states for jugs 4 | a = int(input("Enter Jug A Capacity: ")) 5 | b = int(input("Enter Jug B Capacity: ")) 6 | ai = int(input("Initially Water in Jug A: ")) 7 | bi = int(input("Initially Water in Jug B: ")) 8 | af = int(input("Final State of Jug A: ")) 9 | bf = int(input("Final State of Jug B: ")) 10 | 11 | # Check for negative values 12 | if a <= 0 or b <= 0: 13 | print("Jug capacities must be positive.") 14 | exit(1) 15 | if ai < 0 or bi < 0 or af < 0 or bf < 0: 16 | print("Negative values are not allowed.") 17 | exit(1) 18 | 19 | # Define the water jug solver function 20 | def wjug(a, b, ai, bi, af, bf): 21 | print("List Of Operations You Can Do:\n") 22 | print("1. Fill Jug A Completely") 23 | print("2. Fill Jug B Completely") 24 | print("3. Empty Jug A Completely") 25 | print("4. Empty Jug B Completely") 26 | print("5. Pour From Jug A till Jug B is full or A becomes empty") 27 | print("6. Pour From Jug B till Jug A is full or B becomes empty") 28 | print("7. Pour all from Jug B to Jug A") 29 | print("8. Pour all from Jug A to Jug B") 30 | 31 | # Loop until the final state is reached 32 | while ai != af or bi != bf: 33 | op = int(input("Enter the Operation (1-8): ")) 34 | 35 | if op == 1: # Fill Jug A completely 36 | ai = a 37 | elif op == 2: # Fill Jug B completely 38 | bi = b 39 | elif op == 3: # Empty Jug A 40 | ai = 0 41 | elif op == 4: # Empty Jug B 42 | bi = 0 43 | elif op == 5: # Pour from A to B 44 | pour_amount = min(ai, b - bi) 45 | ai -= pour_amount 46 | bi += pour_amount 47 | elif op == 6: # Pour from B to A 48 | pour_amount = min(bi, a - ai) 49 | bi -= pour_amount 50 | ai += pour_amount 51 | elif op == 7: # Pour all from B to A 52 | pour_amount = min(bi, a - ai) 53 | ai += pour_amount 54 | bi -= pour_amount 55 | elif op == 8: # Pour all from A to B 56 | pour_amount = min(ai, b - bi) 57 | bi += pour_amount 58 | ai -= pour_amount 59 | else: 60 | print("Invalid operation. Please choose a number between 1 and 8.") 61 | continue 62 | 63 | print(f"Jug A: {ai}, Jug B: {bi}") 64 | 65 | if ai == af and bi == bf: 66 | print("Final State Reached: Jug A =", ai, ", Jug B =", bi) 67 | return 68 | 69 | 70 | 71 | print("Final State Reached: Jug A =", ai, ", Jug B =", bi) 72 | 73 | # Check if the final state is achievable using GCD 74 | gcd = math.gcd(a, b) 75 | 76 | if (af <= a and bf <= b) and (af % gcd == bf % gcd == 0): 77 | wjug(a, b, ai, bi, af, bf) 78 | else: 79 | print("The final state is not achievable with the given capacities.") 80 | exit(1) 81 | --------------------------------------------------------------------------------