{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"#  Exploratory Data Analysis (EDA) Notebook \nThis notebook provides a basic framework for evaluating Exploratory Data Analysis (EDA) skills using Python. Students must execute each cell and pass the associated tests to proceed.","metadata":{}},{"cell_type":"code","source":"import os\nimport random\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nimport plotly.offline as pyo\nfrom IPython.display import HTML\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T17:42:36.070317Z","iopub.execute_input":"2025-01-05T17:42:36.07068Z","iopub.status.idle":"2025-01-05T17:42:36.074811Z","shell.execute_reply.started":"2025-01-05T17:42:36.070654Z","shell.execute_reply":"2025-01-05T17:42:36.073802Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv_path = \"/kaggle/input/UBC-OCEAN/train.csv\"\ntrain_images_path = \"/kaggle/input/UBC-OCEAN/train_images\"\ntrain_thumbnails_path = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\ntest_csv_path = \"/kaggle/input/UBC-OCEAN/test.csv\"\ntest_images_path = \"/kaggle/input/UBC-OCEAN/test_images\"\ntest_thumbnails_path = \"/kaggle/input/UBC-OCEAN/test_images\"\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T19:09:02.48159Z","iopub.execute_input":"2025-01-05T19:09:02.481872Z","iopub.status.idle":"2025-01-05T19:09:02.485726Z","shell.execute_reply.started":"2025-01-05T19:09:02.481851Z","shell.execute_reply":"2025-01-05T19:09:02.484965Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(train_csv_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T19:10:14.164993Z","iopub.execute_input":"2025-01-05T19:10:14.165283Z","iopub.status.idle":"2025-01-05T19:10:14.1728Z","shell.execute_reply.started":"2025-01-05T19:10:14.165261Z","shell.execute_reply":"2025-01-05T19:10:14.171875Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T17:42:47.01721Z","iopub.execute_input":"2025-01-05T17:42:47.017535Z","iopub.status.idle":"2025-01-05T17:42:47.033523Z","shell.execute_reply.started":"2025-01-05T17:42:47.017509Z","shell.execute_reply":"2025-01-05T17:42:47.032542Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Verify dataset size\nprint(\"Size of the dataset: \", df.shape[0])\nassert df.shape[0] > 0, \"Dataset size must be greater than 0\"","metadata":{"nbgrader":{"grade":true,"grade_id":"dataset_size","locked":true,"points":1,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T18:11:41.911649Z","iopub.execute_input":"2025-01-05T18:11:41.911922Z","iopub.status.idle":"2025-01-05T18:11:41.915933Z","shell.execute_reply.started":"2025-01-05T18:11:41.911902Z","shell.execute_reply":"2025-01-05T18:11:41.914866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Visual Exploration\n### Random Image Plotting","metadata":{}},{"cell_type":"code","source":"Image.MAX_IMAGE_PIXELS=None\ndef plot_images(folder_path: str, resize: bool = False):\n    image_files = [f for f in os.listdir(folder_path) if f.endswith(('.jpg', '.jpeg', '.png', '.gif'))]\n    num_images_to_plot = 6\n    selected_images = random.sample(image_files, num_images_to_plot)\n    fig, axes = plt.subplots(2, 3, figsize=(12, 8))\n    for i, ax in enumerate(axes.flat):\n        if i < num_images_to_plot:\n            image_path = os.path.join(folder_path, selected_images[i])\n            img = Image.open(image_path)\n            if resize:\n                img = img.resize((512,512))\n            img = np.array(img)\n            ax.imshow(img)\n            ax.set_title(selected_images[i])\n            ax.axis('off')\n    plt.tight_layout()\n    plt.show()\n#Test the function:\nplot_images(\"/kaggle/input/UBC-OCEAN/train_thumbnails\",resize=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T20:07:11.373866Z","iopub.execute_input":"2025-01-05T20:07:11.37416Z","iopub.status.idle":"2025-01-05T20:07:14.30021Z","shell.execute_reply.started":"2025-01-05T20:07:11.374138Z","shell.execute_reply":"2025-01-05T20:07:14.299236Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Label Distribution","metadata":{}},{"cell_type":"code","source":"label_df = pd.DataFrame(df['label'].value_counts())\nlabel_df.reset_index(inplace=True) \nplt.bar(label_df['label'], label_df['count'], color='skyblue')\nplt.xlabel('Labels')\nplt.ylabel('Count')\nplt.title('Label Distribution')\nplt.show()\nassert len(label_df) > 0, \"Label distribution must be displayed correctly\"\n","metadata":{"nbgrader":{"grade":true,"grade_id":"label_distribution","locked":true,"points":3,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T20:07:05.715573Z","iopub.execute_input":"2025-01-05T20:07:05.715895Z","iopub.status.idle":"2025-01-05T20:07:05.937771Z","shell.execute_reply.started":"2025-01-05T20:07:05.715867Z","shell.execute_reply":"2025-01-05T20:07:05.936968Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Class-wise Analysis","metadata":{}},{"cell_type":"code","source":"HGSC = df[df['label']==\"HGSC\"]\nEC = df[df['label']==\"EC\"]\nCC = df[df['label']==\"CC\"]\nLGSC = df[df['label']==\"LGSC\"]\nMC = df[df['label']==\"MC\"]\n\nplt.figure(figsize=(20, 8))\nplt.rcParams['font.size'] = 14\ncolors = ['red', 'lightblue', 'green','magenta', 'yellow']\nplt.pie([len(HGSC), len(EC), len(CC), len(LGSC), len(MC)], \n        labels=['HGSC', 'EC', 'CC', 'LGSC', 'MC'], autopct='%1.1f%%', colors=colors)\nplt.title('Training Set')\nplt.show()\nassert sum([len(HGSC), len(EC), len(CC), len(LGSC), len(MC)]) == len(df), \"Pie chart proportions must match dataset size\"","metadata":{"nbgrader":{"grade":true,"grade_id":"class_analysis","locked":true,"points":4,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T20:06:57.634177Z","iopub.execute_input":"2025-01-05T20:06:57.63455Z","iopub.status.idle":"2025-01-05T20:06:57.755178Z","shell.execute_reply.started":"2025-01-05T20:06:57.634517Z","shell.execute_reply":"2025-01-05T20:06:57.7541Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Seaborn Visualization\n### Distribution Plot","metadata":{}},{"cell_type":"code","source":"\nimport seaborn as sns\nsns.histplot(data=df, x='label', kde=True, color='blue')\nplt.title('Label Distribution with Seaborn')\nplt.show()\nassert not df['label'].isnull().any(), \"Ensure there are no missing values in the label column\"","metadata":{"nbgrader":{"grade":true,"grade_id":"seaborn_distplot","locked":true,"points":3,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T20:06:44.295157Z","iopub.execute_input":"2025-01-05T20:06:44.29549Z","iopub.status.idle":"2025-01-05T20:06:44.534952Z","shell.execute_reply.started":"2025-01-05T20:06:44.295465Z","shell.execute_reply":"2025-01-05T20:06:44.534226Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Correlation Matrix","metadata":{}},{"cell_type":"code","source":"numeric_df = df.select_dtypes(include=[np.number])\ncorrelation_matrix = numeric_df.corr()\nsns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')\nplt.title('Correlation Matrix')\nplt.show()\nassert correlation_matrix.shape[0] > 0, \"Correlation matrix must be generated\"","metadata":{"nbgrader":{"grade":true,"grade_id":"correlation_matrix","locked":true,"points":3,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T20:05:50.012774Z","iopub.execute_input":"2025-01-05T20:05:50.013106Z","iopub.status.idle":"2025-01-05T20:05:50.285874Z","shell.execute_reply.started":"2025-01-05T20:05:50.013083Z","shell.execute_reply":"2025-01-05T20:05:50.284953Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Scatter Plot","metadata":{}},{"cell_type":"code","source":"print(df.columns),'Ckeck the columns existing in dataset'\n#Changing the x and y to available column names:\nsns.scatterplot(data=df, x='image_id', y='image_width', hue='label', palette='Set2')\nplt.title('Scatter Plot of Feature1(image id) vs Feature2(image width)')\nplt.show()\nassert 'image_id' in df.columns and 'image_width' in df.columns, \"Ensure 'image id' and 'image width' columns exist in the dataset\"","metadata":{"nbgrader":{"grade":true,"grade_id":"scatter_plot","locked":true,"points":3,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T20:05:13.199265Z","iopub.execute_input":"2025-01-05T20:05:13.199654Z","iopub.status.idle":"2025-01-05T20:05:13.609187Z","shell.execute_reply.started":"2025-01-05T20:05:13.199623Z","shell.execute_reply":"2025-01-05T20:05:13.608319Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Pair Plot","metadata":{}},{"cell_type":"code","source":"\nsns.pairplot(data=df, hue='label', palette='husl')\nplt.suptitle('Pair Plot of Dataset Features', y=1.02)\nplt.show()\nassert 'label' in df.columns, \"Ensure the 'label' column is included for the pair plot\"","metadata":{"nbgrader":{"grade":true,"grade_id":"pair_plot","locked":true,"points":3,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T19:54:59.816156Z","iopub.execute_input":"2025-01-05T19:54:59.816534Z","iopub.status.idle":"2025-01-05T19:55:05.451326Z","shell.execute_reply.started":"2025-01-05T19:54:59.816503Z","shell.execute_reply":"2025-01-05T19:55:05.450513Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Box Plot","metadata":{}},{"cell_type":"code","source":"sns.boxplot(data=df, x='label', y='image_id', palette='cool')\nplt.title('Box Plot of image_id by Label')\nplt.show()\nassert 'image_id' in df.columns, \"Ensure 'image_id' exists in the dataset\"","metadata":{"nbgrader":{"grade":true,"grade_id":"box_plot","locked":true,"points":3,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T20:04:27.635478Z","iopub.execute_input":"2025-01-05T20:04:27.635837Z","iopub.status.idle":"2025-01-05T20:04:27.903975Z","shell.execute_reply.started":"2025-01-05T20:04:27.635807Z","shell.execute_reply":"2025-01-05T20:04:27.903185Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Count Plot","metadata":{}},{"cell_type":"code","source":"sns.countplot(data=df, x='is_tma', palette='pastel')\nplt.title('Count Plot of is_tma')\nplt.show()\nassert 'is_tma' in df.columns, \"is_tma exists in the dataset\"","metadata":{"nbgrader":{"grade":true,"grade_id":"count_plot","locked":true,"points":2,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-05T20:02:59.363699Z","iopub.execute_input":"2025-01-05T20:02:59.363992Z","iopub.status.idle":"2025-01-05T20:02:59.571345Z","shell.execute_reply.started":"2025-01-05T20:02:59.36397Z","shell.execute_reply":"2025-01-05T20:02:59.570473Z"}},"outputs":[],"execution_count":null}]}