<jupyter_start><jupyter_text>Dataset<jupyter_code>import pandas as pd
import numpy as np
from math import sqrt
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
df = pd.read_csv('avocado.csv')

df['Date'] = pd.to_datetime(df['Date'])
df['month'] = df['Date'].dt.month

seasons_conditions = [(df['month'].between(3,5,inclusive=True)),
                      (df['month'].between(6,8,inclusive=True)),
                      (df['month'].between(9,11,inclusive=True)),
                      (df['month'].between(12,2,inclusive=True))]

df['season'] = np.select(seasons_conditions, range(4))

df = df.drop(['Unnamed: 0', 'Total Volume', 'Total Bags', 'Date', 'year', 'month'], axis=1)
df
Y_COLUMN = 'AveragePrice'
X = df.drop([Y_COLUMN], axis=1)
y = df[Y_COLUMN]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
X_train = X_train.reset_index().drop(["index"], axis=1)
X_test = X_test.reset_index().drop(["index"], axis=1)
y_train = y_train.reset_index().drop(["index"], axis=1)
y_test = y_test.reset_index().drop(["index"], axis=1)<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.pipeline import Pipeline
from sklearn.metrics import r2_score, mean_squared_error
from sklearn.inspection import permutation_importance
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OrdinalEncoder, StandardScaler
def score(pipe, X_test, y_test):
  y_pred = pipe.predict(X_test)
  r2 = r2_score(y_test, y_pred)
  mse = mean_squared_error(y_test, y_pred)
  rmse = sqrt(mse)
  return r2, rmse
numeric_cols = ['4046', '4225', '4770', 'Small Bags', 'Large Bags', 'XLarge Bags']
categorial_cols = ['region', 'type']
preprocessor = ColumnTransformer([
        ('cat', OrdinalEncoder(), categorial_cols),
        ('num', StandardScaler(), numeric_cols)],
        remainder='passthrough')
preprocessor
params = {'random_state': 42,
          'n_estimators': 5000,
          'max_depth': 8,
          'min_samples_split': 3,
          'learning_rate': 0.01,
          'loss': 'ls'}

gb_pipe = Pipeline([("preprocess", preprocessor), ('gb', GradientBoostingRegressor(**params))])
gb_pipe.fit(X_train, y_train)
gb_r2, gb_rmse = score(gb_pipe, X_test, y_test)
print(f"r2: {gb_r2}\nrmse: {gb_rmse}")
reg = gb_pipe.steps[1][1]
feature_importance = reg.feature_importances_
sorted_idx = np.argsort(feature_importance)
pos = np.arange(sorted_idx.shape[0]) + .5

fig = plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.barh(pos, feature_importance[sorted_idx], align='center')
plt.yticks(pos, np.array(X.columns)[sorted_idx])
plt.title('Feature Importance (MDI)')
fig.tight_layout()
plt.show()
feature_importance[sorted_idx], np.array(X.columns)[sorted_idx]
params = {'random_state': 42,
          'n_estimators': 5000,
          'max_depth': 8,
          'min_samples_split': 3,
          }

rf_pipe = Pipeline([('scaler', StandardScaler()), ('rf', RandomForestRegressor(**params))])
rf_pipe.fit(X_train, y_train)
rf_r2, rf_rmse = score(rf_pipe, X_test, y_test)
print(f"r2: {rf_r2}\nrmse: {rf_rmse}")<jupyter_output>r2: 0.7073544483320073
rmse: 0.21683886303773522<jupyter_text>Export<jupyter_code>gb_pipe
import pickle
s = pickle.dumps(gb_pipe)
with open('model.pkl', 'wb') as f:
  f.write(s)
with open('model.pkl', 'rb') as f:
  loaded = f.read()

loaded_pipe = pickle.loads(loaded)
loaded_pipe
gb_r2, gb_rmse = score(loaded_pipe, X_test, y_test)
print(f"r2: {gb_r2}\nrmse: {gb_rmse}")<jupyter_output>r2: 0.856427071388753
rmse: 0.15188064665784784
{'fraction_non_alphanumeric': 0.1579382579933848, 'fraction_numerical': 0.04051819184123484, 'mean_word_length': 5.515260323159785, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '52918', 'license': 'MIT', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1402, 'n_words': 252, 'path': 'Avocado.ipynb', 'repo_name': 'shaySirek/avocado-price-predictor-notebook', 'size': '3628'}