0% found this document useful (0 votes)
15 views34 pages

Machine Learning Lab Manual for Engineers

The document is a lab manual for a Machine Learning course, detailing various practical exercises for students. It includes implementations of algorithms such as Naïve Bayes, Random Forest, SVM, and ANN, along with steps for data preparation, model training, and evaluation. Each practical section provides code snippets and expected outputs for clarity.

Uploaded by

festus adebayo
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)
15 views34 pages

Machine Learning Lab Manual for Engineers

The document is a lab manual for a Machine Learning course, detailing various practical exercises for students. It includes implementations of algorithms such as Naïve Bayes, Random Forest, SVM, and ANN, along with steps for data preparation, model training, and evaluation. Each practical section provides code snippets and expected outputs for clarity.

Uploaded by

festus adebayo
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

FACULTY OF ENGINEERING AND TECHNOLOGY

Department of Computer Engineering


01CE0617– MACHINE LEARNING

Machine Learning(01CE0617)
Lab Manual
______________________________
Name: YASHKUMAR MAYANI
Enrolment No: 92310103058
Class: 6TC3
Batch: B
FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Lab Program Marks Signature

1. Implement Naïve Bayes algorithm using sample data.

2. Implement Random Forest and ensemble learning techniques.

3. Using a dataset implement SVM classifier.

Implement classification techniques evaluation parameters


4.
using sample dataset.
Develop a cost function of linear regression using sample
5. dataset.

Develop a Gradient descent of linear regression using


6.
sample dataset.
Implement a linear regression and multi linear regression
7.
algorithm with regularization using sample dataset.

8. Perform hyper parameter tuning using sample dataset.

Using sample data implement the evaluation parameters of


9.
regression techniques.

10. Implement k-means clustering using a dataset.

Explore the association rule mining techniques using sample


11.
data.

12. Implement ANN using sample data.

13. Exploring activation function in ANN.

14. Implementation of Transformer Neural Network model.

YASHKUMAR MAYANI (92310103058) BATCH: B |1


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 1: Implement Naïve Bayes algorithm using sample data.


Dataset Creation: A simple dataset with categorical variables is created. It simulates customer
behavior for buying computers.
Encoding Categorical Data: Categorical features are converted to numerical values using
LabelEncoder.
Splitting Data: The dataset is divided into training and testing sets using train_test_split.
Model Training: The GaussianNB model is trained using the training data. Predictions: Predictions
are made on the test set.

Basic Algorithmic Steps:


Import Libraries: Load pandas, sklearn modules.
Prepare Data: Create a DataFrame with Temperature, Humidity, Wind Speed, and Condition.
Encode Target Variable: Convert Condition to numerical values using LabelEncoder().
Split Data: Divide into X (features) and y (target), then split into train (80%) and test (20%) sets.
Train Model: Fit GaussianNB() on training data.
Make Predictions: Use the trained model to predict test data.
Evaluate Performance: Compute accuracy_score(y_test, y_pred).
Decode Predictions (Optional): Convert predicted labels back to original weather conditions
Program
# Step 1: Import Libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import LabelEncoder
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score

# Sample Weather Data


data = {
"Temperature": [22, 24, 21, 23, 25, 20, 22],
"Humidity": [60, 65, 55, 70, 75, 50, 58],
"Wind Speed": [10, 12, 8, 15, 14, 9, 11],
"Condition": ["Sunny", "Cloudy", "Rainy", "Sunny", "Cloudy", "Rainy", "Sunny"]
}

# Create DataFrame

YASHKUMAR MAYANI (92310103058) BATCH: B |2


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

df = [Link](data)
print(df)

# Encode "Condition" column (Target Variable)


label_encoder = LabelEncoder()
df["Condition"] = label_encoder.fit_transform(df["Condition"]) # Convert 'Sunny', 'Cloudy', etc. into
numbers

# Features & Target


X = [Link](columns=["Condition"]) # Features
y = df["Condition"] # Target variable

# Train-Test Split (80% Train, 20% Test)


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

# Train Naïve Bayes Model


model = GaussianNB()
[Link](X_train, y_train)

# Predictions
y_pred = [Link](X_test)

# Evaluate Model
accuracy = accuracy_score(y_test, y_pred)
print("Naïve Bayes Accuracy:", accuracy)
# Decode Predicted Conditions (Optional)
decoded_preds = label_encoder.inverse_transform(y_pred)
print("Predicted Weather Conditions:", decoded_preds)
Output:

YASHKUMAR MAYANI (92310103058) BATCH: B |3


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 2: Implement Random Forest and ensemble learning techniques.

Basic Algorithmic Steps:


• Import Libraries: Load pandas, sklearn modules.
• Load Dataset: Use load_iris() to get IRIS data.
• Explore Data: Print species names and feature names.
• Prepare Data: Convert to DataFrame and add target column.
• Split Data: Divide into X (features) and y (target), then split into train (80%) and test (20%)
sets.
• Define Ensemble Models: Use RandomForestClassifier, BaggingClassifier,
AdaBoostClassifier, and GradientBoostingClassifier.
• Train & Evaluate: Fit each model, predict test data, compute accuracy_score(y_test, y_pred),
and print accuracy.
CODE BLOCK [1]
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import RandomForestClassifier, BaggingClassifier, AdaBoostClassifier,
GradientBoostingClassifier
from [Link] import accuracy_score
import pandas as pd

# Load IRIS dataset


iris = load_iris()

# print the label species(setosa, versicolor , virginica )


print(iris.target_names)
# print the names of the four features
print(iris.feature_names)

# Convert to Pandas DataFrame


df = [Link]([Link], columns=iris.feature_names)
[Link]()
OUTPUT:

