{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n\nimport matplotlib.pyplot as plt\nfrom PIL import Image\n\nfrom glob import glob\nimport cv2\nimport skimage","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-08T08:22:45.211937Z","iopub.execute_input":"2022-11-08T08:22:45.212259Z","iopub.status.idle":"2022-11-08T08:22:45.218398Z","shell.execute_reply.started":"2022-11-08T08:22:45.212235Z","shell.execute_reply":"2022-11-08T08:22:45.217055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'{len(os.listdir(\"../input/histopathologic-cancer-detection/train\"))} pictures in train.')\nprint(f'{len(os.listdir(\"../input/histopathologic-cancer-detection/test\"))} pictures in test.')","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:22:51.333550Z","iopub.execute_input":"2022-11-08T08:22:51.333878Z","iopub.status.idle":"2022-11-08T08:22:51.511902Z","shell.execute_reply.started":"2022-11-08T08:22:51.333855Z","shell.execute_reply":"2022-11-08T08:22:51.511213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(\"../input/histopathologic-cancer-detection\"))","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:22:54.768095Z","iopub.execute_input":"2022-11-08T08:22:54.768405Z","iopub.status.idle":"2022-11-08T08:22:54.774146Z","shell.execute_reply.started":"2022-11-08T08:22:54.768381Z","shell.execute_reply":"2022-11-08T08:22:54.772732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = pd.read_csv('../input/histopathologic-cancer-detection/train_labels.csv')\ndata_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:22:56.207594Z","iopub.execute_input":"2022-11-08T08:22:56.207930Z","iopub.status.idle":"2022-11-08T08:22:56.430145Z","shell.execute_reply.started":"2022-11-08T08:22:56.207905Z","shell.execute_reply":"2022-11-08T08:22:56.429249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(25,5))\n# display 20 images\ntrain_imgs = os.listdir(\"../input/histopathologic-cancer-detection/train\")\nfor idx, img in enumerate(np.random.choice(train_imgs, 20)):\n    ax = fig.add_subplot(2, 20//2, idx+1, xticks=[], yticks=[])\n    im = Image.open(\"../input/histopathologic-cancer-detection/train/\" + img)\n    plt.imshow(im)\n    lab = data_train.loc[data_train['id'] == img.split('.')[0], 'label'].values[0]\n    ax.set_title(f'Label: {lab}')","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:22:58.729357Z","iopub.execute_input":"2022-11-08T08:22:58.729704Z","iopub.status.idle":"2022-11-08T08:23:00.219067Z","shell.execute_reply.started":"2022-11-08T08:22:58.729679Z","shell.execute_reply":"2022-11-08T08:23:00.218088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:23:02.809577Z","iopub.execute_input":"2022-11-08T08:23:02.810389Z","iopub.status.idle":"2022-11-08T08:23:02.819254Z","shell.execute_reply.started":"2022-11-08T08:23:02.810353Z","shell.execute_reply":"2022-11-08T08:23:02.817818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = data_train['label']\n","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:23:04.354592Z","iopub.execute_input":"2022-11-08T08:23:04.355247Z","iopub.status.idle":"2022-11-08T08:23:04.359764Z","shell.execute_reply.started":"2022-11-08T08:23:04.355212Z","shell.execute_reply":"2022-11-08T08:23:04.358839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(4, 2),dpi=150)\nplt.bar([1,0], [(y==0).sum(), (y==1).sum()]); \nplt.xticks([1,0],[\"Negative (N={})\".format((y==0).sum()),\"Positive (N={})\".format((y==1).sum())]);\nplt.ylabel(\"# of samples\")","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:23:06.397612Z","iopub.execute_input":"2022-11-08T08:23:06.398369Z","iopub.status.idle":"2022-11-08T08:23:06.506305Z","shell.execute_reply.started":"2022-11-08T08:23:06.398333Z","shell.execute_reply":"2022-11-08T08:23:06.505499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"positive_samples = data_train[y == 1]\nnegative_samples = data_train[y == 0]","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:23:08.808429Z","iopub.execute_input":"2022-11-08T08:23:08.808762Z","iopub.status.idle":"2022-11-08T08:23:08.829855Z","shell.execute_reply.started":"2022-11-08T08:23:08.808738Z","shell.execute_reply":"2022-11-08T08:23:08.828578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"positive_samples.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:41:05.213137Z","iopub.execute_input":"2022-11-08T08:41:05.213672Z","iopub.status.idle":"2022-11-08T08:41:05.225049Z","shell.execute_reply.started":"2022-11-08T08:41:05.213607Z","shell.execute_reply":"2022-11-08T08:41:05.224308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"positive_samples = positive_samples.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:41:53.071670Z","iopub.execute_input":"2022-11-08T08:41:53.072040Z","iopub.status.idle":"2022-11-08T08:41:53.078423Z","shell.execute_reply.started":"2022-11-08T08:41:53.072014Z","shell.execute_reply":"2022-11-08T08:41:53.077681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"positive_samples.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:41:59.330166Z","iopub.execute_input":"2022-11-08T08:41:59.330566Z","iopub.status.idle":"2022-11-08T08:41:59.340539Z","shell.execute_reply.started":"2022-11-08T08:41:59.330532Z","shell.execute_reply":"2022-11-08T08:41:59.339795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(25,5))\n# im = Image.open('../input/histopathologic-cancer-detection/train/c18f2d887b7ae4f6742ee445113fa1aef383ed77.tif')\n# plt.imshow(im)\n\nfor index, row in positive_samples.iloc[:20].iterrows():\n    ax = fig.add_subplot(2, 20//2, index+1, xticks=[], yticks=[])\n    im = Image.open(\"../input/histopathologic-cancer-detection/train/\" + row['id'] + '.tif')\n    plt.imshow(im) \n    lab = row['label']\n    if lab == 1:\n        ax.set_title(\"With Cancer\")\n    else:\n        ax.set_title(\"No Cancer\")\n    ","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:47:26.390070Z","iopub.execute_input":"2022-11-08T08:47:26.390430Z","iopub.status.idle":"2022-11-08T08:47:27.339333Z","shell.execute_reply.started":"2022-11-08T08:47:26.390405Z","shell.execute_reply":"2022-11-08T08:47:27.338237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"negative_samples = negative_samples.reset_index(drop=True)\nnegative_samples.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:47:55.450812Z","iopub.execute_input":"2022-11-08T08:47:55.451160Z","iopub.status.idle":"2022-11-08T08:47:55.466302Z","shell.execute_reply.started":"2022-11-08T08:47:55.451134Z","shell.execute_reply":"2022-11-08T08:47:55.465179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(25,5))\n\nfor index, row in negative_samples.iloc[:20].iterrows():\n    ax = fig.add_subplot(2, 20//2, index+1, xticks=[], yticks=[])\n    im = Image.open(\"../input/histopathologic-cancer-detection/train/\" + row['id'] + '.tif')\n    plt.imshow(im)\n    lab = row['label']\n    if lab == 1:\n        ax.set_title(\"With Cancer\")\n    else:\n        ax.set_title(\"No Cancer\")","metadata":{"execution":{"iopub.status.busy":"2022-11-08T08:48:17.702595Z","iopub.execute_input":"2022-11-08T08:48:17.702967Z","iopub.status.idle":"2022-11-08T08:48:18.629661Z","shell.execute_reply.started":"2022-11-08T08:48:17.702942Z","shell.execute_reply":"2022-11-08T08:48:18.628693Z"},"trusted":true},"execution_count":null,"outputs":[]}]}