Lab Experiment 5
Lab Experiment 5
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
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
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()
cm = confusion_matrix(y_true, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot(cmap=plt.cm.Blues)
plt.show()