Beispiel #1
0
class FactorizationMachineBasedRecommender(BaseEstimator, ClassifierMixin):
    def __init__(self, show_progress=False):
        self.show_progress = show_progress
        self.model = TFFMClassifier(
            order=6,
            rank=10,
            optimizer=tf.train.AdamOptimizer(learning_rate=0.01),
            n_epochs=100,
            batch_size=-1,
            init_std=0.001,
            input_type='dense')

    def fit(self, X, y=None):
        self.model.fit(X, y, show_progress=self.show_progress)

        return self

    def predict_proba(self, X, y=None):
        return self.model.predict_proba(X)[:, 1]

    def fit_predict_proba(self, X, y=None):
        self.fit(X, y)

        return self.predict_proba(X)

    def score(self, X, y=None, **kwargs):
        y_pred = self.predict_proba(X, y)

        return roc_auc_score(y, y_pred)
Beispiel #2
0
class FM_Rec(RecModel):
    def __init__(self):
        self.model = None

    def fit(self, training_data, y):
        self.model = TFFMClassifier(
            order=2,
            rank=64,
            optimizer=tf.train.AdamOptimizer(learning_rate=0.01),
            n_epochs=100,
            batch_size=-1,
            init_std=0.001,
            input_type='sparse',
            verbose=2)
        self.model.fit(X=training_data, y=y)

    def predict(self, predict_users):
        return self.model.predict_proba(predict_users)
Beispiel #3
0
            list_idx = df.loc[test].reset_index(drop=True).groupby(
                'order_id').apply(lambda x: x.index.values.shape[0]).tolist()
            list_idx = np.array(list_idx, dtype=np.int)

            clf = TFFMClassifier(order=2,
                                 rank=10,
                                     optimizer=tf.train.AdamOptimizer(learning_rate=0.01),
                                     n_epochs=100,
                                     batch_size=100000,
                                     init_std=0.001,
                                     input_type='sparse'
                                     )
            
            clf.fit(trn_x, trn_y, show_progress=True)
            pred = clf.predict_proba(val_x)[:, 1]
            all_pred[test] = pred

            _score = log_loss(val_y, pred)
            _score2 = - roc_auc_score(val_y, pred)
            _, _score3, _ = f1_metric(val_y.astype(int), pred.astype(float))
            logger.debug('   _score: %s' % _score3)
            list_score.append(_score)
            list_score2.append(_score2)
            list_score3.append(- 1 * _score3)

            with open(DIR + 'train_cv_pred_%s.pkl' % cnt, 'wb') as f:
                pickle.dump(pred, f, -1)
            """
            with open(DIR + 'model_%s.pkl' % cnt, 'wb') as f:
                pickle.dump(clf, f, -1)
Beispiel #4
0
    for params in ParameterGrid(all_params):
        logger.info('param: %s' % (params))
        for train_idx, test_idx in list(cv)[:1]:
            with gzip.open('train_fm.svm', 'wb') as f:
                dump_svmlight_file(data[train_idx], target[train_idx], f)
            del output
            gc.collect()
            with gzip.open('test_svm.svm', 'wb') as f:
                dump_svmlight_file(data[test_idx], target[test_idx], f)

            model = TFFMClassifier(
                order=2,
                rank=10,
                optimizer=tf.train.AdamOptimizer(learning_rate=0.01),
                n_epochs=50,
                batch_size=100000,
                init_std=0.001,
                reg=0.001,
                input_type='sparse')
            """
            model = FMClassification()
            """
            model.fit(data[train_idx], target[train_idx], show_progress=True)
            ans = model.predict_proba(data[test_idx])[:, 1]

            score = roc_auc_score(target[test_idx], ans)
            logger.info('score: %s' % score)
            logger.info('all thresh: %s, score: %s' %
                        mcc_optimize(ans, target[test_idx]))
            score = roc_auc_score(target[test_idx], ans)