{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3b1658fa-6a10-41f5-8d72-3054ea872efb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import numpy as np\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7e7961d7-da8c-4626-8d76-d6254df214d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the train and test data using pickle\n",
    "with open('train_data.pkl', 'rb') as f:\n",
    "    training_data = pickle.load(f)\n",
    "with open('test_data.pkl', 'rb') as f:\n",
    "    test_data = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4ca46249-9fb1-416c-8c75-59e1b18e9286",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (353298, 2) + inhomogeneous part.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(np\u001b[38;5;241m.\u001b[39many(\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43misnan\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtraining_data\u001b[49m\u001b[43m)\u001b[49m))\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28mprint\u001b[39m(np\u001b[38;5;241m.\u001b[39many(np\u001b[38;5;241m.\u001b[39misinf(training_data)))\n",
      "\u001b[0;31mValueError\u001b[0m: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (353298, 2) + inhomogeneous part."
     ]
    }
   ],
   "source": [
    "print(np.any(np.isnan(training_data)))\n",
    "print(np.any(np.isinf(training_data)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d32033e5-1b12-497c-ace8-2236b25e7a67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.4640, 0.2546, 0.2814],\n",
      "        [0.2864, 0.4272, 0.2864],\n",
      "        [0.3559, 0.3220, 0.3220]])\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "def calculate_accuracy(outputs, labels):\n",
    "    # Convert outputs to probabilities using softmax\n",
    "    probs = torch.nn.functional.softmax(outputs, dim=1)\n",
    "\n",
    "    print(probs)\n",
    "    \n",
    "    indices_0 = torch.nonzero(probs[:, 0] > 0.5).squeeze()\n",
    "    indices_1 = torch.nonzero(probs[:, 1] > 0.5).squeeze()\n",
    "\n",
    "    # Check if both indices_0 and indices_1 are empty\n",
    "    if len(indices_0) == 0 and len(indices_1) == 0:\n",
    "        return 100\n",
    "\n",
    "    correct_count = 0\n",
    "\n",
    "    print(indices_0)\n",
    "    print(indices_1)\n",
    "    \n",
    "    # Check if predictions at indices_0 are correct\n",
    "    for index in indices_0:\n",
    "        if labels[index] == 0:\n",
    "            correct_count += 1\n",
    "\n",
    "    # Check if predictions at indices_1 are correct\n",
    "    for index in indices_1:\n",
    "        if labels[index] == 1:\n",
    "            correct_count += 1\n",
    "\n",
    "    total_indices = len(indices_0) + len(indices_1)\n",
    "    \n",
    "    return 100 * correct_count / total_indices\n",
    "\n",
    "# Test\n",
    "outputs = torch.tensor([[0.7, 0.1, 0.2], [0.2, 0.6, 0.2], [0.4, 0.3, 0.3]])\n",
    "labels = torch.tensor([1, 1, 2])\n",
    "accuracy = calculate_accuracy(outputs, labels)\n",
    "print(accuracy)  # Expected Output: 100.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c29bb55-0213-4b66-8ff3-f71f8d8d01da",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "prac",
   "language": "python",
   "name": "prac"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
