-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_prediction.py
More file actions
156 lines (109 loc) · 3.59 KB
/
ai_prediction.py
File metadata and controls
156 lines (109 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
"""AI Prediction
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1q3pA4LqpvgGDOq-QSzb8ZYFWvrsvwPKo
"""
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import accuracy_score, classification_report
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline
from scipy.sparse import hstack
import re
import os
os.listdir("/content")
df = pd.read_csv("/content/AI-Based Hiring Prediction System (1).csv")
df.head()
df.head()
df.tail()
df.sample(5)
df.shape
df.columns
df.info()
df["Recruiter Decision"].value_counts()
df.describe()
df = df.drop(["Resume_ID", "Name"], axis=1)
df["Recruiter Decision"] = df["Recruiter Decision"].map({"Hire":1, "Reject":0})
df.isnull().sum()
df.fillna("", inplace=True)
df["combined_text"] = df["Skills"] + " " + df["Certifications"] + " " + df["Job Role"]
def clean_text(text):
text = text.lower()
text = re.sub(r"[^a-zA-Z ]", "", text)
text = re.sub(r"\s+", " ", text)
return text
df["combined_text"] = df["combined_text"].apply(clean_text)
tfidf = TfidfVectorizer(max_features=3000)
text_features = tfidf.fit_transform(df["combined_text"])
le = LabelEncoder()
df["Education"] = le.fit_transform(df["Education"])
y = df["Recruiter Decision"]
X_numeric = df[[
"Experience (Years)",
"Salary Expectation ($)",
"Projects Count",
"Education"
]]
X_train_num, X_test_num, y_train, y_test = train_test_split(
X_numeric, y, test_size=0.2, random_state=42
)
X_train_text, X_test_text = train_test_split(
text_features, test_size=0.2, random_state=42
)
scaler = StandardScaler()
X_train_num = scaler.fit_transform(X_train_num)
X_test_num = scaler.transform(X_test_num)
X_train = hstack([X_train_text, X_train_num])
X_test = hstack([X_test_text, X_test_num])
models = {
"Logistic Regression": LogisticRegression(),
"Random Forest": RandomForestClassifier(),
"SVM": SVC(probability=True),
"KNN": KNeighborsClassifier()
}
results = {}
for name, model in models.items():
model.fit(X_train, y_train)
preds = model.predict(X_test)
acc = accuracy_score(y_test, preds)
results[name] = acc
print(name)
print("Accuracy:", acc)
print(classification_report(y_test, preds))
pd.DataFrame(results.items(), columns=["Model", "Accuracy"])
pipe = Pipeline([
("tfidf", TfidfVectorizer()),
("clf", LogisticRegression())
])
params = {"clf__C": [0.1, 1, 10]}
grid = GridSearchCV(pipe, params, cv=5)
grid.fit(df["combined_text"], y)
grid.best_params_
def predict_hiring(skills, experience, education, certs, projects, salary):
text = clean_text(skills + " " + certs)
text_vec = tfidf.transform([text])
edu_encoded = le.transform([education])[0]
num_data = scaler.transform([[experience, salary, projects, edu_encoded]])
final_input = hstack([text_vec, num_data])
model = models["Random Forest"]
prob = model.predict_proba(final_input)[0][1]
pred = model.predict(final_input)[0]
return ("Hire" if pred==1 else "Reject", prob)
predict_hiring(
skills="Python SQL Machine Learning",
experience=3,
education="M.Tech",
certs="AWS Data Science",
projects=5,
salary=60000
)
le.classes_