Advance AI and ML LAB
Advance AI and ML LAB
Advance AI and ML LAB
iris=datasets.load_iris()
#random_state=0
for i in range(len(iris.target_names)):
print("Label", i , "-",str(iris.target_names[i]))
classifier = KNeighborsClassifier(n_neighbors=2)
classifier.fit(x_train, y_train)
y_pred=classifier.predict(x_test)
for r in range(0,len(x_test)):
OUTPUT:
Label 0 - setosa
Label 1 - versicolor
Label 2 - virginica
data = pd.read_csv("C:\\Users\\Ravi\\Downloads\\iris.csv")
kmeans_labels = kmeans.fit_predict(features)
em_labels = em.fit_predict(features)
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.title('K-means Clustering')
plt.colorbar()
plt.subplot(1, 2, 2)
plt.title('EM Clustering')
plt.colorbar()
plt.tight_layout()
plt.show()
kmeans_inertia = kmeans.inertia_
OUTPUT:
import numpy as np
x0 = [1, x0]
X = [[1, i] for i in X]
X = np.asarray(X)
beta = np.linalg.pinv(xw @ X) @ xw @ Y @ x0
return beta
def draw(tau):
plt.show()
X = np.linspace(-3, 3, num=1000)
domain = X
Y = np.log(np.abs(X ** 2 - 1) + .5)
draw(10)
draw(0.1)
draw(0.01)
draw(0.001)
OUTPUT:
Lab 4 : Build an Artificial Neural Network by implementing the Backpropagation algorithm
and test the same using appropriate data sets.
import numpy as np
y = y/100
#Sigmoid Function
def derivatives_sigmoid(x):
return x * (1 - x)
#Variable initialization
wh=np.random.uniform(size=(inputlayer_neurons,hiddenlayer_neurons))
bh=np.random.uniform(size=(1,hiddenlayer_neurons))
wout=np.random.uniform(size=(hiddenlayer_neurons,output_neurons))
bout=np.random.uniform(size=(1,output_neurons))
for i in range(epoch):
#Forward Propogation
hinp1=np.dot(X,wh)
hinp=hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1=np.dot(hlayer_act,wout)
output = sigmoid(outinp)
#Backpropagation
EO = y-output
outgrad = derivatives_sigmoid(output)
EH = d_output.dot(wout.T)
hiddengrad = derivatives_sigmoid(hlayer_act)
d_hiddenlayer = EH * hiddengrad
wh += X.T.dot(d_hiddenlayer) *lr
OUTPUT:
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.89571283]
[0.88239245]
[0.89153673]]
Lab .5 Demonstrate Genetic algorithm by taking a suitable data for any simple application
import random
import numpy as np
from deap import base, creator, tools, algorithms # Add 'algorithms' import here
num_cities = 10
def tsp_distance(individual):
distance = 0
for i in range(len(individual)):
city_start = individual[i]
return distance,
population_size = 100
num_generations = 50
mutation_rate = 0.1
crossover_rate = 0.8
toolbox = base.Toolbox()
toolbox.register("evaluate", tsp_distance)
toolbox.register("mate", tools.cxOrdered)
population = toolbox.population(n=population_size)
stats.register("min", np.min)
stats.register("avg", np.mean)
best = tools.HallOfFame(1)
best_route = best[0]
best_distance = tsp_distance(best_route)[0]
plt.figure(figsize=(6, 6))
best_route_coords = city_coords[best_route]
plt.xlabel('X-coordinate')
plt.ylabel('Y-coordinate')
plt.legend()
plt.show()
OUTPUT:
import numpy as np
import random
Q = np.zeros_like(R)
gamma = 0.8
alpha = 0.1
epsilon = 0.1
state = random.randint(0, 5)
while state != 5:
action = random.randint(0, 5)
else:
action = np.argmax(Q[state])
next_state = action
state = next_state
state = 2
steps = [state]
while state != 5:
action = np.argmax(Q[state])
state = action
steps.append(state)
OUTPUT:
Optimal Path: [2, 0, 1, 5]