{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":25954,"databundleVersionId":2091745,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport warnings\nimport numpy as np\nimport matplotlib.pyplot as plt\nwarnings.filterwarnings(action='ignore')\n\ntrain = pd.read_csv('../input/birdclef-2021/train_metadata.csv',)\ntrain.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-09-14T15:56:25.632326Z","iopub.execute_input":"2024-09-14T15:56:25.632712Z","iopub.status.idle":"2024-09-14T15:56:27.399923Z","shell.execute_reply.started":"2024-09-14T15:56:25.632677Z","shell.execute_reply":"2024-09-14T15:56:27.398628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values_counts = train['primary_label'].value_counts()\nvalues_counts","metadata":{"execution":{"iopub.status.busy":"2024-09-14T15:56:37.366731Z","iopub.execute_input":"2024-09-14T15:56:37.367135Z","iopub.status.idle":"2024-09-14T15:56:37.381347Z","shell.execute_reply.started":"2024-09-14T15:56:37.367101Z","shell.execute_reply":"2024-09-14T15:56:37.379930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_values_count = len(train['primary_label'].value_counts())\nprint(unique_values_count,'\\n')\ntop_5_labels = train['primary_label'].value_counts().head(10)\nprint(top_5_labels)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-14T15:56:39.605657Z","iopub.execute_input":"2024-09-14T15:56:39.606088Z","iopub.status.idle":"2024-09-14T15:56:39.625447Z","shell.execute_reply.started":"2024-09-14T15:56:39.606053Z","shell.execute_reply":"2024-09-14T15:56:39.624040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_5_labels = train['primary_label'].value_counts().index[5:10]\nprint(top_5_labels)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-14T15:56:42.422401Z","iopub.execute_input":"2024-09-14T15:56:42.422787Z","iopub.status.idle":"2024-09-14T15:56:42.433884Z","shell.execute_reply.started":"2024-09-14T15:56:42.422755Z","shell.execute_reply":"2024-09-14T15:56:42.432485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\ndirectory_names = []\n\nfor dirname, _, _ in os.walk('/kaggle/input/birdclef-2021/train_short_audio'):\n    # Check if the current directory is related to one of the top 5 bird labels\n    if any(label in dirname for label in top_5_labels):\n        directory_names.append(dirname)\n\n# Print the stored directory names\nprint(\"Directory names related to top 5 bird labels:\")\nfor directory in directory_names:\n    print(directory)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-14T15:57:11.892644Z","iopub.execute_input":"2024-09-14T15:57:11.893060Z","iopub.status.idle":"2024-09-14T15:57:37.370746Z","shell.execute_reply.started":"2024-09-14T15:57:11.893027Z","shell.execute_reply":"2024-09-14T15:57:37.369613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bird_label_files = {}\n\nfor label_dir in directory_names:\n    label = label_dir.split('/')[-1]  # Extract label from directory path\n    bird_label_files[label] = []  # Initialize list for filenames\n    for _, _, filenames in os.walk(label_dir):\n        for filename in filenames:\n            bird_label_files[label].append(os.path.join(label_dir, filename))\n\n# Print the dictionary containing filenames for each bird label\n# Print the number of files under each label\nfor label, files in bird_label_files.items():\n    print(f\"Number of files under {label}: {len(files)}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-14T16:05:28.655935Z","iopub.execute_input":"2024-09-14T16:05:28.657027Z","iopub.status.idle":"2024-09-14T16:05:28.673866Z","shell.execute_reply.started":"2024-09-14T16:05:28.656987Z","shell.execute_reply":"2024-09-14T16:05:28.672717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n# Directory path\ndirectory = \"/kaggle/input/birdclef-2021/train_short_audio/\"\n\n# Get list of all subdirectories\nsubdirectories = [subdir for subdir in os.listdir(directory) if os.path.isdir(os.path.join(directory, subdir))]\n\nlen(subdirectories)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T18:28:04.377992Z","iopub.execute_input":"2024-09-12T18:28:04.378369Z","iopub.status.idle":"2024-09-12T18:28:04.394115Z","shell.execute_reply.started":"2024-09-12T18:28:04.378338Z","shell.execute_reply":"2024-09-12T18:28:04.392422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n# Directory path\ndirectory = \"/kaggle/input/birdclef-2021/train_short_audio/\"\n\n# Get list of all subdirectories\nsubdirectories = [subdir for subdir in os.listdir(directory) if os.path.isdir(os.path.join(directory, subdir))]\n\n# Remove elements in bird_label_files.keys() from the subdirectories list\nfor label in bird_label_files.keys():\n    if label in subdirectories:\n        subdirectories.remove(label)\n\n# Print the remaining subdirectories\nprint(len(subdirectories))\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T18:28:04.395780Z","iopub.execute_input":"2024-09-12T18:28:04.397316Z","iopub.status.idle":"2024-09-12T18:28:04.411462Z","shell.execute_reply.started":"2024-09-12T18:28:04.397273Z","shell.execute_reply":"2024-09-12T18:28:04.410115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom tqdm import tqdm\n\nbase_dir = '/kaggle/input/birdclef-2021/train_short_audio/'\n\nfor bird_label in bird_label_files.keys():\n    label_dir = os.path.join(base_dir, bird_label)\n    zip_file_name = bird_label + \".zip\"\n    \n    # Count the number of files in the directory\n    num_files = sum([len(files) for _, _, files in os.walk(label_dir)])\n    \n    # Create a zip file with a progress bar\n    with tqdm(total=num_files, desc=f\"Zipping {bird_label} files\") as pbar:\n        !zip -r {zip_file_name} {label_dir} --quiet\n        pbar.update(num_files)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T18:28:04.432337Z","iopub.execute_input":"2024-09-12T18:28:04.432863Z","iopub.status.idle":"2024-09-12T18:29:55.549486Z","shell.execute_reply.started":"2024-09-12T18:28:04.432816Z","shell.execute_reply":"2024-09-12T18:29:55.547835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}