class Predictor_cat5(): def __init__(self): self.traits = ['OPN', 'CON', 'EXT', 'AGR', 'NEU'] self.categories = [ 'OPENNESS', 'CONSCIENTIOUSNESS', 'EXTRAVERSION', 'AGREEABLENESS', 'NEUROTICISM' ] self.Pre_cat = { trait: cat for (trait, cat) in zip(self.traits, self.categories) } self.models = { trait: pickle.load( open(os.getcwd() + '/model/' + trait + '_model.pkl', 'rb')) for i, trait in enumerate(self.traits) } self.dp = DataPrep() def predict(self, X, traits='All', predictions='All'): predictions = {} self.dp.transform(X) if traits == 'All': for trait in self.traits: pkl_model = self.models[trait] # trait_categories = pkl_model.predict(X, regression=False) # predictions[self.Pre_cat[trait]+' '] = str(trait_categories[0]) # trait_scores = pkl_model.predict(X, regression=True).reshape(1, -1) # predictions[self.Pre_cat[trait]+' '] = predictions[self.Pre_cat[trait]+' ']+' '+str(round(trait_scores.flatten()[0]*10))+' % ' trait_categories_probs = pkl_model.predict_proba(X) predictions[self.Pre_cat[trait] + ' '] = str( trait_categories_probs[:, 1][0] * 100) return predictions
class Predictor_cat5(): def __init__(self): """ Loading all regression and classification models of cat5 models (model no 1 ) """ self.traits = ['OPN', 'CON', 'EXT', 'AGR', 'NEU'] self.categories = [ 'OPENNESS', 'CONSCIENTIOUSNESS', 'EXTRAVERSION', 'AGREEABLENESS', 'NEUROTICISM' ] self.Pre_cat = { trait: cat for (trait, cat) in zip(self.traits, self.categories) } self.models = { trait: pickle.load(open('static/' + trait + '_model.pkl', 'rb')) for i, trait in enumerate(self.traits) } self.dp = DataPrep() def predict(self, X, traits='All', predictions='All'): """ Takes features and returns predictions Transforming text into vector and predicting probablity on text """ predictions = {} self.dp.transform(X) if traits == 'All': for trait in self.traits: pkl_model = self.models[trait] # trait_categories = pkl_model.predict(X, regression=False) # predictions[self.Pre_cat[trait]+' '] = str(trait_categories[0]) # trait_scores = pkl_model.predict(X, regression=True).reshape(1, -1) # predictions[self.Pre_cat[trait]+' '] = predictions[self.Pre_cat[trait]+' ']+' '+str(round(trait_scores.flatten()[0]*10))+' % ' trait_categories_probs = pkl_model.predict_proba(X) predictions[self.Pre_cat[trait] + ' '] = trait_categories_probs[:, 1][0] * 100 return predictions