0% found this document useful (0 votes)
26 views13 pages

AML Programs

Uploaded by

poyip44209
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)
26 views13 pages

AML Programs

Uploaded by

poyip44209
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/ 13

1.

Implement a basic neuron model based on McCulloch and


Pitts' model and observe its behavior with different inputs.

# Define the McCulloch and Pitts neuron model


def mcculloch_pitts_neuron(inputs, weights, threshold):
# Calculate the weighted sum of inputs
weighted_sum = sum([x * w for x, w in zip(inputs, weights)])

# Output decision
if weighted_sum >= threshold:
return 1
else:
return 0

# Define input patterns


input_patterns = [
[0, 0],
[0, 1],
[1, 0],
[1, 1]
]

# Define weights and thresholds for different logical operations


# Example 1: AND gate
weights_and = [0.5, 0.5]
threshold_and = 1.0

# Example 2: OR gate
weights_or = [0.5, 0.5]
threshold_or = 0.5
# Test the neuron with different input patterns
print("McCulloch and Pitts Neuron - AND gate:")
for input_pattern in input_patterns:
result = mcculloch_pitts_neuron(input_pattern, weights_and, threshold_and)
print(f"Input: {input_pattern}, Output: {result}")

print("\nMcCulloch and Pitts Neuron - OR gate:")


for input_pattern in input_patterns:
result = mcculloch_pitts_neuron(input_pattern, weights_or, threshold_or)
print(f"Input: {input_pattern}, Output: {result}")
2. Train a perception model using the Perception Training Rule
on a simple binary classification problem and visualize the
decision boundary.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split

# Load the Iris dataset and select two features


iris = datasets.load_iris()
X = iris.data[:100, :2] # Use only the first two features
y = iris.target[:100] # Consider only two classes (0 and 1)

# Split the data into training and testing sets


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

# Initialize weights and learning rate


num_features = X_train.shape[1]
weights = np.random.rand(num_features + 1) # Include bias term
learning_rate = 0.01

# Perceptron Training Rule


num_epochs = 100
for epoch in range(num_epochs):
for i in range(X_train.shape[0]):
input_data = np.hstack((X_train[i], 1)) # Add bias
activation = np.dot(input_data, weights)
prediction = 1 if activation > 0 else 0
error = y_train[i] - prediction
weights += learning_rate * error * input_data

# Plot the data points and decision boundary


plt.scatter(X_train[y_train == 0][:, 0], X_train[y_train == 0][:, 1], c='blue', label='Class 0')
plt.scatter(X_train[y_train == 1][:, 0], X_train[y_train == 1][:, 1], c='red', label='Class 1')

x = np.linspace(X_train[:, 0].min(), X_train[:, 0].max(), 100)


y = -(weights[0] * x + weights[2]) / weights[1]
plt.plot(x, y, '-k', label='Decision Boundary')

plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.title('Perceptron Decision Boundary on Iris Dataset (2 Features)')
plt.show()
3. Implement a feed forward neural network with a
differentiable threshold unit and train it using the back-
propagation algorithm to classify handwritten digits from the
MNIST dataset.
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

# Load and preprocess the MNIST dataset


(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0 # Normalize pixel values to [0, 1]
y_train, y_test = to_categorical(y_train, 10), to_categorical(y_test, 10) # One-hot encode labels

# Define the Differentiable Threshold Unit (smooth step function)


class DifferentiableThresholdUnit(layers.Layer):
def call(self, inputs):
return tf.sigmoid(inputs * 10) # Smooth threshold with sharpness controlled by the factor (10)

# Build the neural network model


model = models.Sequential([
layers.Flatten(input_shape=(28, 28)),
layers.Dense(128, activation='relu'),
layers.Dense(64, activation='relu'),
layers.Dense(10),
DifferentiableThresholdUnit()
])

# Compile the model


model.compile(optimizer='adam',
loss='categorical_crossentropy', # Use categorical crossentropy for multi-class classification
metrics=['accuracy'])

# Train the model


model.fit(x_train, y_train,
epochs=10,
batch_size=32,
validation_data=(x_test, y_test))

# Evaluate the model


test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_accuracy * 100:.2f}%')
4. Apply bagging techniques to a dataset and analyze the impact on
the stability and generalization of the model.
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# Load the Iris dataset


data = load_iris()
X, y = data.data, data.target

# Split the data into a training and testing set


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

# Create a base classifier (e.g., Decision Tree)


base_classifier = DecisionTreeClassifier()

# Apply bagging by creating an ensemble of base classifiers


n_estimators = 10 # Number of base classifiers
bagging = BaggingClassifier(base_classifier, n_estimators=n_estimators, random_state=42)

# Fit the bagging model to the training data


bagging.fit(X_train, y_train)

# Make predictions using the bagging model


y_pred = bagging.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy of Bagging: {accuracy:.2f}")

# Compare the accuracy with the base classifier (without bagging)


