{"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-01T16:05:50.301835Z","iopub.execute_input":"2025-01-01T16:05:50.302141Z","iopub.status.idle":"2025-01-01T16:05:53.234306Z","shell.execute_reply.started":"2025-01-01T16:05:50.302112Z","shell.execute_reply":"2025-01-01T16:05:53.232542Z"}},"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-06T08:27:58.070842Z","iopub.execute_input":"2025-01-06T08:27:58.071182Z","iopub.status.idle":"2025-01-06T08:27:58.076085Z","shell.execute_reply.started":"2025-01-06T08:27:58.071157Z","shell.execute_reply":"2025-01-06T08:27:58.07484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(train_csv_path)\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:50:48.168243Z","iopub.execute_input":"2025-01-06T09:50:48.168566Z","iopub.status.idle":"2025-01-06T09:50:48.183008Z","shell.execute_reply.started":"2025-01-06T09:50:48.168542Z","shell.execute_reply":"2025-01-06T09:50:48.181796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n#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:50:52.860947Z","iopub.execute_input":"2025-01-06T09:50:52.861282Z","iopub.status.idle":"2025-01-06T09:50:52.867379Z","shell.execute_reply.started":"2025-01-06T09:50:52.861255Z","shell.execute_reply":"2025-01-06T09:50:52.866228Z"}},"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:50:57.046656Z","iopub.execute_input":"2025-01-06T09:50:57.04733Z","iopub.status.idle":"2025-01-06T09:50:57.958703Z","shell.execute_reply.started":"2025-01-06T09:50:57.047298Z","shell.execute_reply":"2025-01-06T09:50:57.956867Z"}},"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\"","metadata":{"nbgrader":{"grade":true,"grade_id":"label_distribution","locked":true,"points":3,"solution":false},"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:51:03.117075Z","iopub.execute_input":"2025-01-06T09:51:03.11747Z","iopub.status.idle":"2025-01-06T09:51:03.293081Z","shell.execute_reply.started":"2025-01-06T09:51:03.117436Z","shell.execute_reply":"2025-01-06T09:51:03.291671Z"}},"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\"]\nplt.figure(figsize=(20, 6))\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-06T09:51:08.018467Z","iopub.execute_input":"2025-01-06T09:51:08.018834Z","iopub.status.idle":"2025-01-06T09:51:08.185249Z","shell.execute_reply.started":"2025-01-06T09:51:08.018805Z","shell.execute_reply":"2025-01-06T09:51:08.183884Z"}},"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-06T09:51:14.156162Z","iopub.execute_input":"2025-01-06T09:51:14.156513Z","iopub.status.idle":"2025-01-06T09:51:14.416646Z","shell.execute_reply.started":"2025-01-06T09:51:14.156477Z","shell.execute_reply":"2025-01-06T09:51:14.415426Z"}},"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_r')\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-06T09:51:22.365924Z","iopub.execute_input":"2025-01-06T09:51:22.366315Z","iopub.status.idle":"2025-01-06T09:51:22.663512Z","shell.execute_reply.started":"2025-01-06T09:51:22.366279Z","shell.execute_reply":"2025-01-06T09:51:22.66225Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Scatter Plot","metadata":{}},{"cell_type":"code","source":"sns.scatterplot(data=df=\"/kaggle/input/UBC-OCEAN/test.csv\", x='feature1', y='feature2', hue='label', palette='Set2')\nplt.title('Scatter Plot of feature1 vs feature2')\nplt.show()\nassert 'feature1' in df.columns and 'feature2' 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-06T09:51:27.107065Z","iopub.execute_input":"2025-01-06T09:51:27.107421Z","iopub.status.idle":"2025-01-06T09:51:27.115699Z","shell.execute_reply.started":"2025-01-06T09:51:27.107385Z","shell.execute_reply":"2025-01-06T09:51:27.114066Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n#Load the dataset\ndf=pd.read_csv(\"/kaggle/input/UBC-OCEAN/train.csv\")\n#print the column names to confirm what is available\nprint(df.columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:51:51.127611Z","iopub.execute_input":"2025-01-06T09:51:51.128044Z","iopub.status.idle":"2025-01-06T09:51:51.138298Z","shell.execute_reply.started":"2025-01-06T09:51:51.128011Z","shell.execute_reply":"2025-01-06T09:51:51.136832Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns  \nimport matplotlib.pyplot as plt\n#make sure the columns 'image_width' and 'image_height' exist\nassert 'image_width' and 'image_height' in df.columns\n\"Ensure 'image_width' and 'image_height' columns exist in dataset\"\n#Create a scatter plot\nsns.scatterplot(data=df,x='image_width', y='image_height',hue='label',palette='Set2')\n#Add a title and display the plot\nplt.title('Scatterplot of image width vs image height')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:51:55.350945Z","iopub.execute_input":"2025-01-06T09:51:55.351292Z","iopub.status.idle":"2025-01-06T09:51:55.789047Z","shell.execute_reply.started":"2025-01-06T09:51:55.351266Z","shell.execute_reply":"2025-01-06T09:51:55.787901Z"}},"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-06T09:52:00.853214Z","iopub.execute_input":"2025-01-06T09:52:00.853542Z","iopub.status.idle":"2025-01-06T09:52:07.276831Z","shell.execute_reply.started":"2025-01-06T09:52:00.853515Z","shell.execute_reply":"2025-01-06T09:52:07.275479Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Box Plot","metadata":{}},{"cell_type":"code","source":"sns.boxplot(data=df, x='label', y='numerical_feature', palette='cool')\nplt.title('Box Plot of Numerical Feature by Label')\nplt.show()\nassert 'numerical_feature' 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-06T09:53:18.970089Z","iopub.execute_input":"2025-01-06T09:53:18.970442Z","iopub.status.idle":"2025-01-06T09:53:18.990748Z","shell.execute_reply.started":"2025-01-06T09:53:18.970416Z","shell.execute_reply":"2025-01-06T09:53:18.988939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns \nimport matplotlib.pyplot as plt \n#Ensure 'image_width' exists in dataframe\nassert'image_width'in df.columns\n\"Ensure 'image_width' exists in the dataset\"\n#create a boxplot using 'image_width' as the numerical feature\nsns.boxplot(data=df,x='label', y='image_width',palette='cool')\n#Set title for the plot\nplt.title('Box Plot of the Image Width by Label')\n#Show the plot\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:53:31.708921Z","iopub.execute_input":"2025-01-06T09:53:31.709351Z","iopub.status.idle":"2025-01-06T09:53:31.995506Z","shell.execute_reply.started":"2025-01-06T09:53:31.709322Z","shell.execute_reply":"2025-01-06T09:53:31.994475Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Count Plot","metadata":{}},{"cell_type":"code","source":"sns.countplot(data=df, x='categorical_feature', palette='pastel')\nplt.title('Count Plot of Categorical Feature')\nplt.show()\nassert 'categorical_feature' 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-06T09:53:40.050477Z","iopub.execute_input":"2025-01-06T09:53:40.050916Z","iopub.status.idle":"2025-01-06T09:53:40.069535Z","shell.execute_reply.started":"2025-01-06T09:53:40.050884Z","shell.execute_reply":"2025-01-06T09:53:40.067765Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt \nimport pandas as pd\n\n#Load the dataset\ndf=pd.read_csv(\"/kaggle/input/UBC-OCEAN/train.csv\") #Adjust path as necessary\n#Ensure 'label' exists in data frame\nassert 'label' in df.columns,\"Ensure 'label' exists in the data set\"\n#create a count plot using 'label' as the categorical feature\nsns.countplot(data=df,x='label',palette='pastel')\n\n#set the title for the plot\nplt.title('count Plot of categorical feature')\n#show the plot\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-06T09:53:50.095919Z","iopub.execute_input":"2025-01-06T09:53:50.096249Z","iopub.status.idle":"2025-01-06T09:53:50.265693Z","shell.execute_reply.started":"2025-01-06T09:53:50.096223Z","shell.execute_reply":"2025-01-06T09:53:50.264572Z"}},"outputs":[],"execution_count":null}]}