def train(self, df, model_path=''): ''' 隐语义模型训练 :param df: 格式包含该三列 --》 userid,iteamid,rating :param model_path:模型持久化地址,默认为空,不执行持久化 :return: 训练好的模型 ''' # 数据类型转换为 surprise 需要的格式 data = Dataset.load_from_df(df, self.reader) trainset = data.build_full_trainset() algo_lfm = SVDpp() algo_lfm.fit(trainset) if model_path: surprise.dump.dump(model_path, algo=algo_lfm, verbose=1) return algo_lfm
def run_svd(data, params, svdpp=False): '''Returns trained SVD model based on matrix factorization''' if svdpp: alg = SVDpp(n_factors=utils.get_param(params, 'n_factors'), n_epochs=utils.get_param(params, 'n_epochs'), lr_all=utils.get_param(params, 'learning_rate'), reg_all=utils.get_param(params, 'reg'), verbose=True) else: alg = SVD(biased=utils.get_param(params, 'biased'), n_factors=utils.get_param(params, 'n_factors'), n_epochs=utils.get_param(params, 'n_epochs'), lr_all=utils.get_param(params, 'learning_rate'), reg_all=utils.get_param(params, 'reg'), verbose=True) alg.fit(data) return alg