CODE BLOCK [2]:


# Add target column

YASHKUMAR MAYANI (92310103058) BATCH: B |4


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

df["target"] = [Link]

# Split Data (80% Train, 20% Test)


X = [Link][:, :-1] # Select all columns except target
y = df["target"] # Target column

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

# Define Ensemble Models


models = {
"Random Forest": RandomForestClassifier(n_estimators=100, random_state=42),
"Bagging": BaggingClassifier(n_estimators=50, random_state=42),
"AdaBoost": AdaBoostClassifier(n_estimators=50, random_state=42),
"Gradient Boosting": GradientBoostingClassifier(n_estimators=100, random_state=42)
}

# Train & Evaluate Each Model


for name, model in [Link]():
[Link](X_train, y_train)
y_pred = [Link](X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"{name} Accuracy: {accuracy:.4f}")
OUTPUT:

YASHKUMAR MAYANI (92310103058) BATCH: B |5


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 3: Using a dataset implement SVM classifier.

Basic Algorithmic Steps:


Import Required Libraries: Import [Link], numpy, and necessary modules from sklearn.
Load Dataset: Load the breast cancer dataset from [Link].
Feature Selection: Select the first two features from the dataset for visualization.
Split Data: Divide the dataset into training (70%) and testing (30%) sets using train_test_split().
Data Preprocessing: Standardize the features using StandardScaler.
Train SVM Classifier: Train an SVM model using an RBF kernel with gamma=0.5 and C=1.0.
Decision Boundary Visualization: Plot the decision boundary using
DecisionBoundaryDisplay.from_estimator(). Overlay training and testing points for reference.
Model Evaluation :Predict labels on the test set. Calculate and print accuracy using accuracy_score().
CODE:
import [Link] as plt
import numpy as np
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import DecisionBoundaryDisplay

# Import the breast cancer dataset


cancer = datasets.load_breast_cancer()

# Take the first two features for visualization. We could avoid this by using PCA.
X = [Link][:, :2]
y = [Link]

# Split data into training and testing sets


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

# Scale the data


sc = StandardScaler()
[Link](X_train)
X_train_std = [Link](X_train)
X_test_std = [Link](X_test)
# Train the SVM classifier
svm_model = [Link](kernel='rbf',gamma=0.5 , C=1.0, )
svm_model.fit(X_train_std, y_train)
# Plot decision boundary
_, ax = [Link]()

YASHKUMAR MAYANI (92310103058) BATCH: B |6


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

DecisionBoundaryDisplay.from_estimator(
svm_model,
X_train_std,
alpha=0.8,
response_method="predict",
plot_method="pcolormesh",
ax=ax,
xlabel=cancer.feature_names[0],
ylabel=cancer.feature_names[1],
)
# Plot the training points
[Link](X_train_std[:, 0], X_train_std[:, 1], c=y_train, edgecolors="k", cmap=[Link])
# Plot the testing points
[Link](X_test_std[:, 0], X_test_std[:, 1], c=y_test, edgecolors="k", cmap=[Link],
marker="s")
[Link]("SVM Decision Boundary")
[Link]()
# Evaluate the model (optional)
from [Link] import accuracy_score
y_pred = svm_model.predict(X_test_std)
print(f"Accuracy: {accuracy_score(y_test, y_pred)}")

OUTPUT:

YASHKUMAR MAYANI (92310103058) BATCH: B |7


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 4: Implement classification techniques evaluation parameters using sample


dataset.

Basic Algorithmic Steps:


Import Libraries: Bring in the necessary Python libraries (Pandas, Scikit-Learn, Seaborn, Matplotlib).
Load Data: Define your extended dataset with 20 records of weather, temperature, humidity, windy, and
game playing info.
Create DataFrame: Convert your dictionary data into a Pandas DataFrame.
Encode Categorical Columns: Use LabelEncoder to convert all categorical columns into numeric values.
Define Features & Target:
Features (X): All columns except "Played_Games".
Target (y): The "Played_Games" column.
Train-Test Split: Split the data into training (80%) and testing (20%) sets.
Train Model: Fit a Gaussian Naïve Bayes model using the training data.
Predict: Use the model to predict game playing outcomes on the test data.
Evaluate: Calculate accuracy, precision, recall, F1 score, and generate a confusion matrix to assess the
model’s performance.
Visualize Results(optional): Plot the confusion matrix using Seaborn for a clear visual summary.

Code:
# Step 1: Import Libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import LabelEncoder
from sklearn.naive_bayes import GaussianNB
from [Link] import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
import seaborn as sns
import [Link] as plt

# Extended Weather vs. Games Played Data (20 records)


data = {
"Weather": ["Sunny", "Sunny", "Overcast", "Rainy", "Rainy", "Rainy", "Overcast", "Sunny", "Sunny",
"Rainy",
"Overcast", "Sunny", "Rainy", "Rainy", "Sunny", "Overcast", "Overcast", "Rainy", "Sunny",
"Rainy"],

YASHKUMAR MAYANI (92310103058) BATCH: B |8


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

"Temperature": ["Hot", "Hot", "Hot", "Mild", "Cool", "Cool", "Cool", "Mild", "Cool", "Mild",
"Hot", "Mild", "Mild", "Cool", "Hot", "Mild", "Cool", "Hot", "Mild", "Cool"],
"Humidity": ["High", "High", "High", "High", "Normal", "Normal", "Normal", "High", "Normal",
"Normal",
"High", "Normal", "Normal", "High", "High", "Normal", "High", "Normal", "Normal", "High"],
"Windy": ["No", "Yes", "No", "No", "No", "Yes", "Yes", "No", "No", "No",
"Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "No", "Yes"],
"Played_Games": ["No", "No", "Yes", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes",
"Yes", "No", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "No", "Yes"]
}

# Create DataFrame
df = [Link](data)

# Encode Categorical Columns


label_encoder = LabelEncoder()
for column in ["Weather", "Temperature", "Humidity", "Windy", "Played_Games"]:
df[column] = label_encoder.fit_transform(df[column])

# Features & Target


X = [Link](columns=["Played_Games"]) # Features
y = df["Played_Games"] # Target variable

# Train-Test Split (80% Train, 20% Test)


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

# Train Naïve Bayes Model


model = GaussianNB()
[Link](X_train, y_train)

# Predictions
y_pred = [Link](X_test)

# Evaluate Model
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)

