AI Practical (1 to 9) - Without NumPy & Matplotlib
------------------------------------------------------------
1. Pandas – Data Preprocessing
------------------------------
import pandas as pd
data = {"Name":["Ram",None,"Mohan"], "Age":[20,None,30]}
df = [Link](data)
df["Name"] = df["Name"].fillna("Unknown")
df["Age"] = df["Age"].fillna(df["Age"].mean())
print(df)
2. BFS – Breadth First Search
-----------------------------
from collections import deque
graph={'A':['B','C'],'B':['D','E'],'C':['F'],'D':[], 'E':[], 'F':[]}
def bfs(start):
visited=set()
queue=deque([start])
while queue:
node=[Link]()
if node not in visited:
print(node,end=" ")
[Link](node)
[Link](graph[node])
bfs('A')
3. DFS – Depth First Search
---------------------------
graph={'A':['B','C'],'B':['D','E'],'C':['F'],'D':[], 'E':[], 'F':[]}
def dfs(node,visited=set()):
if node not in visited:
print(node,end=" ")
[Link](node)
for x in graph[node]:
dfs(x,visited)
dfs('A')
4. A* Search Algorithm
----------------------
from queue import PriorityQueue
graph={'A':{'B':1,'C':4},'B':{'D':2},'C':{'E':3},'D':{},'E':{}}
heuristic={'A':5,'B':3,'C':4,'D':0,'E':0}
def astar(start,goal):
pq=PriorityQueue()
[Link]((0,start))
visited=set()
while not [Link]():
_,node=[Link]()
if node==goal:
print("Reached:",node)
break
if node not in visited:
print(node)
[Link](node)
for child in graph[node]:
cost=graph[node][child]+heuristic[child]
[Link]((cost,child))
astar('A','D')
5. Hill Climbing
----------------
import random
current=[Link](1,20)
print("Start:",current)
while True:
nxt=current+[Link]([-1,1])
if nxt>current:
current=nxt
print("Move:",current)
else:
break
print("Peak:",current)
6. Minimax Algorithm
--------------------
def minimax(depth,index,isMax,values,height):
if depth==height:
return values[index]
if isMax:
return max(minimax(depth+1,index*2,False,values,height),
minimax(depth+1,index*2+1,False,values,height))
else:
return min(minimax(depth+1,index*2,True,values,height),
minimax(depth+1,index*2+1,True,values,height))
values=[3,5,2,9]
print("Minimax:",minimax(0,0,True,values,2))
7. Alpha-Beta Pruning
---------------------
def alphabeta(depth,index,isMax,values,alpha,beta,height):
if depth==height:
return values[index]
if isMax:
best=-999
for i in range(2):
val=alphabeta(depth+1,index*2+i,False,values,alpha,beta,height)
best=max(best,val)
alpha=max(alpha,best)
if beta<=alpha:
break
return best
else:
best=999
for i in range(2):
val=alphabeta(depth+1,index*2+i,True,values,alpha,beta,height)
best=min(best,val)
beta=min(beta,best)
if beta<=alpha:
break
return best
values=[3,5,2,9]
print("Alpha-Beta:",alphabeta(0,0,True,values,-999,999,2))
8. Linear Regression (No NumPy)
-------------------------------
x=[1,2,3]; y=[2,4,5]; n=len(x)
m=(n*sum(x[i]*y[i] for i in range(n))-sum(x)*sum(y))/(n*sum(i*i for i in x)-(sum(x))**2)
c=(sum(y)-m*sum(x))/n
print("Slope:",m)
print("Intercept:",c)
print("Prediction(4):",m*4+c)
9. K-Means Clustering (No NumPy)
--------------------------------
import random
data=[2,4,6,8,10]
k1=[Link](data)
k2=[Link](data)
for _ in range(3):
c1=[x for x in data if abs(x-k1)<abs(x-k2)]
c2=[x for x in data if x not in c1]
k1=sum(c1)/len(c1)
k2=sum(c2)/len(c2)
print("Cluster1:",c1)
print("Cluster2:",c2)