0% found this document useful (0 votes)
11 views43 pages

Ann Experiential Learning

The document outlines various machine learning tasks including spectral remote sensing data classification using Ordinary Least Squares, Linear Discriminant Analysis for model tuning, and the implementation of the Perceptron algorithm for binary classification. It also discusses building an employee churn prediction model with a Multi-layer perceptron and evaluating its performance with different learning rates, as well as analyzing the efficiency of a Radial Basis Function network for classification tasks. Each section includes code snippets and outputs demonstrating the implementation and results of the models.

Uploaded by

anuarg chowdary
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)
11 views43 pages

Ann Experiential Learning

The document outlines various machine learning tasks including spectral remote sensing data classification using Ordinary Least Squares, Linear Discriminant Analysis for model tuning, and the implementation of the Perceptron algorithm for binary classification. It also discusses building an employee churn prediction model with a Multi-layer perceptron and evaluating its performance with different learning rates, as well as analyzing the efficiency of a Radial Basis Function network for classification tasks. Each section includes code snippets and outputs demonstrating the implementation and results of the models.

Uploaded by

anuarg chowdary
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/ 43

EXPERIENTIAL LEARNING

1.
a.Classify spectral remote sensing data using Ordinary Least Squares and estimate
the confusion matrix.

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.metrics import confusion_matrix, classification_report

X = np.random.rand(100, 5)
y = np.random.randint(0, 3, 100)

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

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

encoder = OneHotEncoder(sparse_output=False)
y_train_oh = encoder.fit_transform(y_train.reshape(-1, 1))

weights = np.linalg.pinv(X_train.T @ X_train) @ X_train.T @ y_train_oh

y_pred_prob = X_test @ weights


y_pred = np.argmax(y_pred_prob, axis=1)

cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", cm)
print("\nClassification Report:\n", classification_report(y_test, y_pred))

OUTPUT::

Confusion Matrix:
[[2 3 3]
[2 5 5]
[2 6 2]]
Classification Report:
precision recall f1-score support

0 0.33 0.25 0.29 8


1 0.36 0.42 0.38 12
2 0.20 0.20 0.20 10

accuracy 0.30 30
macro avg 0.30 0.29 0.29 30
weighted avg 0.30 0.30 0.30 30

b.Fit, evaluate, and make predictions with the Linear Discriminant Analysis model.
Also tune the hyperparameters of the model.

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

# Generate a synthetic dataset


np.random.seed(42)
X = np.random.rand(100, 5)
y = np.random.randint(0, 3, 100)

# Check class distribution


print("Class distribution in y:", np.bincount(y))

# Ensure balanced class distribution in train-test split


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

# Standardize the features


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Train LDA model


lda = LinearDiscriminantAnalysis()
lda.fit(X_train, y_train)

# Make predictions
y_pred = lda.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred, zero_division=1)) #
Avoids warnings

# Define parameter grid with conditional shrinkage parameter


param_grid = [
{'solver': ['svd']}, # 'svd' solver does not support shrinkage
{'solver': ['lsqr', 'eigen'], 'shrinkage': ['auto', 0.1, 0.5, 0.9]}
]

# Perform Grid Search with cross-validation


lda_tuned = GridSearchCV(
LinearDiscriminantAnalysis(),
param_grid,
scoring='accuracy',
cv=5
)
lda_tuned.fit(X_train, y_train)

# Print best parameters and cross-validation accuracy


print("\nBest Parameters:", lda_tuned.best_params_)
print("Best Cross-Validation Accuracy:", lda_tuned.best_score_)

# Make predictions with the best model


y_pred_tuned = lda_tuned.predict(X_test)
print("\nTuned Model Accuracy:", accuracy_score(y_test, y_pred_tuned))
print("Confusion Matrix for Tuned Model:\n", confusion_matrix(y_test, y_pred_tuned))
print("\nClassification Report for Tuned Model:\n", classification_report(y_test, y_pred_tuned,
zero_division=1)) # Avoids warnings
OUTPUT::

Class distribution in y: [42 30 28]


Accuracy: 0.43333333333333335
Confusion Matrix:
[[6 2 5]
[4 2 3]
[3 0 5]]

Classification Report:
precision recall f1-score support

0 0.46 0.46 0.46 13


1 0.50 0.22 0.31 9
2 0.38 0.62 0.48 8

accuracy 0.43 30
macro avg 0.45 0.44 0.42 30
weighted avg 0.45 0.43 0.42 30

Best Parameters: {'solver': 'svd'}


Best Cross-Validation Accuracy: 0.4142857142857143

Tuned Model Accuracy: 0.43333333333333335


Confusion Matrix for Tuned Model:
[[6 2 5]
[4 2 3]
[3 0 5]]

Classification Report for Tuned Model:


precision recall f1-score support

0 0.46 0.46 0.46 13


1 0.50 0.22 0.31 9
2 0.38 0.62 0.48 8

accuracy 0.43 30
macro avg 0.45 0.44 0.42 30
weighted avg 0.45 0.43 0.42 30