# Display Results

YASHKUMAR MAYANI (92310103058) BATCH: B |9


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

print("Naïve Bayes Accuracy:", accuracy)


print("Precision:", precision)
print("Recall:", recall)
print("F1 Score:", f1)
print("Confusion Matrix:\n", conf_matrix)
# Plot Confusion Matrix
[Link](figsize=(5, 4))
[Link](conf_matrix, annot=True, fmt="d", cmap="Blues", xticklabels=["No", "Yes"],
yticklabels=["No", "Yes"])
[Link]("Predicted")
[Link]("Actual")
[Link]("Confusion Matrix")
[Link]()

Output:

YASHKUMAR MAYANI (92310103058) BATCH: B |10


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 5: Develop a cost function of linear regression using sample dataset.

Basic Algorithmic Steps:


Load Data:
Get your dataset with categorical features and a target.
Preprocess Data:
One-hot encode the categorical features.
Convert the target ("Played_Games") to 0 and 1.
Split Data:
Divide the data into training and testing sets.
Train Model:
Use linear regression on the training set.
Predict:
Generate predictions with the trained model.
Calculate Cost:
Compute the cost using:

Evaluate:
Optionally, check the cost on the test set.

Code:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

# 1. Define the dataset with 20 rows


data = {
"Weather": ["Sunny", "Sunny", "Overcast", "Rainy", "Rainy", "Rainy", "Overcast", "Sunny", "Sunny",
"Rainy",
"Overcast", "Sunny", "Rainy", "Rainy", "Sunny", "Overcast", "Overcast", "Rainy", "Sunny",
"Rainy"],
"Temperature": ["Hot", "Hot", "Hot", "Mild", "Cool", "Cool", "Cool", "Mild", "Cool", "Mild",
"Hot", "Mild", "Mild", "Cool", "Hot", "Mild", "Cool", "Hot", "Mild", "Cool"],
"Humidity": ["High", "High", "High", "High", "Normal", "Normal", "Normal", "High", "Normal", "Normal",
"High", "Normal", "Normal", "High", "High", "Normal", "High", "Normal", "Normal", "High"],
"Windy": ["No", "Yes", "No", "No", "No", "Yes", "Yes", "No", "No", "No",
"Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No", "No", "Yes"],
"Played_Games": ["No", "No", "Yes", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes",
"Yes", "No", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "No", "Yes"]
}

# 2. Create a DataFrame
df = [Link](data)

YASHKUMAR MAYANI (92310103058) BATCH: B |11


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

# print("Original DataFrame:")
# print([Link]())

# 3. One-hot encode categorical features


# (drop_first=True removes one dummy to avoid multicollinearity)
df_encoded = pd.get_dummies(df, columns=["Weather", "Temperature", "Humidity", "Windy"],
drop_first=True)
print("\nEncoded DataFrame:")
print(df_encoded.head())

# 4. Convert target variable 'Played_Games' to binary (No -> 0, Yes -> 1)


df_encoded["Played_Games"] = df_encoded["Played_Games"].map({"No": 0, "Yes": 1})

# 5. Separate features (X) and target (y)


X = df_encoded.drop("Played_Games", axis=1)
y = df_encoded["Played_Games"]

# 6. Split data into training and testing sets (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 7. Train a linear regression model


lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)

# 8. Predict on the training set


predictions_train = lin_reg.predict(X_train)
m = len(y_train)

# 9. Define the cost function for linear regression (Mean Squared Error)
def compute_cost(predictions, y, m):
cost = (1 / (2 * m)) * [Link]((predictions - y) ** 2)
return cost

# 10. Compute and display the cost on the training set


cost_train = compute_cost(predictions_train, y_train, m)
print(f"\nCost on Training Set: {cost_train:.4f}")

# (Optional) You can also compute the cost on the test set:
predictions_test = lin_reg.predict(X_test)
cost_test = compute_cost(predictions_test, y_test, len(y_test))
print(f"Cost on Test Set: {cost_test:.4f}")

YASHKUMAR MAYANI (92310103058) BATCH: B |12


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Output:

YASHKUMAR MAYANI (92310103058) BATCH: B |13


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 9: Using sample data implement the evaluation parameters of regression


techniques.

Basic Algorithmic Steps:


Import Libraries: Bring in NumPy, Pandas, and the necessary scikit-learn modules.
Data Creation: Create a sample dataset with independent variable x and dependent variable y, and store it in a
DataFrame.
Feature & Target Definition: Define X (features) as x and y (target) as the output values.
Train-Test Split: Divide the data into training and testing sets (80% training, 20% testing).
Model Training: Train a Linear Regression model using the training data.
Prediction: Use the trained model to predict y values for the test set.
Evaluation: Calculate evaluation metrics like Mean Squared Error (MSE), Root Mean Squared Error (RMSE),
Mean Absolute Error (MAE), and R-squared (R²) to assess the model's performance.
Display Results: Print the evaluation metrics to see how well the model did.

