text_plain_output_1.png image_output_1.png
from colorama import Style, Fore

from sklearn.metrics import mean_squared_error

from sklearn.model_selection import KFold

from sklearn.pipeline import Pipeline

from xgboost import XGBRegressor

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

import warnings

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_error
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.base import TransformerMixin
from sklearn.model_selection import KFold
from sklearn.ensemble import GradientBoostingRegressor
from lightgbm import LGBMRegressor
rc = {'axes.facecolor': '#FFF9ED', 'figure.facecolor': '#FFF9ED', 'axes.edgecolor': '#000000', 'grid.color': '#EBEBE7', 'font.family': 'serif', 'axes.labelcolor': '#000000', 'xtick.color': '#000000', 'ytick.color': '#000000', 'grid.alpha': 0.4}
sns.set(rc=rc)
from colorama import Style, Fore
red = Style.BRIGHT + Fore.RED
blue = Style.BRIGHT + Fore.BLUE
megnta = Style.BRIGHT + Fore.MAGENTA
gold = Style.BRIGHT + Fore.YELLOW
res = Style.RESET_ALL
import warnings
warnings.filterwarnings('ignore')

old_df = pd.read_csv('/kaggle/input/regression-with-neural-networking/concrete_data.csv')
original_df = pd.read_csv('/kaggle/input/playground-series-s3e9/train.csv').drop(columns=['id'])
test_df = pd.read_csv('/kaggle/input/playground-series-s3e9/test.csv').drop(columns=['id'])
sample = pd.read_csv('/kaggle/input/playground-series-s3e9/sample_submission.csv')

original_num_cols_int = [col for col in original_df.columns if original_df[col].dtype == 'int']
original_num_cols_float = [col for col in original_df.columns if original_df[col].dtype == 'float']
original_num_cols_obj = [col for col in original_df.columns if original_df[col].dtype == 'o']
old_num_cols_int = [col for col in old_df.columns if old_df[col].dtype == 'int']
old_num_cols_float = [col for col in old_df.columns if old_df[col].dtype == 'float']
old_num_cols_obj = [col for col in old_df.columns if old_df[col].dtype == 'o']
test_num_cols_int = [col for col in test_df.columns if test_df[col].dtype == 'int']
test_num_cols_float = [col for col in test_df.columns if test_df[col].dtype == 'float']
test_num_cols_obj = [col for col in test_df.columns if test_df[col].dtype == 'o']

original_df_column = [col for col in original_df.columns]
old_df_columns = [col for col in old_df.columns]
name_cng_dict = {key: value for key, value in zip(old_df_columns, original_df_column)}
old_df.rename(columns=name_cng_dict, inplace=True)

fig,ax = plt.subplots(3,3,figsize=(15,10),dpi=100)
ax = ax.flatten()

for i,column in enumerate(original_df.columns[:-1]):
    plot_axes = [ax[i]]
    sns.kdeplot(
        original_df[column], label='Original_df',
        ax=ax[i], color='#9E3F00'
    )
    
    
    sns.kdeplot(
        test_df[column], label='Test_df',
        ax=ax[i], color='yellow'
    )
    
    sns.kdeplot(
        old_df[column], label='Old_df',
        ax=ax[i], color='#20BEFF'
    )
    
    # titles
    ax[i].set_title(f'{column} Distribution');
    ax[i].set_xlabel(None)
    
    # remove axes to show only one at the end
    plot_axes = [ax[i]]
    handles = []
    labels = []
    for plot_ax in plot_axes:
        handles += plot_ax.get_legend_handles_labels()[0]
        labels += plot_ax.get_legend_handles_labels()[1]
        plot_ax.legend().remove()
    
for i in range(i+1, len(ax)):
    ax[i].axis('off')

fig.suptitle(f'Dataset Feature Distributions\n\n\n', ha='center',  fontweight='bold', fontsize=25)
fig.legend(handles, labels, loc='upper center', bbox_to_anchor=(0.5, 0.96), fontsize=25, ncol=3)
plt.tight_layout()

# correlation 

corr = original_df.corr()

# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=np.bool))

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))

# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)

# Draw the heatmap with the mask and correct aspect ratio
f.suptitle(f'Heatmap for Original DF\n\n\n', ha='center',  fontweight='bold', fontsize=25)
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
            square=True, linewidths=.5, cbar_kws={"shrink": .5})

# correlation 

corr = old_df.corr()

# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=np.bool))

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))

# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)

# Draw the heatmap with the mask and correct aspect ratio
f.suptitle(f'Heatmap for Old DF\n\n\n', ha='center',  fontweight='bold', fontsize=25)
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
            square=True, linewidths=.5, cbar_kws={"shrink": .5})

final_df = pd.concat([original_df, old_df], ignore_index=True)

def model_training(train, test, model):
    kf = KFold(n_splits=5)
    X = train.copy()
    test_ = test.copy()
    preprocess_pipeline = preprocess()
    predictions = []
    scores = []
    for i, (train, val) in enumerate(kf.split(X)):
        X_train = X.iloc[train].drop(columns=['Strength'])
        y_train = X['Strength'].iloc[train]
        y_train = y_train.to_numpy()
        X_val = X.iloc[val].drop(columns=['Strength'])
        y_val = X['Strength'].iloc[val]
        y_val = y_val.to_numpy()
        pipeline = Pipeline([('preprocess', preprocess_pipeline), ('training', model)])
        pipeline.fit(X_train, y_train)
        test_pred = pipeline.predict(test_)
        predictions.append(test_pred)
        y_pred = pipeline.predict(X_val)
        mse = mean_squared_error(y_val, y_pred)
        rmse = np.sqrt(mse)
        scores.append(rmse)
    return (predictions, model)

learning_rate = 0.2
n_estimators = 250
n_jobs = -1
max_depth = 5
min_child_weight = 2
gamma = 0.01
reg_alpha = 0.01
reg_lambda = 0.01
subsample = 0.8
colsample_bytree = 0.67
seed = 42

xgbr = XGBRegressor(n_estimators=n_estimators, learning_rate=learning_rate, max_depth=max_depth, min_child_weight=min_child_weight, gamma=gamma, reg_alpha=reg_alpha, reg_lambda=reg_lambda, subsample=subsample, colsample_bytree=colsample_bytree, seed=seed, n_jobs=n_jobs)

xgbr, model_xgb = model_training(final_df, test_df, xgbr)