base_classifier.fit(X_train, y_train)
y_base_pred = base_classifier.predict(X_test)
base_accuracy = accuracy_score(y_test, y_base_pred)
print(f"Accuracy of Base Classifier: {base_accuracy:.2f}")

# Analyze the impact on stability and generalization


# You can compare the variance of predictions using bagging vs. a single base classifier

bagging_predictions = []
base_classifier_predictions = []
n_iterations = 100

for _ in range(n_iterations):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=None)
# Bagging predictions
bagging.fit(X_train, y_train)
y_pred = bagging.predict(X_test)
bagging_predictions.append(y_pred)

# Base classifier predictions


base_classifier.fit(X_train, y_train)
y_base_pred = base_classifier.predict(X_test)
base_classifier_predictions.append(y_base_pred)

# Calculate the variance of predictions


bagging_variance = np.var(bagging_predictions)
base_classifier_variance = np.var(base_classifier_predictions)

print(f"Variance of Bagging Predictions: {bagging_variance:.2f}")


print(f"Variance of Base Classifier Predictions: {base_classifier_variance:.2f}")
5. Implement and experiment with AdaBoost Algorithm
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Create a synthetic dataset for classification


X, y = make_classification(n_samples=200, n_features=2, n_informative=2, n_redundant=0,
random_state=42)

# Split the dataset into training and testing sets


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

# Create a DecisionTreeClassifier as the base estimator


base_estimator = DecisionTreeClassifier(max_depth=1)

# Create an AdaBoostClassifier with the base estimator


adaboost = AdaBoostClassifier(base_estimator=base_estimator, n_estimators=50, random_state=42)

# Fit the AdaBoost model to the training data


adaboost.fit(X_train, y_train)

# Make predictions on the test data


y_pred = adaboost.predict(X_test)

# Calculate accuracy on the test data


accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")

# Visualization: Plot the decision boundary


xx, yy = np.meshgrid(np.arange(X[:, 0].min() - 1, X[:, 0].max() + 1, 0.01),
np.arange(X[:, 1].min() - 1, X[:, 1].max() + 1, 0.01))
Z = adaboost.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.contourf(xx, yy, Z, alpha=0.8)


plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=plt.cm.RdBu, marker='o')
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.title("AdaBoost Decision Boundary")
plt.show()
6. Implement and analyze basic genetic algorithms for optimization
import random

# Define the objective function to be optimized


def objective_function(x):
return -x**2 + 4*x

# GA Parameters
population_size = 100
num_generations = 50
mutation_rate = 0.1

# Initialization
population = [random.uniform(-5, 5) for _ in range(population_size)]

# Main Genetic Algorithm Loop


for generation in range(num_generations):
# Evaluate the fitness of each individual in the population
fitness_values = [objective_function(x) for x in population]

# Select parents for reproduction (using roulette wheel selection)


selected_parents = []
total_fitness = sum(fitness_values)
for _ in range(population_size):
rand_val = random.uniform(0, total_fitness)
cumulative_fitness = 0
for i, fitness in enumerate(fitness_values):
cumulative_fitness += fitness
if cumulative_fitness >= rand_val:
selected_parents.append(population[i])
break

# Create the next generation (crossover and mutation)


new_population = []
for _ in range(population_size):
parent1 = random.choice(selected_parents)
parent2 = random.choice(selected_parents)
crossover_point = random.randint(1, len(str(parent1)) - 1)
child = float(str(parent1)[:crossover_point] + str(parent2)[crossover_point:])

# Apply mutation
if random.random() < mutation_rate:
mutation = random.uniform(-0.5, 0.5)
child += mutation

new_population.append(child)
population = new_population

# Find the best solution


best_solution = max(population, key=objective_function)
best_fitness = objective_function(best_solution)

print(f"Best solution: x = {best_solution}, f(x) = {best_fitness}")


7. Build a simple recommendation system using collaborative
filtering and evaluate its effectiveness by measuring the precision
and recall of the recommendations.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score, recall_score

# Load the MovieLens dataset


movies = pd.read_csv('movies.csv')
ratings = pd.read_csv('ratings.csv')

# Merge movies and ratings data


movie_ratings = pd.merge(ratings, movies, on='movieId')

# Create a user-item matrix


user_movie_matrix = movie_ratings.pivot_table(index='userId', columns='title', values='rating')

# Fill missing values with 0


user_movie_matrix = user_movie_matrix.fillna(0)

# Split the data into a training set and a test set


train_data, test_data = train_test_split(user_movie_matrix, test_size=0.2)

# User-based collaborative filtering


def user_based_collaborative_filtering(user_id, movie_title):
# Find users who rated the same movie
movie_ratings = user_movie_matrix[movie_title]
similar_users = user_movie_matrix.corrwith(movie_ratings)

# Remove NaN values and sort by similarity


similar_users = similar_users.dropna()
similar_users = similar_users.sort_values(ascending=False)