Code:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, mean_absolute_error, r2_score

# --- Sample Data Creation ---


# Let's create some simple data with a linear-ish relationship
# x: independent variable, y: dependent variable
data = {
'x': [Link](1, 21), # values from 1 to 20
'y': [Link]([3, 4, 5, 8, 9, 11, 13, 15, 16, 18, 20, 22, 23, 25, 27, 28, 30, 32, 33, 35])
}
df = [Link](data)

# --- Features & Target ---


X = df[['x']] # as a 2D array for the model
y = df['y']

# --- Train-Test Split ---


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

# --- Model Training ---


model = LinearRegression()
[Link](X_train, y_train)

# --- Prediction ---


y_pred = [Link](X_test)

YASHKUMAR MAYANI (92310103058) BATCH: B |14


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

# --- Evaluation Metrics ---


mse = mean_squared_error(y_test, y_pred) # Mean Squared Error
rmse = [Link](mse) # Root Mean Squared Error
mae = mean_absolute_error(y_test, y_pred) # Mean Absolute Error
r2 = r2_score(y_test, y_pred) # R-squared Score

# --- Display the Results ---


print("Evaluation Metrics:")
print("-------------------")
print(f"Mean Squared Error (MSE): {mse:.4f}")
print(f"Root Mean Squared Error (RMSE): {rmse:.4f}")
print(f"Mean Absolute Error (MAE): {mae:.4f}")
print(f"R-squared (R²): {r2:.4f}")

Output:

YASHKUMAR MAYANI (92310103058) BATCH: B |15


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 6: Develop a gradient descent of linear retraction using simple dataset.

Basic Algorithmic Steps:


1. Initialize parameters θ (intercept and slope) with random values.
2. Set the learning rate (α) and number of iterations.
3. Compute the cost function (MSE) to measure error:
1. J(θ) = (1/m) * Σ [(θ0 + θ1 * x_i) - y_i]^2

4. Compute the gradients (partial derivatives):


1. ∂J/∂θ0 = (2/m) * Σ [(θ0 + θ1 * x_i) - y_i]
2. ∂J/∂θ1 = (2/m) * Σ [(θ0 + θ1 * x_i) - y_i] * x_i

5. Update θ using gradient descent:


1. θ0 = θ0 - α * (∂J/∂θ0)
2. θ1 = θ1 - α * (∂J/∂θ1)

6. Repeat the process for a fixed number of iterations or until convergence.

Code:
import numpy as np
import [Link] as plt

# Create a sample dataset (y = 3x + 4 + noise)


[Link](42)
X = 2 * [Link](100, 1)
y = 4 + 3 * X + [Link](100, 1)

# Add x0 = 1 to each instance (for the intercept term)


X_b = np.c_[[Link]((100, 1)), X]

# Hyperparameters for gradient descent


learning_rate = 0.1
n_iterations = 1000
m = 100 # number of samples

# Initialize theta (parameters: [intercept, slope]) randomly


theta = [Link](2, 1)

# Store MSE values for plotting


mse_list = []

# Perform gradient descent iterations


for iteration in range(n_iterations):
gradients = 2/m * X_b.[Link](X_b.dot(theta) - y)
theta = theta - learning_rate * gradients
mse = (1/m) * [Link]((X_b.dot(theta) - y) ** 2) # Mean Squared Error

YASHKUMAR MAYANI (92310103058) BATCH: B |16


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

mse_list.append(mse)

print("Estimated parameters (intercept(C), slope(m)):", theta)

# Plot MSE vs. Iterations


[Link](figsize=(7, 4))
[Link](range(n_iterations), mse_list, 'r-', label="MSE over Iterations")
[Link]("Iterations")
[Link]("Mean Squared Error")
[Link]("Gradient Descent Convergence")
[Link]()
[Link]()

Output:

YASHKUMAR MAYANI (92310103058) BATCH: B |17


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 7: Implement a linear regression and multi linear regression algorithm with
regularization using sample dataset.

Basic Algorithmic Steps:

1. Generate Data:
o Create a dataset with 3 independent variables (X).
o Define a linear equation (y = 3X1 + 5X2 - 2X3 + noise).
o Convert it into a DataFrame.
2. Split Dataset:
o Divide the dataset into training (80%) and testing (20%) sets.
3. Train Models:
o Linear Regression: Fit a basic linear regression model.
o Ridge Regression (L2 Regularization): Train with L2 penalty to reduce overfitting.
o Lasso Regression (L1 Regularization): Train with L1 penalty to perform feature selection.
4. Evaluate Models:
o Calculate Mean Squared Error (MSE) and R² Score for all models.
5. Visualize Predictions:
o Plot actual vs predicted values for all three models.
o Add a dashed line (y_test = y_test) for reference.

Code:
import numpy as np
import pandas as pd
import [Link] as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from [Link] import mean_squared_error, r2_score

# Generate sample dataset


[Link](42)
X = [Link](100, 3) # 3 independent variables
coefficients = [Link]([3, 5, -2])
y = X @ coefficients + [Link](100) * 0.5 # Adding some noise

# Convert to DataFrame
df = [Link](X, columns=['Feature1', 'Feature2', 'Feature3'])
df['Target'] = y

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

# Linear Regression
linear_model = LinearRegression()
linear_model.fit(X_train, y_train)
y_pred_linear = linear_model.predict(X_test)

YASHKUMAR MAYANI (92310103058) BATCH: B |18


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

# Ridge Regression (L2 Regularization)


ridge_model = Ridge(alpha=1.0)
ridge_model.fit(X_train, y_train)
y_pred_ridge = ridge_model.predict(X_test)

