<commit_before>from collections import OrderedDict
import pandas as pd
import seaborn as sns
from sklearn.metrics import accuracy_score, log_loss, roc_auc_score

try:
    import xgboost as xgb
except ImportError:
    pass

from .base import Base


class Classifier(Base):
    def _plot_target(self):
        return sns.countplot(self._y())

    def _plot_category(self, name):
        data = self._train_features_df.dropna(subset=[name])
        self._plot(sns.countplot(x=name, data=data))
        self._plot(sns.barplot(x=name, y=self._target_col,
                               data=data))

    def _plot_number(self, name):
        data = self._train_features_df.dropna(subset=[name])
        data = self._drop_outliers(data, name)

        self._plot(sns.distplot(data[name]))
        self._plot(sns.boxplot(y=name, x=self._target_col,
                               data=data))

    def _build_models(self, train_x, train_y, test_x, test_y):
        predictions = OrderedDict()

        if self._eval_metric == 'mlogloss':
            counts = pd.Series(train_y).value_counts()
            prob = counts.apply(lambda x: x / float(counts.sum()))
            row = [prob[k] for k in range(len(self._classes))]
            predictions['Mean'] = pd.DataFrame([row for x in range(len(test_x))])
        else:
            mode = self._mode(train_y)
            predictions['Mode'] = pd.Series([mode] * len(test_x))

        predictions['XGBoost'] = self._xgboost_predict(train_x, train_y, test_x, test_y)
        return predictions

    def _xgboost_predict(self, train_x, train_y, test_x, test_y):
        if self._eval_metric == 'mlogloss':
            params = {
                'seed': self._seed,
                'objective': 'multi:softprob',
                'eval_metric': 'mlogloss',
                'num_class': len(self._classes)
            }

            dtrain = xgb.DMatrix(data=train_x, label=train_y)
            dtest = xgb.DMatrix(data=test_x)

            num_rounds = 100

            if test_y is None:
                watchlist = []
                model = xgb.train(params, dtrain, num_rounds)
            else:
                wtest = xgb.DMatrix(data=test_x, label=test_y)
                watchlist = [(dtrain, 'train'), (wtest, 'test')]
                evals_result = {}
                model = xgb.train(params, dtrain, num_rounds, watchlist, verbose_eval=10, evals_result=evals_result)
                self._evals_result = evals_result

            self._xgboost_model = model # hack

            return model.predict(dtest)
        else:
            if test_y is None:
                eval_set = []
            else:
                eval_set = [(train_x, train_y), (test_x, test_y)]
            model = xgb.XGBClassifier(seed=self._seed)
            model.fit(train_x, train_y, eval_set=eval_set, eval_metric=(self._eval_metric or 'error'))<commit_msg>More rounds<commit_after>from collections import OrderedDict
import pandas as pd
import seaborn as sns
from sklearn.metrics import accuracy_score, log_loss, roc_auc_score

try:
    import xgboost as xgb
except ImportError:
    pass

from .base import Base


class Classifier(Base):
    def _plot_target(self):
        return sns.countplot(self._y())

    def _plot_category(self, name):
        data = self._train_features_df.dropna(subset=[name])
        self._plot(sns.countplot(x=name, data=data))
        self._plot(sns.barplot(x=name, y=self._target_col,
                               data=data))

    def _plot_number(self, name):
        data = self._train_features_df.dropna(subset=[name])
        data = self._drop_outliers(data, name)

        self._plot(sns.distplot(data[name]))
        self._plot(sns.boxplot(y=name, x=self._target_col,
                               data=data))

    def _build_models(self, train_x, train_y, test_x, test_y):
        predictions = OrderedDict()

        if self._eval_metric == 'mlogloss':
            counts = pd.Series(train_y).value_counts()
            prob = counts.apply(lambda x: x / float(counts.sum()))
            row = [prob[k] for k in range(len(self._classes))]
            predictions['Mean'] = pd.DataFrame([row for x in range(len(test_x))])
        else:
            mode = self._mode(train_y)
            predictions['Mode'] = pd.Series([mode] * len(test_x))

        predictions['XGBoost'] = self._xgboost_predict(train_x, train_y, test_x, test_y)
        return predictions

    def _xgboost_predict(self, train_x, train_y, test_x, test_y):
        if self._eval_metric == 'mlogloss':
            params = {
                'seed': self._seed,
                'objective': 'multi:softprob',
                'eval_metric': 'mlogloss',
                'num_class': len(self._classes)
            }

            dtrain = xgb.DMatrix(data=train_x, label=train_y)
            dtest = xgb.DMatrix(data=test_x)

            num_rounds = 300

            if test_y is None:
                watchlist = []
                model = xgb.train(params, dtrain, num_rounds)
            else:
                wtest = xgb.DMatrix(data=test_x, label=test_y)
                watchlist = [(dtrain, 'train'), (wtest, 'test')]
                evals_result = {}
                model = xgb.train(params, dtrain, num_rounds, watchlist, verbose_eval=10,
                                  evals_result=evals_result, early_stopping_rounds=25)
                self._evals_result = evals_result

            self._xgboost_model = model # hack

            return model.predict(dtest)
        else:
            if test_y is None:
                eval_set = []
            else:
                eval_set = [(train_x, train_y), (test_x, test_y)]
            model = xgb.XGBClassifier(seed=self._seed)
            model.fit(train_x, train_y, eval_set=eval_set, eval_metric=(self._eval_metric or 'error'))
{'fraction_non_alphanumeric': 0.11030927835051546, 'fraction_numerical': 0.002061855670103093, 'mean_word_length': 2.097924427887174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7206348', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1807, 'n_words': 359}