c.Implementing the Perceptron Algorithm for binary classification task and plot the
decision boundary.

import numpy as np

import matplotlib.pyplot as plt

np.random.seed(42)

X = np.random.randn(100, 2)

y = np.where(X[:, 0] + X[:, 1] > 0, 1, -1)

class Perceptron:

def _init_(self, learning_rate=0.01, max_iter=1000):

self.learning_rate = learning_rate

self.max_iter = max_iter

def fit(self, X, y):

n_samples, n_features = X.shape

self.weights = np.zeros(n_features)

self.bias = 0

for _ in range(self.max_iter):

for idx, x_i in enumerate(X):

if y[idx] * (np.dot(x_i, self.weights) + self.bias) <= 0:

self.weights += self.learning_rate * y[idx] * x_i

self.bias += self.learning_rate * y[idx]

def predict(self, X):

linear_output = np.dot(X, self.weights) + self.bias


return np.sign(linear_output)

model = Perceptron(learning_rate=0.1, max_iter=1000)

model.fit(X, y)

def plot_decision_boundary(X, y, model):

plt.scatter(X[:, 0], X[:, 1], c=y, cmap='bwr', edgecolors='k', s=50)

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1

y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1

xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100))

Z = model.predict(np.c_[xx.ravel(), yy.ravel()])

Z = Z.reshape(xx.shape)

plt.contourf(xx, yy, Z, alpha=0.2, cmap='bwr')

plt.xlabel("Feature 1")

plt.ylabel("Feature 2")

plt.title("Perceptron Decision Boundary")

plt.show()

plot_decision_boundary(X, y, model)
OUTPUT::
2.Build an employee churn prediction model using Multi-layer perceptron and
evaluate the model performance with different learning rate.

file_name = "your_dataset.csv"
data = pd.read_csv(file_name)

print(data.head())

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.metrics import accuracy_score
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Input

file_path = "your_dataset.csv"
data = pd.read_csv(file_path)

target_column = 'churn'

if target_column not in data.columns:


raise ValueError(f"Column '{target_column}' not found in the dataset.")

data = data.dropna(subset=[target_column])

X = data.drop(columns=[target_column])
y = data[target_column]

X = pd.get_dummies(X, drop_first=True)

X.fillna(X.mean(), inplace=True)

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)

X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_encoded, test_size=0.2,


random_state=42)

def build_and_train_mlp(learning_rate):
model = Sequential([
Input(shape=(X_train.shape[1],)),
Dense(32, activation='relu'),
Dense(16, activation='relu'),
Dense(len(np.unique(y_encoded)), activation='softmax')
])
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2,
verbose=0)
return model, history

learning_rates = [0.001, 0.01, 0.1]


results = {}

for lr in learning_rates:
print(f"\nTraining with learning rate: {lr}")
model, history = build_and_train_mlp(learning_rate=lr)

y_pred = np.argmax(model.predict(X_test), axis=1)

acc = accuracy_score(y_test, y_pred)

print(f"Accuracy: {acc:.4f}")

results[lr] = {'accuracy': acc, 'history': history}

print("Training complete.")

OUTPUT::

Training with learning rate: 0.001


1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 70ms/step
Accuracy: 0.0000

Training with learning rate: 0.01


1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 70ms/step
Accuracy: 1.0000

Training with learning rate: 0.1


1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 75ms/step
Accuracy: 1.0000
Training complete.

import matplotlib.pyplot as plt


# Compare Accuracy
for lr, result in results.items():
print(f"Learning Rate: {lr} -> Accuracy: {result['accuracy']:.4f}")

# Plot Loss Curves


plt.figure(figsize=(10, 5))

for lr, result in results.items():


history = result['history']
plt.plot(history.history['loss'], label=f"Train Loss (LR={lr})")
plt.plot(history.history['val_loss'], label=f"Val Loss (LR={lr})", linestyle="dashed")

plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.title("Training vs Validation Loss")
plt.legend()
plt.show()

OUTPUT::
3.Analyze the efficiency of a Radial Basis function network for a classificationtask and assess its convergence
ability.

import numpy as np

import matplotlib.pyplot as plt

from sklearn.cluster import KMeans

from sklearn.metrics import accuracy_score, classification_report

from sklearn.model_selection import train_test_split

from sklearn.datasets import make_classification

from sklearn.preprocessing import StandardScaler

from sklearn.linear_model import LogisticRegression

from sklearn.svm import SVC

X, y = make_classification(n_samples=500, n_features=2, n_informative=2,

n_redundant=0, n_clusters_per_class=1, random_state=42)

X = StandardScaler().fit_transform(X)

y = y.ravel()

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

log_reg = LogisticRegression(random_state=42)

log_reg.fit(X_train, y_train)

y_pred_log = log_reg.predict(X_test)

acc_log = accuracy_score(y_test, y_pred_log)

print("Logistic Regression Test Accuracy:", acc_log)

print("Classification Report:", classification_report(y_test, y_pred_log))