# Lasso Regression (L1 Regularization)


lasso_model = Lasso(alpha=0.1)
lasso_model.fit(X_train, y_train)
y_pred_lasso = lasso_model.predict(X_test)

# Evaluate models
def evaluate_model(model_name, y_true, y_pred):
mse = mean_squared_error(y_true, y_pred)
r2 = r2_score(y_true, y_pred)
print(f"{model_name}: MSE = {mse:.4f}, R2 Score = {r2:.4f}")

evaluate_model("Linear Regression", y_test, y_pred_linear)


evaluate_model("Ridge Regression", y_test, y_pred_ridge)
evaluate_model("Lasso Regression", y_test, y_pred_lasso)

# Plot results
[Link](figsize=(10, 5))
[Link](y_test, y_pred_linear, label="Linear", alpha=0.7)
[Link](y_test, y_pred_ridge, label="Ridge", alpha=0.7)
[Link](y_test, y_pred_lasso, label="Lasso", alpha=0.7)
[Link](y_test, y_test, color='black', linestyle='dashed')
[Link]("Actual Values")
[Link]("Predicted Values")
[Link]()
[Link]("Regression Predictions vs Actual Values")
[Link]()

Output:

YASHKUMAR MAYANI (92310103058) BATCH: B |19


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 8: Perform hyper parameter tuning using sample dataset.

Basic Algorithmic Steps:

• Load Dataset:

• Import the Iris dataset from [Link].


• Extract features (X) and target labels (y).

• Split Data:

• Divide the dataset into 80% training and 20% testing using train_test_split().

• Define Model:

• Initialize a RandomForestClassifier as the base model.

• Set Hyperparameter Search Space:

• Define a dictionary of hyperparameters (param_dist), including:


o n_estimators: Number of trees in the forest.
o max_depth: Maximum depth of trees.
o min_samples_split: Minimum samples needed to split a node.
o min_samples_leaf: Minimum samples per leaf.

• Perform Hyperparameter Tuning:

• Use RandomizedSearchCV to find the best hyperparameters:


o n_iter=20: Randomly samples 20 sets of hyperparameters.
o cv=5: Uses 5-fold cross-validation.
o scoring='accuracy': Optimizes for the best accuracy.

• Train & Evaluate:

• Fit the random_search model on training data.


• Print the best hyperparameters found.

Code:
from [Link] import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV
from [Link] import RandomForestClassifier
import numpy as np
# Load dataset
iris = load_iris()
X, y = [Link], [Link]

YASHKUMAR MAYANI (92310103058) BATCH: B |20


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

# Split dataset 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)
# Define the model
model = RandomForestClassifier(random_state=42)
# Define hyperparameter grid for GridSearchCV
param_grid = {
'n_estimators': [10, 50, 100, 200],
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}

# Perform GridSearchCV
grid_search = GridSearchCV(model, param_grid, cv=5, scoring='accuracy', n_jobs=-1)
grid_search.fit(X_train, y_train)
print("Best parameters from GridSearchCV:", grid_search.best_params_)
# Define hyperparameter distribution for RandomizedSearchCV
param_dist = {
'n_estimators': [Link](10, 200, 10),
'max_depth': [None, 10, 20, 30, 40, 50],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
}
# Perform RandomizedSearchCV
random_search = RandomizedSearchCV(model, param_distributions=param_dist, n_iter=20, cv=5,
scoring='accuracy', random_state=42)
random_search.fit(X_train, y_train)
print("Best parameters from RandomizedSearchCV:", random_search.best_params_)

Output:

YASHKUMAR MAYANI (92310103058) BATCH: B |21


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 10: Implement k-means clustering using a dataset.


Basic Algorithmic Steps:
Load Dataset
Use a sample dataset (e.g., Iris) from [Link].

Preprocess Data
Extract features only (no labels needed for unsupervised learning like clustering).

Apply K-Means Clustering


Use KMeans from [Link].

Visualize Clusters (optional but helps understand the result in 2D)

Evaluate with Inertia & Silhouette Score


Inertia gives internal measure, Silhouette gives clustering quality.

Code:
from [Link] import load_iris
from [Link] import KMeans
from [Link] import silhouette_score
import [Link] as plt

# 1. Load Dataset
iris = load_iris()
X = [Link]

# 2. Apply K-Means Clustering


k = 3 # Number of clusters
kmeans = KMeans(n_clusters=k, random_state=42)
[Link](X)
labels = kmeans.labels_

# 3. Evaluation
inertia = kmeans.inertia_
sil_score = silhouette_score(X, labels)

print(f"Inertia: {inertia:.2f}")
print(f"Silhouette Score: {sil_score:.2f}")

# 4. Optional: Visualize (using only first two features for 2D plot)


[Link](X[:, 0], X[:, 1], c=labels, cmap='viridis', edgecolor='k', s=50)
[Link](kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1],
s=200, c='red', marker='X', label='Centroids')
[Link]('Feature 1')
[Link]('Feature 2')
[Link]('K-Means Clustering on Iris Dataset')
[Link]()
[Link](True)
[Link]()

YASHKUMAR MAYANI (92310103058) BATCH: B |22


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

OUTPUT.

YASHKUMAR MAYANI (92310103058) BATCH: B |23


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 11: Explore the association rule mining techniques using sample data.

Basic Algorithmic Steps:


