{"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":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30822,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"\n\nimport 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\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:52:27.649399Z","iopub.execute_input":"2025-01-06T09:52:27.649779Z","iopub.status.idle":"2025-01-06T09:52:27.655052Z","shell.execute_reply.started":"2025-01-06T09:52:27.649751Z","shell.execute_reply":"2025-01-06T09:52:27.653888Z"}},"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\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:52:33.523031Z","iopub.execute_input":"2025-01-06T09:52:33.523414Z","iopub.status.idle":"2025-01-06T09:52:33.528196Z","shell.execute_reply.started":"2025-01-06T09:52:33.523376Z","shell.execute_reply":"2025-01-06T09:52:33.526952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(train_csv_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:52:38.52071Z","iopub.execute_input":"2025-01-06T09:52:38.521231Z","iopub.status.idle":"2025-01-06T09:52:38.541021Z","shell.execute_reply.started":"2025-01-06T09:52:38.521189Z","shell.execute_reply":"2025-01-06T09:52:38.539809Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndf.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:52:43.850067Z","iopub.execute_input":"2025-01-06T09:52:43.850398Z","iopub.status.idle":"2025-01-06T09:52:43.871097Z","shell.execute_reply.started":"2025-01-06T09:52:43.850369Z","shell.execute_reply":"2025-01-06T09:52:43.869922Z"}},"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-06T09:53:15.115204Z","iopub.execute_input":"2025-01-06T09:53:15.115543Z","iopub.status.idle":"2025-01-06T09:53:15.122079Z","shell.execute_reply.started":"2025-01-06T09:53:15.115516Z","shell.execute_reply":"2025-01-06T09:53:15.120662Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Visual Exploration\n### Random Image Plotting","metadata":{}},{"cell_type":"code","source":"def plot_images(folder_path: str, resize: bool = False):\n    # Get a list of image file names in the folder\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\n# Test the function\nplot_images('/kaggle/input/UBC-OCEAN/train_thumbnails', resize=True)","metadata":{"nbgrader":{"grade":true,"grade_id":"plot_images","locked":true,"points":2,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:57:40.971637Z","iopub.execute_input":"2025-01-06T09:57:40.972033Z","iopub.status.idle":"2025-01-06T09:57:43.844981Z","shell.execute_reply.started":"2025-01-06T09:57:40.972004Z","shell.execute_reply":"2025-01-06T09:57:43.843427Z"}},"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['label'], 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\"","metadata":{"nbgrader":{"grade":true,"grade_id":"label_distribution","locked":true,"points":3,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:59:10.32343Z","iopub.execute_input":"2025-01-06T09:59:10.323779Z","iopub.status.idle":"2025-01-06T09:59:10.470258Z","shell.execute_reply.started":"2025-01-06T09:59:10.323752Z","shell.execute_reply":"2025-01-06T09:59:10.46887Z"}},"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, 6))\nplt.rcParams['font.size'] = 14\ncolors = ['green', 'lightblue', 'red','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-06T10:00:37.900421Z","iopub.execute_input":"2025-01-06T10:00:37.900809Z","iopub.status.idle":"2025-01-06T10:00:38.095804Z","shell.execute_reply.started":"2025-01-06T10:00:37.90078Z","shell.execute_reply":"2025-01-06T10:00:38.094026Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Seaborn Visualization\n### Distribution Plot","metadata":{}},{"cell_type":"code","source":"import 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-06T10:02:00.018774Z","iopub.execute_input":"2025-01-06T10:02:00.019188Z","iopub.status.idle":"2025-01-06T10:02:00.244862Z","shell.execute_reply.started":"2025-01-06T10:02:00.019158Z","shell.execute_reply":"2025-01-06T10:02:00.243644Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Correlation Matrix","metadata":{}},{"cell_type":"code","source":"numeric_df=df.select_dtypes(include=['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-06T10:37:10.229628Z","iopub.execute_input":"2025-01-06T10:37:10.230142Z","iopub.status.idle":"2025-01-06T10:37:10.49998Z","shell.execute_reply.started":"2025-01-06T10:37:10.230106Z","shell.execute_reply":"2025-01-06T10:37:10.498885Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Scatter Plot","metadata":{}},{"cell_type":"code","source":"sns.scatterplot(data=df, x='image_id', y='is_tma', hue='label', palette='Blues')\nplt.title('Scatter Plot of image_id vs is_tma')\nplt.show()\nassert 'image_id' in df.columns and 'is_tma' in df.columns, \"Ensure 'feature1' and 'feature2' 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-06T10:42:04.203664Z","iopub.execute_input":"2025-01-06T10:42:04.204116Z","iopub.status.idle":"2025-01-06T10:42:04.764185Z","shell.execute_reply.started":"2025-01-06T10:42:04.204083Z","shell.execute_reply":"2025-01-06T10:42:04.763Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Pair Plot","metadata":{}},{"cell_type":"code","source":"sns.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-06T10:43:32.200452Z","iopub.execute_input":"2025-01-06T10:43:32.200968Z","iopub.status.idle":"2025-01-06T10:43:38.310898Z","shell.execute_reply.started":"2025-01-06T10:43:32.200924Z","shell.execute_reply":"2025-01-06T10:43:38.309634Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Box Plot","metadata":{}},{"cell_type":"code","source":"sns.boxplot(data=df, x='label', y='image_height', palette='cool')\nplt.title('Box Plot of Numerical Feature by Label')\nplt.show()\nassert 'image_height' in df.columns, \"Ensure 'numerical_feature' 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-06T10:46:56.076916Z","iopub.execute_input":"2025-01-06T10:46:56.077284Z","iopub.status.idle":"2025-01-06T10:46:56.497172Z","shell.execute_reply.started":"2025-01-06T10:46:56.077258Z","shell.execute_reply":"2025-01-06T10:46:56.495899Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Count Plot","metadata":{}},{"cell_type":"code","source":"sns.countplot(data=df, x='label', palette='pastel')\nplt.title('Count Plot of label')\nplt.show()\nassert 'label' in df.columns, \"Ensure 'categorical_feature' 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-06T10:48:17.861515Z","iopub.execute_input":"2025-01-06T10:48:17.861911Z","iopub.status.idle":"2025-01-06T10:48:18.106107Z","shell.execute_reply.started":"2025-01-06T10:48:17.861882Z","shell.execute_reply":"2025-01-06T10:48:18.104932Z"}},"outputs":[],"execution_count":null}]}