svm = SVC(random_state=42)

svm.fit(X_train, y_train)

y_pred_svm = svm.predict(X_test)
acc_svm = accuracy_score(y_test, y_pred_svm)

print("SVM Test Accuracy:", acc_svm)

print("Classification Report:", classification_report(y_test, y_pred_svm))

def train_rbf_closedform(X_train, y_train, X_test, y_test, n_clusters, sigma_factor=1.0):

kmeans = KMeans(n_clusters=n_clusters, random_state=42, n_init=10).fit(X_train)

centers = kmeans.cluster_centers_

base_sigmas = []

for i, c in enumerate(centers):

other_centers = np.delete(centers, i, axis=0)

nearest_distance = np.min(np.linalg.norm(other_centers - c, axis=1))

base_sigmas.append(nearest_distance)

base_sigma = np.mean(base_sigmas)

sigma = sigma_factor * base_sigma

def rbf(x, c, sigma):

return np.exp(-np.linalg.norm(x - c, axis=1) ** 2 / (2 * sigma ** 2))

phi_train = np.array([rbf(X_train, c, sigma) for c in centers]).T

phi_test = np.array([rbf(X_test, c, sigma) for c in centers]).T

weights = np.linalg.pinv(phi_train).dot(y_train)

y_pred_train = (phi_train.dot(weights) > 0.5).astype(int)

y_pred_test = (phi_test.dot(weights) > 0.5).astype(int)

acc_train = accuracy_score(y_train, y_pred_train)

acc_test = accuracy_score(y_test, y_pred_test)

return acc_train, acc_test, centers, sigma, weights


n_clusters_baseline = 4

sigma_factor_baseline = 1.0

