def run_bellkor(self, rs: RowSettings, average_times: dict) -> Tuple[float, RowSettings]: """ Bellkor Algorithm :param rs: Row and Parameters :param average_times: Dictionary of User -> Avg Time :return: Prediction """ logger = logging.getLogger("BellKor.BellkorAlgorithm.run_bellkor") # Find the difference from the Average Review data delta = (rs.Time - average_times[rs.User]) # Find the Sign sign = -1 if delta < 0 else 1 # Rescale delta = abs(delta) / self.TIME_DIFF # Find the 'deviation' dev = sign * math.pow(delta, 0.4) rs.set_dev(dev=dev) # Find P P = rs.p + rs.alpha_p * dev # Calculate the Model Prediction output = self.GLOBAL_MEAN + rs.b_u + (rs.alpha_u * dev) + rs.b_ut + \ (rs.b_i + rs.b_ibin) * (rs.c_u + rs.c_ut) + np.dot(rs.q.T, P) logger.debug("Model Prediction: {}".format(output)) return output, rs
def test_row_settings(self): # Setup rs = RowSettings(user=1, movie=100, binval=20, time=123123123123, rating=4.5, time_index=1, **self.TEST_INPUTS) # Assertions self.assertEqual(rs.User, 1) rs.set_dev(4.123123) # Further Assetions self.assertEqual(rs.Dev, 4.123123)
def get_row_settings(self, inputs): """ Get the Row Settings [Index, TimePeriod, User, Item, BaseRating] :param inputs: Array Row :return: RowSettings """ logger = logging.getLogger("BellKor.BellkorAlgorithm.get_row_settings") # Row Values user = int(inputs[2]) movie = int(inputs[3]) time_val = inputs[1] binval = int((time_val - self.START_TIME) / (self.TIME_DIFF / 30)) # Get datetime Index time_index = int( time.mktime( datetime.datetime.fromtimestamp(time_val).date().timetuple())) time_index = int(self.TIME_MAP[time_index]) # Collate the Row Settings rs = RowSettings( user=user, movie=movie, rating=inputs[4], binval=binval, time=time_val, time_index=time_index, b_u=self.PARAMS.b_u[user], alpha_u=self.PARAMS.alpha_u[user], b_i=self.PARAMS.b_i[movie], c_u=self.PARAMS.c_u[user], b_ut=self.PARAMS.b_ut[user, time_index], c_ut=self.PARAMS.c_ut[user, time_index], b_ibin=self.PARAMS.b_ibin[movie - 1, binval], p=self.PARAMS.p[user], q=self.PARAMS.q[movie], alpha_p=self.PARAMS.alpha_p[user], ) logger.debug(f"Row Setting: {rs}") return rs