Ann Experiential Learning
Ann 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)
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))
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
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
# 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
Classification Report:
precision recall f1-score support
accuracy 0.43 30
macro avg 0.45 0.44 0.42 30
weighted avg 0.45 0.43 0.42 30
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
np.random.seed(42)
X = np.random.randn(100, 2)
class Perceptron:
self.learning_rate = learning_rate
self.max_iter = max_iter
self.weights = np.zeros(n_features)
self.bias = 0
for _ in range(self.max_iter):
model.fit(X, y)
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
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'
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)
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
for lr in learning_rates:
print(f"\nTraining with learning rate: {lr}")
model, history = build_and_train_mlp(learning_rate=lr)
print(f"Accuracy: {acc:.4f}")
print("Training complete.")
OUTPUT::
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
X = StandardScaler().fit_transform(X)
y = y.ravel()
log_reg = LogisticRegression(random_state=42)
log_reg.fit(X_train, y_train)
y_pred_log = log_reg.predict(X_test)
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)
centers = kmeans.cluster_centers_
base_sigmas = []
for i, c in enumerate(centers):
base_sigmas.append(nearest_distance)
base_sigma = np.mean(base_sigmas)
weights = np.linalg.pinv(phi_train).dot(y_train)
sigma_factor_baseline = 1.0
weights = np.zeros(phi_train.shape[1])
errors = []
predictions = phi_train.dot(weights)
mse = np.mean(error ** 2)
errors.append(mse)
plt.plot(training_errors, marker='o')
plt.xlabel("Epoch")
plt.grid(True)
plt.show()
acc_tests_sigma = []
for sf in sigma_factors:
n_clusters_baseline, sigma_factor=sf)
acc_tests_sigma.append(acc_test)
plt.figure(figsize=(8, 5))
plt.xlabel("Sigma Factor")
plt.ylabel("Test Accuracy")
plt.grid(True)
plt.show()
acc_tests_nclusters = []
for n in n_clusters_list:
acc_tests_nclusters.append(acc_test)
plt.figure(figsize=(8, 5))
plt.ylabel("Test Accuracy")
plt.grid(True)
plt.show()
OUTPUT::
# 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
# 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
# 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
results = {}
for lr in learning_rates:
for m in momentum_values:
history = train_sgd(lr, m)
results[(lr, m)] = history
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
# 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]
OUTPUT:
Description:
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
# Pruning parameters
pruning_params = {
'pruning_schedule': sparsity.PolynomialDecay(initial_sparsity=0.2, final_sparsity=0.8,
begin_step=2000, end_step=10000)
}
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")
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:
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
# Compute predictions
y_pred_prob = X_test_bias @ coefficients
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
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
X, y = make_classification(
n_samples=1000,
n_features=10,
n_classes=3,
n_informative=8,
n_redundant=2,
random_state=42
)
# 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))
# 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)
plt.show()
OUTPUT:
Accuracy: 0.7267
Confusion Matrix:
[[75 7 15]
[10 76 15]
[ 6 29 67]]
Classification Report:
precision recall f1-score support
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
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
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Perceptron Decision Boundary')
plt.legend()
plt.show()
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
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
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
OUTPUT:
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_
OUTPUT:
Accuracy: 0.9333
precision recall f1-score support
accuracy 0.93 30
macro avg 0.93 0.93 0.93 30
weighted avg 0.93 0.93 0.93 30