{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.703285Z","iopub.execute_input":"2026-08-27T14:37:54.703716Z","iopub.status.idle":"2026-08-27T14:37:54.707863Z","shell.execute_reply.started":"2026-08-27T14:37:54.703654Z","shell.execute_reply":"2026-08-27T14:37:54.706986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data=pd.read_csv(\"/kaggle/input/competitions/rsna-knee-abnormality-detection/train.csv\")\ndata.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.709241Z","iopub.execute_input":"2026-08-27T14:37:54.709545Z","iopub.status.idle":"2026-08-27T14:37:54.811996Z","shell.execute_reply.started":"2026-08-27T14:37:54.709522Z","shell.execute_reply":"2026-08-27T14:37:54.811391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"study_id = data[\"StudyInstanceUID\"].iloc[0]\nprint(study_id)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.812893Z","iopub.execute_input":"2026-08-27T14:37:54.813202Z","iopub.status.idle":"2026-08-27T14:37:54.817808Z","shell.execute_reply.started":"2026-08-27T14:37:54.81317Z","shell.execute_reply":"2026-08-27T14:37:54.816847Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.819595Z","iopub.execute_input":"2026-08-27T14:37:54.81992Z","iopub.status.idle":"2026-08-27T14:37:54.837013Z","shell.execute_reply.started":"2026-08-27T14:37:54.8199Z","shell.execute_reply":"2026-08-27T14:37:54.836278Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data2=pd.read_csv(\"/kaggle/input/competitions/rsna-knee-abnormality-detection/train_series.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.838046Z","iopub.execute_input":"2026-08-27T14:37:54.838303Z","iopub.status.idle":"2026-08-27T14:37:54.894723Z","shell.execute_reply.started":"2026-08-27T14:37:54.83828Z","shell.execute_reply":"2026-08-27T14:37:54.893765Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data2.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.895775Z","iopub.execute_input":"2026-08-27T14:37:54.896225Z","iopub.status.idle":"2026-08-27T14:37:54.904997Z","shell.execute_reply.started":"2026-08-27T14:37:54.89619Z","shell.execute_reply":"2026-08-27T14:37:54.904203Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data2.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.905916Z","iopub.execute_input":"2026-08-27T14:37:54.90617Z","iopub.status.idle":"2026-08-27T14:37:54.927833Z","shell.execute_reply.started":"2026-08-27T14:37:54.906149Z","shell.execute_reply":"2026-08-27T14:37:54.92707Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data2[data2[\"StudyInstanceUID\"] == study_id]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.928626Z","iopub.execute_input":"2026-08-27T14:37:54.928857Z","iopub.status.idle":"2026-08-27T14:37:54.947952Z","shell.execute_reply.started":"2026-08-27T14:37:54.928837Z","shell.execute_reply":"2026-08-27T14:37:54.947349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"series_id = data2[\n    data2[\"StudyInstanceUID\"] == study_id\n][\"SeriesInstanceUID\"].iloc[0]\n\nprint(series_id)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.948956Z","iopub.execute_input":"2026-08-27T14:37:54.949213Z","iopub.status.idle":"2026-08-27T14:37:54.96443Z","shell.execute_reply.started":"2026-08-27T14:37:54.949193Z","shell.execute_reply":"2026-08-27T14:37:54.963546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_series_path=\"/kaggle/input/competitions/rsna-knee-abnormality-detection/train_series\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.967411Z","iopub.execute_input":"2026-08-27T14:37:54.967618Z","iopub.status.idle":"2026-08-27T14:37:54.978096Z","shell.execute_reply.started":"2026-08-27T14:37:54.967598Z","shell.execute_reply":"2026-08-27T14:37:54.977206Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.979087Z","iopub.execute_input":"2026-08-27T14:37:54.979503Z","iopub.status.idle":"2026-08-27T14:37:54.992294Z","shell.execute_reply.started":"2026-08-27T14:37:54.979469Z","shell.execute_reply":"2026-08-27T14:37:54.991584Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"series_path = os.path.join(\n    train_series_path,\n    study_id,\n    series_id\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:54.993449Z","iopub.execute_input":"2026-08-27T14:37:54.993767Z","iopub.status.idle":"2026-08-27T14:37:55.005916Z","shell.execute_reply.started":"2026-08-27T14:37:54.993745Z","shell.execute_reply":"2026-08-27T14:37:55.005382Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pydicom\n\nfiles = os.listdir(series_path)\n\nslices = []\n\nfor file in files:\n    file_path = os.path.join(series_path, file)\n    ds = pydicom.dcmread(file_path)\n\n    slices.append(ds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:55.006817Z","iopub.execute_input":"2026-08-27T14:37:55.007131Z","iopub.status.idle":"2026-08-27T14:37:55.054019Z","shell.execute_reply.started":"2026-08-27T14:37:55.00711Z","shell.execute_reply":"2026-08-27T14:37:55.053119Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(slices)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:55.055032Z","iopub.execute_input":"2026-08-27T14:37:55.055576Z","iopub.status.idle":"2026-08-27T14:37:55.060132Z","shell.execute_reply.started":"2026-08-27T14:37:55.055553Z","shell.execute_reply":"2026-08-27T14:37:55.05958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"slices.sort(key=lambda x: x.InstanceNumber)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:55.061286Z","iopub.execute_input":"2026-08-27T14:37:55.061701Z","iopub.status.idle":"2026-08-27T14:37:55.072777Z","shell.execute_reply.started":"2026-08-27T14:37:55.061675Z","shell.execute_reply":"2026-08-27T14:37:55.0722Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfor ds in slices[:]:\n    plt.figure(figsize=(5, 5))\n    plt.imshow(ds.pixel_array, cmap=\"gray\")\n    plt.axis(\"off\")\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:55.073735Z","iopub.execute_input":"2026-08-27T14:37:55.074041Z","iopub.status.idle":"2026-08-27T14:37:57.188814Z","shell.execute_reply.started":"2026-08-27T14:37:55.07401Z","shell.execute_reply":"2026-08-27T14:37:57.188Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_columns = [\n    \"ACL\",\n    \"MCL\",\n    \"Medial Meniscus\",\n    \"Lateral Meniscus\",\n    \"Medial OA\",\n    \"Lateral OA\",\n    \"PF OA\",\n    \"Effusion\",\n    \"Synovitis\",\n    \"Baker's\",\n    \"Contusion\",\n    \"Fracture\"\n]\n\nlabeled_data = data.dropna(subset=label_columns)\n\nprint(labeled_data.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:57.189881Z","iopub.execute_input":"2026-08-27T14:37:57.190214Z","iopub.status.idle":"2026-08-27T14:37:57.197569Z","shell.execute_reply.started":"2026-08-27T14:37:57.19019Z","shell.execute_reply":"2026-08-27T14:37:57.196735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_data, valid_data = train_test_split(\n    labeled_data,\n    test_size=0.2,\n    random_state=42\n)\n\nprint(\"Training studies:\", len(train_data))\nprint(\"Validation studies:\", len(valid_data))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:57.198619Z","iopub.execute_input":"2026-08-27T14:37:57.198977Z","iopub.status.idle":"2026-08-27T14:37:57.219961Z","shell.execute_reply.started":"2026-08-27T14:37:57.198955Z","shell.execute_reply":"2026-08-27T14:37:57.219379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\n\ncnn = keras.Sequential([\n    \n    layers.Conv2D(\n        32, 3,\n        activation=\"relu\",\n        padding=\"same\",\n        input_shape=(128, 128, 1)\n    ),\n    layers.MaxPool2D(),\n\n    layers.Conv2D(\n        64, 3,\n        activation=\"relu\",\n        padding=\"same\"\n    ),\n    layers.MaxPool2D(),\n\n    layers.Conv2D(\n        128, 3,\n        activation=\"relu\",\n        padding=\"same\"\n    ),\n    layers.MaxPool2D(),\n\n    layers.GlobalAveragePooling2D()\n])\n\ncnn.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:57.221057Z","iopub.execute_input":"2026-08-27T14:37:57.221631Z","iopub.status.idle":"2026-08-27T14:37:57.273961Z","shell.execute_reply.started":"2026-08-27T14:37:57.221607Z","shell.execute_reply":"2026-08-27T14:37:57.273271Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"study_input = keras.Input(\n    shape=(None, 128, 128, 1)\n)\n\nslice_features = layers.TimeDistributed(cnn)(study_input)\n\nstudy_features = layers.GlobalAveragePooling1D()(slice_features)\n\nx = layers.Dense(128, activation=\"relu\")(study_features)\nx = layers.Dropout(0.3)(x)\n\noutput = layers.Dense(\n    12,\n    activation=\"sigmoid\"\n)(x)\n\nmodel = keras.Model(\n    inputs=study_input,\n    outputs=output\n)\n\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:57.274987Z","iopub.execute_input":"2026-08-27T14:37:57.275276Z","iopub.status.idle":"2026-08-27T14:37:57.312153Z","shell.execute_reply.started":"2026-08-27T14:37:57.275256Z","shell.execute_reply":"2026-08-27T14:37:57.311386Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    optimizer=keras.optimizers.Adam(learning_rate=0.001),\n    loss=\"binary_crossentropy\",\n    metrics=[\n        keras.metrics.BinaryAccuracy(name=\"accuracy\"),\n        keras.metrics.AUC(name=\"roc_auc\", multi_label=True,num_labels=12)\n    ]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:57.313076Z","iopub.execute_input":"2026-08-27T14:37:57.313376Z","iopub.status.idle":"2026-08-27T14:37:57.325434Z","shell.execute_reply.started":"2026-08-27T14:37:57.313341Z","shell.execute_reply":"2026-08-27T14:37:57.324844Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pydicom\nimport cv2\nimport numpy as np\n\nIMG_SIZE = 128\n\ndef load_study(study_id):\n\n    study_images = []\n\n    # Find all series belonging to this study\n    study_series = data2[\n        data2[\"StudyInstanceUID\"] == study_id\n    ]\n\n    # Go through every series\n    for series_id in study_series[\"SeriesInstanceUID\"]:\n\n        series_path = os.path.join(\n            train_series_path,\n            study_id,\n            series_id\n        )\n\n        slices = []\n\n        # Load every DICOM file\n        for filename in os.listdir(series_path):\n\n            file_path = os.path.join(\n                series_path,\n                filename\n            )\n\n            ds = pydicom.dcmread(file_path)\n            slices.append(ds)\n\n        # Put slices in their correct order\n        slices.sort(key=lambda x: x.InstanceNumber)\n\n        # Convert every slice to pixels\n        for ds in slices:\n\n            image = ds.pixel_array.astype(\"float32\")\n\n            # Normalize\n            image -= image.min()\n\n            if image.max() > 0:\n                image /= image.max()\n\n            # Resize\n            image = cv2.resize(\n                image,\n                (IMG_SIZE, IMG_SIZE)\n            )\n\n            # Add grayscale channel\n            image = image[..., np.newaxis]\n\n            study_images.append(image)\n\n    return np.array(study_images, dtype=\"float32\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:57.326276Z","iopub.execute_input":"2026-08-27T14:37:57.326597Z","iopub.status.idle":"2026-08-27T14:37:57.334905Z","shell.execute_reply.started":"2026-08-27T14:37:57.326565Z","shell.execute_reply":"2026-08-27T14:37:57.334121Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def select_slices(study_images, num_slices=150):\n\n    indices = np.linspace(\n        0,\n        len(study_images) - 1,\n        num_slices\n    ).astype(int)\n\n    return study_images[indices]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:57.335792Z","iopub.execute_input":"2026-08-27T14:37:57.336057Z","iopub.status.idle":"2026-08-27T14:37:57.352275Z","shell.execute_reply.started":"2026-08-27T14:37:57.336024Z","shell.execute_reply":"2026-08-27T14:37:57.351314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = [\n    load_study(study_id)\n    for study_id in train_data[\"StudyInstanceUID\"]\n]\n\nX_valid = [\n    load_study(study_id)\n    for study_id in valid_data[\"StudyInstanceUID\"]\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:37:57.353813Z","iopub.execute_input":"2026-08-27T14:37:57.354395Z","iopub.status.idle":"2026-08-27T14:38:10.336934Z","shell.execute_reply.started":"2026-08-27T14:37:57.354365Z","shell.execute_reply":"2026-08-27T14:38:10.335064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_fixed = [\n    select_slices(study)\n    for study in X_train\n]\n\nX_valid_fixed = [\n    select_slices(study)\n    for study in X_valid\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.337914Z","iopub.status.idle":"2026-08-27T14:38:10.338235Z","shell.execute_reply.started":"2026-08-27T14:38:10.33811Z","shell.execute_reply":"2026-08-27T14:38:10.338127Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(X_train_fixed[0].shape)\nprint(X_valid_fixed[0].shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.339573Z","iopub.status.idle":"2026-08-27T14:38:10.339925Z","shell.execute_reply.started":"2026-08-27T14:38:10.339798Z","shell.execute_reply":"2026-08-27T14:38:10.33982Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_fixed = np.array(X_train_fixed, dtype=\"float32\")\nX_valid_fixed = np.array(X_valid_fixed, dtype=\"float32\")\n\nprint(X_train_fixed.shape)\nprint(X_valid_fixed.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.34108Z","iopub.status.idle":"2026-08-27T14:38:10.341308Z","shell.execute_reply.started":"2026-08-27T14:38:10.341198Z","shell.execute_reply":"2026-08-27T14:38:10.341212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_train = train_data[label_columns].values.astype(\"float32\")\n\nY_valid = valid_data[label_columns].values.astype(\"float32\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.342592Z","iopub.status.idle":"2026-08-27T14:38:10.342999Z","shell.execute_reply.started":"2026-08-27T14:38:10.342816Z","shell.execute_reply":"2026-08-27T14:38:10.342838Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\nearly_stopping = EarlyStopping(\n    monitor=\"val_roc_auc\",\n    mode=\"max\",\n    patience=5,\n    restore_best_weights=True\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.344462Z","iopub.status.idle":"2026-08-27T14:38:10.344793Z","shell.execute_reply.started":"2026-08-27T14:38:10.344605Z","shell.execute_reply":"2026-08-27T14:38:10.344624Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train_fixed,\n    Y_train,\n    validation_data=(X_valid_fixed, Y_valid),\n    epochs=40,\n    batch_size=4,\n    callbacks=[early_stopping]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.346151Z","iopub.status.idle":"2026-08-27T14:38:10.34649Z","shell.execute_reply.started":"2026-08-27T14:38:10.346324Z","shell.execute_reply":"2026-08-27T14:38:10.346367Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df=pd.DataFrame(history.history)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.347545Z","iopub.status.idle":"2026-08-27T14:38:10.347761Z","shell.execute_reply.started":"2026-08-27T14:38:10.347654Z","shell.execute_reply":"2026-08-27T14:38:10.347667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df[\"accuracy\"].plot()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.348925Z","iopub.status.idle":"2026-08-27T14:38:10.349195Z","shell.execute_reply.started":"2026-08-27T14:38:10.349081Z","shell.execute_reply":"2026-08-27T14:38:10.349096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df[\"val_accuracy\"].plot()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.350198Z","iopub.status.idle":"2026-08-27T14:38:10.350792Z","shell.execute_reply.started":"2026-08-27T14:38:10.350526Z","shell.execute_reply":"2026-08-27T14:38:10.350584Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df[\"loss\"].plot()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.351883Z","iopub.status.idle":"2026-08-27T14:38:10.352147Z","shell.execute_reply.started":"2026-08-27T14:38:10.352031Z","shell.execute_reply":"2026-08-27T14:38:10.352046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df[\"val_loss\"].plot()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.35366Z","iopub.status.idle":"2026-08-27T14:38:10.353912Z","shell.execute_reply.started":"2026-08-27T14:38:10.353787Z","shell.execute_reply":"2026-08-27T14:38:10.3538Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df[\"accuracy\"].max ()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.355072Z","iopub.status.idle":"2026-08-27T14:38:10.355434Z","shell.execute_reply.started":"2026-08-27T14:38:10.355244Z","shell.execute_reply":"2026-08-27T14:38:10.355276Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df[\"val_accuracy\"].max()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.356675Z","iopub.status.idle":"2026-08-27T14:38:10.356962Z","shell.execute_reply.started":"2026-08-27T14:38:10.356834Z","shell.execute_reply":"2026-08-27T14:38:10.356853Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df[\"loss\"].min()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.357882Z","iopub.status.idle":"2026-08-27T14:38:10.358179Z","shell.execute_reply.started":"2026-08-27T14:38:10.358048Z","shell.execute_reply":"2026-08-27T14:38:10.358067Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df[\"val_loss\"].min()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.359272Z","iopub.status.idle":"2026-08-27T14:38:10.359559Z","shell.execute_reply.started":"2026-08-27T14:38:10.359449Z","shell.execute_reply":"2026-08-27T14:38:10.359464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df[\"roc_auc\"].max()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.361616Z","iopub.status.idle":"2026-08-27T14:38:10.361979Z","shell.execute_reply.started":"2026-08-27T14:38:10.361858Z","shell.execute_reply":"2026-08-27T14:38:10.361873Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_df[\"val_roc_auc\"].max()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.363112Z","iopub.status.idle":"2026-08-27T14:38:10.363439Z","shell.execute_reply.started":"2026-08-27T14:38:10.363255Z","shell.execute_reply":"2026-08-27T14:38:10.363269Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_data[label_columns].sum())\nprint()\nprint(valid_data[label_columns].sum())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.364892Z","iopub.status.idle":"2026-08-27T14:38:10.365224Z","shell.execute_reply.started":"2026-08-27T14:38:10.365054Z","shell.execute_reply":"2026-08-27T14:38:10.365075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\nimport numpy as np\n\nY_pred = model.predict(X_valid_fixed)\n\nfor i, label in enumerate(label_columns):\n    \n    y_true = Y_valid[:, i]\n    y_score = Y_pred[:, i]\n    \n    if len(np.unique(y_true)) < 2:\n        print(f\"{label}: Cannot calculate AUC (only one class)\")\n    else:\n        auc = roc_auc_score(y_true, y_score)\n        print(f\"{label}: {auc:.3f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.366213Z","iopub.status.idle":"2026-08-27T14:38:10.3666Z","shell.execute_reply.started":"2026-08-27T14:38:10.366379Z","shell.execute_reply":"2026-08-27T14:38:10.366404Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\nimport numpy as np\n\nauc_scores = []\n\nfor i, label in enumerate(label_columns):\n    y_true = Y_valid[:, i]\n    y_score = Y_pred[:, i]\n\n    if len(np.unique(y_true)) == 2:\n        auc = roc_auc_score(y_true, y_score)\n        auc_scores.append(auc)\n\nprint(\"Macro ROC-AUC:\", np.mean(auc_scores))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-27T14:38:10.367615Z","iopub.status.idle":"2026-08-27T14:38:10.367976Z","shell.execute_reply.started":"2026-08-27T14:38:10.367835Z","shell.execute_reply":"2026-08-27T14:38:10.367859Z"}},"outputs":[],"execution_count":null}]}