{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":82611,"databundleVersionId":9553358,"sourceType":"competition"}],"dockerImageVersionId":30776,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%%capture\n!pip install optuna-integration","metadata":{"execution":{"iopub.status.busy":"2024-09-25T20:35:22.664381Z","iopub.execute_input":"2024-09-25T20:35:22.664721Z","iopub.status.idle":"2024-09-25T20:35:35.761041Z","shell.execute_reply.started":"2024-09-25T20:35:22.664687Z","shell.execute_reply":"2024-09-25T20:35:35.760018Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%capture\n!pip install git+https://github.com/ThomasMeissnerDS/BlueCast.git@move_cat_encoding_inside_train_loop","metadata":{"execution":{"iopub.status.busy":"2024-09-25T20:35:35.762974Z","iopub.execute_input":"2024-09-25T20:35:35.763297Z","iopub.status.idle":"2024-09-25T20:36:04.439203Z","shell.execute_reply.started":"2024-09-25T20:35:35.763261Z","shell.execute_reply":"2024-09-25T20:36:04.438283Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Setup Cell\nimport pandas as pd\nimport numpy as np\n\nfrom bluecast.blueprints.cast import BlueCast\nfrom bluecast.blueprints.cast_regression import BlueCastRegression\nfrom bluecast.blueprints.cast_cv import BlueCastCV\nfrom bluecast.blueprints.cast_cv_regression import BlueCastCVRegression\nfrom bluecast.config.training_config import TrainingConfig, XgboostTuneParamsConfig\nfrom bluecast.preprocessing.custom import CustomPreprocessing\nfrom bluecast.general_utils.general_utils import save_to_production, load_for_production\n\nfrom bluecast.preprocessing.feature_types import FeatureTypeDetector\nfrom bluecast.preprocessing.feature_creation import AddRowLevelAggFeatures, FeatureClusteringScorer, GroupLevelAggFeatures, add_groupby_agg_feats\n# Load datasets with low_memory=False to suppress DtypeWarning\ntrain = pd.read_csv('/kaggle/input/brist1d/train.csv', low_memory=False)\ntest = pd.read_csv('/kaggle/input/brist1d/test.csv', low_memory=False)\n\ntarget = 'bg+1:00'","metadata":{"execution":{"iopub.status.busy":"2024-09-25T20:40:37.700475Z","iopub.execute_input":"2024-09-25T20:40:37.700865Z","iopub.status.idle":"2024-09-25T20:40:55.717237Z","shell.execute_reply.started":"2024-09-25T20:40:37.700828Z","shell.execute_reply":"2024-09-25T20:40:55.716187Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2024-09-25T20:36:23.637496Z","iopub.execute_input":"2024-09-25T20:36:23.637985Z","iopub.status.idle":"2024-09-25T20:36:23.802133Z","shell.execute_reply.started":"2024-09-25T20:36:23.637922Z","shell.execute_reply":"2024-09-25T20:36:23.801217Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2024-09-25T20:36:23.803026Z","iopub.execute_input":"2024-09-25T20:36:23.803299Z","iopub.status.idle":"2024-09-25T20:36:23.836667Z","shell.execute_reply.started":"2024-09-25T20:36:23.803268Z","shell.execute_reply":"2024-09-25T20:36:23.835800Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert 'time' column to datetime and extract features\ntrain['time'] = pd.to_datetime(train['time'], format='%H:%M:%S')\ntest['time'] = pd.to_datetime(test['time'], format='%H:%M:%S')\n\n\n# Prepare data for modeling\nX_train = train.drop(['id'], axis=1)\nX_test = test.drop(['id'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T20:40:27.373511Z","iopub.execute_input":"2024-09-25T20:40:27.374266Z","iopub.status.idle":"2024-09-25T20:40:27.732420Z","shell.execute_reply.started":"2024-09-25T20:40:27.374225Z","shell.execute_reply":"2024-09-25T20:40:27.731389Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Define parameters for the add_groupby_agg_feats function\ngroupby_cols = [\"p_num\"]\nto_group_cols = [col for col in X_train.columns.to_list() if \"bg\" in col]\nnum_col_prefix = \"agg\"\ntarget_col = target\naggregations = [\"min\", \"max\", \"mean\", \"sum\", \"std\"]\n\n# Call the function\nX_train = add_groupby_agg_feats(\n    X_train, groupby_cols, to_group_cols, num_col_prefix, target_col, aggregations\n)\nX_test = add_groupby_agg_feats(\n    X_test, groupby_cols, to_group_cols, num_col_prefix, target_col, aggregations\n)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T20:40:58.274008Z","iopub.execute_input":"2024-09-25T20:40:58.274394Z","iopub.status.idle":"2024-09-25T20:41:01.598170Z","shell.execute_reply.started":"2024-09-25T20:40:58.274355Z","shell.execute_reply":"2024-09-25T20:41:01.597357Z"},"jupyter":{"source_hidden":true},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-25T20:41:19.882044Z","iopub.execute_input":"2024-09-25T20:41:19.882709Z","iopub.status.idle":"2024-09-25T20:41:19.888809Z","shell.execute_reply.started":"2024-09-25T20:41:19.882664Z","shell.execute_reply":"2024-09-25T20:41:19.887909Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"automl = BlueCastCVRegression(class_problem=\"regression\")\nautoml.conf_training.hypertuning_cv_folds = 5\nautoml.conf_training.hypertuning_cv_repeats = 1\nautoml.conf_training.autotune_on_device = \"gpu\"\nautoml.conf_xgboost.max_depth_max = 7\nautoml.conf_training.hyperparameter_tuning_max_runtime_secs = 60 * 60 * 1.0\nautoml.conf_training.sample_data_during_tuning = True\n#automl.conf_training.enable_feature_selection = True","metadata":{"execution":{"iopub.status.busy":"2024-09-25T20:41:31.899622Z","iopub.execute_input":"2024-09-25T20:41:31.900368Z","iopub.status.idle":"2024-09-25T20:41:31.904894Z","shell.execute_reply.started":"2024-09-25T20:41:31.900328Z","shell.execute_reply":"2024-09-25T20:41:31.903981Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"automl.fit(X_train.copy(), target_col=target)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T20:41:33.054773Z","iopub.execute_input":"2024-09-25T20:41:33.055460Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Create submission with different mean types","metadata":{}},{"cell_type":"code","source":"# Predict on test data\ntest_preds = automl.predict(X_test, mean_type=\"arithmetic\")\n\n# Create submission file\nsubmission = pd.DataFrame({\n    'id': test['id'],\n    'bg+1:00': test_preds\n})\n\nsubmission.to_csv('/kaggle/working/submission_arithmetic_mean.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-09-24T02:13:19.936044Z","iopub.execute_input":"2024-09-24T02:13:19.936433Z","iopub.status.idle":"2024-09-24T02:13:20.1549Z","shell.execute_reply.started":"2024-09-24T02:13:19.9364Z","shell.execute_reply":"2024-09-24T02:13:20.15392Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on test data\ntest_preds = automl.predict(X_test, mean_type=\"median\")\n\n# Create submission file\nsubmission = pd.DataFrame({\n    'id': test['id'],\n    'bg+1:00': test_preds\n})\n\nsubmission.to_csv('/kaggle/working/submission_median_mean.csv', index=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on test data\ntest_preds = automl.predict(X_test, mean_type=\"geometric\")\n\n# Create submission file\nsubmission = pd.DataFrame({\n    'id': test['id'],\n    'bg+1:00': test_preds\n})\n\nsubmission.to_csv('/kaggle/working/submission_geometric_mean.csv', index=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict on test data\ntest_preds = automl.predict(X_test, mean_type=\"harmonic\")\n\n# Create submission file\nsubmission = pd.DataFrame({\n    'id': test['id'],\n    'bg+1:00': test_preds\n})\n\nsubmission.to_csv('/kaggle/working/submission_harmonic_mean.csv', index=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}