0% found this document useful (0 votes)
5 views

Lab Experiment 5

The document outlines five machine learning experiments, including linear regression for predicting student marks, logistic regression for classifying student status, decision tree classification for accuracy prediction, data visualization using Matplotlib and Seaborn, and confusion matrix visualization. Each experiment includes code snippets demonstrating the implementation of the respective algorithms and visualization techniques. The experiments cover various aspects of machine learning and data analysis, providing practical examples for learners.

Uploaded by

praveen846375
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab Experiment 5

The document outlines five machine learning experiments, including linear regression for predicting student marks, logistic regression for classifying student status, decision tree classification for accuracy prediction, data visualization using Matplotlib and Seaborn, and confusion matrix visualization. Each experiment includes code snippets demonstrating the implementation of the respective algorithms and visualization techniques. The experiments cover various aspects of machine learning and data analysis, providing practical examples for learners.

Uploaded by

praveen846375
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ML_LAB EXPERIMENTS

Experiment 1: Write a linear regression program to


predict the students mark and also to visualize the model
by using graph

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
hours = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
marks = np.array([10, 20, 30, 40, 50])
model = LinearRegression()
model.fit(hours, marks)
input_hours = float(input("Enter the number of hours studied:
"))
input_hours_reshaped = np.array([[input_hours]])
predicted_marks = model.predict(input_hours_reshaped)
print(f"Predicted marks for {input_hours} hours of study:
{predicted_marks[0]}")
plt.scatter(hours, marks, color='blue', label='Actual')
plt.plot(hours, model.predict(hours), color='red',
label='Predicted')
plt.xlabel("Hours Studied")
plt.ylabel("Marks Obtained")
plt.legend()
plt.show()
Experiment 2: Write a Logistic Regression program to
find the student status as good, average or poor

import numpy as np
from sklearn.linear_model import LogisticRegression
X = np.array([[10], [20], [30], [40], [50], [60]])
y = np.array([0 if mark[0] < 40 else 1 for mark in X])
model = LogisticRegression()
model.fit(X, y)
def get_student_status(mark):
if mark >= 60:
return "Good"
elif 40 <= mark < 60:
return "Average"
else:
return "Poor"
try:
test_mark = float(input("Enter the mark to predict::"))
test_subject = np.array([[test_mark]])
prediction = model.predict(test_subject)
probability = model.predict_proba(test_subject)
status = get_student_status(test_mark)
print(f"\nInput Mark: {test_mark}")
print(f"Prediction: {'Pass' if prediction[0] == 1 else 'Fail'}")
print(f"Student Status: {status}")

except ValueError:
print("Invalid input. Please enter a numeric value.")
Experiment 3: Write and implement a Decision Tree
Classifier algorithm code to predict the accuracy

from sklearn.model_selection import train_test_split


from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
import numpy as np

X = np.array([[0], [0], [0], [1], [1], [1], [1], [1]])


y = np.array([0, 0, 0, 1, 1, 1, 1, 1])

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.3, random_state=40)

clf = DecisionTreeClassifier()

clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)

accuracy = (accuracy_score(y_test, y_pred))


print("Decision Tree Model Accuracy:", accuracy)

Experiment 4: Write a program for data visualization to


Comparative Visualization of Two Line Plots Using
Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="whitegrid")
a = [1, 2, 3, 4, 5]
b = [2, 3, 9, 7, 11]

c = [1, 2, 3, 4, 5]
d = [2, 3, 9, 7, 11]

plt.figure(figsize=(5,5))
plt.subplot(1, 2, 1)
plt.plot(a, b, marker='o', color='g', label='Line Plot 1')
plt.title('first')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(c, d, marker='o', color='r', label='Line Plot 2')
plt.title('second')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.legend()
plt.tight_layout()

plt.show()

Experiment 5: Write a program for confusion matrix to


Visualization of confusion matrix graph Using Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix,
ConfusionMatrixDisplay
y_true = np.array([0, 1, 2, 2, 0, 1, 1, 0, 2, 2])
y_pred = np.array([0, 0, 2, 2, 0, 1, 2, 0, 2, 1])

cm = confusion_matrix(y_true, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot(cmap=plt.cm.Blues)
plt.show()

You might also like