acc_train_rbf, acc_test_rbf, centers, sigma_used, weights = train_rbf_closedform(

X_train, y_train, X_test, y_test, n_clusters_baseline, sigma_factor_baseline

print("RBF Network Test Accuracy:", acc_test_rbf)

def train_rbf_iterative(phi_train, y_train, learning_rate=0.1, epochs=100):

weights = np.zeros(phi_train.shape[1])

errors = []

for epoch in range(epochs):

predictions = phi_train.dot(weights)

error = predictions - y_train

mse = np.mean(error ** 2)

errors.append(mse)

gradient = (2 / phi_train.shape[0]) * phi_train.T.dot(error)

weights = weights - learning_rate * gradient

return weights, errors

def get_rbf_features(X, centers, sigma):

def rbf(x, c, sigma):

return np.exp(-np.linalg.norm(x - c, axis=1) ** 2 / (2 * sigma ** 2))

return np.array([rbf(X, c, sigma) for c in centers]).T

phi_train_iter = get_rbf_features(X_train, centers, sigma_used)

weights_iter, training_errors = train_rbf_iterative(phi_train_iter, y_train, learning_rate=0.1,


epochs=100)
plt.figure(figsize=(8, 5))

plt.plot(training_errors, marker='o')

plt.xlabel("Epoch")

plt.ylabel("Mean Squared Error")

plt.title("Convergence of RBF Network Training")

plt.grid(True)

plt.show()

sigma_factors = [0.5, 0.75, 1.0, 1.25, 1.5, 2.0]

acc_tests_sigma = []

for sf in sigma_factors:

_, acc_test, _, _, _ = train_rbf_closedform(X_train, y_train, X_test, y_test,

n_clusters_baseline, sigma_factor=sf)

acc_tests_sigma.append(acc_test)

plt.figure(figsize=(8, 5))

plt.plot(sigma_factors, acc_tests_sigma, marker='o')

plt.xlabel("Sigma Factor")

plt.ylabel("Test Accuracy")

plt.title("Sensitivity of RBF Network to Sigma Factor")

plt.grid(True)

plt.show()

n_clusters_list = [2, 4, 6, 8, 10, 12]

acc_tests_nclusters = []

for n in n_clusters_list:

_, acc_test, _, _, _ = train_rbf_closedform(X_train, y_train, X_test, y_test,


n_clusters=n, sigma_factor=sigma_factor_baseline)

acc_tests_nclusters.append(acc_test)

plt.figure(figsize=(8, 5))

plt.plot(n_clusters_list, acc_tests_nclusters, marker='o')

plt.xlabel("Number of RBF Neurons")

plt.ylabel("Test Accuracy")

plt.title("Sensitivity of RBF Network to Number of Neurons")

plt.grid(True)

plt.show()

OUTPUT::

Logistic Regression Test Accuracy: 0.88

Classification Report: precision recall f1-score support

0 0.88 0.88 0.88 51

1 0.88 0.88 0.88 49

accuracy 0.88 100

macro avg 0.88 0.88 0.88 100

weighted avg 0.88 0.88 0.88 100

SVM Test Accuracy: 0.92

Classification Report: precision recall f1-score support

0 1.00 0.84 0.91 51

1 0.86 1.00 0.92 49


accuracy 0.92 100

macro avg 0.93 0.92 0.92 100

weighted avg 0.93 0.92 0.92 100

RBF Network Test Accuracy: 0.87


4. Evaluate the cross entropy error functions and compare its performance with the Log
Loss.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import log_loss

# Generate a synthetic dataset


X, y = make_classification(n_samples=1000, n_features=10, n_classes=2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train a logistic regression model


model = LogisticRegression()
model.fit(X_train, y_train)

# Predict probabilities
y_pred_proba = model.predict_proba(X_test)[:, 1]

# Cross-Entropy Error
def cross_entropy(y_true, y_pred_proba):
epsilon = 1e-15 # To avoid log(0)
y_pred_proba = np.clip(y_pred_proba, epsilon, 1 - epsilon)
ce = -np.mean(y_true * np.log(y_pred_proba) + (1 - y_true) * np.log(1 - y_pred_proba))
return ce

ce_error = cross_entropy(y_test, y_pred_proba)


print(f"Cross-Entropy Error: {ce_error:.4f}")

# Log Loss
log_loss_value = log_loss(y_test, y_pred_proba)
print(f"Log Loss: {log_loss_value:.4f}")

OUTPUT:
Cross-Entropy Error: 0.3779

Log Loss: 0.3779


5. Analyze the efficiency of L1 and L2 regularization techniques in resolving overfitting
issues in multi-layer neural network model.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.regularizers import l1, l2
# Generate synthetic data
X, y = make_classification(n_samples=1000, n_features=20, n_informative=15,
random_state=42)
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
# Define a function to build models
def build_model(regularizer=None):
model = Sequential([
Dense(64, activation='relu', input_dim=X_train.shape[1], kernel_regularizer=regularizer),
Dense(32, activation='relu', kernel_regularizer=regularizer),
Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return model

# Train models
no_reg_model = build_model()
l1_model = build_model(regularizer=l1(0.01))
l2_model = build_model(regularizer=l2(0.01))
history_no_reg = no_reg_model.fit(X_train, y_train, epochs=50, batch_size=32,
validation_data=(X_val, y_val), verbose=0)
history_l1 = l1_model.fit(X_train, y_train, epochs=50, batch_size=32, validation_data=(X_val,
y_val), verbose=0)
history_l2 = l2_model.fit(X_train, y_train, epochs=50, batch_size=32, validation_data=(X_val,
y_val), verbose=0)

# Plot results
def plot_history(histories, title):
plt.figure(figsize=(10, 6))
for name, history in histories.items():
plt.plot(history.history['val_loss'], label=f'{name}')
plt.title(title)
plt.xlabel('Epochs')
plt.ylabel('Validation Loss')
plt.legend()
plt.show()
plot_history({'No Regularization': history_no_reg, 'L1 Regularization': history_l1, 'L2
Regularization': history_l2},"Validation Loss Comparison")
OUTPUT:
6 A. Investigate the efficiency of SGD in terms of convergence speed and accuracy with
different learning rate and momentum values.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD

# Generate a synthetic dataset


X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

# Define a function to build and train models with SGD


def train_sgd(learning_rate, momentum):
model = Sequential([
Dense(64, activation='relu', input_dim=X_train.shape[1]),
Dense(32, activation='relu'),
Dense(1, activation='sigmoid')
])
optimizer = SGD(learning_rate=learning_rate, momentum=momentum)
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])

history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_data=(X_val,


y_val), verbose=0)
return history

# Test different learning rates and momentum values


learning_rates = [0.001, 0.01, 0.1, 0.5]
momentum_values = [0.0, 0.5, 0.9, 0.99]

results = {}
for lr in learning_rates:
for m in momentum_values:
history = train_sgd(lr, m)
results[(lr, m)] = history

# Plot validation loss for each configuration


plt.figure(figsize=(12, 8))
for (lr, m), history in results.items():
plt.plot(history.history['val_loss'], label=f"LR={lr}, Momentum={m}")

plt.title("Validation Loss for Different SGD Configurations")


plt.xlabel("Epochs")
plt.ylabel("Validation Loss")
plt.legend()
plt.show()

OUTPUT

6 B. Using stacked generalization, an ensemble method build a new model to best combine
the predictions from multiple existing models.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split, KFold
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report
import matplotlib.pyplot as plt

# Generate synthetic data


X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Define base models


base_models = [
RandomForestClassifier(n_estimators=100, random_state=42),
GradientBoostingClassifier(n_estimators=100, random_state=42),
SVC(probability=True, random_state=42)
]

# Train base models and collect predictions


kf = KFold(n_splits=5, shuffle=True, random_state=42)
base_predictions = np.zeros((X_train.shape[0], len(base_models)))

# Perform KFold Cross-Validation for base models


for i, model in enumerate(base_models):
for train_idx, val_idx in kf.split(X_train):
model.fit(X_train[train_idx], y_train[train_idx])
base_predictions[val_idx, i] = model.predict_proba(X_train[val_idx])[:, 1]

# Train meta-learner (logistic regression)


meta_model = LogisticRegression()
meta_model.fit(base_predictions, y_train)

# Evaluate base models performance on the test set


base_accuracies = []
for model in base_models:
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
base_accuracies.append(accuracy_score(y_test, y_pred))

# Evaluate stacked model performance


test_base_predictions = np.column_stack([
model.predict_proba(X_test)[:, 1] for model in base_models
])
final_predictions = meta_model.predict(test_base_predictions)
stacked_accuracy = accuracy_score(y_test, final_predictions)

# Print the performance of each base model


for i, model in enumerate(base_models):
print(f"Base Model {i+1} ({model.__class__.__name__}) Classification Report:")
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred))

# Print the accuracy of the stacked model


print(f"Stacked Model Accuracy: {stacked_accuracy:.4f}")

# Create a bar plot comparing the performance of base models and stacked model
model_names = ['Random Forest', 'Gradient Boosting', 'SVC', 'Stacked Model']
accuracies = base_accuracies + [stacked_accuracy]

plt.bar(model_names, accuracies, color=['blue', 'green', 'red', 'purple'])


plt.xlabel('Models')
plt.ylabel('Accuracy')
plt.title('Comparison of Model Accuracies')
plt.show()

OUTPUT:

Base Model 1 (RandomForestClassifier) Classification Report:


precision recall f1-score support

0 0.85 0.95 0.90 93


1 0.95 0.86 0.90 107

accuracy 0.90 200


macro avg 0.90 0.90 0.90 200
weighted avg 0.90 0.90 0.90 200

Base Model 2 (GradientBoostingClassifier) Classification Report:


precision recall f1-score support

0 0.86 0.97 0.91 93


1 0.97 0.86 0.91 107

accuracy 0.91 200


macro avg 0.91 0.91 0.91 200
weighted avg 0.92 0.91 0.91 200

Base Model 3 (SVC) Classification Report:


precision recall f1-score support

0 0.80 0.89 0.84 93


1 0.90 0.80 0.85 107

accuracy 0.84 200


macro avg 0.85 0.85 0.84 200
weighted avg 0.85 0.84 0.85 200

Stacked Model Accuracy: 0.8950


7A ) Using pruning algorithm reduce the complexity of a trained neural network.

