Untitled
Anonymous
plain_text
04/25/2026 7:29 AM
2.0 KB
10
Indexable
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score ------------------ DATASET ------------------
# Dummy student data: [Exam Score 1, Exam Score 2]
X = np.array([
[85, 75], [90, 95], [60, 50], [45, 30],
[30, 40], [95, 95], [70, 60], [50, 55],
[40, 45], [60, 70]
])
# Output labels: 1 = Pass, 0 = Fail
y = np.array([1, 1, 0, 0, 0, 1, 1, 0, 0, 1]) ------------------ SPLIT DATA ------------------
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42) ------------------ MODEL ------------------
# Create KNN model with k=3
knn = KNeighborsClassifier(n_neighbors=3)
# Train the model
knn.fit(X_train, y_train) ------------------ TESTING ------------------
# Predict test data
y_pred = knn.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy on test set: {:.2f}".format(accuracy)) ------------------ USER INPUT ------------------
# Take input from user
exam_score1 = float(input("Enter Exam Score 1: "))
exam_score2 = float(input("Enter Exam Score 2: "))
# Convert input into array
user_input = np.array([[exam_score1, exam_score2]])
# Predict result
predicted_outcome = knn.predict(user_input)
# Display result
if predicted_outcome[0] == 1:
print("Based on the exam scores provided, the student is predicted to pass.")
else:
print("Based on the exam scores provided, the student is predicted to fail.")Editor is loading...
Leave a Comment