image_output_1.png
from sklearn import linear_model

from sklearn.metrics import mean_absolute_error

from sklearn.model_selection import RepeatedKFold

from tqdm.notebook import tqdm

import catboost as cb

import lightgbm as lgbm

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

import pandas as pd
import numpy as np
from tqdm.notebook import tqdm
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import shap
from sklearn.model_selection import RepeatedKFold
from sklearn.metrics import mean_absolute_error
from sklearn import linear_model
import xgboost as xgb
import catboost as cb
import lightgbm as lgbm
RANDOM_STATE = 69

train = pd.read_csv('/kaggle/input/playground-series-s3e14/train.csv', index_col='id')
test = pd.read_csv('/kaggle/input/playground-series-s3e14/test.csv', index_col='id')
original = pd.read_csv('/kaggle/input/wild-blueberry-yield-prediction-dataset/WildBlueberryPollinationSimulationData.csv')
original['id'] = original['Row#']
original.drop(columns=['Row#'], inplace=True)
original.set_index('id', inplace=True)

fig, ax = plt.subplots(1, 1, figsize=(15, 12))
sns.heatmap(train.corr(), annot=True)

fig, ax = plt.subplots(1, 1, figsize=(15, 12))
sns.heatmap(original.corr(), annot=True)

fig, ax = plt.subplots(1, 1, figsize=(15, 12))
sns.heatmap(train.corr() - original.corr(), annot=True)

train[train.drop(columns=['yield']).duplicated()]

test[test.duplicated()]

for df in [train, test]:
    trange_cols = [col for col in train.columns if 'TRange' in col]
    rain_cols = [col for col in train.columns if 'RainingDays' in col]
    df['TSum'] = df[trange_cols].sum(axis=1)
    df['RSum'] = df[rain_cols].sum(axis=1)
    df.drop(columns=trange_cols + rain_cols, inplace=True)

def kfold(model, X, y, n_splits=5, n_repeats=5):
    kf = RepeatedKFold(n_splits=n_splits, n_repeats=n_repeats, random_state=RANDOM_STATE)
    scores = np.zeros(n_splits * n_repeats)
    for i, (train_idx, val_idx) in tqdm(enumerate(kf.split(X, y)), total=n_splits * n_repeats):
        X_train, X_val = (X.iloc[train_idx], X.iloc[val_idx])
        y_train, y_val = (y.iloc[train_idx], y.iloc[val_idx])
        eval_set = [(X_val, y_val)]
        if isinstance(model, lgbm.sklearn.LGBMRegressor):
            callbacks = [lgbm.callback.early_stopping(100, verbose=False), lgbm.callback.log_evaluation(0)]
            model.fit(X_train, y_train, eval_set=eval_set, eval_metric='MAE', callbacks=callbacks)
        elif isinstance(model, cb.core.CatBoostRegressor):
            model.fit(X_train, y_train, eval_set=eval_set, early_stopping_rounds=100)
        else:
            model.fit(X_train, y_train)
        scores[i] = mean_absolute_error(y_val, model.predict(X_val))
    return np.mean(scores)

models = {'cb': cb.CatBoostRegressor(verbose=0, random_state=RANDOM_STATE, objective='MAE'), 'lgbm': lgbm.LGBMRegressor(verbose=0, force_col_wise=True, random_state=RANDOM_STATE, objective='MAE'), 'linreg': linear_model.LinearRegression(), 'lasso': linear_model.LassoCV(), 'ridge': linear_model.RidgeCV()}
X, y = (train.drop(columns=['yield']), train['yield'])
model_scores = dict()
for name, model in models.items():
    if name in ['cb', 'lgbm', 'xgb']:
        model_scores[name] = kfold(model, X, y)
    else:
        model_scores[name] = kfold(model, X, y)
for name, score in model_scores.items():
    print(f'{name}: {score:.3f}')