You are on page 1of 4

Department of Computer Science HITEC

University, Taxila BS Computer Science


Program (Batch 2021)

CS-428 Introduction to Machine Learning


3(2+1)
Section B

Lab Report # 12
Muhammad Jahangir
21-cs-089
Basit Ali
21-cs-104
Instructor: Ms. Faiza Jahangir
Task1 Generate ‘generative_model.txt’ file and write the detail paragraph
about generative model on it.
Code
fileID = fopen('generative_model.txt', 'w');
fprintf(fileID, 'Generative Model Details:\n');
fprintf(fileID, 'A generative model learns the joint probability
distribution of data and labels, enabling the generation of new
samples.\n');
fprintf(fileID, 'It is utilized in tasks such as data
generation, missing value imputation, and more.\n');
fclose(fileID);

Results

Code Explanation
1. fopen('generative_model.txt', 'w'): Opens a file named 'generative_model.txt' in write mode
('w').
2. fprintf(fileID, ... ): Writes text to the opened file using the file identifier (fileID). Each fprintf
call writes a line to the file with the specified content.
3. fclose(fileID): Closes the file associated with the file identifier to ensure that changes are saved
and the file is properly closed.

Task 2 Read any of your own choice ‘.csv’ file and display their content in
command window.
Code
file_path = 'iris.csv';
data = readtable(file_path);
disp(data);
Result

Task 3 Apply Naive Bayes algorithm on the above ‘.csv’ file’.Code


file_path = 'iris.csv';
data = readtable(file_path);
X = data(:, 1:end-1);
y = data(:, end);
X = table2array(X);
y = table2array(y);
naiveBayesModel = fitcnb(X, y);
disp(naiveBayesModel);

Result
Explanation:
This reads the 'iris.csv' dataset, separates it into features and target labels, preprocesses the target
labels to ensure compatibility with the Naive Bayes algorithm, trains a Naive Bayes classifier
using the prepared data, and displays essential information about the trained model. This script
effectively establishes a workflow for classification, enabling the analysis and prediction of
classes based on the given features within the 'iris.csv' dataset.

Conclusion:
In this lab several tasks were completed effectively. The creation of 'generative_model.txt'
provided a detailed overview of generative models, highlighting their role in data understanding
and generation. Reading and displaying a CSV file exhibited MATLAB's capability to handle
and showcase dataset contents within the command window. Additionally, while not including
specific training steps, the groundwork for Naive Bayes implementation showcased the potential
for machine learning applications within MATLAB. These tasks underscore MATLAB's
versatility, showcasing its prowess in file operations, data presentation, and laying the foundation
for machine learning exploration

You might also like