Posts

Showing posts from September, 2022

Checking Vowels in String

 Checking Vowels in String def CheckVovels(var): Vovels=['a','e','i','o','u','A','E','I','O','U'] count=0 for i in range(0,len(Vovels),1): if Vovels[i]==var: return True else: count=count+1 if count>0: return False text=input("Enter something about you:") List=text.split(); for i in range(0,len(List),1): if CheckVovels(List[i][0]): List[i]=List[i]+"hey" else: temp1=List[i]; temp2=List[i][0]; temp3=temp1.replace(temp2,''); List[i]=temp3 List[i]=List[i]+"hey" print(List)

Reflex Cleaning Agent

Reflex Cleaning Agent  percept=["dirty","dirty"] action=["Move Right","Move Left","Clean Dirt"] class Agent: def __init__(self): self.position=0 self.currentAction=0 self.runAgent() def runAgent(self): self.getAction(percept[0]) def getAction(self,cPercept): if(cPercept=="dirty"): self.currentAction=action[2] self.updateRooms() elif(cPercept=="clean"): if(self.position==0): self.currentAction=action[0] self.updatePosition() elif(self.position==1): self.currentAction=action[1] self.updatePosition() def updatePosition(self): if(self.currentAction==action[0]): self.position=1 self.getAction(percept[1]) elif(self.currentAction==action[1]): self.position=0 def updateRooms(self):

Principle Component Analysis (PCA) without using function

 PCA without using function import matplotlib.pyplot as plt from sklearn import datasets from sklearn.preprocessing import MinMaxScaler import numpy as np iris=datasets.load_iris() data=np.array(iris.data[:10,:2]) scaler=MinMaxScaler() scaler.fit(data) data=scaler.transform(data) print(data) irisDataset=data irisDataset_mean = irisDataset - np.mean(irisDataset , axis = 0) cov_mat = np.cov(irisDataset_mean , rowvar = False) eigen_values , eigen_vectors = np.linalg.eigh(cov_mat) sorted_index = np.argsort(eigen_values)[::-1] sorted_eigenvalue = eigen_values[sorted_index] sorted_eigenvectors = eigen_vectors[:,sorted_index] n_components = 2 eigenvector_subset = sorted_eigenvectors[:,0:n_components] irisDataset_reduced = np.dot(eigenvector_subset.transpose(),irisDataset_mean.transpose()).transpose() print(irisDataset_reduced) plt.plot(data[:,0],data[:,1]) plt.title('Before PCA') plt.show() plt.plot(irisDataset_reduced[:,0],irisDataset_reduced[:,1]) plt.title('After PCA') pl

PCA using build in function

 PCA using build in function import matplotlib.pyplot as plt from sklearn import datasets from sklearn.preprocessing import MinMaxScaler import numpy as np iris=datasets.load_iris() data=np.array(iris.data[:10,:2]) scaler=MinMaxScaler() scaler.fit(data) data=scaler.transform(data) print("old data ",data) plt.plot(data[:,0],data[:,1]) plt.title('Before PCA') plt.show() from sklearn.decomposition import PCA pca=PCA(n_components=2) pca.fit(data) newData=pca.transform(data) print(data.shape,newData.shape) print("new Data ",newData) plt.plot(data[:,0],data[:,1]) plt.title('After PCA') plt.show()

Principle Component Analysis (PCA)

 Principle Component Analysis (PCA) This is Machine Learning based algorithm lies under the Unsupervised Learning. This is the simplest code using python import matplotlib.pyplot as plt from sklearn import datasets import numpy as np iris=datasets.load_iris() data=np.array(iris.data[:10,:2]) xdata=data[:,0] ydata=data[:,1] n=len(xdata) print(n) x=np.mean(xdata) y=np.mean(ydata) c=np.cov(xdata,ydata) values, vector = np.linalg.eig(c) values[::-1].sort() vector_subset = vector[:,0:2] values_subset = np.dot(vector_subset.transpose(),values.transpose()).transpose() print("Vector Subset is ",vector_subset) print("Values Subset ",values_subset) plt.plot(values,vector[:,0]) plt.title('After PCA') plt.xlabel('PC1') plt.ylabel('PC2') plt.legend() plt.show() plt.plot(xdata,ydata) plt.title('Before PCA') plt.xlabel('PC1') plt.ylabel('PC2') plt.legend() plt.show()