Description:

Before executing the code install tensor flow in Anaconda prompt:


>> pip install tensorflow==2.14.0 (use tis command)

>> pip install tensorflow-model-optimization

Verify installation in jupyter notebook

import tensorflow as tf
print(tf.__version__)

CODE:

import os
import time
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam
import tensorflow_model_optimization.sparsity.keras as sparsity
import numpy as np

# Load dataset (MNIST for example)


(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
X_train, X_test = X_train / 255.0, X_test / 255.0 # Normalize

# Define original model


def create_model():
model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation='relu'),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
return model

# Train original model


model = create_model()
model.compile(optimizer=Adam(), loss='sparse_categorical_crossentropy', metrics=['accuracy'])

print("Training the original model...")


model.fit(X_train, y_train, epochs=5, batch_size=64, validation_split=0.2)
# Evaluate original model
original_loss, original_accuracy = model.evaluate(X_test, y_test)
print(f"Original Model Accuracy: {original_accuracy * 100:.2f}%")

# Pruning parameters
pruning_params = {
'pruning_schedule': sparsity.PolynomialDecay(initial_sparsity=0.2, final_sparsity=0.8,
begin_step=2000, end_step=10000)
}

# Define pruned model


pruned_model = Sequential([
Flatten(input_shape=(28, 28)),
sparsity.prune_low_magnitude(Dense(128, activation='relu'), **pruning_params),
sparsity.prune_low_magnitude(Dense(64, activation='relu'), **pruning_params),
sparsity.prune_low_magnitude(Dense(10, activation='softmax'), **pruning_params)
])

# Compile pruned model


