image_output_1.png
from sklearn.metrics import roc_auc_score, mean_absolute_error

from sklearn.model_selection import train_test_split, RepeatedKFold, StratifiedKFold

from sklearn.preprocessing import StandardScaler

from xgboost import XGBRegressor, XGBClassifier

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

import sklearn

target = 'yield'
df1 = pd.read_csv(TRAIN_CSV)
df1.rename({'Id': 'id'}, axis=1, inplace=True)
df1['test'] = 0
df1['gen'] = 1
df2 = pd.read_csv(TEST_CSV)
df2.rename({'Id': 'id'}, axis=1, inplace=True)
df2['test'] = 1
df2['gen'] = 1
df3 = pd.read_csv(EXTERNAL_CSV)
df3.rename({'Row#': 'id'}, axis=1, inplace=True)
df3['test'] = 0
df3['gen'] = 0
df = pd.concat([df1, df2, df3])
df.id.fillna(-1, inplace=True)
df.id = df.id.astype(int)
df.reset_index(inplace=True)
df.drop('index', axis=1, inplace=True)

df.columns

if True:
    num_columns = ['fruitset', 'fruitmass', 'seeds',]

    ncols = 4

    for n, col in enumerate(num_columns):
        if n % ncols == 0:
            fig, axs = plt.subplots(ncols=ncols, figsize=(24,6))
        ax = axs[n % ncols]
        #sns.kdeplot(data=df, x=col, hue=target, ax=ax, palette='bright', common_norm=False, warn_singular=False)
        sns.histplot(data=df[col], ax=ax);

def explore_categorical_value(df, col, target='yield'):
    values = df[col].unique()
    values.sort()
    for value in values:
        select = df.loc[df[col] == value]
        mn = select[target].mean()
        cnt = len(select)
        cnt_test = len(select[select.test == 1])
cat_columns = ['clonesize', 'honeybee', 'bumbles', 'andrena', 'osmia', 'MaxOfUpperTRange', 'MinOfUpperTRange', 'AverageOfUpperTRange', 'MaxOfLowerTRange', 'MinOfLowerTRange', 'AverageOfLowerTRange', 'RainingDays', 'AverageRainingDays']
for col in cat_columns:
    explore_categorical_value(df, col)

if True:
    for col in num_columns + cat_columns:
        df[f'log_{col}'] = np.log(df[col] + 1e-06)

score_function = mean_absolute_error
def get_numpy_arrays(data):
    X = data.drop(['id', 'test', target], axis=1).to_numpy()
    y = data[target].to_numpy()
    scaler = StandardScaler()
    X = scaler.fit_transform(X)
    return (X, y)
def ensamble_pred(clfs, X):
    pred = 0
    for clf in clfs:
        pred += clf.predict_proba(X) if sklearn.base.is_classifier(clf) else clf.predict(X).squeeze()
    return np.argmax(pred, axis=1) if sklearn.base.is_classifier(clf) else pred / len(clfs)
def print_validation_score(clfs):
    pred = ensamble_pred(clfs, X_test)
    score = score_function(y_test, pred)
    return score
def examine_clf(X_train_val, y_train_val, clf, rkf, verbose=None):
    avg_score = 0
    clfs = []
    for n, (train_index, val_index) in enumerate(rkf.split(X_train_val, y_train_val)):
        X_train = X_train_val[train_index]
        y_train = y_train_val[train_index]
        X_val = X_train_val[val_index]
        y_val = y_train_val[val_index]
        clf = sklearn.base.clone(clf)
        if verbose is None:
            clf.fit(X_train, y_train)
        else:
            clf.fit(X_train, y_train, verbose=verbose)
        pred = np.argmax(clf.predict_proba(X_val), axis=1) if sklearn.base.is_classifier(clf) else clf.predict(X_val)
        score = score_function(y_val, pred)
        avg_score += score
        clfs.append(clf)
    avg_score /= n + 1
    return (clfs, avg_score)
def examine_clf_print(X_train_val, y_train_val, clf, rkf, verbose=None):
    clfs, avg_score = examine_clf(X_train_val, y_train_val, clf, rkf, verbose=verbose)
    return clfs

def adv_get_numpy_arrays(data):
    X = data.drop(['id', 'test', 'gen', target], axis=1).to_numpy()
    y = data['test'].to_numpy()
    scaler = StandardScaler()
    X = scaler.fit_transform(X)
    return (X, y)
def get_CV_results(X_train_val, y_train_val, clf, rkf, verbose=None):
    results = np.zeros_like(y_train_val, dtype=float)
    all_clfs = []
    for n, (train_index, val_index) in enumerate(rkf.split(X_train_val, y_train_val)):
        X_train = X_train_val[train_index]
        y_train = y_train_val[train_index]
        X_val = X_train_val[val_index]
        y_val = y_train_val[val_index]
        clf = sklearn.base.clone(clf)
        if verbose is None:
            clf.fit(X_train, y_train)
        else:
            clf.fit(X_train, y_train, verbose=verbose)
        pred = clf.predict_proba(X_val)[:, 1] if sklearn.base.is_classifier(clf) else clf.predict(X_val)
        results[val_index] += pred
        all_clfs.append(clf)
    return (results / n, all_clfs)
adv_df = df.copy()
adv_X, adv_y = adv_get_numpy_arrays(adv_df)
rkf = StratifiedKFold(n_splits=5, shuffle=True, random_state=10)

tree_method = 'gpu_hist' if gpu_available else 'hist'
xgbc = XGBClassifier(n_estimators=140, max_depth=3, tree_method=tree_method)
xgbc_results, xgbc_clfs = get_CV_results(adv_X, adv_y, xgbc, rkf)
print(roc_auc_score(adv_y, xgbc_results))