Step 1: Prepare Dataset
• Collect a list of user transactions (each as a list of items bought together).
• Convert transactions into a format suitable for model input.
Step 2: Encode Transactions
• Use TransactionEncoder to transform the list of transactions into a boolean matrix (one-hot encoded
DataFrame).
Step 3: Find Frequent Itemsets
• Apply the Apriori algorithm using min_support threshold to extract frequent item combinations.
Step 4: Generate Association Rules
• Use association_rules() with a confidence threshold to generate if-then rules like:
o If (popcorn, coke) → Then (chips)
Step 5: Filter Rules (Optional)
• Remove rules where antecedents and consequents overlap (e.g., popcorn → popcorn).
Step 6: Build Recommender Logic
• Take a user's current cart (list of items).
• Convert it to a frozenset.
• Search for rules where all items in the rule’s antecedent are in the user's cart.
• Sort matching rules by confidence.
• Recommend the item(s) in the highest-confidence rule's consequent.
Step 7: Return Suggestion
• If a matching rule exists → Return the recommended item.
• Else → Return "No strong recommendation found."

Code:
import pandas as pd
from [Link] import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules

# Sample transactions
dataset = [
['popcorn', 'coke', 'chips'],
['popcorn', 'coke'],
['popcorn', 'chips'],
['coke', 'chips'],
['popcorn', 'coke', 'nachos'],
['coke', 'nachos'],
['popcorn', 'coke', 'chips'],
['popcorn', 'coke'],
['chips', 'nachos'],
['popcorn', 'chips'],
['popcorn', 'coke', 'cookies'],
['chips', 'cookies'],

YASHKUMAR MAYANI (92310103058) BATCH: B |24


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

['popcorn', 'cookies'],
['popcorn', 'coke', 'chips', 'nachos'],
['coke', 'chips', 'cookies'],
['popcorn', 'ice cream'],
['chips', 'ice cream'],
['popcorn', 'coke', 'pizza'],
['pizza', 'nachos', 'coke'],
['popcorn', 'pizza', 'cookies']
]

# Preprocessing
te = TransactionEncoder()
te_data = [Link](dataset).transform(dataset)
df = [Link](te_data, columns=te.columns_)

# Train model
frequent_itemsets = apriori(df, min_support=0.2, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.6)

# Clean rules: ignore if antecedents and consequents overlap


rules = rules[rules['antecedents'] != rules['consequents']]

# Recommendation system
def recommend(user_cart, rules_df):
user_cart_set = frozenset(user_cart)
matches = rules_df[rules_df['antecedents'].apply(lambda x: [Link](user_cart_set))]

if [Link]:
return f"User bought {list(user_cart_set)} → No strong recommendation found."

# Pick the rule with highest confidence


top_match = matches.sort_values(by='confidence', ascending=False).iloc[0]
return f"User bought {list(user_cart_set)} → Recommend: {list(top_match['consequents'])[0]} (Confidence:
{top_match['confidence']:.2f})"

# -----------------
# Test Outputs
# -----------------
print(recommend(['popcorn', 'coke'], rules))
print(recommend(['chips', 'cookies'], rules))
print(recommend(['nachos', 'pizza'], rules))
print(recommend(['ice cream', 'chips'], rules))
print(recommend(['popcorn', 'pizza'], rules))

YASHKUMAR MAYANI (92310103058) BATCH: B |25


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

OUTPUT.

YASHKUMAR MAYANI (92310103058) BATCH: B |26


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 12: Implement ANN using sample data.

Basic Algorithmic Steps:


Import Required Libraries
Import necessary modules from numpy, sklearn, and [Link].
Load Dataset
Load the Iris dataset using load_iris().
Extract Features and Labels
Assign feature matrix X and reshape labels y.
Encode Labels
Use OneHotEncoder to convert categorical labels into one-hot encoded vectors.
Normalize Feature Data
Apply StandardScaler to scale features to have zero mean and unit variance.
Split Dataset
Divide the data into training and testing sets using train_test_split().
Build ANN Model
Initialize a Sequential model.
Add one or more Dense layers with activation function 'relu'.
End with a Dense output layer using 'softmax' for multi-class classification.
Compile the Model
Set the optimizer to 'adam', loss function to 'categorical_crossentropy', and evaluation metric to 'accuracy'.
Train the Model
Fit the model on training data with:
epochs = 50
batch_size = 8
verbose = 1 to show progress.
Evaluate the Model
Test the model on the test set and print the final accuracy.

Code:
import numpy as np
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import OneHotEncoder, StandardScaler
from [Link] import Sequential
from [Link] import Dense

# Load the Iris dataset


iris = load_iris()
X = [Link]
y = [Link](-1, 1)

# One-hot encode the target


encoder = OneHotEncoder(sparse_output=False)
y_encoded = encoder.fit_transform(y)

# Normalize the features

YASHKUMAR MAYANI (92310103058) BATCH: B |27


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

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

# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_encoded, test_size=0.2, random_state=42)

# Build ANN model


modelrelu = Sequential()
[Link](Dense(10, input_shape=(4,), activation='relu'))
[Link](Dense(8, activation='relu'))
[Link](Dense(3, activation='softmax')) # 3 classes for Iris

# Compile the model


[Link](optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Train the model


[Link](X_train, y_train, epochs=50, batch_size=8, verbose=1)

# Evaluate the model


lossrelu, accuracymodelrelu = [Link](X_test, y_test)
print(f'\nTest accuracymodelrelu: {accuracymodelrelu:.2f}')

OUTPUT.
Epoch 1/50
/usr/local/lib/python3.11/dist-packages/keras/src/layers/core/[Link]: UserWarning: Do not pass an
`input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an
`Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer, **kwargs)
15/15 ━━━━━━━━━━━━━━━━━━━━ 1s 4ms/step - accuracy: 0.4177 - loss: 0.9845
Epoch 2/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.5192 - loss: 0.8998
Epoch 3/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.5409 - loss: 0.7813
Epoch 4/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.7026 - loss: 0.7730
Epoch 5/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.7486 - loss: 0.7528
Epoch 6/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.7577 - loss: 0.7611
Epoch 7/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.8688 - loss: 0.6049
Epoch 8/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.8615 - loss: 0.6120
Epoch 9/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.8538 - loss: 0.5578