pruned_model.compile(optimizer=Adam(), loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

# Train pruned model


print("Training the pruned model...")
pruned_model.fit(X_train, y_train, epochs=5, batch_size=64, validation_split=0.2,
callbacks=[sparsity.UpdatePruningStep()])

# Strip pruning for final deployment model


final_model = sparsity.strip_pruning(pruned_model)
final_model.compile(optimizer=Adam(), loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

# Evaluate final pruned model


print("Evaluating the final pruned model...")
pruned_loss, pruned_accuracy = final_model.evaluate(X_test, y_test)
print(f"Final Pruned Model Accuracy: {pruned_accuracy * 100:.2f}%")

# Measure model size before & after pruning


def get_model_size(model):
model.save("temp_model.h5")
size = os.path.getsize("temp_model.h5") / 1024 # Convert bytes to KB
os.remove("temp_model.h5")
return size

original_size = get_model_size(model)
pruned_size = get_model_size(final_model)
print(f"Original Model Size: {original_size:.2f} KB")
print(f"Pruned Model Size: {pruned_size:.2f} KB")

# Measure inference time


def measure_inference_time(model):
start_time = time.time()
model.evaluate(X_test, y_test, verbose=0)
return time.time() - start_time

original_inference_time = measure_inference_time(model)
pruned_inference_time = measure_inference_time(final_model)
print(f"Original Model Inference Time: {original_inference_time:.4f} sec")
print(f"Pruned Model Inference Time: {pruned_inference_time:.4f} sec")

OUTPUT:

Training the original model...


Epoch 1/5
750/750 [==============================] - 7s 5ms/step - loss: 0.3057 - accuracy:
0.9123 - val_loss: 0.1556 - val_accuracy: 0.9557
Epoch 2/5
750/750 [==============================] - 3s 5ms/step - loss: 0.1260 - accuracy:
0.9629 - val_loss: 0.1130 - val_accuracy: 0.9655
Epoch 3/5
750/750 [==============================] - 3s 5ms/step - loss: 0.0849 - accuracy:
0.9743 - val_loss: 0.1030 - val_accuracy: 0.9694
Epoch 4/5
750/750 [==============================] - 3s 5ms/step - loss: 0.0635 - accuracy:
0.9806 - val_loss: 0.0952 - val_accuracy: 0.9727
Epoch 5/5
750/750 [==============================] - 3s 5ms/step - loss: 0.0491 - accuracy:
0.9844 - val_loss: 0.0948 - val_accuracy: 0.9731
313/313 [==============================] - 1s 3ms/step - loss: 0.0794 - accuracy:
0.9764
Original Model Accuracy: 97.64%
Training the pruned model...
Epoch 1/5
750/750 [==============================] - 7s 6ms/step - loss: 0.3017 - accuracy:
0.9130 - val_loss: 0.1460 - val_accuracy: 0.9567
Epoch 2/5
750/750 [==============================] - 4s 5ms/step - loss: 0.1245 - accuracy:
0.9636 - val_loss: 0.1123 - val_accuracy: 0.9663
Epoch 3/5
750/750 [==============================] - 4s 5ms/step - loss: 0.0840 - accuracy:
0.9752 - val_loss: 0.0972 - val_accuracy: 0.9709
Epoch 4/5
750/750 [==============================] - 4s 5ms/step - loss: 0.0596 - accuracy:
0.9816 - val_loss: 0.1052 - val_accuracy: 0.9692
Epoch 5/5
750/750 [==============================] - 4s 5ms/step - loss: 0.0454 - accuracy:
0.9869 - val_loss: 0.0868 - val_accuracy: 0.9744
Evaluating the final pruned model...
313/313 [==============================] - 1s 3ms/step - loss: 0.0790 - accuracy:
0.9750
Final Pruned Model Accuracy: 97.50%
Original Model Size: 1315.17 KB
Pruned Model Size: 446.49 KB
Original Model Inference Time: 0.8202 sec
Pruned Model Inference Time: 0.8751 sec
7 B. Classify spectral remote sensing data using Ordinary Least Squares and estimate the
confusion matrix.

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.preprocessing import OneHotEncoder
import matplotlib.pyplot as plt

# Simulate spectral data for demonstration purposes


np.random.seed(42)
num_samples = 1000
num_features = 10
num_classes = 3

# Features and labels


X = np.random.rand(num_samples, num_features)
y = np.random.choice(num_classes, num_samples)

# Split 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)

# One-hot encode the labels for OLS


encoder = OneHotEncoder(sparse=False)
y_train_onehot = encoder.fit_transform(y_train.reshape(-1, 1))

# Add bias term to features


X_train_bias = np.hstack([np.ones((X_train.shape[0], 1)), X_train])

# Compute OLS coefficients


coefficients = np.linalg.pinv(X_train_bias.T @ X_train_bias) @ X_train_bias.T @
y_train_onehot

# Add bias term to test features


X_test_bias = np.hstack([np.ones((X_test.shape[0], 1)), X_test])

# Compute predictions
y_pred_prob = X_test_bias @ coefficients

# Assign class with the highest probability


y_pred = np.argmax(y_pred_prob, axis=1)

# Compute confusion matrix


conf_matrix = confusion_matrix(y_test, y_pred)
# Print classification report
print("Classification Report:")
print(classification_report(y_test, y_pred))

# Plot confusion matrix


plt.figure(figsize=(8, 6))
plt.imshow(conf_matrix, cmap='Blues', interpolation='nearest')
plt.colorbar()
plt.title("Confusion Matrix")
plt.xlabel("Predicted Labels")
plt.ylabel("True Labels")
plt.xticks(np.arange(num_classes), labels=np.arange(num_classes))
plt.yticks(np.arange(num_classes), labels=np.arange(num_classes))

for i in range(conf_matrix.shape[0]):
for j in range(conf_matrix.shape[1]):
plt.text(j, i, conf_matrix[i, j], ha='center', va='center', color='red')

plt.show()

OUTPUT:

Classification Report:
precision recall f1-score support

0 0.35 0.15 0.21 110


1 0.34 0.41 0.37 97
2 0.29 0.42 0.35 93

accuracy 0.32 300


macro avg 0.33 0.33 0.31 300
weighted avg 0.33 0.32 0.31 300
7C. Fit, evaluate, and make predictions with the Linear Discriminant Analysis model. Also
tune the hyperparameters of the model.

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import matplotlib.pyplot as plt

# Generate synthetic data for demonstration purposes


from sklearn.datasets import make_classification

X, y = make_classification(
n_samples=1000,
n_features=10,
n_classes=3,
n_informative=8,
n_redundant=2,
random_state=42
)

# Split into training and test sets


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

# Initialize and fit the LDA model


lda = LinearDiscriminantAnalysis()
lda.fit(X_train, y_train)

# Make predictions
y_pred = lda.predict(X_test)

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

# Confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
print(conf_matrix)

# Classification report
print("Classification Report:")
print(classification_report(y_test, y_pred))

# Define the parameter grid


param_grid = {
'solver': ['svd', 'lsqr', 'eigen'],
'shrinkage': [None, 'auto', 0.1, 0.5] # Shrinkage only applies for 'lsqr' and 'eigen'
}

# Grid search
grid_search = GridSearchCV(
estimator=LinearDiscriminantAnalysis(),
param_grid=param_grid,
cv=5, # 5-fold cross-validation
scoring='accuracy'
)

grid_search.fit(X_train, y_train)

# Best parameters and score


print("Best Parameters:", grid_search.best_params_)
print("Best Cross-Validation Accuracy:", grid_search.best_score_)

plt.show()

OUTPUT:

Accuracy: 0.7267
Confusion Matrix:
[[75 7 15]
[10 76 15]
[ 6 29 67]]
Classification Report:
precision recall f1-score support

0 0.82 0.77 0.80 97


1 0.68 0.75 0.71 101
2 0.69 0.66 0.67 102

accuracy 0.73 300


macro avg 0.73 0.73 0.73 300
weighted avg 0.73 0.73 0.73 300
Best Parameters: {'shrinkage': None, 'solver': 'svd'}
Best Cross-Validation Accuracy: 0.7171428571428571
Accuracy with Tuned Model: 0.7266666666666667
8. Implementing the Perceptron Algorithm for binary classification task and plot the
decision boundary.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification

# Generate synthetic binary classification data


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

# Convert labels to {-1, 1} for Perceptron compatibility


y = np.where(y == 0, -1, 1)

class Perceptron:
def __init__(self, learning_rate=0.01, max_epochs=100):
self.learning_rate = learning_rate
self.max_epochs = max_epochs
self.weights = None
self.bias = 0

def fit(self, X, y):


# Initialize weights and bias
self.weights = np.zeros(X.shape[1])
self.bias = 0

for epoch in range(self.max_epochs):


for idx, x_i in enumerate(X):
# Perceptron update rule
linear_output = np.dot(x_i, self.weights) + self.bias
y_pred = np.sign(linear_output)

if y[idx] * y_pred<= 0: # Misclassified


self.weights += self.learning_rate * y[idx] * x_i
self.bias += self.learning_rate * y[idx]

def predict(self, X):


linear_output = np.dot(X, self.weights) + self.bias
return np.sign(linear_output)

# Initialize and train the Perceptron


perceptron = Perceptron(learning_rate=0.1, max_epochs=10)
perceptron.fit(X, y)

# Define the decision boundary


def plot_decision_boundary(X, y, model):
# Plot data points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='coolwarm', edgecolors='k')

# Plot decision boundary


x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x_vals = np.linspace(x_min, x_max, 100)
y_vals = -(model.weights[0] * x_vals + model.bias) / model.weights[1]
plt.plot(x_vals, y_vals, color='black', linestyle='--', label='Decision Boundary')

plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Perceptron Decision Boundary')
plt.legend()
plt.show()

# Visualize the decision boundary


plot_decision_boundary(X, y, perceptron)
OUTPUT:
9. Build an employee churn prediction model using Multi-layer perceptron and evaluate
the model performance with different learning rate.

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report, roc_auc_score
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
import matplotlib.pyplot as plt

# Load and preprocess data


data = pd.read_csv("C:\\Users\\Hp\\Desktop\\churn_prediction.csv")
# Replace with your dataset path
X = data.drop('churn', axis=1) # Features
y = data['churn'] # Target

# Encode categorical variables and scale data


X = pd.get_dummies(X, drop_first=True)
scaler = StandardScaler()
X = scaler.fit_transform(X)

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Function to build the MLP model


def build_mlp():
model = Sequential([
Dense(64, activation='relu', input_shape=(X_train.shape[1],)),
Dense(32, activation='relu'),
Dense(1, activation='sigmoid') # Binary classification
])
return model

# Experiment with different learning rates


learning_rates = [0.001, 0.01, 0.1]
history_dict = {}

for lr in learning_rates:
print(f"Training with learning rate: {lr}")
model = build_mlp()
optimizer = tf.keras.optimizers.Adam(learning_rate=lr)
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2, verbose=0)
history_dict[lr] = history

# Evaluate on test set


test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=0)
y_pred = (model.predict(X_test) > 0.5).astype("int32")
auc = roc_auc_score(y_test, y_pred)
print(f"Test Accuracy: {test_accuracy:.4f}, AUC: {auc:.4f}")
print(classification_report(y_test, y_pred))