AI Based Packman Game

 AI Based Packman Game This is completely scratch code for packman using very simple manner using python. We can also say that this is a Mini Project based on AI. Even I did not use the Numpy in this Code. actions=["Move Right","Move Left","Move Up","Move Down","Eat Food","Eat power pallet","Avoid Gost","Eat Ghost"] percepts=[['p','pp','f','pp'], ['f','g','f','pp'], ['pp','f','pp','g'], ['pp','f','g','f']] wlr1=[0,1] wlr2=[2,1] wlr3=[3,0] wll1=[0,2] wll2=[2,2] wll3=[3,1] wlr=[wlr1,wlr2,wlr3] wll=[wll1,wll2,wll3] m=len(percepts) n=len(percepts[0]) wlrm=len(wlr) wllm=len(wll) wlm=len(percepts[0]) class Agent(): def __init__(self): self.currentAction=0 self.r=0 self.c=0 self.power=False self.chwlr=False

Min Max Itrative Algorithm

 Min Max Algorithm This is backtracking algorithm by nature but I try to make this algorithm using iterative manner using python. We mostly use this Min Max algorithm in games. This is scratch code using Numpy import numpy as np import math arr=np.array([-1,8,-3,-1,2,1,-3,4]) k=0 def MinMax(temp1,minmax,h): for j in range(0,h): k=0 for i in range(0,len(temp1)-1,2): if(minmax==1): temp1[k]=max(temp1[i],temp1[i+1]) k=k+1 else: temp1[k]=min(temp1[i],temp1[i+1]) k=k+1 if minmax==1: minmax=0 else: minmax=1 return temp1[0] print(MinMax(arr,1,3))

K-Mean Clustering using Hamming Distance

 K-Mean Clustering using Hamming Distance I already uploaded the post for K-Mean Clustering but that was using Euclidean distance and now I made this algorithm using Hamming Distance. This is completely scratch code which I made using Numpy. This will be good if you understand the topic of k-mean clustering.  import numpy as np var=np.array([["small","green","Irregular","no"],["large","red","irregular","yes"],["large","red","circle","yes"], ["large","green","circle","no"],["large","green","irregular","no"],["small","red","circle","yes"], ["large","green","irregular","no"],["small","red","irrgular","no"],["small","green",&q

K-Mean Clustering

 K-Mean Clustering K-mean clustering is also one of the most popular algorithm of Machine Learning lies in unsupervised learning. It works by making clusters. You have to clear the concept about the k-mean clustering before understanding this code. Furthermore, this algorithm is in scratch format using simple Numpy and also we using matplotlib for plotting the data.  import numpy as np import matplotlib.pyplot as plt var=np.array([[1.0,1.0],[1.5,2.0],[3.0,4.0],[5.0,7.0],[3.5,5.0],[4.5,5.0],[3.5,4.5]]) if len(var)>1: centroid1=var[0] centroid2=var[1] x=y=0 plot1=np.array([centroid1]) plot2=np.array([centroid2]) x+=1 y+=1 for i in range(2,len(var)): ed1=np.sqrt(((centroid1[0]-var[i][0])**2)+((centroid1[1]-var[i][1])**2)) ed2=np.sqrt(((centroid2[0]-var[i][0])**2)+((centroid2[1]-var[i][1])**2)) if(ed1>ed2): centroid2[0]=(centroid2[0]+var[i][0])/2 centroid2[1]=(centroid2[1]+var[i][1])/2 plot

Constraint Satisfaction Problem

 Constraint Satisfaction Problem  This is one of the most popular Machine Learning based algorithm and code for this is given below in python using Numpy in scratch format. import numpy as np rooms={'A':'','B':'A','C':'B','D':'','E':'','F':'D'} person={'x':['P'],'y':['P'],'z':['P'],'a':['Q'],'b':['Q'],'s':['R']} contory=['P','Q','R'] hotelLiving={'A':[],'B':[],'C':[],'D':[],'E':[],'F':[]} count=0 for i in hotelLiving: for j in person: for z in rooms: if(rooms[z]==''): for a in hotelLiving: if hotelLiving[a]==[j]: count+=1 break if count==0: if hotelLiving[z]==[]: