text_html_output_1.png
from sklearn.metrics import make_scorer,mean_squared_error, r2_score, mean_absolute_error

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import train_test_split

from sklearn.tree import DecisionTreeRegressor

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

def summary(df):
    result = pd.DataFrame(df.dtypes, columns=['data type'])
    result['#duplicate'] = df.duplicated().sum()
    result['#missing'] = df.isnull().sum().values
    result['#unique'] = df.nunique().values
    return result

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

train_df.drop(['id'], axis=1, inplace=True)
test_df.drop(['id'], axis=1, inplace=True)

train_df.describe().T

# explore correlation of features
corr = train_df.corr()
mask = np.triu(corr)
ax, fig = plt.subplots(figsize = (13,13))
sns.heatmap(corr, mask = mask, annot = True, fmt=".3f")

fig, axes = plt.subplots(6, 3, figsize = (13, 13))
  
fig.suptitle('Histogram for all numerical variables in the dataset')
  
for i, col in enumerate(train_df.columns):
    ax = axes[i//3, i%3]
    sns.histplot(x = col, data = train_df, kde = True, ax = ax)
    ax.axvline(x=train_df[col].mean(), c='r', ls='-', lw=1)

plt.xticks(rotation=90)

X = train_df.drop(['yield'], axis=1)
y = train_df['yield']

X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=14)

def adj_r2_score(predictors, targets, predictions):
    r2 = r2_score(targets, predictions)
    n = predictors.shape[0]
    k = predictors.shape[1]
    return 1 - (1 - r2) * (n - 1) / (n - k - 1)
def mape_score(targets, predictions):
    return np.mean(np.abs(targets - predictions) / targets) * 100
def model_performance_regression(model, predictors, target):
    """
    Function to compute different metrics to check regression model performance
    model: regressor
    predictors: independent variables
    target: dependent variable
    """
    pred = model.predict(predictors)
    r2 = r2_score(target, pred)
    adjr2 = adj_r2_score(predictors, target, pred)
    rmse = np.sqrt(mean_squared_error(target, pred))
    mae = mean_absolute_error(target, pred)
    mape = mape_score(target, pred)
    df_perf = pd.DataFrame({'RMSE': rmse, 'MAE': mae, 'R-squared': r2, 'Adj. R-squared': adjr2, 'MAPE': mape}, index=[0])
    return df_perf

dtree_tuned = DecisionTreeRegressor(random_state=1)
parameters = {'max_depth': np.arange(2, 9), 'criterion': ['squared_error', 'friedman_mse'], 'min_samples_leaf': [1, 3, 5, 7], 'max_leaf_nodes': [2, 5, 7] + [None]}
scorer = make_scorer(mean_absolute_error, greater_is_better=False)
grid_obj = GridSearchCV(dtree_tuned, parameters, scoring=scorer, cv=5)
grid_obj = grid_obj.fit(X_train, y_train)
dtree_tuned_regressor = grid_obj.best_estimator_
dtree_tuned_regressor.fit(X_train, y_train)

dtree_tuned_regressor_perf_test = model_performance_regression(dtree_tuned_regressor, X_val, y_val)
dtree_tuned_regressor_perf_test