{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","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":65542,"databundleVersionId":7217587,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":false},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nfrom skimage import io, color, transform, feature\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.svm import LinearSVC\nfrom sklearn.metrics import (confusion_matrix, precision_score, recall_score,\n                             f1_score, roc_auc_score, classification_report)\nfrom sklearn.metrics import accuracy_score\n\n# Define the data directory and the categories (subfolder names)\ndata_dir = '/kaggle/input/congenital-heart-disease'\ncategories = ['ASD', 'Normal', 'PDA', 'VSD']\n\nfeatures = []\nlabels = []\n\n# Iterate over each category to load images, extract features, and assign labels\nfor label, category in enumerate(categories):\n    category_path = os.path.join(data_dir, category)\n    for file in os.listdir(category_path):\n        if file.lower().endswith('.jpg'):\n            image_path = os.path.join(category_path, file)\n            image = io.imread(image_path)\n            # Resize image to a fixed size (e.g., 128x128 pixels) for consistency\n            image = transform.resize(image, (128, 128))\n            # Convert to grayscale if needed\n            if image.ndim == 3:\n                image = color.rgb2gray(image)\n            # Extract HOG features\n            hog_features = feature.hog(image, \n                                       orientations=9, \n                                       pixels_per_cell=(8, 8),\n                                       cells_per_block=(2, 2),\n                                       block_norm='L2-Hys')\n            features.append(hog_features)\n            labels.append(label)\n\n# Convert lists to numpy arrays\nX = np.array(features)\ny = np.array(labels)\n\n# Split data into training and testing sets (80% train, 20% test)\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Train a Linear SVM classifier on the extracted features\nclf = LinearSVC(random_state=42, max_iter=10000)\nclf.fit(X_train, y_train)\n\n# Evaluate the classifier on the test set\ny_pred = clf.predict(X_test)\n\naccuracy = accuracy_score(y_test, y_pred)\nprint(\"Test Accuracy:\", accuracy)\n\n# Confusion Matrix\ncm = confusion_matrix(y_test, y_pred)\nprint(\"Confusion Matrix:\")\nprint(cm)\n\n# Classification Report (includes precision, recall, f1-score)\nprint(\"\\nClassification Report:\")\nprint(classification_report(y_test, y_pred, target_names=categories))\n\n# Compute macro-average precision, recall, and F1\nprecision = precision_score(y_test, y_pred, average='macro')\nrecall = recall_score(y_test, y_pred, average='macro')\nf1 = f1_score(y_test, y_pred, average='macro')\nprint(\"Macro Precision: {:.4f}\".format(precision))\nprint(\"Macro Recall:    {:.4f}\".format(recall))\nprint(\"Macro F1 Score:  {:.4f}\".format(f1))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-11T20:03:58.47449Z","iopub.execute_input":"2025-03-11T20:03:58.474882Z","iopub.status.idle":"2025-03-11T20:07:36.604528Z","shell.execute_reply.started":"2025-03-11T20:03:58.474852Z","shell.execute_reply":"2025-03-11T20:07:36.603581Z"}},"outputs":[],"execution_count":null}]}