def factorization( model_path='models/simple_factorization', simple=True, use_item_data = True, item_data_output='data/model_data/item_data.csv', model_params={'nmf':False, 'verbose': False} ): """builds and saves factorization model""" get.model_data(**data_params) mat = gl.SFrame.read_csv(data_params['output_path']) if simple: mat = mat[['user_id', 'item_id']] if use_item_data: get.item_data(item_data_output) item_data = gl.SFrame.read_csv(item_data_output) model = gl.recommender.ranking_factorization_recommender.create( mat, item_data=item_data, **model_params ) else: model = gl.recommender.ranking_factorization_recommender.create( mat, **model_params ) save_model(model_path, model) log.info('saved model as %s' % model_path)
def main(): # create csvs to train models _load_data() is_df = gl.SFrame.read_csv(IS_DF_PATH) nmf_df = gl.SFrame.read_csv(NMF_DF_PATH) pop_df = gl.SFrame.read_csv(POP_DF_PATH) item_df = gl.SFrame(get.item_data(BEERS_PATH, BREWERS_PATH)) # list of beers to exclude from recs, by user exclude_beers = pd.read_csv(FTISO_PATH)[['user_id', 'beer_id']] exclude_beers.columns = ['user_id', 'item_id'] exclude_beers = gl.SFrame(exclude_beers) # build & save models build.is_model(is_df, IS_MODEL_PATH) build.nmf_model(nmf_df, item_df, NMF_MODEL_PATH) build.pop_model(pop_df, POP_MODEL_PATH) # load all models is_model = gl.load_model(IS_MODEL_PATH) nmf_model = gl.load_model(NMF_MODEL_PATH) pop_model = gl.load_model(POP_MODEL_PATH) # get recommendations & export as json users = pd.read_csv(USERS_PATH)['id'].to_frame(name='id') users = gl.SFrame(users) is_recs = is_model.recommend(users=users['id'], exclude=exclude_beers, diversity=3) nmf_recs = nmf_model.recommend(users=users['id'], exclude=exclude_beers, diversity=3) pop_recs = pop_model.recommend(users=users['id'], exclude=exclude_beers, diversity=3) # save recommendations is_recs.export_json(IS_REC_PATH, orient='records') nmf_recs.export_json(NMF_REC_PATH, orient='records') pop_recs.export_json(POP_REC_PATH, orient='records')
def build_nmf_model(): """builds an nmf model from completed trades, including beer side data""" # load data df = get.model_data(sparse=4, outlier=500, iso_rate=0, proposed_rate=0, traded_rate=1, trade_path=TRADE_PATH, trade_item_path=TRADE_ITEM_PATH, ftiso_path=FTISO_PATH) mat = gl.SFrame(df[['user_id', 'item_id']]) item_data = gl.SFrame(get.item_data()) # build model model = gl.recommender.ranking_factorization_recommender.create( mat, item_data=item_data, nmf=False ) return model
def build_nmf_model(): """builds an nmf model from completed trades, including beer side data""" # load data df = get.model_data(sparse=4, outlier=500, iso_rate=0, proposed_rate=0, traded_rate=1, trade_path=TRADE_PATH, trade_item_path=TRADE_ITEM_PATH, ftiso_path=FTISO_PATH) mat = gl.SFrame(df[['user_id', 'item_id']]) item_data = gl.SFrame(get.item_data()) # build model model = gl.recommender.ranking_factorization_recommender.create(mat, item_data=item_data, nmf=False) # check recs users = [3381, 14239, 6601, 8958, 1440, 880, 57] ft, user_beers, beers = check.load_rec_data() recommends = check.recs(users, model, ft, user_beers, beers) # # check precision & recall # train, test = gl.recommender.util.random_split_by_user(gl.SFrame(user_beers)) # model = gl.recommender.ranking_factorization_recommender.create(train, item_data=None, nmf=False, verbose=False) # pr = model.evaluate(test, metric='precision_recall') return model, recommends#, pr