# Exclude the active user (if in the list)


if user_id in similar_users:
similar_users = similar_users.drop(user_id)

# Filter out movies that the user has already rated


user_ratings = user_movie_matrix.loc[user_id]
similar_users = similar_users.drop(user_ratings[user_ratings > 0].index)

return similar_users.index[:10]

# Evaluate the recommendation system


def evaluate_recommendations(user_id, test_data):
actual_movies_watched = test_data.columns[test_data.loc[user_id] > 0]
recommendations = user_based_collaborative_filtering(user_id, actual_movies_watched[0])

# Calculate precision and recall


intersection = len(set(recommendations) & set(actual_movies_watched))
precision = intersection / len(recommendations)
recall = intersection / len(actual_movies_watched)

return precision, recall

# Test the recommendation system for a user


user_id = 1
precision, recall = evaluate_recommendations(user_id, test_data)
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
8. Apply Q-Learning algorithm to train an agent in a simple grid
world environment and observe its learning progress.
import numpy as np
import random

# Define the grid world environment


grid_world = np.array([
[0, 0, 0, 0, 0],
[0, -1, 0, -1, 0],
[0, 0, 0, 0, 0],
[0, -1, 0, -1, 0],
[0, 0, 0, 0, 1]
]) # 0 represents empty cells, -1 represents obstacles, and 1 is the goal state

# Define Q-Learning parameters


learning_rate = 0.1
discount_factor = 0.9
exploration_prob = 0.2
num_episodes = 1000

# Initialize the Q-table


num_states = grid_world.size
num_actions = 4 # 4 possible actions: up, down, left, right
q_table = np.zeros((num_states, num_actions)) # Fixed missing parenthesis here

# Define state and action mapping functions


def state_to_index(state):
return state[0] * grid_world.shape[1] + state[1]

def index_to_state(index):
row = index // grid_world.shape[1]
col = index % grid_world.shape[1]
return (row, col)

# Define possible actions (up, down, left, right)


actions = [(0, 1), (0, -1), (1, 0), (-1, 0)]

# Q-Learning algorithm
for episode in range(num_episodes):
state = (0, 0) # Start state
while state != (4, 4): # Continue until reaching the goal state
current_state_index = state_to_index(state)
# Choose an action with epsilon-greedy policy
if random.uniform(0, 1) < exploration_prob:
action = random.choice(range(num_actions)) # Exploration
else:
action = np.argmax(q_table[current_state_index]) # Exploitation

# Take the chosen action and observe the next state and reward
new_state = (state[0] + actions[action][0], state[1] + actions[action][1])
if new_state[0] < 0 or new_state[0] >= grid_world.shape[0] or new_state[1] < 0 or
new_state[1] >= grid_world.shape[1] or grid_world[new_state] == -1:
# If the new state is outside the grid or is an obstacle, penalize the agent
reward = -1
new_state = state
else:
reward = grid_world[new_state]

new_state_index = state_to_index(new_state)

# Update the Q-value for the current state-action pair


q_table[current_state_index][action] = (1 - learning_rate) *
q_table[current_state_index][action] + \
learning_rate * (reward + discount_factor *
np.max(q_table[new_state_index]))

state = new_state

# Once training is complete, follow the learned Q-table to find the optimal path from the
start to the goal state
state = (0, 0)
path = [state]
while state != (4, 4):
state_index = state_to_index(state)
action = np.argmax(q_table[state_index])
new_state = (state[0] + actions[action][0], state[1] + actions[action][1])
path.append(new_state)
state = new_state

# Print the learned path


for step, state in enumerate(path):
print(f"Step {step}: {state}")
9. Implement a feed forward neural network with a differentiable
threshold unit and train it using the back-propagation algorithm to
classify handwritten digits from the MNIST dataset.
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

# Load and preprocess the MNIST dataset


(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0 # Normalize pixel values to [0, 1]
y_train, y_test = to_categorical(y_train, 10), to_categorical(y_test, 10) # One-hot encode labels

# Define the Differentiable Threshold Unit (smooth step function)


class DifferentiableThresholdUnit(layers.Layer):
def call(self, inputs):
return tf.sigmoid(inputs * 10) # Smooth threshold with sharpness controlled by the factor (10)

# Build the neural network model


model = models.Sequential([
layers.Flatten(input_shape=(28, 28)),
layers.Dense(128, activation='relu'),
layers.Dense(64, activation='relu'),
layers.Dense(10),
DifferentiableThresholdUnit()
])

# Compile the model


model.compile(optimizer='adam',
loss='categorical_crossentropy', # Use categorical crossentropy for multi-class classification
metrics=['accuracy'])

# Train the model


model.fit(x_train, y_train,
epochs=10,
batch_size=32,
validation_data=(x_test, y_test))

# Evaluate the model


test_loss, test_accuracy = model.evaluate(x_test, y_test)
print(f'Test accuracy: {test_accuracy * 100:.2f}%')

You might also like