Machine Learning Lab Manual
Machine Learning Lab Manual
LAB MANUAL
SEMESTER <7th>
RUNGTA COLLEGE
Rungta Educational Campus,
Kohka-Kurud Road, Bhilai,
Chhattisgarh, India
Phone No. 0788-6666666
MANAGED BY : SANTOSH RUNGTA GROUP OF INSTITUTIONS
Prepared By
<Faculty Name 1> <Faculty Name 2>
RUNGTA COLLEGE OF ENGINEERING & TECHNOLOGY
LAB MANUAL
DOs:
▪ Take help from the Manual / Work Book for preparation of the experiment.
▪ For any abnormal working of the machine consult the Faculty In-charge/
Lab Assistant.
▪ Shut down the machine and switch off the power supply after performing
the experiment.
DON’Ts :
▪ Do not tamper the instruments in the Lab and do not disturb their settings.
LIST OF EXPERIMENTS
AS PER THE SYLLABUS PRESCRIBED BY THE UNIVERSITY
For Example…
Chhattisgarh Swami Vivekananda Technical
University, Bhilai
Branch : Computer Science and Engineering Semester : VII
Subject : Machine Learning Lab Subject Code:
D022721(022)
Total Lab Periods : 36 Batch Size: 30
Maximum Marks : 40 Minimum Marks:
20
COURSE OBJECTIVES
1. To be able to use Numpy along with Matplotlib for visual representation of
data.
2. To be able to create a Supervised Learning models in Python.
3. To be able to create an Un-Supervised Learning models in Python.
4. To be able to implement Artificial Neural Network in Python.
COURSE OUTCOMES
After successful completion of this course, the students will be able to-
1. ApplyNumpy along with Matplotlib for visual analysis of data.
2. Apply Supervised Learning models for problem solving.
3. Apply Un-Supervised Learning models for problem solving.
4. Apply Artificial Neural Network for problem solving.
List of Experiments
1. Write programs to understand the use of Matplotlib for Simple Interactive
Chart, Set the Properties of the Plot, matplotlib and NumPy.
2. Write programs to understand the use of Matplotlib for Working with Multiple
Figures and Axes, Adding Text, Adding a Grid and Adding a Legend.
3. Write programs to understand the use of Matplotlib for Working with Line
Chart, Histogram, Bar Chart, Pie Charts.
4. Write a program in Python to implement Linear Regression for house price
prediction.
(DataSource:https://forge.scilab.org/index.php/p/rdataset/source/file/master/
csv/MASS/Boston).
5. Write a program in Python to implement K Nearest Neighbor classifier for
diabetes classification.(DataSource:https://www.kaggle.com/uciml/pima-indians-
diabetes-database/data).
6. Build a Naive Bayes model in Python totackle a spam classification problem.
(DataSource:(https://www.kaggle.com/uciml/sms-spam-collectiondataset/
downloads/spam.csv/1).
7. Write a Python code to tackle a multi-class classification problem where the
challenge is to classify wine into three types using Decision Tree.
(DataSource:https://gist.github.com/tijptjik/9408623/archive/
b237fa5848349a14a14e5d4107dc7897c21951f5.zip).
8. Write a program in Python to implement Support Vector Machine for diabetes
classification.
(DataSource:https://www.kaggle.com/uciml/pima-indians-diabetes-database/
data).
9. Demonstrate the application of Artificial Neural Network using Python.
TEXT BOOKS
1. Hands On MachineLearning With Python– John Anderson, AI Sciences LLC.
2. Python for Data Analysis, Wes McKinney, O’Reilly.
REFERENCE BOOKS
LIST OF EXPERIMENTS
AS PER RUNGTA COLLEGE OF ENGINEERING & TECHNOLOGY
LIST OF EXPERIMENTS
Exp.
Name of Experiment Page No.
No.
1 Write programs to understand the use of Matplotlib 1
for Simple Interactive Chart, Set the Properties of
the Plot, matplotlib and NumPy.
Experiment No. 1
1.1
#Simple Interactive Chart using Matplotlib:
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
1.2
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y1 = [10, 8, 6, 4, 2]
y2 = [5, 4, 3, 2, 1]
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.show()
1.3
#Using Matplotlib and NumPy together:
y = np.sin(x)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.show()
Experiment No. 2
Aim : Write programs to understand the use of Matplotlib for Working with
Multiple Figures and Axes, Adding Text, Adding a Grid and Adding a Legend
Source Code :
2.1
2.1#Working with Multiple Figures and Axes:
import numpy as np
# Sample data
y1 = np.sin(x)
y2 = np.cos(x)
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Y-axis')
ax1.set_title('Sine Wave')
# Plot data on the second figure
ax2.set_xlabel('X-axis')
ax2.set_ylabel('Y-axis')
ax2.set_title('Cosine Wave')
plt.show()
2.2
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
2.3
2.3#Adding a Grid:
python
Copy code
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
#Adding a Legend:
# Sample data
x = [1, 2, 3, 4, 5]
y1 = [10, 8, 6, 4, 2]
y2 = [5, 4, 3, 2, 1]
# Create two lines on the same plot
plt.legend()
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Experiment No. 3
Aim : Write programs to understand the use of Matplotlib for Working with
Line Chart, Histogram, Bar Chart, Pie Charts.
Source Code :
3.1
##3.1Working with Line Chart:
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
3.2
#3.2Working with Histogram:
import numpy as np
# Create a histogram
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.title('Histogram Example')
plt.show()
3.3
#3.3Working with Bar Chart:
# Sample data
plt.xlabel('Categories')
plt.ylabel('Values')
plt.show()
# Sample data
# Set title
plt.show()
Experiment No. 4
Source Code :
import pandas as pd
import numpy as np
url = "https://forge.scilab.org/index.php/p/rdataset/source/file/master/
csv/MASS/Boston.csv"
boston_data = pd.read_csv(url)
# Select the features and target variable
# Split the data into training and testing sets (80% training, 20%
testing)
model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
r2 = r2_score(y_test, y_pred)
print("R-squared:", r2)
Experiment No. 5
Source Code :
import pandas as pd
import numpy as np
url = "https://www.kaggle.com/uciml/pima-indians-diabetes-database/data"
diabetes_data = pd.read_csv(url)
# Select the features and target variable
# Split the data into training and testing sets (80% training, 20%
testing)
# Create a KNN classifier with k=3 (you can change the value of k)
knn_classifier = KNeighborsClassifier(n_neighbors=3)
knn_classifier.fit(X_train, y_train)
y_pred = knn_classifier.predict(X_test)
print("Accuracy:", accuracy)
print("Confusion Matrix:")
print(conf_matrix)
print("Classification Report:")
print(class_report)
Experiment No. 6
Source Code :
import nltk
nltk.download('punkt')
nltk.download('stopwords')
import pandas as pd
import numpy as np
url =
"https://www.kaggle.com/uciml/sms-spam-collection-dataset/downloads/
spam.csv/1"
# Select the features (SMS text) and target variable (spam or ham)
X = spam_data['v2']
y = spam_data['v1']
def preprocess_text(text):
tokens = word_tokenize(text)
# Remove stopwords
stop_words = set(stopwords.words('english'))
X = X.apply(preprocess_text)
# Split the data into training and testing sets (80% training, 20%
testing)
vectorizer = CountVectorizer()
X_train_counts = vectorizer.fit_transform(X_train)
X_test_counts = vectorizer.transform(X_test)
naive_bayes_classifier = MultinomialNB()
naive_bayes_classifier.fit(X_train_counts, y_train)
y_pred = naive_bayes_classifier.predict(X_test_counts)
print("Accuracy:", accuracy)
print("Confusion Matrix:")
print(conf_matrix)
print("Classification Report:")
print(class_report)
Experiment No. 7
Source Code :
import pandas as pd
import numpy as np
import requests
# Download and extract the Wine dataset from the provided link
url =
"https://gist.github.com/tijptjik/9408623/archive/b237fa5848349a14a14e5d41
07dc7897c21951f5.zip"
response = requests.get(url)
zip_file.extractall()
wine_data =
pd.read_csv("9408623-9408623/b237fa5848349a14a14e5d4107dc7897c21951f5/
wine.csv")
# Split the data into training and testing sets (80% training, 20%
testing)
decision_tree_classifier = DecisionTreeClassifier()
decision_tree_classifier.fit(X_train, y_train)
print("Accuracy:", accuracy)
print("Confusion Matrix:")
print(conf_matrix)
print("Classification Report:")
print(class_report)
Experiment No. 8
Source Code :
import pandas as pd
import numpy as np
url = "https://www.kaggle.com/uciml/pima-indians-diabetes-database/data"
diabetes_data = pd.read_csv(url)
# Select the features and target variable
# Split the data into training and testing sets (80% training, 20%
testing)
svm_classifier = SVC()
svm_classifier.fit(X_train, y_train)
y_pred = svm_classifier.predict(X_test)
print("Accuracy:", accuracy)
print("Confusion Matrix:")
print(conf_matrix)
print("Classification Report:")
print(class_report)
Experiment No. 9
Source Code :
import pandas as pd
import numpy as np
iris_data = load_iris()
X = iris_data.data
y = iris_data.target
y_encoded = to_categorical(y)
# Split the data into training and testing sets (80% training, 20%
testing)
model = Sequential()
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])