{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":112509,"databundleVersionId":14254895,"isSourceIdPinned":false,"sourceType":"competition"}],"dockerImageVersionId":31192,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.ensemble import RandomForestRegressor\n\n# Load data\ntrain_df = pd.read_csv('/kaggle/input/csiro-biomass/train.csv')\ntest_df = pd.read_csv('/kaggle/input/csiro-biomass/test.csv')\n\nprint(f'Train shape: {train_df.shape}')\nprint(f'Test shape: {test_df.shape}')\n\n# Simple feature engineering\nle_state = LabelEncoder()\nle_species = LabelEncoder()\nle_target = LabelEncoder()\n\ntrain_df['state_code'] = le_state.fit_transform(train_df['State'].fillna('Unknown'))\ntrain_df['species_code'] = le_species.fit_transform(train_df['Species'].fillna('Unknown'))\ntrain_df['target_code'] = le_target.fit_transform(train_df['target_name'])\n\ntest_df['state_code'] = le_state.transform(test_df['State'].fillna('Unknown')) if 'State' in test_df.columns else 0\ntest_df['species_code'] = le_species.transform(test_df['Species'].fillna('Unknown')) if 'Species' in test_df.columns else 0\ntest_df['target_code'] = le_target.transform(test_df['target_name'])\n\n# Use simple features for prediction\nfeatures = ['state_code', 'species_code', 'target_code']\n\nif 'Pre_GSHH_NDVI' in train_df.columns and 'Pre_GSHH_NDVI' in test_df.columns:\n    features.append('Pre_GSHH_NDVI')\n    train_df['Pre_GSHH_NDVI'] = train_df['Pre_GSHH_NDVI'].fillna(train_df['Pre_GSHH_NDVI'].median(), )\n    if 'Pre_GSHH_NDVI' in test_df.columns:\n        test_df['Pre_GSHH_NDVI'] = test_df['Pre_GSHH_NDVI'].fillna(train_df['Pre_GSHH_NDVI'].median(),)\n\nif 'Height_Ave_cm' in train_df.columns and 'Height_Ave_cm' in test_df.columns\n    features.append('Height_Ave_cm')\n    train_df['Height_Ave_cm'] = train_df['Height_Ave_cm'].fillna(train_df['Height_Ave_cm'].median())\n    if 'Height_Ave_cm' in test_df.columns:\n        test_df['Height_Ave_cm'] = test_df['Height_Ave_cm'].fillna(train_df['Height_Ave_cm'].median(),)\n\nX_train = train_df[features]\ny_train = train_df['target']\n\n# Train model\nmodel = RandomForestRegressor(n_estimators=50, random_state=42, n_jobs=-1)\nmodel.fit(X_train, y_train)\n\nX_test = test_df[features]\npredictions = model.predict(X_test)\npredictions = np.maximum(predictions, 0)\n\n# Create submission\nsubmission = pd.DataFrame({\n    'sample_id': test_df['sample_id'],\n    'target': predictions\n})\n\nsubmission.to_csv('/kaggle/working/submission.csv', index=False)\nprint('\\nSubmission created!')\nprint(f'Shape: {submission.shape}')\nprint(submission.head())","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-11-12T05:54:12.717085Z","iopub.execute_input":"2025-11-12T05:54:12.717551Z","iopub.status.idle":"2025-11-12T05:54:12.752754Z","shell.execute_reply.started":"2025-11-12T05:54:12.717521Z","shell.execute_reply":"2025-11-12T05:54:12.751346Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Data Preprocessing & Feature Engineering\nprint('\\n=== DATA PREPROCESSING ===')\nprint(f'\\nTarget types: ')\nprint(train_df['target_name'].value_counts())\n\n# Handle missing values and encode categorical features\nfrom sklearn.preprocessing import LabelEncoder\n\nstate_encoder = LabelEncoder()\nspecies_encoder = LabelEncoder()\ntarget_encoder = LabelEncoder()\n\n# Encode training data\ntrain_df['state_enc'] = state_encoder.fit_transform(train_df['State'].fillna('Unknown'))\ntrain_df['species_enc'] = species_encoder.fit_transform(train_df['Species'].fillna('Unknown'))\ntrain_df['target_name_enc'] = target_encoder.fit_transform(train_df['target_name'])\n\n# Prepare test data columns\ntest_cols = test_df.columns.tolist()\nif 'State' in test_cols:\n    test_df['state_enc'] = state_encoder.transform(test_df['State'].fillna('Unknown'))\nelse:\n    test_df['state_enc'] = 0\n    \nif 'Species' in test_cols:\n    test_df['species_enc'] = species_encoder.transform(test_df['Species'].fillna('Unknown'))\nelse:\n    test_df['species_enc'] = 0\n    \ntest_df['target_name_enc'] = target_encoder.transform(test_df['target_name'])\n\n# Fill missing numerical values\nfor col in ['Pre_GSHH_NDVI', 'Height_Ave_cm']:\n    if col in train_df.columns:\n        med = train_df[col].median()\n        train_df[col] = train_df[col].fillna(med)\n        if col in test_df.columns:\n            test_df[col] = test_df[col].fillna(med)\n\nprint('\\nPreprocessing complete!')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T05:55:53.941082Z","iopub.execute_input":"2025-11-12T05:55:53.942593Z","iopub.status.idle":"2025-11-12T05:55:53.962895Z","shell.execute_reply.started":"2025-11-12T05:55:53.942543Z","shell.execute_reply":"2025-11-12T05:55:53.962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Image Feature Extraction using Transfer Learning\nprint('\\n=== EXTRACTING IMAGE FEATURES ===\\n')\n\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import GlobalAveragePooling2D\n\n# Load pre-trained model\nbase_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\nfeature_extractor = tf.keras.Sequential([base_model, GlobalAveragePooling2D()])\nfeature_extractor.trainable = False\n\ndef extract_features(img_path, root_path):\n    try:\n        full_path = f'{root_path}/{img_path}'\n        img = keras_image.load_img(full_path, target_size=(224, 224))\n        x = keras_image.img_to_array(img)\n        x = np.expand_dims(x, axis=0)\n        x = tf.keras.applications.resnet50.preprocess_input(x)\n        return feature_extractor.predict(x, verbose=0)[0]\n    except:\n        return np.zeros(2048)\n\nprint('Extracting training image features...')\ntrain_features = np.array([extract_features(p, INPUT_PATH) for p in train_df['image_path']])\nprint(f'Training features shape: {train_features.shape}\\n')\nprint('Extracting test image features...')\ntest_features = np.array([extract_features(p, INPUT_PATH) for p in test_df['image_path']])\nprint(f'Test features shape: {test_features.shape}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T05:56:18.193665Z","iopub.execute_input":"2025-11-12T05:56:18.194153Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Combine features and build models\nprint('\\n=== BUILDING PREDICTION MODEL ===\\n')\n\n# Combine image features with metadata features\nmetadata_features = ['state_enc', 'species_enc', 'target_name_enc']\nif 'Pre_GSHH_NDVI' in train_df.columns:\n    metadata_features.append('Pre_GSHH_NDVI')\nif 'Height_Ave_cm' in train_df.columns:\n    metadata_features.append('Height_Ave_cm')\n\n# Scale metadata\nscaler = StandardScaler()\ntrain_meta = scaler.fit_transform(train_df[metadata_features])\ntest_meta = scaler.transform(test_df[metadata_features])\n\n# Combine all features\nX_train = np.hstack([train_features, train_meta])\nX_test = np.hstack([test_features, test_meta])\ny_train = train_df['target'].values\n\nprint(f'Final X_train shape: {X_train.shape}')\nprint(f'Final X_test shape: {X_test.shape}')\nprint(f'y_train shape: {y_train.shape}')\n\n# Build ensemble model using Gradient Boosting\nfrom sklearn.ensemble import GradientBoostingRegressor\n\nprint('\\nTraining Gradient Boosting model...')\nmodel = GradientBoostingRegressor(\n    n_estimators=100,\n    learning_rate=0.1,\n    max_depth=5,\n    min_samples_split=5,\n    min_samples_leaf=2,\n    subsample=0.8,\n    random_state=42,\n    n_iter_no_change=5\n)\n\nmodel.fit(X_train, y_train)\nprint('Model trained successfully!')\n\n# Evaluate on training data\ntrain_pred = model.predict(X_train)\ntrain_r2 = r2_score(y_train, train_pred)\nprint(f'\\nTraining R2 Score: {train_r2:.4f}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T05:49:21.574361Z","iopub.status.idle":"2025-11-12T05:49:21.575287Z","shell.execute_reply.started":"2025-11-12T05:49:21.575047Z","shell.execute_reply":"2025-11-12T05:49:21.57507Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make Predictions and Create Submission\nprint('\\n=== MAKING PREDICTIONS ===\\n')\n\n# Make predictions on test set\ntest_predictions = model.predict(X_test)\n\nprint(f'Test predictions shape: {test_predictions.shape}')\nprint(f'Test predictions min: {test_predictions.min():.2f}')\nprint(f'Test predictions max: {test_predictions.max():.2f}')\nprint(f'Test predictions mean: {test_predictions.mean():.2f}')\n\n# Ensure predictions are non-negative\ntest_predictions = np.maximum(test_predictions, 0)\n\n# Create submission dataframe\nprint('\\n=== CREATING SUBMISSION ===\\n')\n\nsubmission = test_df[['sample_id']].copy()\nsubmission['target'] = test_predictions\n\nprint(f'Submission shape: {submission.shape}')\nprint(f'Sample rows:')\nprint(submission.head(10))\n\n# Save submission file\noutput_path = '/kaggle/working/submission.csv'\nsubmission.to_csv(output_path, index=False)\n\nprint(f'\\nSubmission saved!')\nprint(f'File: {output_path}')\nprint(f'Total samples: {len(submission)}')\nprint('\\n=== READY FOR SUBMISSION ===')\nprint('The submission.csv file is now ready to be submitted!')\nprint(f'Shape: {submission.shape}')\nprint(f'Columns: {submission.columns.tolist()}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-12T05:49:21.578043Z","iopub.status.idle":"2025-11-12T05:49:21.578563Z","shell.execute_reply.started":"2025-11-12T05:49:21.578317Z","shell.execute_reply":"2025-11-12T05:49:21.578355Z"}},"outputs":[],"execution_count":null}]}