{"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":"markdown","source":"### **<span style=\"color:#F7B2B0;\">Give a thumbs up if you like this kernel!</span>**\n","metadata":{}},{"cell_type":"markdown","source":"![](https://customsitesmedia.usc.edu/wp-content/uploads/sites/59/2022/09/28163539/breast_cancer_detection_web.jpg)","metadata":{}},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #0bb886;\"><b style=\"color:white;\">Import Libraries\n</b></h1></center>","metadata":{}},{"cell_type":"code","source":"import sys\nimport os\nimport glob\n\nimport pandas as pd\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\n\nimport warnings\n\nwarnings.filterwarnings('ignore')\n\nplt.style.use('ggplot')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-02T05:49:12.493918Z","iopub.execute_input":"2022-12-02T05:49:12.494819Z","iopub.status.idle":"2022-12-02T05:49:12.500347Z","shell.execute_reply.started":"2022-12-02T05:49:12.494776Z","shell.execute_reply":"2022-12-02T05:49:12.499489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_DIR = '/kaggle/input/rsna-breast-cancer-detection'","metadata":{"execution":{"iopub.status.busy":"2022-12-02T05:05:25.115236Z","iopub.execute_input":"2022-12-02T05:05:25.11598Z","iopub.status.idle":"2022-12-02T05:05:25.121894Z","shell.execute_reply.started":"2022-12-02T05:05:25.115933Z","shell.execute_reply":"2022-12-02T05:05:25.12053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #0bb886;\"><b style=\"color:white;\">Reading Data\n</b></h1></center>","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(os.path.join(BASE_DIR,'train.csv'))\ntest_df = pd.read_csv(os.path.join(BASE_DIR,'test.csv'))\nsample_df = pd.read_csv(os.path.join(BASE_DIR,'sample_submission.csv'))","metadata":{"execution":{"iopub.status.busy":"2022-12-02T05:05:25.128874Z","iopub.execute_input":"2022-12-02T05:05:25.130377Z","iopub.status.idle":"2022-12-02T05:05:25.298137Z","shell.execute_reply.started":"2022-12-02T05:05:25.130326Z","shell.execute_reply":"2022-12-02T05:05:25.297088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #0bb886;\"><b style=\"color:white;\">Check Nulls In Data\n</b></h1></center>","metadata":{}},{"cell_type":"markdown","source":"* BIRADS & Density is having Lot of Nulls","metadata":{}},{"cell_type":"code","source":"null_ = train_df.isnull().sum().reset_index(name=\"count\")\ncm = sns.light_palette(\"blue\", as_cmap=True)\nnull_.style.background_gradient(cmap=cm)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T05:20:10.556056Z","iopub.execute_input":"2022-12-02T05:20:10.556559Z","iopub.status.idle":"2022-12-02T05:20:10.587848Z","shell.execute_reply.started":"2022-12-02T05:20:10.556518Z","shell.execute_reply":"2022-12-02T05:20:10.586612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #0bb886;\"><b style=\"color:white;\">Describe dataset to get value range and Quantile Info\n</b></h1></center>","metadata":{}},{"cell_type":"code","source":"description =train_df.describe() \ndescription = description.drop(['site_id','patient_id','image_id','machine_id'],axis=1)\ncm = sns.light_palette(\"red\", as_cmap=True)\ndescription.style.background_gradient(cmap=cm)","metadata":{"execution":{"iopub.status.busy":"2022-12-02T05:05:39.04064Z","iopub.execute_input":"2022-12-02T05:05:39.041033Z","iopub.status.idle":"2022-12-02T05:05:39.112269Z","shell.execute_reply.started":"2022-12-02T05:05:39.040988Z","shell.execute_reply":"2022-12-02T05:05:39.110879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #0bb886;\"><b style=\"color:white;\">Count Image By Patient ID,s\n</b></h1></center>","metadata":{}},{"cell_type":"markdown","source":"* Here a thing to note that not all the Patient is having 4 images it varies a lot , There are patient having 12-14 Image , Quite some imbalence there","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(20,5))\nimage_count_by_pid = train_df.groupby('patient_id')['image_id'] \\\n        .count().reset_index(name='image_count') \\\n        .sort_values(by='image_count',ascending=False) \\\n        .reset_index(drop=True).head(10)\nplt.figure(figsize=(15,5))\nsns.barplot(data = image_count_by_pid,x='patient_id',y='image_count',capsize=.4, errcolor=\".5\",\n                            linewidth=3, edgecolor=\".5\", facecolor=(0, 0, 0, 0), ax=ax[0])\nsns.countplot(train_df.groupby('patient_id').size(), ax=ax[1])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-02T05:50:31.596937Z","iopub.execute_input":"2022-12-02T05:50:31.597359Z","iopub.status.idle":"2022-12-02T05:50:32.436131Z","shell.execute_reply.started":"2022-12-02T05:50:31.597325Z","shell.execute_reply":"2022-12-02T05:50:32.435077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #0bb886;\"><b style=\"color:white;\">Checking Variable Distribution\n</b></h1></center>","metadata":{}},{"cell_type":"markdown","source":"* laterality: Almost the %age is same for the image from left & right breast\n* View : There is quite some imbalence in this column\n* Less number of patient had breast implants\n* Dense : For most of the patient the breast tissue is averagely dense","metadata":{}},{"cell_type":"code","source":"laterality_dist = train_df.laterality.value_counts().reset_index(name='count')\nview_dist = train_df.view.value_counts().reset_index(name='count')\ndensity_dist = train_df.density.value_counts().reset_index(name='count')\nimplant_dist = train_df.implant.value_counts().reset_index(name='count')","metadata":{"execution":{"iopub.status.busy":"2022-12-02T06:16:55.917505Z","iopub.execute_input":"2022-12-02T06:16:55.918003Z","iopub.status.idle":"2022-12-02T06:16:55.945031Z","shell.execute_reply.started":"2022-12-02T06:16:55.917965Z","shell.execute_reply":"2022-12-02T06:16:55.944107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])\nfig.add_trace(go.Pie(labels=laterality_dist['index'], values=laterality_dist['count'], name=\"laterality\"),1, 1)\nfig.add_trace(go.Pie(labels=view_dist['index'], values=view_dist['count'], name=\"view\"),1, 2)\n\n# Use `hole` to create a donut-like pie chart\nfig.update_traces(hole=.4, hoverinfo=\"label+percent+name\",showlegend=False)\n\nfig.update_layout(\n    title_text=\"\",\n    # Add annotations in the center of the donut pies.\n    annotations=[dict(text='laterality', x=0.18, y=0.5, font_size=20, showarrow=False),\n                 dict(text='view', x=0.82, y=0.5, font_size=20, showarrow=False)])\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-02T06:16:56.673236Z","iopub.execute_input":"2022-12-02T06:16:56.674189Z","iopub.status.idle":"2022-12-02T06:16:56.704861Z","shell.execute_reply.started":"2022-12-02T06:16:56.674141Z","shell.execute_reply":"2022-12-02T06:16:56.703324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])\nfig.add_trace(go.Pie(labels=density_dist['index'], values=density_dist['count'], name=\"view\"),1, 1)\nfig.add_trace(go.Pie(labels=implant_dist['index'], values=implant_dist['count'], name=\"view\"),1, 2)\n\n# Use `hole` to create a donut-like pie chart\nfig.update_traces(hole=.4, hoverinfo=\"label+percent+name\",showlegend=False)\n\nfig.update_layout(\n    title_text=\"\",\n    # Add annotations in the center of the donut pies.\n    annotations=[\n                dict(text='density', x=0.18, y=0.5, font_size=20, showarrow=False),\n                dict(text='implant', x=0.82, y=0.5, font_size=20, showarrow=False)])\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-02T06:16:57.137391Z","iopub.execute_input":"2022-12-02T06:16:57.138543Z","iopub.status.idle":"2022-12-02T06:16:57.17072Z","shell.execute_reply.started":"2022-12-02T06:16:57.138484Z","shell.execute_reply":"2022-12-02T06:16:57.16966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #0bb886;\"><b style=\"color:white;\">Site Information\n</b></h1></center>","metadata":{}},{"cell_type":"markdown","source":"* We already know site 1 only provides breast implant information at the patient level\n* Source hospital contribution is almost same","metadata":{}},{"cell_type":"code","source":"# machine_distribution = \nsite_distribution = train_df.groupby(['site_id','cancer'])['cancer'].count().reset_index(name='count')\nsite_distribution['cancer'] = site_distribution.cancer.map({0:'No Cancer',1:'Cancer'})\nfig = px.bar(site_distribution, x=\"cancer\", y=\"count\", color=\"site_id\",\n             pattern_shape=\"cancer\", pattern_shape_sequence=[\".\", \"x\", \"+\"],width=1000)\nfig.update_coloraxes(showscale=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-02T06:40:22.135832Z","iopub.execute_input":"2022-12-02T06:40:22.136251Z","iopub.status.idle":"2022-12-02T06:40:22.221571Z","shell.execute_reply.started":"2022-12-02T06:40:22.136217Z","shell.execute_reply":"2022-12-02T06:40:22.220415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #0bb886;\"><b style=\"color:white;\">Machine Information\n</b></h1></center>","metadata":{}},{"cell_type":"markdown","source":"* Most of the images are coming from 49, 21, 29 and 48\n* ALso these are the 4 machine which are contributing to most image where people are having cancer","metadata":{}},{"cell_type":"code","source":"machine_distribution = train_df.groupby(['machine_id','cancer'])['cancer'].sum().reset_index(name='cancer_count')\nfig = px.scatter(machine_distribution, x=\"machine_id\", y=\"cancer_count\", size=\"cancer_count\", color=\"machine_id\",\n           hover_name=\"machine_id\",labels ='machine_id',  log_x=True, size_max=60,width=1000)\nfig.update_coloraxes(showscale=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-02T06:53:21.346597Z","iopub.execute_input":"2022-12-02T06:53:21.347051Z","iopub.status.idle":"2022-12-02T06:53:21.423646Z","shell.execute_reply.started":"2022-12-02T06:53:21.347004Z","shell.execute_reply":"2022-12-02T06:53:21.422347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #0bb886;\"><b style=\"color:white;\">Contribution of Age to cancer\n</b></h1></center>","metadata":{}},{"cell_type":"markdown","source":"* People with Age of 55 to 75 are having cancer the most","metadata":{}},{"cell_type":"code","source":"# machine_distribution = \nage_distribution = train_df.query(\"cancer==1\").groupby(['age','cancer'])['cancer'].count().reset_index(name='count')\nage_distribution['cancer'] = age_distribution.cancer.map({0:'No Cancer',1:'Cancer'})\nfig = px.bar(age_distribution, x=\"age\", y=\"count\", color=\"age\",\n             pattern_shape=\"age\", pattern_shape_sequence=[\".\", \"x\", \"+\"],width=1000)\nfig.update_coloraxes(showscale=False)\nfig.update_layout(showlegend=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-02T06:59:42.629563Z","iopub.execute_input":"2022-12-02T06:59:42.629968Z","iopub.status.idle":"2022-12-02T06:59:42.909603Z","shell.execute_reply.started":"2022-12-02T06:59:42.629936Z","shell.execute_reply":"2022-12-02T06:59:42.908224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #b80b3d;\"><b style=\"color:white;\">End</b></h1></center>","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}