0% found this document useful (0 votes)
5 views8 pages

Ai Myh

The document contains code examples and explanations of different machine learning algorithms including linear regression, backward chaining, forward chaining, depth-first search, breadth-first search, 8-queens problem solving, a simple reflex agent, and a decision tree classifier.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Ai Myh

The document contains code examples and explanations of different machine learning algorithms including linear regression, backward chaining, forward chaining, depth-first search, breadth-first search, 8-queens problem solving, a simple reflex agent, and a decision tree classifier.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

AI LAB MANUAL BY MYH

LINEAR REGRESSION
import numpy as np

import matplotlib.pyplot as plt

num_samples = 20

sizes = np.random.randint(500, 3500, size=num_samples)

prices = np.random.randint(100000, 1000000, size=num_samples)

m, b = np.polyfit(sizes, prices, 1)

plt.scatter(sizes, prices, color='blue', label='Data')

plt.plot(sizes, m * sizes + b, color='red', label='Linear Regression')

plt.xlabel('Size (sq ft)')

plt.ylabel('Price ($)')

plt.title('House Prices Prediction with Linear Regression')

plt.legend()

plt.grid(True)

plt.show()

MYH 1
AI LAB MANUAL BY MYH

BACKWARD CHAINING
print("-----Backward Chaining-----")

a = input("Is it a mammal or bird? ")

if a == "mammal":

print("It is a Mammal." if input("Does it have fur? (yes/no) ") == "yes" else "It is not a Mammal.")

elif a == "bird":

print("It is a Bird." if input("Does it lay eggs or can it fly? (yes/no) ") == "yes" else "It is not a Bird.")

else:

print("Invalid option selected.")

MYH 2
AI LAB MANUAL BY MYH

FORWARD CHAINING
traits = ["Has fur", "Lays eggs"]

print("-----Forward Chaining-----")

print(traits)

x = int(input("Select one: "))

print("\nIt's likely a Mammal." if x == 1 else "\nIt's likely a Bird." if x == 2 else "\nInvalid Option Selected.")

MYH 3
AI LAB MANUAL BY MYH

DFS
graph = {

'A': ['B', 'C'],

'B': ['D', 'E'],

'C': ['F'],

'D': [],

'E': ['F'],

'F': []

visited = set()

def dfs(visited, graph, node):

if node not in visited:

print(node)

visited.add(node)

for neighbour in graph[node]:

dfs(visited, graph, neighbour)

dfs(visited, graph, 'A')

MYH 4
AI LAB MANUAL BY MYH

BFS
graph = {

'A': ['B', 'C'],

'B': ['D', 'E'],

'C': ['F'],

'D': [],

'E': ['F'],

'F': []

queue = []

visited = set()

def bfs(visited, graph, node):

queue.append(node)

visited.add(node)

while queue:

current_node = queue.pop(0)

print(current_node)

for neighbor in graph[current_node]:

if neighbor not in visited:

queue.append(neighbor)

visited.add(neighbor)

bfs(visited, graph, 'A')

MYH 5
AI LAB MANUAL BY MYH

8-QUEENS
def isSafe(board, row, col):

for i in range(row):

if board[i] == col or abs(board[i] - col) == row - i:

return False

return True

def solve(board, row):

if row == 8:

return True

for col in range(8):

if isSafe(board, row, col):

board[row] = col

if solve(board, row + 1):

return True

return False

board = [-1] * 8

result = solve(board, 0)

print(result)

MYH 6
AI LAB MANUAL BY MYH

SIMPLE REFLEXT AGENT


print("Action to perform:", "Suck" if input("Status (Clean/Dirty): ") == "Dirty" else "Right" if input("Location (A/B): ")
== "A" else "Left")

(ALL IN ONE LINE)

MYH 7
AI LAB MANUAL BY MYH

DECISION TREE
import numpy as np

from sklearn.model_selection import train_test_split

from sklearn.tree import DecisionTreeClassifier

from sklearn.metrics import accuracy_score

np.random.seed(42)

X = np.random.rand(100, 4)

y = np.random.randint(0, 3, 100)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

accuracy = DecisionTreeClassifier().fit(X_train, y_train).score(X_test, y_test)

print("Accuracy:", accuracy)

MYH 8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy