-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprocess_data.py
173 lines (137 loc) · 6.2 KB
/
process_data.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from nltk.tokenize import TreebankWordTokenizer
from nltk.stem import PorterStemmer
from sklearn.decomposition import TruncatedSVD, NMF
from sklearn.metrics.pairwise import cosine_similarity
import re
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score, train_test_split, GridSearchCV
from sklearn.metrics import accuracy_score
import pickle
def tokenize_stem(series):
tokenizer =TreebankWordTokenizer()
stemmer = PorterStemmer()
series = series.apply(lambda x: x.replace("\n", ' '))
series = series.apply(lambda x: tokenizer.tokenize(x))
series = series.apply(lambda x: [stemmer.stem(w) for w in x])
series = series.apply(lambda x: ' '.join(x))
return series
def display_topics(model, feature_names, no_top_words, topic_names=None):
'''
displays topics and returns list of toppics
'''
topic_list = []
for i, topic in enumerate(model.components_):
if not topic_names or not topic_names[i]:
print("\nTopic ", i)
else:
print("\nTopic: '",topic_names[i],"'")
print(", ".join([feature_names[k]
for k in topic.argsort()[:-no_top_words - 1:-1]]))
topic_list.append(", ".join([feature_names[k]
for k in topic.argsort()[:-no_top_words - 1:-1]]))
return model.components_, topic_list
def return_topics(series, num_topics, no_top_words, model, vectorizer):
'''
returns document_topic matrix and topic modeling model
'''
#turn job into series
series = tokenize_stem(series)
#transform series into corpus
ex_label = [e[:30]+"..." for e in series]
#set vectorizer ngrams = (2,2)
vec = vectorizer(stop_words = 'english')
doc_word = vec.fit_transform(series)
#build model
def_model = model(num_topics)
def_model = def_model.fit(doc_word)
doc_topic = def_model.transform(doc_word)
#print('model components: ', def_model.components_[0].shape)
#print('doc_topic', doc_topic[0])
model_components, topic_list = display_topics(def_model, vec.get_feature_names(), no_top_words)
return def_model.components_, doc_topic, def_model, vec, topic_list#, topics
def process_data():
'''
uses the functions above to read in files, model, and return a topic_document dataframe
'''
#read in jobs file and get descriptions
df = pd.read_csv('jobs.csv')
#df = df[df.keyword!='marketing']
jobs_df = pd.DataFrame(zip(df['Job Description'], df['keyword']), columns = ['Description', 'Job'])
array, doc, topic_model, vec, topic_list = return_topics(jobs_df['Description'],20, 10, TruncatedSVD, TfidfVectorizer)
topic_df = pd.DataFrame(doc)
topic_df.columns = ['Topic ' + str(i+1) for i in range(len(topic_df.columns)) ]
topic_df['job'] = jobs_df.Job
#Topic_DF.to_csv('topic_df.csv')
return topic_df, topic_model, vec, topic_list
def predictive_modeling(df):
'''
fits, optimizes, and predicts job class based on topic modeling corpus
'''
X,y = df.iloc[:,0:-1], df.iloc[:, -1]
X_tr, X_te, y_tr, y_te = train_test_split(X,y)
param_grid = {'n_estimators': [100,300, 400, 500, 600], 'max_depth': [3,7,9, 11]}
# search = GridSearchCV(RandomForestClassifier(), param_grid, cv=5)
# search.fit(X_tr, y_tr)
# bp = search.best_params_
# print(bp)
#rfc = RandomForestClassifier(n_estimators = bp['n_estimators'], max_depth = bp['max_depth'])
rfc = RandomForestClassifier(n_estimators = 500, max_depth = 9)
rfc.fit(X_tr, y_tr)
print('acc: ', np.mean(cross_val_score(rfc, X_tr, y_tr, scoring = 'accuracy', cv=5)))
print('test_acc: ', accuracy_score(y_te, rfc.predict(X_te)))
print(rfc.predict(X_te))
return rfc
def predict_resume(topic_model, model, resume):
'''
transforms a resume based on the topic modeling model and return prediction probabilities per each job class
'''
doc = topic_model.transform(resume)
return model.predict_proba(doc), model.classes_
def get_topic_classification_models():
jobs_df, model, vec , topic_list= process_data()
model_1 = predictive_modeling(jobs_df)
return model, model_1, vec
# topic_model, classifier, vec= get_topic_classification_models()
# topic_model_name = 'topic_model.sav'
# classifier_name = 'classification_model.sav'
# vec_name = 'job_vec.sav'
# pickle.dump(topic_model, open(topic_model_name, 'wb'))
# pickle.dump(classifier, open(classifier_name, 'wb'))
# pickle.dump(vec, open(vec_name, 'wb'))
def main(resume, topic_model, predictor, vec):
'''
run code that predicts resume
'''
#jobs_df, model, vec , topic_list= process_data()
#model_1 = predictive_modeling(jobs_df)
doc = tokenize_stem(resume)
doc = vec.transform(doc)
probabilities, classes = predict_resume(topic_model, predictor, doc)
return classes, probabilities[0]*100
# for i,doc in enumerate(resumes.Resumes):
# doc = tokenize_stem(pd.Series(doc))
# doc = vec.transform(doc)
# #do cosine similarity compared to job 1 to job 100
# #this would spit out the specifc job application +
# print(resumes.Person[i])
# print('----------------')
# probabilities, classes = predict_resume(model, model_1, doc)
# for i in range(len(probabilities[0])):
# print(classes[i], ': ', probabilities[0][i]*100, '%')
#main()
#we tried out SVD and NMF, Count Vec and TFIDIF - best combo is SVD and TFIDF
# look at topics and see what makes sense
# recommend roles that are best for you based on supervised learnings
# these are the jobs descriptions that are best for you based on your resume
# these are the best words for your resume based on which job you want to go into
# NEXT STEPS:
# Now we have to fit resumes according to the topic modeling model -
#we my want to add a new function in here to fit the resumes and test out the product
#then we have to turn the model into a streamlit app
#last we want to make the app pretty and have users test it / upload it to the internet for real
#make presentation
# Also have to create app and return the
#get chart of top toppics from topic modeling