# Plot learning curves for different learning rates


plt.figure(figsize=(12, 8))
for lr, history in history_dict.items():
plt.plot(history.history['val_accuracy'], label=f'LR={lr}')
plt.title('Validation Accuracy vs. Epochs')
plt.xlabel('Epochs')
plt.ylabel('Validation Accuracy')
plt.legend()
plt.show()

OUTPUT:

Training with learning rate: 0.001


Training with learning rate: 0.01
Training with learning rate: 0.1
178/178 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step
Test Accuracy: 0.8172, AUC: 0.5000
precision recall f1-score support

0 0.82 1.00 0.90 4639


1 0.00 0.00 0.00 1038

accuracy 0.82 5677


macro avg 0.41 0.50 0.45 5677
weighted avg 0.67 0.82 0.73 5677
10. Analyze the efficiency of a Radial Basis function network for a classification task and
assess its convergence ability.

import numpy as np
def fit(self, X, y):
# Step 1: Determine centers using k-means clustering
kmeans = KMeans(n_clusters=self.num_centers, n_init=10, random_state=42)
kmeans.fit(X)
self.centers = kmeans.cluster_centers_

# Step 2: Calculate hidden layer outputs using horizontal stacking


hidden_outputs = np.hstack([rbf(X, center, self.spread) for center in self.centers])

