Assignment 1 — Employee Salary Category Prediction using K-Nearest Neighbors (KNN)
Objective
Build a K-Nearest Neighbors (KNN) Classification model to predict whether an employee belongs to the High Salary or Low Salary category.
Dataset
File Name
employee_salary_knn.csv
| EmployeeID | ExperienceYears | Education | Skills | InterviewScore | SalaryCategory |
|---|---|---|---|---|---|
| 1 | 1 | Bachelors | Python | 55 | Low |
| 2 | 2 | Bachelors | Java | 58 | Low |
| 3 | 3 | Masters | Python | 65 | Low |
| 4 | 4 | Masters | Java | 68 | Low |
| 5 | 5 | Masters | Python | 72 | High |
| 6 | 6 | Masters | Python | 75 | High |
| 7 | 7 | PhD | Python | 80 | High |
| 8 | 8 | PhD | Data Science | 85 | High |
| 9 | 2 | Diploma | Java | 52 | Low |
| 10 | 3 | Diploma | Java | 60 | Low |
| 11 | 4 | Bachelors | Python | 66 | Low |
| 12 | 5 | Bachelors | Data Science | 70 | High |
| 13 | 6 | Masters | Data Science | 74 | High |
| 14 | 7 | Masters | Python | 79 | High |
| 15 | 8 | PhD | AI | 90 | High |
| 16 | 1 | Diploma | Java | 50 | Low |
| 17 | 2 | Diploma | Python | 57 | Low |
| 18 | 3 | Bachelors | Java | 63 | Low |
| 19 | 4 | Masters | AI | 69 | Low |
| 20 | 5 | Masters | AI | 73 | High |
| 21 | 6 | Masters | Python | 76 | High |
| 22 | 7 | PhD | AI | 82 | High |
| 23 | 8 | PhD | Data Science | 88 | High |
| 24 | 2 | Diploma | Python | 56 | Low |
| 25 | 3 | Bachelors | Python | 64 | Low |
| 26 | 4 | Bachelors | Java | 67 | Low |
| 27 | 5 | Masters | Data Science | 71 | High |
| 28 | 6 | Masters | AI | 77 | High |
| 29 | 7 | PhD | Python | 83 | High |
| 30 | 8 | PhD | AI | 91 | High |
| 31 | 1 | Diploma | Java | 51 | Low |
| 32 | 2 | Bachelors | Java | 59 | Low |
| 33 | 3 | Bachelors | Python | 62 | Low |
| 34 | 4 | Masters | Data Science | 68 | Low |
| 35 | 5 | Masters | Python | 72 | High |
| 36 | 6 | Masters | AI | 75 | High |
| 37 | 7 | PhD | AI | 81 | High |
| 38 | 8 | PhD | Data Science | 86 | High |
| 39 | 2 | Diploma | Python | 54 | Low |
| 40 | 3 | Diploma | Java | 61 | Low |
| 41 | 4 | Bachelors | AI | 66 | Low |
| 42 | 5 | Bachelors | Data Science | 70 | High |
| 43 | 6 | Masters | Python | 74 | High |
| 44 | 7 | Masters | AI | 80 | High |
| 45 | 8 | PhD | Python | 87 | High |
| 46 | 2 | Diploma | Java | 53 | Low |
| 47 | 3 | Bachelors | Python | 65 | Low |
| 48 | 5 | Masters | AI | 73 | High |
| 49 | 6 | Masters | Data Science | 78 | High |
| 50 | 8 | PhD | AI | 92 | High |
Before Training
This dataset cannot be used directly.
The following preprocessing is required.
Step 1 — Handle Missing Values (if any)
Check whether the dataset contains missing values.
Step 2 — Encode Categorical Columns
Use Label Encoding for:
Education
Skills
SalaryCategory
Example
| Original | Encoded |
|---|---|
| Diploma | 0 |
| Bachelors | 1 |
| Masters | 2 |
| PhD | 3 |
Step 3 — Feature Scaling
Since KNN is a distance-based algorithm, apply StandardScaler (or MinMaxScaler) to the input features before training.
Features (X)
ExperienceYears
Education
Skills
InterviewScore
Target (y)
SalaryCategory
Practical Questions
Question 1
Load the dataset using Pandas.
Question 2
Check the first five records.
Question 3
Display the shape of the dataset.
Question 4
Check the data types of all columns.
Question 5
Check whether the dataset contains missing values.
Question 6
Apply Label Encoding to all categorical columns.
Question 7
Separate the features (X) and target (y).
Question 8
Split the dataset into training and testing sets using an 80:20 ratio.
Question 9
Apply StandardScaler to the training and testing data.
Question 10
Create a KNN classifier with:
K = 3
Train the model.
Question 11
Predict the salary category for the test dataset.
Question 12
Calculate the Accuracy of the model.
Question 13
Generate the Confusion Matrix.
Question 14
Calculate:
Precision
Recall
F1-Score
Question 15
Repeat the experiment using:
K = 5
K = 7
K = 9
Compare the accuracy of each model.
Question 16
Which value of K gives the best accuracy?
Question 17
Predict the salary category of the following employee:
| Experience | Education | Skills | Interview Score |
|---|---|---|---|
| 6 Years | Masters | AI | 78 |
# ==========================================
# Employee Salary Category Prediction using KNN
# ==========================================
import pandas as pd
# Machine Learning Libraries
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
# Evaluation Metrics
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import classification_report
# ==========================================
# Load Dataset
# ==========================================
df = pd.read_csv("employee_salary_knn.csv")
print("Dataset Loaded Successfully\n")
# ==========================================
# Display Dataset
# ==========================================
print("First 5 Records")
print(df.head())
print("\nLast 5 Records")
print(df.tail())
print("\nShape")
print(df.shape)
print("\nInformation")
print(df.info())
print("\nData Types")
print(df.dtypes)
print("\nMissing Values")
print(df.isnull().sum())
# ==========================================
# Label Encoding
# ==========================================
education_encoder = LabelEncoder()
skills_encoder = LabelEncoder()
salary_encoder = LabelEncoder()
df["Education"] = education_encoder.fit_transform(df["Education"])
df["Skills"] = skills_encoder.fit_transform(df["Skills"])
df["SalaryCategory"] = salary_encoder.fit_transform(df["SalaryCategory"])
print("\nEncoded Dataset")
print(df.head())
# ==========================================
# Features and Target
# ==========================================
X = df[
[
"ExperienceYears",
"Education",
"Skills",
"InterviewScore"
]
]
y = df["SalaryCategory"]
# ==========================================
# Split Dataset
# ==========================================
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.20,
random_state=42
)
# ==========================================
# Feature Scaling
# ==========================================
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# ==========================================
# Create KNN Model
# ==========================================
model = KNeighborsClassifier(
n_neighbors=3
)
# ==========================================
# Train Model
# ==========================================
model.fit(
X_train,
y_train
)
print("\nModel Trained Successfully")
# ==========================================
# Prediction
# ==========================================
y_pred = model.predict(X_test)
print("\nActual Values")
print(y_test.values)
print("\nPredicted Values")
print(y_pred)
# ==========================================
# Accuracy
# ==========================================
accuracy = accuracy_score(
y_test,
y_pred
)
print("\nAccuracy")
print(accuracy)
print("Accuracy Percentage :", accuracy * 100)
# ==========================================
# Confusion Matrix
# ==========================================
cm = confusion_matrix(
y_test,
y_pred
)
print("\nConfusion Matrix")
print(cm)
# ==========================================
# Precision
# ==========================================
precision = precision_score(
y_test,
y_pred
)
print("\nPrecision")
print(precision)
# ==========================================
# Recall
# ==========================================
recall = recall_score(
y_test,
y_pred
)
print("\nRecall")
print(recall)
# ==========================================
# F1 Score
# ==========================================
f1 = f1_score(
y_test,
y_pred
)
print("\nF1 Score")
print(f1)
# ==========================================
# Classification Report
# ==========================================
print("\nClassification Report")
print(
classification_report(
y_test,
y_pred
)
)
# ==========================================
# Compare Different K Values
# ==========================================
print("\n==============================")
print("Compare Different K Values")
print("==============================")
for k in [3, 5, 7, 9]:
model = KNeighborsClassifier(
n_neighbors=k
)
model.fit(
X_train,
y_train
)
prediction = model.predict(X_test)
accuracy = accuracy_score(
y_test,
prediction
)
print("K =", k, "Accuracy =", accuracy)
# ==========================================
# Best K
# ==========================================
best_accuracy = 0
best_k = 0
for k in range(1, 16):
model = KNeighborsClassifier(
n_neighbors=k
)
model.fit(
X_train,
y_train
)
prediction = model.predict(X_test)
accuracy = accuracy_score(
y_test,
prediction
)
if accuracy > best_accuracy:
best_accuracy = accuracy
best_k = k
print("\nBest K :", best_k)
print("Best Accuracy :", best_accuracy)
# ==========================================
# Predict New Employee
# ==========================================
new_employee = pd.DataFrame(
{
"ExperienceYears": [6],
"Education": ["Masters"],
"Skills": ["AI"],
"InterviewScore": [78]
}
)
# Encode using previously trained encoders
new_employee["Education"] = education_encoder.transform(
new_employee["Education"]
)
new_employee["Skills"] = skills_encoder.transform(
new_employee["Skills"]
)
# Scale
new_employee = scaler.transform(new_employee)
# Prediction
prediction = model.predict(new_employee)
print("\nPrediction")
if prediction[0] == 1:
print("High Salary Category")
else:
print("Low Salary Category")
Assignment 2 — Loan Approval Prediction using K-Nearest Neighbors (KNN)
Objective
Build a K-Nearest Neighbors (KNN) model to predict whether a customer's loan application will be Approved or Rejected.
Banks often compare a new applicant with previous applicants having similar income, employment, credit history, and education. This makes KNN a suitable algorithm for this problem.
Dataset
File Name
loan_approval_knn.csv
| CustomerID | Age | Education | EmploymentType | MonthlyIncome | CreditScore | ExistingLoans | City | LoanApproved |
|---|---|---|---|---|---|---|---|---|
| 1 | 24 | Graduate | Private | 25000 | 620 | 0 | Delhi | No |
| 2 | 26 | Graduate | Private | 28000 | 640 | 1 | Delhi | No |
| 3 | 28 | Post Graduate | Government | 35000 | 690 | 1 | Mumbai | Yes |
| 4 | 30 | Post Graduate | Government | 42000 | 730 | 1 | Mumbai | Yes |
| 5 | 22 | Graduate | Private | 22000 | 610 | 0 | Jaipur | No |
| 6 | 32 | Post Graduate | Government | 50000 | 760 | 2 | Delhi | Yes |
| 7 | 35 | PhD | Government | 65000 | 810 | 1 | Bangalore | Yes |
| 8 | 27 | Graduate | Private | 30000 | 660 | 1 | Pune | No |
| 9 | 29 | Graduate | Business | 45000 | 710 | 1 | Delhi | Yes |
| 10 | 31 | Post Graduate | Business | 52000 | 750 | 2 | Mumbai | Yes |
| 11 | 25 | Graduate | Private | 27000 | 630 | 0 | Delhi | No |
| 12 | 33 | PhD | Government | 70000 | 820 | 1 | Bangalore | Yes |
| 13 | 34 | Post Graduate | Business | 56000 | 770 | 2 | Mumbai | Yes |
| 14 | 23 | Graduate | Private | 24000 | 615 | 0 | Jaipur | No |
| 15 | 36 | PhD | Government | 72000 | 830 | 1 | Delhi | Yes |
| 16 | 26 | Graduate | Private | 29000 | 645 | 1 | Pune | No |
| 17 | 28 | Graduate | Business | 40000 | 700 | 1 | Delhi | Yes |
| 18 | 30 | Post Graduate | Government | 48000 | 740 | 2 | Mumbai | Yes |
| 19 | 21 | Graduate | Private | 21000 | 600 | 0 | Jaipur | No |
| 20 | 37 | PhD | Government | 76000 | 840 | 2 | Bangalore | Yes |
| 21 | 24 | Graduate | Private | 26000 | 625 | 0 | Delhi | No |
| 22 | 27 | Graduate | Business | 39000 | 695 | 1 | Pune | Yes |
| 23 | 29 | Post Graduate | Government | 46000 | 725 | 1 | Mumbai | Yes |
| 24 | 31 | Business | Business | 54000 | 760 | 2 | Delhi | Yes |
| 25 | 22 | Graduate | Private | 23000 | 610 | 0 | Jaipur | No |
| 26 | 34 | PhD | Government | 68000 | 815 | 1 | Bangalore | Yes |
| 27 | 28 | Graduate | Private | 31000 | 665 | 1 | Delhi | No |
| 28 | 30 | Business | Business | 50000 | 745 | 2 | Mumbai | Yes |
| 29 | 35 | Post Graduate | Government | 62000 | 800 | 1 | Delhi | Yes |
| 30 | 23 | Graduate | Private | 24000 | 620 | 0 | Jaipur | No |
| 31 | 26 | Graduate | Private | 28000 | 640 | 1 | Delhi | No |
| 32 | 32 | Government | Government | 53000 | 755 | 2 | Mumbai | Yes |
| 33 | 36 | PhD | Government | 74000 | 835 | 1 | Bangalore | Yes |
| 34 | 24 | Graduate | Private | 25500 | 628 | 0 | Delhi | No |
| 35 | 27 | Graduate | Business | 41000 | 705 | 1 | Pune | Yes |
| 36 | 29 | Post Graduate | Government | 47000 | 730 | 2 | Mumbai | Yes |
| 37 | 22 | Graduate | Private | 22500 | 608 | 0 | Jaipur | No |
| 38 | 34 | Business | Business | 59000 | 785 | 1 | Delhi | Yes |
| 39 | 31 | Post Graduate | Government | 51000 | 750 | 2 | Mumbai | Yes |
| 40 | 37 | PhD | Government | 78000 | 845 | 2 | Bangalore | Yes |
| 41 | 25 | Graduate | Private | 27500 | 635 | 0 | Delhi | No |
| 42 | 28 | Graduate | Business | 43000 | 715 | 1 | Pune | Yes |
| 43 | 30 | Post Graduate | Government | 49500 | 740 | 2 | Mumbai | Yes |
| 44 | 21 | Graduate | Private | 21500 | 602 | 0 | Jaipur | No |
| 45 | 35 | PhD | Government | 71000 | 825 | 1 | Delhi | Yes |
| 46 | 27 | Graduate | Private | 30000 | 660 | 1 | Delhi | No |
| 47 | 33 | Business | Business | 57000 | 775 | 2 | Mumbai | Yes |
| 48 | 29 | Post Graduate | Government | 48500 | 735 | 2 | Delhi | Yes |
| 49 | 23 | Graduate | Private | 23500 | 618 | 0 | Jaipur | No |
| 50 | 38 | PhD | Government | 80000 | 850 | 2 | Bangalore | Yes |
Data Cleaning Required
Check the dataset for:
Missing values
Duplicate records
Incorrect data types
Encoding Required
Use Label Encoding for the following columns:
Education
EmploymentType
City
LoanApproved
Feature Scaling Required
Since KNN is a distance-based algorithm, apply:
StandardScaler (recommended)
or
MinMaxScaler
Features (X)
Age
Education
EmploymentType
MonthlyIncome
CreditScore
ExistingLoans
City
Target (y)
LoanApproved
Practical Questions
Question 1
Load the dataset using Pandas.
Question 2
Display the first five records.
Question 3
Display the last five records.
Question 4
Display the shape of the dataset.
Question 5
Display the information (info()) of the dataset.
Question 6
Check all missing values.
Question 7
Check duplicate records.
Question 8
Apply Label Encoding to all categorical columns.
Question 9
Separate the input features (X) and target (y).
Question 10
Split the dataset into training and testing sets (80:20).
Question 11
Apply StandardScaler to the training and testing data.
Question 12
Train a KNN Classifier with:
K = 5
Question 13
Predict the loan approval status of the test dataset.
Question 14
Calculate the model accuracy.
Question 15
Generate the confusion matrix.
Question 16
Calculate:
Precision
Recall
F1-Score
Question 17
Display the classification report.
Question 18
Compare the model performance for:
K = 3
K = 5
K = 7
K = 9
K = 11
Which value of K gives the highest accuracy?
Question 19
Predict the loan approval status of the following new customer:
| Age | Education | EmploymentType | MonthlyIncome | CreditScore | ExistingLoans | City |
|---|---|---|---|---|---|---|
| 30 | Post Graduate | Government | 52000 | 760 | 1 | Delhi |
0 Comments