text_html_output_1.png
from scipy.spatial.distance import mahalanobis

from scipy.stats import chi2

from sklearn.metrics import mean_squared_error, r2_score

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import StandardScaler

from xgboost import XGBRegressor

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')

features = ['clonesize', 'honeybee', 'bumbles', 'andrena', 'osmia', 'AverageRainingDays', 'fruitset', 'fruitmass', 'seeds']
X = train[features]
y = train['yield']

from scipy.spatial.distance import mahalanobis
from scipy.stats import chi2
def mahalanobisR(X, meanCol, IC):
    m = []
    for i in range(X.shape[0]):
        m.append(mahalanobis(X.iloc[i, :], meanCol, IC) ** 2)
    return m
def MD_detectOutliers(x, alpha=0.001):
    n = x.shape[0]
    p = x.shape[1]
    chi = chi2.ppf(1 - alpha, p)
    x_minus_mean = x - np.mean(x)
    S = 1 / (n - 1) * np.dot(np.transpose(x_minus_mean), x_minus_mean)
    S_inv = np.linalg.inv(S)
    d = mahalanobisR(x, np.mean(x), S_inv)
    outliers = []
    for i in range(len(d)):
        if d[i] > chi:
            outliers.append(i)
    return {'MD': d, 'Outliers': outliers}
outliers = MD_detectOutliers(X, alpha=0.001)
X = X.drop(outliers['Outliers'])
y = y.drop(outliers['Outliers'])

xgb_pipeline = Pipeline([('scaler', StandardScaler()), ('xgb', XGBRegressor(objective='reg:squarederror', n_jobs=-1, random_state=418, colsample_bytree=0.7, learning_rate=0.01, max_depth=5, n_estimators=1000, subsample=0.7))])

xgb_pipeline.fit(X_train, y_train)
y_pred = xgb_pipeline.predict(X_val)
rmse = np.sqrt(mean_squared_error(y_val, y_pred))
r2 = r2_score(y_val, y_pred)
print('RMSE: ', rmse)
print('R2 Score: ', r2)