# Step 3: Optimize weights using least squares


self.weights = np.linalg.pinv(hidden_outputs).dot(y)

def predict(self, X):


hidden_outputs = np.hstack([rbf(X, center, self.spread) for center in self.centers])
predictions = hidden_outputs.dot(self.weights)
return np.argmax(predictions, axis=1)

# Train the RBF Network


num_centers = 10
spread = 1.0

rbf_network = RBFNetwork(num_centers=num_centers, spread=spread)


rbf_network.fit(X_train, y_train)

# Predict and Evaluate


y_pred = rbf_network.predict(X_test)
y_test_labels = np.argmax(y_test, axis=1)

accuracy = accuracy_score(y_test_labels, y_pred)


print(f"Accuracy: {accuracy:.4f}")
print(classification_report(y_test_labels, y_pred))

# Analyze convergence by varying centers and spread


centers_range = [5, 10, 15, 20]
spread_range = [0.5, 1.0, 1.5, 2.0]
accuracies = []

for centers in centers_range:


for spread in spread_range:
rbf_net = RBFNetwork(num_centers=centers, spread=spread)
rbf_net.fit(X_train, y_train)
y_pred = rbf_net.predict(X_test)
acc = accuracy_score(y_test_labels, y_pred)
accuracies.append((centers, spread, acc))

# Convert to structured NumPy array for easier plotting


accuracies = np.array(accuracies, dtype=[("centers", int), ("spread", float), ("accuracy", float)])

# Plot accuracy vs. centers and spreads


fig, ax = plt.subplots(figsize=(10, 6))
for spread in spread_range:
subset = accuracies[accuracies["spread"] == spread]
ax.plot(subset["centers"], subset["accuracy"], label=f"Spread={spread}")
ax.set_xlabel("Number of Centers")
ax.set_ylabel("Accuracy")
ax.set_title("Accuracy vs. Number of Centers and Spread")
ax.legend()
plt.show()

OUTPUT:

Accuracy: 0.9333
precision recall f1-score support

0 1.00 1.00 1.00 10


1 0.89 0.89 0.89 9
2 0.91 0.91 0.91 11

accuracy 0.93 30
macro avg 0.93 0.93 0.93 30
weighted avg 0.93 0.93 0.93 30

Centers: 5, Spread: 0.5, Accuracy: 0.9000


Centers: 5, Spread: 1.0, Accuracy: 0.9000
Centers: 5, Spread: 1.5, Accuracy: 0.9000
Centers: 5, Spread: 2.0, Accuracy: 0.9000
Centers: 10, Spread: 0.5, Accuracy: 0.9333
Centers: 10, Spread: 1.0, Accuracy: 0.9333
Centers: 10, Spread: 1.5, Accuracy: 0.9667
Centers: 10, Spread: 2.0, Accuracy: 0.9667
Centers: 15, Spread: 0.5, Accuracy: 1.0000
Centers: 15, Spread: 1.0, Accuracy: 1.0000
Centers: 15, Spread: 1.5, Accuracy: 1.0000
Centers: 15, Spread: 2.0, Accuracy: 1.0000
Centers: 20, Spread: 0.5, Accuracy: 1.0000
Centers: 20, Spread: 1.0, Accuracy: 1.0000
Centers: 20, Spread: 1.5, Accuracy: 1.0000
Centers: 20, Spread: 2.0, Accuracy: 1.0000

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy