{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# # This Python 3 environment comes with many helpful analytics libraries installed\n# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# # For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-19T13:52:20.62203Z","iopub.execute_input":"2023-02-19T13:52:20.622451Z","iopub.status.idle":"2023-02-19T13:52:20.628553Z","shell.execute_reply.started":"2023-02-19T13:52:20.622417Z","shell.execute_reply":"2023-02-19T13:52:20.627054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Load the training data\ntrain_data = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\n\n# Load the test data\ntest_data = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')\n","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:20.644023Z","iopub.execute_input":"2023-02-19T13:52:20.644405Z","iopub.status.idle":"2023-02-19T13:52:20.743268Z","shell.execute_reply.started":"2023-02-19T13:52:20.644375Z","shell.execute_reply":"2023-02-19T13:52:20.741966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:20.745635Z","iopub.execute_input":"2023-02-19T13:52:20.746026Z","iopub.status.idle":"2023-02-19T13:52:20.772603Z","shell.execute_reply.started":"2023-02-19T13:52:20.745961Z","shell.execute_reply":"2023-02-19T13:52:20.771239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:20.774114Z","iopub.execute_input":"2023-02-19T13:52:20.774555Z","iopub.status.idle":"2023-02-19T13:52:20.79047Z","shell.execute_reply.started":"2023-02-19T13:52:20.774512Z","shell.execute_reply":"2023-02-19T13:52:20.788888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport math\n\nratio = 0.8\npatient_id = train_data['patient_id'].unique()\nindexs = np.random.permutation(len(patient_id))\nsplit_idx = math.ceil(len(indexs) * ratio)\n\ntrain_patients = patient_id[indexs[:split_idx]]\nval_patients = patient_id[indexs[split_idx:]]\n\nval_data = train_data.loc[train_data['patient_id'].isin(val_patients)]\ntrain_data = train_data.loc[train_data['patient_id'].isin(train_patients)]\n\n\nprint(\"{} for training, {} for validation.\".format(len(train_data), len(val_data)))\nprint(val_data['patient_id'].unique())\nprint(train_data['patient_id'].unique())\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:20.793617Z","iopub.execute_input":"2023-02-19T13:52:20.794024Z","iopub.status.idle":"2023-02-19T13:52:20.81939Z","shell.execute_reply.started":"2023-02-19T13:52:20.79396Z","shell.execute_reply":"2023-02-19T13:52:20.817937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_decision_forests as tfdf\n\n\n# Convert the dataset into a TensorFlow dataset with a larger batch size.\nbatch_size = 512\ntrain_data = tfdf.keras.pd_dataframe_to_tf_dataset(train_data.loc[:, ['laterality', 'view', 'age', 'implant', 'cancer']], label=\"cancer\", batch_size=batch_size)\nval_data = tfdf.keras.pd_dataframe_to_tf_dataset(val_data.loc[:, ['laterality', 'view', 'age', 'implant', 'cancer']], label=\"cancer\", batch_size=batch_size)\ntest_data = tfdf.keras.pd_dataframe_to_tf_dataset(test_data.loc[:, ['laterality', 'view', 'age', 'implant']], batch_size=batch_size)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:20.820869Z","iopub.execute_input":"2023-02-19T13:52:20.82134Z","iopub.status.idle":"2023-02-19T13:52:20.877049Z","shell.execute_reply.started":"2023-02-19T13:52:20.821303Z","shell.execute_reply":"2023-02-19T13:52:20.875552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train a Random Forest model.\n# model = tfdf.keras.GradientBoostedTreesModel(verbose=10)\n# model.fit(train_data)\n\n\n# Train the model\nmodel = tfdf.keras.GradientBoostedTreesModel(verbose=1)\nmodel.fit(train_data)","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:20.87856Z","iopub.execute_input":"2023-02-19T13:52:20.878988Z","iopub.status.idle":"2023-02-19T13:52:22.867231Z","shell.execute_reply.started":"2023-02-19T13:52:20.878945Z","shell.execute_reply":"2023-02-19T13:52:22.865916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Summary of the model structure.\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:22.868566Z","iopub.execute_input":"2023-02-19T13:52:22.868907Z","iopub.status.idle":"2023-02-19T13:52:22.890933Z","shell.execute_reply.started":"2023-02-19T13:52:22.868876Z","shell.execute_reply":"2023-02-19T13:52:22.889352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the model\ntfdf.model_plotter.plot_model_in_colab(model, tree_idx=2, max_depth=10)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:22.892776Z","iopub.execute_input":"2023-02-19T13:52:22.893195Z","iopub.status.idle":"2023-02-19T13:52:22.915141Z","shell.execute_reply.started":"2023-02-19T13:52:22.893161Z","shell.execute_reply":"2023-02-19T13:52:22.91316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model.\ndecision_forests_modal = model.evaluate(val_data)","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:22.919196Z","iopub.execute_input":"2023-02-19T13:52:22.919595Z","iopub.status.idle":"2023-02-19T13:52:23.050828Z","shell.execute_reply.started":"2023-02-19T13:52:22.919564Z","shell.execute_reply":"2023-02-19T13:52:23.049398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform the prediction on test data\npredictions = model.predict(test_data)\npredictions\n","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:23.054743Z","iopub.execute_input":"2023-02-19T13:52:23.055127Z","iopub.status.idle":"2023-02-19T13:52:23.173289Z","shell.execute_reply.started":"2023-02-19T13:52:23.055087Z","shell.execute_reply":"2023-02-19T13:52:23.171964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the sample submission file\nsample_submission = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/sample_submission.csv')\n\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:23.175232Z","iopub.execute_input":"2023-02-19T13:52:23.17558Z","iopub.status.idle":"2023-02-19T13:52:23.191137Z","shell.execute_reply.started":"2023-02-19T13:52:23.17555Z","shell.execute_reply":"2023-02-19T13:52:23.189651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('prediction shape',predictions.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:23.192866Z","iopub.execute_input":"2023-02-19T13:52:23.193501Z","iopub.status.idle":"2023-02-19T13:52:23.20332Z","shell.execute_reply.started":"2023-02-19T13:52:23.193461Z","shell.execute_reply":"2023-02-19T13:52:23.201604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Load the original test data with the prediction_id column\ntest_data_orig = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')\n\n# Copy the prediction_id column from the original test data\nprediction_ids = test_data_orig['prediction_id'].copy()\n\n# Flatten the predictions array\npredictions = predictions.ravel()\n\n# Create a dataframe with the required format\nsubmission = pd.DataFrame({'prediction_id': prediction_ids, 'cancer': predictions}).groupby('prediction_id').mean().reset_index()\n\n# Save the dataframe to a CSV file\nsubmission.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:23.204999Z","iopub.execute_input":"2023-02-19T13:52:23.205391Z","iopub.status.idle":"2023-02-19T13:52:23.220334Z","shell.execute_reply.started":"2023-02-19T13:52:23.205356Z","shell.execute_reply":"2023-02-19T13:52:23.219366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Final check of the submissiom csv file\npd.read_csv('/kaggle/working/submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-19T13:52:23.221643Z","iopub.execute_input":"2023-02-19T13:52:23.222646Z","iopub.status.idle":"2023-02-19T13:52:23.235968Z","shell.execute_reply.started":"2023-02-19T13:52:23.222609Z","shell.execute_reply":"2023-02-19T13:52:23.234518Z"},"trusted":true},"execution_count":null,"outputs":[]}]}