text_plain_output_1.png
from sklearn.metrics import mean_absolute_error, r2_score, roc_curve, confusion_matrix, classification_report, accuracy_score, auc, log_loss

from sklearn.model_selection import StratifiedKFold, GridSearchCV

from sklearn.preprocessing import OneHotEncoder, StandardScaler, MinMaxScaler, RobustScaler, LabelEncoder

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import xgboost as xgb

def apk(actual, predicted, k=10):
    """
    Computes the average precision at k.
    This function computes the average prescision at k between two lists of
    items.
    Parameters
    ----------
    actual : list
             A list of elements that are to be predicted (order doesn't matter)
    predicted : list
                A list of predicted elements (order does matter)
    k : int, optional
        The maximum number of predicted elements
    Returns
    -------
    score : double
            The average precision at k over the input lists
    """
    if not actual:
        return 0.0
    if len(predicted) > k:
        predicted = predicted[:k]
    score = 0.0
    num_hits = 0.0
    for i, p in enumerate(predicted):
        if p in actual and p not in predicted[:i]:
            num_hits += 1.0
            score += num_hits / (i + 1.0)
    return score / min(len(actual), k)
def mapk(actual, predicted, k=10):
    """
    Computes the mean average precision at k.
    This function computes the mean average prescision at k between two lists
    of lists of items.
    Parameters
    ----------
    actual : list
             A list of lists of elements that are to be predicted 
             (order doesn't matter in the lists)
    predicted : list
                A list of lists of predicted elements
                (order matters in the lists)
    k : int, optional
        The maximum number of predicted elements
    Returns
    -------
    score : double
            The mean average precision at k over the input lists
    """
    return np.mean([apk(a, p, k) for a, p in zip(actual, predicted)])

df_train = pd.read_csv('/kaggle/input/playground-series-s3e13/train.csv').drop(['id'], axis=1)
df_test = pd.read_csv('/kaggle/input/playground-series-s3e13/test.csv').drop(['id'], axis=1)
df_submission = pd.read_csv('/kaggle/input/playground-series-s3e13/sample_submission.csv')

df_train_real = pd.read_csv('/kaggle/input/vector-borne-disease-prediction/trainn.csv')
df_test_real = pd.read_csv('/kaggle/input/vector-borne-disease-prediction/testt.csv')
df_real = pd.concat([df_train_real, df_test_real])

df_train = pd.concat([df_train, df_real])
df_train = df_train.drop_duplicates()

le = LabelEncoder()
df_train['prognosis_label'] = le.fit_transform(df_train['prognosis'])

X = df_train.drop(['prognosis', 'prognosis_label'], axis=1)
y = df_train.pop('prognosis_label')

params_k = {'boosting_type': 'gbdt', 'objective': 'multiclass', 'num_class': 11, 'subsample': 0.6, 'subsample_freq': 3, 'learning_rate': 0.013, 'num_leaves': 2 ** 11 - 1, 'max_bin': 150, 'n_estimators': 750, 'boost_from_average': False, 'random_seed': 42}
xgb_params = {'max_depth': 6, 'max_bin': 256, 'subsample': 0.6, 'n_estimators': 30, 'learning_rate': 0.1, 'random_state': 1995, 'colsample_bytree': 0.12, 'objective': 'multi:softprob', 'booster': 'dart'}
xgb_basic_params = {'random_state': 13, 'objective': 'multi:softprob', 'eval_metric': 'map@3', 'learning_rate': 0.1, 'n_estimators': 5000, 'max_depth': 9}

skf = StratifiedKFold(n_splits=5, random_state=13, shuffle=True)

for fold, (train_idx, val_idx) in enumerate(skf.split(X, y)):
    X_train = X.iloc[train_idx]
    y_train = y.iloc[train_idx]
    X_valid = X.iloc[val_idx]
    y_valid = y.iloc[val_idx]
    model = xgb.XGBClassifier(**xgb_params)
    model.fit(X_train, y_train)
    pred_train = model.predict_proba(X_train)
    pred_valid = model.predict_proba(X_valid)
    train_score = log_loss(y_train, pred_train)
    valid_score = log_loss(y_valid, pred_valid)
    train_logloss.append(train_score)
    valid_logloss.append(valid_score)
    train_index = np.argsort(-pred_train)[:, :3]
    valid_index = np.argsort(-pred_valid)[:, :3]
    train_mapk_score = mapk(y_train.to_numpy().reshape(-1, 1), train_index, 3)
    valid_mapk_score = mapk(y_valid.to_numpy().reshape(-1, 1), valid_index, 3)
    train_map3.append(train_mapk_score)
    valid_map3.append(valid_mapk_score)

test_predictions = model.predict_proba(df_test)
test_sorted_prediction_ids = np.argsort(-test_predictions, axis=1)
test_top_3_prediction_ids = test_sorted_prediction_ids[:, :3]

original_shape = test_top_3_prediction_ids.shape
test_top_3_predictions = le.inverse_transform(test_top_3_prediction_ids.reshape(-1, 1))
test_top_3_predictions = test_top_3_predictions.reshape(original_shape)
test_top_3_predictions[:10]