YASHKUMAR MAYANI (92310103058) BATCH: B |28


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Epoch 10/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.8309 - loss: 0.5873
Epoch 11/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.8396 - loss: 0.5107
Epoch 12/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.8824 - loss: 0.4528
Epoch 13/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.8717 - loss: 0.4495
Epoch 14/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.8017 - loss: 0.4702
Epoch 15/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.8670 - loss: 0.4247
Epoch 16/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.8362 - loss: 0.4228
Epoch 17/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.9511 - loss: 0.3077
Epoch 18/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.9021 - loss: 0.3525
Epoch 19/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9134 - loss: 0.3509
Epoch 20/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.9275 - loss: 0.3199
Epoch 21/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9601 - loss: 0.2630
Epoch 22/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.9121 - loss: 0.2963
Epoch 23/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 17ms/step - accuracy: 0.9115 - loss: 0.2822
Epoch 24/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.9183 - loss: 0.2299
Epoch 25/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.8959 - loss: 0.2652
Epoch 26/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.9027 - loss: 0.2680
Epoch 27/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.9263 - loss: 0.2165
Epoch 28/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.9693 - loss: 0.2055
Epoch 29/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 10ms/step - accuracy: 0.9818 - loss: 0.1834
Epoch 30/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 23ms/step - accuracy: 0.9559 - loss: 0.2215
Epoch 31/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - accuracy: 0.9590 - loss: 0.2215

YASHKUMAR MAYANI (92310103058) BATCH: B |29


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Epoch 32/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 13ms/step - accuracy: 0.9576 - loss: 0.1799
Epoch 33/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 11ms/step - accuracy: 0.9626 - loss: 0.1772
Epoch 34/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 15ms/step - accuracy: 0.9577 - loss: 0.1898
Epoch 35/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 1s 8ms/step - accuracy: 0.9477 - loss: 0.1902
Epoch 36/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 16ms/step - accuracy: 0.9783 - loss: 0.1622
Epoch 37/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 1s 11ms/step - accuracy: 0.9860 - loss: 0.1189
Epoch 38/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 11ms/step - accuracy: 0.9781 - loss: 0.1437
Epoch 39/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step - accuracy: 0.9763 - loss: 0.1346
Epoch 40/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.9736 - loss: 0.1541
Epoch 41/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - accuracy: 0.9693 - loss: 0.1099
Epoch 42/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 14ms/step - accuracy: 0.9710 - loss: 0.1106
Epoch 43/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.9769 - loss: 0.1255
Epoch 44/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9769 - loss: 0.1268
Epoch 45/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9411 - loss: 0.1532
Epoch 46/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.9493 - loss: 0.1219
Epoch 47/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9595 - loss: 0.1110
Epoch 48/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9630 - loss: 0.1258
Epoch 49/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9733 - loss: 0.1136
Epoch 50/50
15/15 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - accuracy: 0.9875 - loss: 0.0858
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 192ms/step - accuracy: 1.0000 - loss: 0.0643

Test accuracymodelrelu: 1.00

YASHKUMAR MAYANI (92310103058) BATCH: B |30


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

Practical 13: Exploring activation function in ANN.

Basic Algorithmic Steps:


1. Import Libraries
Import required libraries for data handling, model building, and visualization.
2. Check for GPU Availability
Enable GPU memory growth if available to improve training efficiency.
3. Load Dataset
Load the Iris dataset from scikit-learn.
4. Preprocess Data
5. Encode target labels using one-hot encoding.
6. Standardize the feature values using StandardScaler.
7. Split the dataset into training and testing sets.
8. Define Model Structure
Create a function that builds an ANN model with:
9. Input layer with 4 neurons
10. Two hidden layers with 10 and 8 neurons respectively
11. Output layer with 3 neurons (softmax activation)
12. Customizable activation function for hidden layers
13. Train Models with Different Activation Functions
For each activation function (relu, tanh, sigmoid):
14. Build and compile the model
15. Train the model for 50 epochs
16. Store the training loss for each epoch
17. Record final loss after 50 epochs
18. Plot Training Loss (Black & White Friendly)
Create a line plot showing loss trends for all three activation functions using different line styles and markers.
19. Display Final Loss Values

Code:
import numpy as np
import [Link] as plt
import tensorflow as tf
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import OneHotEncoder, StandardScaler
from [Link] import Sequential
from [Link] import Dense
from [Link] import Input

#GPU Setup
gpus = [Link].list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
[Link].set_memory_growth(gpu, True)
print(f"Using GPU: {gpus[0].name}")
except RuntimeError as e:
print(e)

YASHKUMAR MAYANI (92310103058) BATCH: B |31


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

else:
print("No GPU detected. Running on CPU.")

# Load and preprocess data


iris = load_iris()
X = [Link]
y = [Link](-1, 1)

encoder = OneHotEncoder(sparse_output=False)
y_encoded = encoder.fit_transform(y)

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

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

