-
Notifications
You must be signed in to change notification settings - Fork 2
/
trainModel.py
84 lines (60 loc) · 2.57 KB
/
trainModel.py
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
import pandas as pd
import json
import warnings
warnings.filterwarnings('ignore')
import re
import nltk
from nltk.corpus import stopwords
import string
import joblib
from sklearn import metrics
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.neighbors import KNeighborsClassifier
def cleanResume(resumeText):
resumeText = re.sub('[%s]' % re.escape("""!"$%&'()*,-/:;<=>?@[\]^_`{|}~"""), ' ', resumeText) # remove punctuations
resumeText = re.sub(r'[^\x00-\x7f]', r' ', resumeText)
resumeText = re.sub('\s+', ' ', resumeText) # remove extra whitespace
return resumeText
def getData():
resumeDataSet = pd.read_csv('Datasets/new_dataset_of_resume_skills1.csv')
resumeDataSet['cleaned_resume_skills'] = ''
resumeDataSet['cleaned_resume_skills'] = resumeDataSet.Resume.apply(lambda x: cleanResume(x))
return resumeDataSet
def encoding(resumeDataSet):
le = LabelEncoder()
resumeDataSet['Category'] = le.fit_transform(resumeDataSet['Category'])
joblib.dump(le, 'models/labelEncoder.pkl')
def vectorizing():
skillDataSet = pd.read_csv('Datasets/new_dataset_of_resume_skills1.csv')
word_vectorizer = TfidfVectorizer(sublinear_tf=True, stop_words='english')
word_vectorizer.fit(skillDataSet['skills'].values)
WordFeatures = word_vectorizer.transform(skillDataSet['skills'].values)
joblib.dump(word_vectorizer, 'models/wordVectorizer.pkl')
# return WordFeatures
def trainModel(X, Y):
x_train, x_test, y_train, y_test = train_test_split(X, Y, random_state=4, test_size=0.25)
model = KNeighborsClassifier(n_neighbors=10).fit(X, Y)
joblib.dump(model, 'models/knn.pkl')
def printAccuracy(x_test, y_test):
model = joblib.load("models/knn.pkl")
print('Accuracy of KNN :- {:.2f}'.format(model.score(x_test, y_test)))
if __name__ == "__main__":
# Get data
resumeDataSet = getData()
# Cleaned Resume values
reqText = resumeDataSet['cleaned_resume_skills'].values
# Encoding the Categories
# encoding(resumeDataSet)
# The encoded required category values
reqTarget = resumeDataSet['Category']
# For TF-IDF
# vectorizing()
wordVec = joblib.load('models/wordVectorizer.pkl')
wordFeatures = wordVec.transform(reqText)
# Model Training
# trainModel(wordFeatures, reqTarget)
x_train, x_test, y_train, y_test = train_test_split(wordFeatures, reqTarget, random_state=4, test_size=0.25)
printAccuracy(x_test, y_test)