Ai Lab Document-1
Ai Lab Document-1
PENDING PROGRSMS:4TH,5TH.
1.Python program on problem solving by searching :breadth first
search.
graph={
'5':['3','7'],
'3':['2','4'],
'7':['8'],
'2':[],
'4':['8'],
'8':[]
}
visited=[]#lidt for vidited nodes.
queue=[]#initialize a queue
while queue:
m=queue.pop(0)
print(m,end="")
for neighbour in graph[m]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
#divide code
print("following is the breadth-first search")
bfs(visited,graph,'5')#function calling
output:
following is the breadth-first search
537248
#driver code
print("following is the depth-first search")
dfs(visited,graph,'5')
output:
4
8
7
import heapq
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
# Heuristic values for each node (example values)
heuristics = {
'A': 6,
'B': 4,
'C': 2,
'D': 7,
'E': 3,
'F': 1
}
# Function to perform Greedy Best-First Search
def gbfs(graph, start, goal):
visited = set()
priority_queue = [(heuristics[start], start)]
while priority_queue:
# Get the node with the lowest heuristic value
_, node = heapq.heappop(priority_queue)
if node not in visited:
print(node)
visited.add(node)
if node == goal:
print(f"Goal {goal} found!")
return
import heapq
class Graph:
def __init__(self, graph, heuristic):
self.graph = graph
self.heuristic = heuristic
self.visited = set()
self.queue = []
heapq.heappush(self.queue, (self.heuristic['A'], 'START',
[])) # (heuristic value, current node, path)
if node in self.visited:
continue
self.visited.add(node)
path = path + [node]
if node == goal:
return path
return None
# Example usage
graph = {
'START': {'A': 10, 'B': 5},
'A': {'C': 5},
'B': {'C': 20},
'C': {'GOAL': 5},
'GOAL': {}
}
heuristic = {
'A': 7, # Heuristic values are estimates
'B': 6,
'C': 2,
'GOAL': 0
}
g = Graph(graph, heuristic)
path = g.greedy_best_first_search('GOAL')
if path:
print("Greedy Best-First Search path:", path)
else:
print("Goal not reachable.")
output:
Greedy Best-First Search path: ['START', 'B', 'C', 'GOAL']
x=[5,7,8,7,2,17,2,9,4,11,12,9,6]
y=[99,86,87,88,111,86,103,87,94,78,77,85,86]
slope,intercept,r,p,std_err=stats.linregress(x,y)
def myfunc(x):
return slope*x+intercept
speed=myfunc(10)
print (speed)
output:
85.59308314937454
7.python program to predict the price of the car using decision tree.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
data = {
'km_driven':[50000,60000,30000,35000,40000],
'year' :[2016,2014,2018,2015,2017],
'make': ['Toyota','Honda','Toyota','Honda','Toyota'],
'model':['camry','Accord','corolla','Civic','Rav4'],
'price':[22000,18000,24000,17000,26000]
}
df = pd.DataFrame(data)
#df=pd.read_csv('cardekho.csv)
X = df[['km_driven','year']]
y = df['price']
X_train,X_test,y_train,y_test =
train_test_split(X,y,test_size=0.2,random_state=42)
model=DecisionTreeRegressor(random_state=42)
model.fit(X_train,y_train)
predictions=model.predict(X_test)
new_car_features =[[45000,2019]]
predicted_price = model.predict(new_car_features)
print(f'Predicted price for the new car: Rupeess
{predicted_price[0]}')
output:
Predicted price for the new car: Rupeess 24000.0
-OR-
7. Python program to predict the price of the car using decision tree.
data = {
'km_driven': [50000, 60000, 30000, 35000, 40000],
'year': [2016, 2014, 2018, 2015, 2017],
'make': ['Toyota', 'Honda', 'Toyota', 'Honda', 'Toyota'],
'model': ['Camry', 'Accord', 'Corolla', 'Civic', 'Rav4'],
'price': [22000, 18000, 24000, 17000, 26000]
}
df = pd.DataFrame(data)
#df=pd.read_csv('cardekho.csv')
X = df[['km_driven', 'year']]
y = df['price']
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
model = DecisionTreeRegressor(random_state=42)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
new_car_features = [[45000, 2019]]
predicted_price = model.predict(new_car_features)
print(f'Predicted price for the new car: Rupees
{predicted_price[0]}')
output:
Predicted price for the new car: Rupees 24000.0
8.Python program of weather prediction model that predicts whether or not their’ll be
rain on a particular day.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
data = {
"temp": [25, 38, 27,40],
"humidity": [80, 70, 75,60],
"rain":[1,0,1,0]
}
df = pd.DataFrame(data)
print(df)
X = df[['temp', 'humidity']]
y = df['rain']
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=0)
model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
new_data = [[38,90]]
prediction = model.predict(new_data)
if prediction[0] == 1:
print("Prediction: It will rain.")
else:
print("Prediction: It will not rain.")
output:
temp humidity rain
0 25 80 1
1 38 70 0
2 27 75 1
3 40 60 0
C:\Users\LENOVO\AppData\Local\Programs\Python\Python310\lib\site-
packages\sklearn\base.py:493: UserWarning: X does not have valid feature names, but
LogisticRegression was fitted with feature names
warnings.warn(
9.Python program of profit prediction model that states the probable profit that can be
generated from the sale of a product.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
data = {
"price": [100, 120, 110],
"advertise": [20, 30, 25],
"sold":[100,90,95],
"profit":[5000,5500,6000]
}
df = pd.DataFrame(data)
print(df)
X = df[['price', 'advertise', 'sold']] # Features
y = df['profit']
# Split the data into training and testing sets (80% train, 20%
test)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=0)
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, classification_report
emails = [
("Hey, there is a sale going on, don't miss out!", "spam"),
("Meeting agenda for today's discussion", "not spam"),
("Get a free gift card on purchases over $50", "spam"),
("Reminder: Team meeting at 2 PM", "not spam"),
("Limited time offer, get 50% off on all items", "spam"),
("Please review and approve the proposal", "not spam")
]
classifier = MultinomialNB()
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
#new_email = ["Hurry! Limited time offer, buy now and get 30%
off"]
new_email=["Requesting notes"]
new_email_vectorized = vectorizer.transform(new_email)
prediction = classifier.predict(new_email_vectorized)[0]
print(f"\nPrediction for '{new_email[0]}': {prediction}")
output:
Classification Report email:
accuracy 1.00 2
macro avg 1.00 1.00 1.00 2
weighted avg 1.00 1.00 1.00 2
11.Python program that demonstrates how to classify flowers using a support vector
machine(svm) classifier.
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import classification_report, accuracy_score
output:
Iris Dataset:
Sepal Length Sepal Width Petal Length Petal Width Class
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0
Classification Report:
precision recall f1-score support
setosa 1.00 1.00 1.00 10
versicolor 1.00 0.89 0.94 9
virginica 0.92 1.00 0.96 11
accuracy 0.97 30
macro avg 0.97 0.96 0.97 30
weighted avg 0.97 0.97 0.97 30
12.Python program that demonstrates how to use a basic artificial neural network(ANN)
to classify students based on their height and weight.
input=[0.1,0.5,0.2]
weight=[0.4,0.3,0.6]
t=0.5
def step(ws):
if ws>t:
return 1
else:
return 0
def percept():
ws=0
for x,w in zip(input,weight):
ws+=x*w
print(ws)
return step(ws)
output=percept()
print(output)
output:
0.04000000000000001
0.19
0.31
0
13.python program that demonstrates text classification using scikit-learn and a naïve
bayes classifier.
import numpy as np
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
newsgroups = fetch_20newsgroups(subset='all', remove=('headers', 'footers', 'quotes'))
model = make_pipeline(
TfidfVectorizer(stop_words='english'),
MultinomialNB()
)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print("\nClassification Report:")
print(classification_report(y_test, y_pred, target_names=newsgroups.target_names))
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))
print("ssssssssssssss")
output:
Accuracy: 0.72
Classification Report:
precision recall f1-score support
Confusion Matrix:
[[ 42 0 1 1 0 0 0 1 4 1 2 6 0 2 3 68 5 13
1 1]
[ 1 138 14 15 0 6 4 1 7 0 1 6 0 0 4 4 0 1
0 0]
[ 1 14 129 26 3 7 0 0 10 0 0 3 1 0 0 1 0 0
0 0]
[ 0 6 16 142 6 1 3 1 1 0 0 1 3 2 0 1 0 0
0 0]
[ 0 2 8 24 138 0 5 1 12 0 0 7 2 0 3 2 1 0
0 0]
[ 0 15 11 3 1 174 1 1 3 0 2 0 0 0 2 2 0 0
0 0]
[ 0 3 1 26 5 1 135 3 3 0 1 6 2 1 4 1 1 0
0 0]
[ 0 1 0 1 1 1 3 149 15 0 2 3 4 0 3 6 5 1
1 0]
[ 0 2 0 0 0 1 6 8 130 3 2 3 2 1 3 5 2 0
0 0]
[ 0 0 0 0 0 0 1 0 13 176 7 3 0 0 0 8 0 3
0 0]
[ 0 0 0 0 0 0 0 2 6 1 182 1 0 1 0 4 0 1
0 0]
[ 0 2 3 0 0 1 0 0 4 1 2 172 0 1 2 5 4 3
1 0]
[ 0 8 2 19 5 1 12 4 6 1 1 6 127 2 4 2 1 1
0 0]
[ 0 2 1 0 0 0 0 1 6 1 1 0 3 167 2 7 1 1
1 0]
[ 0 3 2 1 0 0 1 1 10 1 0 5 3 1 157 4 0 0
0 0]
[ 1 0 1 0 0 0 0 0 4 1 0 0 0 1 0 190 0 4
0 0]
[ 0 0 1 0 0 0 0 1 10 1 2 9 0 0 2 11 150 0
1 0]
[ 1 1 0 0 0 1 0 0 8 0 0 3 1 1 1 11 2 151
1 0]
[ 1 0 0 0 0 0 0 3 6 1 1 7 1 3 5 22 29 10
70 0]
[ 10 0 1 0 0 0 0 1 5 3 2 3 0 1 1 89 13 3
0 4]]
ssssssssssssss
14.Python program using speech recognition library to perform speech recognition.
import speech_recognition as s
sr=s.Recognizer()
print("listening........")
with s.Microphone() as m:
audio=sr.listen(m)
query=sr.recognize_google(audio,language='eng-in')
print(query)
output:
listening........
15.Python program using the PIL(pillow) library to illustrate basic image processing
operations like opening an image, resizing it, applying a filter, and saving the processed
image.