text_plain_output_1.png
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor

from sklearn.metrics import mean_absolute_error

from sklearn.metrics import mean_absolute_error

from sklearn.model_selection import train_test_split

from sklearn.model_selection import train_test_split, GridSearchCV

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

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

train_df.shape

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

columns_to_drop = ['MinOfUpperTRange', 'AverageOfUpperTRange', 'MaxOfLowerTRange', 'MinOfLowerTRange', 'AverageOfLowerTRange']
train_df = train_df.drop(columns=columns_to_drop)
test_df = test_df.drop(columns=columns_to_drop)

features = train_df.columns.tolist()

# create subplots
fig, axes = plt.subplots(4, 3, figsize=(12, 12))
axes = axes.ravel()

# loop over features and plot scatter plot
for ax, feature in zip(axes, features):
    ax.scatter(train_df[feature], train_df['yield'])
    ax.set_xlabel(feature)
    ax.set_ylabel('yield')

# adjust subplot spacing
# plt.subplots_adjust(hspace=0.5)

# display plot
plt.show()

train_df.columns

train_df.describe().T

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
train_data, test_data = train_test_split(train_df, test_size=0.2, random_state=42)
X_train = train_data.drop(columns=['yield'])
y_train = train_data['yield']
X_test = test_data.drop(columns=['yield'])
y_test = test_data['yield']

import pandas as pd
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import mean_absolute_error
train_data, test_data = train_test_split(train_df, test_size=0.2, random_state=42)
X_train = train_data.drop(columns=['yield'])
y_train = train_data['yield']
X_test = test_data.drop(columns=['yield'])
y_test = test_data['yield']

rf_model = RandomForestRegressor()
gb_model = GradientBoostingRegressor()
rf_param_grid = {'n_estimators': [5, 20, 50, 100], 'max_features': ['auto', 'sqrt'], 'max_depth': [int(x) for x in np.linspace(10, 120, num=12)], 'min_samples_split': [2, 6, 10], 'min_samples_leaf': [1, 3, 4], 'bootstrap': [True, False]}
gb_param_grid = {'learning_rate': [0.01, 0.1], 'n_estimators': [100, 500, 1000], 'max_depth': [3, 5, 7], 'min_samples_split': [2, 5, 10], 'min_samples_leaf': [1, 2, 4], 'subsample': [0.5, 0.8, 1.0], 'max_features': ['sqrt', 'log2', None]}
rf_gs = GridSearchCV(rf_model, rf_param_grid, cv=5, n_jobs=-1)
gb_gs = GridSearchCV(gb_model, gb_param_grid, cv=5, n_jobs=-1)
rf_gs.fit(X_train, y_train)
gb_gs.fit(X_train, y_train)
rf_pred = rf_gs.predict(X_test)
gb_pred = gb_gs.predict(X_test)
rf_mae = mean_absolute_error(y_test, rf_pred)
gb_mae = mean_absolute_error(y_test, gb_pred)
print('Random Forest MAE: ', rf_mae)
print('Gradient Boosting MAE: ', gb_mae)