# Model builder
def build_model(activation_func):
model = Sequential()
[Link](Input(shape=(4,)))
[Link](Dense(10, activation=activation_func))
[Link](Dense(8, activation=activation_func))
[Link](Dense(3, activation='softmax'))
[Link](optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
return model

# Train models and track losses


activations = ['relu', 'tanh', 'sigmoid']
loss_history = {}
final_losses = {}

for act in activations:


[Link].clear_session()
model = build_model(act)
history = [Link](X_train, y_train, epochs=50, batch_size=8, verbose=0)
loss_history[act] = [Link]['loss']
final_losses[act] = [Link]['loss'][-1]

# Plot loss over epochs with line styles for B&W


[Link](figsize=(10, 6))
[Link](range(1, 51), loss_history['relu'], label='ReLU', linestyle='-', linewidth=2, marker='o', markersize=4)
[Link](range(1, 51), loss_history['tanh'], label='Tanh', linestyle='--', linewidth=2, marker='s', markersize=4)
[Link](range(1, 51), loss_history['sigmoid'], label='Sigmoid', linestyle='-.', linewidth=2, marker='^',
markersize=4)

[Link]('Training Loss over Epochs (Black & White Friendly)')


[Link]('Epoch')

YASHKUMAR MAYANI (92310103058) BATCH: B |32


FACULTY OF ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
01CE0617– MACHINE LEARNING

[Link]('Training Loss')
[Link]()
[Link](True, linestyle='--', alpha=0.6)
plt.tight_layout()
[Link]()

# Print final test losses for documentation


print("\nFinal Training Loss After 50 Epochs:")
for act in activations:
print(f"{[Link]()} → Loss: {final_losses[act]:.4f}")

OUTPUT.

YASHKUMAR MAYANI (92310103058) BATCH: B |33

Common questions

Powered by AI

Training an ANN involves several steps: data preprocessing, model building, compilation, training, and evaluation. In preprocessing, data is typically normalized and labels encoded; this ensures uniform range and format for effective learning. The model architecture is designed by defining input shapes and layer specifications. Compilation involves setting the optimizer, loss function, and metrics to be used. During training, epoch numbers and batch sizes influence convergence and resource utilization. Each of these steps requires careful consideration of data properties, computational constraints, and desired outcomes to optimize model performance without overfitting .

SVM is a discriminative classifier defined by a separating hyperplane, and it is effective in high-dimensional spaces. It works well for both linear and non-linear data by using kernels. SVM is more computationally intensive than Naïve Bayes but can achieve higher accuracy in complex datasets. Gaussian Naïve Bayes is a probabilistic classifier based on Bayes' Theorem with an assumption of independence among predictors. It is simple to implement, requires less training data, and can perform well with small datasets or datasets where the assumption of independence (between features) holds more closely .

The Sequential model in Keras is constructed with layers added in sequence: it begins with an input layer matching the feature size (4 neurons for the Iris dataset), followed by hidden layers to capture non-linearities, and ends with an output layer using softmax activation for classification into three classes. The hidden layers with 'relu' activation functions help in learning complex patterns by introducing non-linearity. The model is compiled with 'adam' optimizer and 'categorical_crossentropy' loss due to its suitability for multi-class classification, aiming to maximize accuracy by effectively optimizing weights during training .

Precision, recall, and F1 score offer nuanced insights into classifier performance beyond accuracy, which only provides a measure of total correct predictions. Precision measures the correctness of positive predictions (True Positives / (True Positives + False Positives)), recall assesses the ability of a classifier to find all relevant instances (True Positives / (True Positives + False Negatives)), and F1 score, the harmonic mean of precision and recall, balances both metrics, useful in imbalanced class situations where accuracy might be misleading .

The decision boundary of an SVM is visualized using DecisionBoundaryDisplay.from_estimator() in a plot with training and testing points overlaid. This visualization shows how the SVM distinguishes between classes by finding the hyperplane that maximally separates different classes. This allows us to assess the model's ability to generalize across the dataset by observing how well it discriminates between classes, indicating regions of potential overlap and helping in diagnosing underfitting or overfitting issues .

MSE is the standard metric for gauging the performance of linear regression models as it provides a clear, easily interpretable measure of prediction error by averaging the squared differences between predicted and actual values. The use of squares penalizes larger errors, making MSE particularly sensitive to outliers in data. MSE is calculated as the average of squared residuals: \( \frac{1}{n} \sum_{i=1}^{n}(y_i - \hat{y}_i)^2 \), where \(y_i\) is the actual value and \(\hat{y}_i\) is the predicted value for the dataset .

The choice of activation function affects how an ANN learns through backpropagation, influencing convergence speed and the ability to capture non-linear patterns. Different functions (relu, tanh, sigmoid) can affect the ability of the network to break linearity and complexity in the input space. ReLU offers faster convergence due to its non-saturating nature, tanh works well with zero-centered data, and sigmoid can cause vanishing gradients due to its bounded output. The output layer commonly uses softmax for multi-class classification which helps in predicting class probabilities .

Scaling the data improves the efficiency and performance of SVMs by standardizing the range of input features. This is critical for methods like SVM that rely on distance metrics, as unscaled data can lead to a dominance of features with larger ranges, potentially resulting in suboptimal decision boundaries. In the provided implementation, StandardScaler is used to transform the data so it has a mean of zero and a unit variance, which helps in achieving a more accurate and stable classification .

One-hot encoding transforms categorical variables into a format that can be provided to machine learning algorithms to improve their performance. This technique avoids giving an ordinal relationship among categorical values which could mislead models. In the context of linear regression, one-hot encoding helps in preventing the model from assuming any priority or ordering among categories, thereby avoiding false parameter estimates .

A confusion matrix provides a detailed breakdown of how a classification model is performing, displaying the number of true positives, true negatives, false positives, and false negatives. It's a crucial tool for evaluating classifiers beyond simple accuracy, providing insights into class-specific performance. In the setup, the confusion matrix is visualized using Seaborn's heatmap to offer clear, intuitive insights into model performance, making it easier to identify areas where the model might need improvement .

You might also like