text_plain_output_1.png
from sklearn.decomposition import PCA

from sklearn.metrics import mean_absolute_error

from sklearn.model_selection import KFold

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import StandardScaler

import lightgbm as lgb

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

train = pd.read_csv('/kaggle/input/playground-series-s3e14/train.csv')
test = pd.read_csv('/kaggle/input/playground-series-s3e14/test.csv')
sample = pd.read_csv('/kaggle/input/playground-series-s3e14/sample_submission.csv')

train.describe().T

train.isna().mean()

plt.figure(figsize=(16, 6))
heatmap = sns.heatmap(train.corr(), vmin=-1, vmax=1, annot=True)
heatmap.set_title('Correlation Heatmap', fontdict={'fontsize':12}, pad=12);

from sklearn.decomposition import PCA
def perform_feature_engineering(df):
    df['total_bees'] = df['honeybee'] + df['bumbles'] + df['andrena'] + df['osmia']
    df['temperature_range'] = df['MaxOfUpperTRange'] - df['MinOfUpperTRange'] + df['MaxOfLowerTRange'] - df['MinOfLowerTRange']
    df['rainfall_intensity'] = df['RainingDays'] * df['AverageRainingDays']
    df['fruit_quality'] = df['fruitset'] * df['fruitmass']
    df['seed_to_fruit_ratio'] = df['seeds'] / df['fruitmass']
    temp_range_features = ['MaxOfUpperTRange', 'MinOfUpperTRange', 'AverageOfUpperTRange', 'MaxOfLowerTRange', 'MinOfLowerTRange', 'AverageOfLowerTRange']
    pca = PCA(n_components=1)
    df[['temperature_pca']] = pca.fit_transform(df[temp_range_features])
    df = df.drop(temp_range_features, axis=1)
    return df

train = perform_feature_engineering(train)
test = perform_feature_engineering(test)

train.columns

features = ['clonesize', 'honeybee', 'bumbles', 'andrena', 'osmia', 'RainingDays', 'AverageRainingDays', 'fruitset', 'fruitmass', 'seeds', 'total_bees', 'temperature_range', 'rainfall_intensity', 'fruit_quality', 'seed_to_fruit_ratio', 'temperature_pca']
target = 'yield'

from sklearn.model_selection import train_test_split
X = train[features]
y = train[target]

import lightgbm as lgb
from sklearn.model_selection import KFold
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_absolute_error
params_lgb = {'n_estimators': 9999, 'learning_rate': 0.03, 'max_depth': 5, 'num_leaves': 16, 'subsample': 0.7, 'colsample_bytree': 0.8, 'reg_lambda': 5e-07, 'objective': 'regression_l1', 'metric': 'mean_absolute_error', 'boosting_type': 'gbdt', 'device': 'GPU', 'random_state': 42}
n_splits = 5
kf = KFold(n_splits=n_splits, shuffle=True, random_state=42)
lgb_model = Pipeline([('scaler', StandardScaler()), ('lgbm', lgb.LGBMRegressor(**params_lgb))])
mae_scores = []
for train_index, test_index in kf.split(X):
    X_train, y_train = (X.iloc[train_index], y.iloc[train_index])
    X_test, y_test = (X.iloc[test_index], y.iloc[test_index])
    lgb_model.fit(X_train, y_train)
    y_pred_lgb = lgb_model.predict(X_test)
    mae_lgb = mean_absolute_error(y_test, y_pred_lgb)
    mae_scores.append(mae_lgb)
print('Mean absolute error:', np.mean(mae_scores))
print('Scores:', mae_scores)