In [4]:
import data_io
from features import FeatureMapper, SimpleTransform
import numpy as np
import pickle
from sklearn.ensemble import RandomForestRegressor
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.pipeline import Pipeline
import re
from nltk.corpus import stopwords
from nltk import word_tokenize
from nltk.stem import WordNetLemmatizer
cachedStopWords = stopwords.words("english") #cache stop words to speed-up removing them.
class LemmaTokenizer(object):
def __init__(self):
self.wnl = WordNetLemmatizer()
def __call__(self, doc):
return [self.wnl.lemmatize(t) for t in word_tokenize(doc)]
def pre_processor(text):
return re.sub(r'[^0-9a-zA-Z]+', " ", text).lower()
vectorizor = CountVectorizer(max_features=100,tokenizer=LemmaTokenizer(),preprocessor=pre_processor)
def feature_extractor():
features = [('FullDescription-Bag of Words', 'FullDescription', vectorizor),
('Title-Bag of Words', 'Title', vectorizor),
('LocationRaw-Bag of Words', 'LocationRaw', vectorizor),
('LocationNormalized-Bag of Words', 'LocationNormalized', vectorizor)]
combined = FeatureMapper(features)
return combined
def get_pipeline():
features = feature_extractor()
steps = [("extract_features", features),
("classify", RandomForestRegressor(n_estimators=50,
verbose=2,
n_jobs=4,
min_samples_split=30,
random_state=3465343))]
return Pipeline(steps)
In [5]:
print("Reading in the training data")
train = data_io.get_train_df()
print("Extracting features and training model")
classifier = get_pipeline()
classifier.fit(train, train["SalaryNormalized"])
print("Saving the classifier")
data_io.save_model(classifier)
In [8]:
print("Making predictions")
valid = data_io.get_valid_df()
predictions = classifier.predict(valid)
predictions = predictions.reshape(len(predictions), 1)
print("Writing predictions to file")
data_io.write_submission(predictions)
In [37]:
my_job = valid.loc[valid.Id==13656201]
my_job.Title = "Mathematical epidemiology PostDoctoral Fellow"
my_job.FullDescription = "Mathematical epidemiology. Modelling of infectious diseases. Must be proficient in Matlab, R or Python."
my_job.Company = "University of Warwick"
my_job.LocationRaw = "Coventry"
my_job.LocationNormalized = "Coventry"
predictions = classifier.predict(my_job)
print(predictions)