text_plain_output_1.png
from sklearn.metrics import mean_absolute_error

from sklearn.model_selection import KFold, StratifiedKFold, train_test_split, GridSearchCV

from xgboost import XGBRegressor

import math

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')
original = pd.read_csv('/kaggle/input/originall/dataa.csv')

original = original.rename(columns={'Row': 'id'})

desc = train.describe().T
desc['nunique'] = train.nunique()
desc['%unique'] = desc['nunique'] / len(train) * 100
desc['null'] = train.isna().sum()
desc['type'] = train.dtypes
desc.head(60)

desc = test.describe().T
desc['nunique'] = test.nunique()
desc['%unique'] = desc['nunique'] / len(test) * 100
desc['null'] = test.isna().sum()
desc['type'] = test.dtypes
desc.head(60)

import math
train_data=train.drop(columns=['id','yield'],axis=1)
test_data=test.drop(columns='id',axis=1)
original_data=original.drop(columns=['yield','id'],axis=1)

numerical_columns = train_data.select_dtypes(include=['float64', 'int64']).columns.tolist()

# calculate number of rows and columns needed for subplots
num_plots = len(numerical_columns)
num_rows = math.ceil(num_plots/3)
num_cols = min(num_plots, 3)

# create subplots
fig, axes = plt.subplots(nrows=num_rows, ncols=num_cols, figsize=(12, 4*num_rows))

# loop over columns and plot distribution
plot_idx = 0
for i in range(num_rows):
    for j in range(num_cols):
        if plot_idx >= num_plots:
            break
        if (train[numerical_columns[plot_idx]].count() > 0) and (test[numerical_columns[plot_idx]].count() > 0):
            sns.kdeplot(train_data[numerical_columns[plot_idx]], ax=axes[i][j], color='red', label='train')
            sns.kdeplot(test_data[numerical_columns[plot_idx]], ax=axes[i][j], color='green', label='test')
            sns.kdeplot(original_data[numerical_columns[plot_idx]], ax=axes[i][j], color='yellow', label='original')
            axes[i][j].set_xlabel(numerical_columns[plot_idx])
            axes[i][j].legend()
            plot_idx += 1
        else:
            # empty plot, no data to plot
            axes[i][j].axis('off')

plt.tight_layout()
plt.show()

    
def heatmap(dataset,title):
    corr = dataset.corr()
    fig, axes = plt.subplots(figsize=(20, 10))
    mask = np.zeros_like(corr)
    mask[np.triu_indices_from(mask)] = True
    sns.heatmap(corr,  linewidths=.5, mask=mask, cmap='plasma', annot=True,)
    plt.title(title, fontsize=30)
    plt.show()

# plot_correlation_heatmap(original, 'Original Dataset Correlation')
heatmap(train_data, 'Train Dataset Correlation')
heatmap(original_data,'original data correlation')
heatmap(test_data,'test data correlation')

train.drop('id', axis=1, inplace=True)
X = train.drop('yield', axis=1)
Y = train['yield']

from sklearn.metrics import mean_absolute_error
cv_scores = list()
importance_xgb = list()
preds = list()
for i in range(3):
    print(f'{i} fold cv begin')
    skf = KFold(n_splits=3, random_state=1004, shuffle=True)
    for train_ix, test_ix in skf.split(X, Y):
        X_train, X_test = (X.iloc[train_ix], X.iloc[test_ix])
        Y_train, Y_test = (Y.iloc[train_ix], Y.iloc[test_ix])
        XGB_md = XGBRegressor(tree_method='gpu_hist', objective='reg:squarederror', colsample_bytree=0.8, gamma=0.8, learning_rate=0.01, max_depth=5, min_child_weight=10, n_estimators=1000, subsample=0.8).fit(X_train, Y_train)
        importance_xgb.append(XGB_md.feature_importances_)
        XGB_pred_1 = XGB_md.predict(X_test)
        cv_scores.append(mean_absolute_error(Y_test, XGB_pred_1))
        print(f'{i} fold cv done')
scores = np.mean(cv_scores)
print('The average RMSE over 3-folds (run 3 times) is:', scores)