{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# About this Notebook\n\nIn this notebook i will show how to do ensembling in a better way and by submitting this kernel, you will get LB as high as 0.66\n\n\n**<span style=\"color:Red\">Please upvote this kernel if you like it . It motivates me to produce more quality content :)**","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch.nn.functional as F\nimport os\n\n# Any results you write to the current directory are saved as output.\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset,DataLoader\nfrom torchvision import transforms,models\nfrom tqdm import tqdm_notebook as tqdm\nimport math\nimport torch.utils.model_zoo as model_zoo\nimport cv2\nfrom sklearn.metrics import cohen_kappa_score\n\nimport openslide\n# Option 2: Load images using skimage (requires that tifffile is installed)\nimport skimage.io\nimport random\nfrom sklearn.metrics import cohen_kappa_score\nimport albumentations\n# General packages\n\n# import PIL\nfrom PIL import Image\n\n# from IPython.display import Image, display","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-18T22:07:46.277364Z","iopub.execute_input":"2023-05-18T22:07:46.278084Z","iopub.status.idle":"2023-05-18T22:07:46.285956Z","shell.execute_reply.started":"2023-05-18T22:07:46.27805Z","shell.execute_reply":"2023-05-18T22:07:46.285043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* For EDA and visualizations, Please visit https://www.kaggle.com/rohitsingh9990/panda-eda-better-visualization/comments\n\n* For Simple inference using Resnext50 please visit https://www.kaggle.com/rohitsingh9990/panda-resnext-inference","metadata":{}},{"cell_type":"markdown","source":"## Config","metadata":{}},{"cell_type":"code","source":"class config:\n    device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n    IMG_WIDTH = 256\n    IMG_HEIGHT = 256\n    TEST_BATCH_SIZE = 16\n    CLASSES = 6\n    # In order to check weather your submission will work or not on test data simply set DEBUG = True\n    DEBUG = True","metadata":{"execution":{"iopub.status.busy":"2023-05-18T20:32:41.007269Z","iopub.execute_input":"2023-05-18T20:32:41.008006Z","iopub.status.idle":"2023-05-18T20:32:41.039546Z","shell.execute_reply.started":"2023-05-18T20:32:41.00797Z","shell.execute_reply":"2023-05-18T20:32:41.038203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading Data","metadata":{}},{"cell_type":"code","source":"BASE_PATH = '../input/prostate-cancer-grade-assessment'\n\ndata_dir = f'{BASE_PATH}/test_images'\ntest = pd.read_csv(f'{BASE_PATH}/test.csv')\nsubmission = pd.read_csv(f'{BASE_PATH}/sample_submission.csv')\n\nif config.DEBUG:\n    data_dir = f'{BASE_PATH}/train_images'\n    test = pd.read_csv(f'{BASE_PATH}/train.csv').head(200)","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2023-05-18T20:32:41.042557Z","iopub.execute_input":"2023-05-18T20:32:41.043331Z","iopub.status.idle":"2023-05-18T20:32:41.098162Z","shell.execute_reply.started":"2023-05-18T20:32:41.04328Z","shell.execute_reply":"2023-05-18T20:32:41.097321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-18T20:32:41.099509Z","iopub.execute_input":"2023-05-18T20:32:41.09992Z","iopub.status.idle":"2023-05-18T20:32:41.117067Z","shell.execute_reply.started":"2023-05-18T20:32:41.099886Z","shell.execute_reply":"2023-05-18T20:32:41.116102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_torch(seed=42):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n\nseed_torch(seed=42)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T20:32:41.11864Z","iopub.execute_input":"2023-05-18T20:32:41.119004Z","iopub.status.idle":"2023-05-18T20:32:41.131004Z","shell.execute_reply.started":"2023-05-18T20:32:41.118968Z","shell.execute_reply":"2023-05-18T20:32:41.129901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ResNext Model","metadata":{}},{"cell_type":"code","source":"from collections import OrderedDict\nimport math\n\n\nclass SEModule(nn.Module):\n\n    def __init__(self, channels, reduction):\n        super(SEModule, self).__init__()\n        self.avg_pool = nn.AdaptiveAvgPool2d(1)\n        self.fc1 = nn.Conv2d(channels, channels // reduction, kernel_size=1,\n                             padding=0)\n        self.relu = nn.ReLU(inplace=True)\n        self.fc2 = nn.Conv2d(channels // reduction, channels, kernel_size=1,\n                             padding=0)\n        self.sigmoid = nn.Sigmoid()\n\n    def forward(self, x):\n        module_input = x\n        x = self.avg_pool(x)\n        x = self.fc1(x)\n        x = self.relu(x)\n        x = self.fc2(x)\n        x = self.sigmoid(x)\n        return module_input * x\n\n\nclass Bottleneck(nn.Module):\n    \"\"\"\n    Base class for bottlenecks that implements `forward()` method.\n    \"\"\"\n    def forward(self, x):\n        residual = x\n\n        out = self.conv1(x)\n        out = self.bn1(out)\n        out = self.relu(out)\n\n        out = self.conv2(out)\n        out = self.bn2(out)\n        out = self.relu(out)\n\n        out = self.conv3(out)\n        out = self.bn3(out)\n\n        if self.downsample is not None:\n            residual = self.downsample(x)\n\n        out = self.se_module(out) + residual\n        out = self.relu(out)\n\n        return out\n\n\nclass SEBottleneck(Bottleneck):\n    \"\"\"\n    Bottleneck for SENet154.\n    \"\"\"\n    expansion = 4\n\n    def __init__(self, inplanes, planes, groups, reduction, stride=1,\n                 downsample=None):\n        super(SEBottleneck, self).__init__()\n        self.conv1 = nn.Conv2d(inplanes, planes * 2, kernel_size=1, bias=False)\n        self.bn1 = nn.BatchNorm2d(planes * 2)\n        self.conv2 = nn.Conv2d(planes * 2, planes * 4, kernel_size=3,\n                               stride=stride, padding=1, groups=groups,\n                               bias=False)\n        self.bn2 = nn.BatchNorm2d(planes * 4)\n        self.conv3 = nn.Conv2d(planes * 4, planes * 4, kernel_size=1,\n                               bias=False)\n        self.bn3 = nn.BatchNorm2d(planes * 4)\n        self.relu = nn.ReLU(inplace=True)\n        self.se_module = SEModule(planes * 4, reduction=reduction)\n        self.downsample = downsample\n        self.stride = stride\n\n\nclass SEResNetBottleneck(Bottleneck):\n    \"\"\"\n    ResNet bottleneck with a Squeeze-and-Excitation module. It follows Caffe\n    implementation and uses `stride=stride` in `conv1` and not in `conv2`\n    (the latter is used in the torchvision implementation of ResNet).\n    \"\"\"\n    expansion = 4\n\n    def __init__(self, inplanes, planes, groups, reduction, stride=1,\n                 downsample=None):\n        super(SEResNetBottleneck, self).__init__()\n        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False,\n                               stride=stride)\n        self.bn1 = nn.BatchNorm2d(planes)\n        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1,\n                               groups=groups, bias=False)\n        self.bn2 = nn.BatchNorm2d(planes)\n        self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)\n        self.bn3 = nn.BatchNorm2d(planes * 4)\n        self.relu = nn.ReLU(inplace=True)\n        self.se_module = SEModule(planes * 4, reduction=reduction)\n        self.downsample = downsample\n        self.stride = stride\n\n\nclass SEResNeXtBottleneck(Bottleneck):\n    \"\"\"\n    ResNeXt bottleneck type C with a Squeeze-and-Excitation module.\n    \"\"\"\n    expansion = 4\n\n    def __init__(self, inplanes, planes, groups, reduction, stride=1,\n                 downsample=None, base_width=4):\n        super(SEResNeXtBottleneck, self).__init__()\n        width = math.floor(planes * (base_width / 64)) * groups\n        self.conv1 = nn.Conv2d(inplanes, width, kernel_size=1, bias=False,\n                               stride=1)\n        self.bn1 = nn.BatchNorm2d(width)\n        self.conv2 = nn.Conv2d(width, width, kernel_size=3, stride=stride,\n                               padding=1, groups=groups, bias=False)\n        self.bn2 = nn.BatchNorm2d(width)\n        self.conv3 = nn.Conv2d(width, planes * 4, kernel_size=1, bias=False)\n        self.bn3 = nn.BatchNorm2d(planes * 4)\n        self.relu = nn.ReLU(inplace=True)\n        self.se_module = SEModule(planes * 4, reduction=reduction)\n        self.downsample = downsample\n        self.stride = stride\n\n\nclass SENet(nn.Module):\n\n    def __init__(self, block, layers, groups, reduction, dropout_p=0.2,\n                 inplanes=128, input_3x3=True, downsample_kernel_size=3,\n                 downsample_padding=1, num_classes=1000):\n        super(SENet, self).__init__()\n        self.inplanes = inplanes\n        if input_3x3:\n            layer0_modules = [\n                ('conv1', nn.Conv2d(3, 64, 3, stride=2, padding=1,\n                                    bias=False)),\n                ('bn1', nn.BatchNorm2d(64)),\n                ('relu1', nn.ReLU(inplace=True)),\n                ('conv2', nn.Conv2d(64, 64, 3, stride=1, padding=1,\n                                    bias=False)),\n                ('bn2', nn.BatchNorm2d(64)),\n                ('relu2', nn.ReLU(inplace=True)),\n                ('conv3', nn.Conv2d(64, inplanes, 3, stride=1, padding=1,\n                                    bias=False)),\n                ('bn3', nn.BatchNorm2d(inplanes)),\n                ('relu3', nn.ReLU(inplace=True)),\n            ]\n        else:\n            layer0_modules = [\n                ('conv1', nn.Conv2d(3, inplanes, kernel_size=7, stride=2,\n                                    padding=3, bias=False)),\n                ('bn1', nn.BatchNorm2d(inplanes)),\n                ('relu1', nn.ReLU(inplace=True)),\n            ]\n        # To preserve compatibility with Caffe weights `ceil_mode=True`\n        # is used instead of `padding=1`.\n        layer0_modules.append(('pool', nn.MaxPool2d(3, stride=2,\n                                                    ceil_mode=True)))\n        self.layer0 = nn.Sequential(OrderedDict(layer0_modules))\n        self.layer1 = self._make_layer(\n            block,\n            planes=64,\n            blocks=layers[0],\n            groups=groups,\n            reduction=reduction,\n            downsample_kernel_size=1,\n            downsample_padding=0\n        )\n        self.layer2 = self._make_layer(\n            block,\n            planes=128,\n            blocks=layers[1],\n            stride=2,\n            groups=groups,\n            reduction=reduction,\n            downsample_kernel_size=downsample_kernel_size,\n            downsample_padding=downsample_padding\n        )\n        self.layer3 = self._make_layer(\n            block,\n            planes=256,\n            blocks=layers[2],\n            stride=2,\n            groups=groups,\n            reduction=reduction,\n            downsample_kernel_size=downsample_kernel_size,\n            downsample_padding=downsample_padding\n        )\n        self.layer4 = self._make_layer(\n            block,\n            planes=512,\n            blocks=layers[3],\n            stride=2,\n            groups=groups,\n            reduction=reduction,\n            downsample_kernel_size=downsample_kernel_size,\n            downsample_padding=downsample_padding\n        )\n        self.avg_pool = nn.AvgPool2d(7, stride=1)\n        self.dropout = nn.Dropout(dropout_p) if dropout_p is not None else None\n        self.last_linear = nn.Linear(512 * block.expansion, num_classes)\n\n    def _make_layer(self, block, planes, blocks, groups, reduction, stride=1,\n                    downsample_kernel_size=1, downsample_padding=0):\n        downsample = None\n        if stride != 1 or self.inplanes != planes * block.expansion:\n            downsample = nn.Sequential(\n                nn.Conv2d(self.inplanes, planes * block.expansion,\n                          kernel_size=downsample_kernel_size, stride=stride,\n                          padding=downsample_padding, bias=False),\n                nn.BatchNorm2d(planes * block.expansion),\n            )\n\n        layers = []\n        layers.append(block(self.inplanes, planes, groups, reduction, stride,\n                            downsample))\n        self.inplanes = planes * block.expansion\n        for i in range(1, blocks):\n            layers.append(block(self.inplanes, planes, groups, reduction))\n\n        return nn.Sequential(*layers)\n\n    def features(self, x):\n        x = self.layer0(x)\n        x = self.layer1(x)\n        x = self.layer2(x)\n        x = self.layer3(x)\n        x = self.layer4(x)\n        return x\n\n    def logits(self, x):\n        x = self.avg_pool(x)\n        if self.dropout is not None:\n            x = self.dropout(x)\n        x = x.view(x.size(0), -1)\n        x = self.last_linear(x)\n        return x\n\n    def forward(self, x):\n        x = self.features(x)\n        x = self.logits(x)\n        return x\n\n\ndef initialize_pretrained_model(model, num_classes, settings):\n    assert num_classes == settings['num_classes'], \\\n        'num_classes should be {}, but is {}'.format(\n            settings['num_classes'], num_classes)\n    model.load_state_dict(model_zoo.load_url(settings['url']))\n    model.input_space = settings['input_space']\n    model.input_size = settings['input_size']\n    model.input_range = settings['input_range']\n    model.mean = settings['mean']\n    model.std = settings['std']\n\n\ndef se_resnext50_32x4d(num_classes=1000, pretrained='imagenet'):\n    model = SENet(SEResNeXtBottleneck, [3, 4, 6, 3], groups=32, reduction=16,\n                  dropout_p=None, inplanes=64, input_3x3=False,\n                  downsample_kernel_size=1, downsample_padding=0,\n                  num_classes=num_classes)\n    if pretrained is not None:\n        settings = config.pretrained_settings['se_resnext50_32x4d'][pretrained]\n        initialize_pretrained_model(model, num_classes, settings)\n    return model\n\n\ndef se_resnext101_32x4d(num_classes=1000, pretrained='imagenet'):\n    model = SENet(SEResNeXtBottleneck, [3, 4, 23, 3], groups=32, reduction=16,\n                  dropout_p=None, inplanes=64, input_3x3=False,\n                  downsample_kernel_size=1, downsample_padding=0,\n                  num_classes=num_classes)\n    if pretrained is not None:\n        settings = config.pretrained_settings['se_resnext101_32x4d'][pretrained]\n        initialize_pretrained_model(model, num_classes, settings)\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2023-05-18T20:32:41.132489Z","iopub.execute_input":"2023-05-18T20:32:41.13302Z","iopub.status.idle":"2023-05-18T20:32:41.179644Z","shell.execute_reply.started":"2023-05-18T20:32:41.132986Z","shell.execute_reply":"2023-05-18T20:32:41.178603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomSEResNeXt(nn.Module):\n\n    def __init__(self, model_name='se_resnext50_32x4d'):\n        assert model_name in ('se_resnext50_32x4d')\n        super().__init__()\n        \n        self.model = se_resnext50_32x4d(pretrained=None)\n        self.model.avg_pool = nn.AdaptiveAvgPool2d(1)\n        self.model.last_linear = nn.Linear(self.model.last_linear.in_features, config.CLASSES)\n        \n    def forward(self, x):\n        x = self.model(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2023-05-18T20:32:41.181386Z","iopub.execute_input":"2023-05-18T20:32:41.18207Z","iopub.status.idle":"2023-05-18T20:32:41.192063Z","shell.execute_reply.started":"2023-05-18T20:32:41.182007Z","shell.execute_reply":"2023-05-18T20:32:41.191166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset","metadata":{}},{"cell_type":"code","source":"class PandaDataset(Dataset):\n    def __init__(self, images, img_height, img_width):\n        self.images = images\n        self.img_height = img_height\n        self.img_width = img_width\n        \n        # we are in validation part\n        self.aug = albumentations.Compose([\n            albumentations.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], always_apply=True)\n        ])\n\n    def __len__(self):\n        return len(self.images)\n\n\n    def __getitem__(self, idx):\n\n        img_name = self.images[idx]\n        img_path = os.path.join(data_dir, f'{img_name}.tiff')\n\n        img = skimage.io.MultiImage(img_path)\n        img = cv2.resize(img[-1], (512, 512))\n        save_path =  f'{img_name}.png'\n        cv2.imwrite(save_path, img)\n        img = skimage.io.MultiImage(save_path)\n            \n        img = cv2.resize(img[-1], (self.img_height, self.img_width))\n\n        img = Image.fromarray(img).convert(\"RGB\")\n        img = self.aug(image=np.array(img))[\"image\"]\n        img = np.transpose(img, (2, 0, 1)).astype(np.float32)\n\n        return { 'image': torch.tensor(img, dtype=torch.float) }\n    ","metadata":{"execution":{"iopub.status.busy":"2023-05-18T20:32:41.193449Z","iopub.execute_input":"2023-05-18T20:32:41.193806Z","iopub.status.idle":"2023-05-18T20:32:41.205789Z","shell.execute_reply.started":"2023-05-18T20:32:41.193774Z","shell.execute_reply":"2023-05-18T20:32:41.204747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ENSEMBLES = [\n    {\n        'model_name': 'se_resnext50_32x4d',\n        'model_weight': '../input/panda-open-models/resnext50_0.pth',\n        'ensemble_weight': 1 \n    },\n    {\n        'model_name': 'se_resnext50_32x4d',\n        'model_weight': '../input/panda-open-models/resnext50_2.pth',\n        'ensemble_weight': 1 \n    },\n    {\n        'model_name': 'se_resnext50_32x4d',\n        'model_weight': '../input/panda-open-models/resnext50_1.pth',\n        'ensemble_weight': 1\n    },\n]","metadata":{"execution":{"iopub.status.busy":"2023-05-18T20:32:41.21Z","iopub.execute_input":"2023-05-18T20:32:41.210396Z","iopub.status.idle":"2023-05-18T20:32:41.216186Z","shell.execute_reply.started":"2023-05-18T20:32:41.210365Z","shell.execute_reply":"2023-05-18T20:32:41.215193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = config.device\nmodels = []\nfor ensemble in ENSEMBLES:\n    model = CustomSEResNeXt(model_name=ensemble['model_name'])\n    model.load_state_dict(torch.load(ensemble['model_weight'], map_location=device))\n    model.to(device)\n    models.append(model)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T20:32:41.217793Z","iopub.execute_input":"2023-05-18T20:32:41.218175Z","iopub.status.idle":"2023-05-18T20:32:48.423233Z","shell.execute_reply.started":"2023-05-18T20:32:41.218143Z","shell.execute_reply":"2023-05-18T20:32:48.42233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check_for_images_dir():\n    if config.DEBUG:\n        return os.path.exists('../input/prostate-cancer-grade-assessment/train_images')\n    else:\n        return os.path.exists('../input/prostate-cancer-grade-assessment/test_images')\n        ","metadata":{"execution":{"iopub.status.busy":"2023-05-18T20:32:48.424559Z","iopub.execute_input":"2023-05-18T20:32:48.425116Z","iopub.status.idle":"2023-05-18T20:32:48.431509Z","shell.execute_reply.started":"2023-05-18T20:32:48.425082Z","shell.execute_reply":"2023-05-18T20:32:48.430597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference","metadata":{}},{"cell_type":"code","source":"model.eval()\npredictions = []\n\nif check_for_images_dir():\n    test_dataset = PandaDataset(\n        images=test.image_id.values,\n        img_height=config.IMG_HEIGHT,\n        img_width=config.IMG_WIDTH,\n    )\n\n    test_data_loader = torch.utils.data.DataLoader(\n        test_dataset,\n        batch_size=config.TEST_BATCH_SIZE,\n        shuffle=False,\n    )\n    \n    for model in models:\n        preds = []\n        for idx, d in tqdm(enumerate(test_data_loader), total=len(test_data_loader)):\n            inputs = d[\"image\"]\n            inputs = inputs.to(device)\n\n            with torch.no_grad():\n                outputs = model(inputs)\n            preds.append(outputs.to('cpu').numpy())\n                    \n        predictions.append(np.concatenate(preds))\n    predictions = np.mean(predictions, axis=0)\n    predictions = predictions.argmax(1)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-18T20:32:48.433147Z","iopub.execute_input":"2023-05-18T20:32:48.434012Z","iopub.status.idle":"2023-05-18T21:09:49.257486Z","shell.execute_reply.started":"2023-05-18T20:32:48.433979Z","shell.execute_reply":"2023-05-18T21:09:49.256331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Save results","metadata":{}},{"cell_type":"code","source":"if config.DEBUG:\n    def quadratic_weighted_kappa(y_hat, y):\n        return cohen_kappa_score(y_hat, y, weights='quadratic')\n\n    count = 0\n    for index, val in enumerate(test.isup_grade.values):\n        if predictions[index] == val:\n            count += 1\n\n    print(f\"Accuracy Train is {(count / test.shape[0])* 100}\")\n    print(f'Kappa Train is {quadratic_weighted_kappa(predictions, test.isup_grade.values)}')\nelse:\n    if len(predictions) > 0:\n        submission.isup_grade = predictions\n    submission.isup_grade = submission['isup_grade'].astype(int)\n    submission.to_csv('submission.csv',index=False)\n    print(submission.head())     ","metadata":{"execution":{"iopub.status.busy":"2023-05-18T21:09:49.259122Z","iopub.execute_input":"2023-05-18T21:09:49.259765Z","iopub.status.idle":"2023-05-18T21:09:49.273577Z","shell.execute_reply.started":"2023-05-18T21:09:49.259728Z","shell.execute_reply":"2023-05-18T21:09:49.27245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Assuming 'predictions' and 'true_labels' are your predicted and true labels, respectively\ncm = confusion_matrix(predictions, test.isup_grade.values)\n\n# Plot confusion matrix as a heatmap\nplt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\")\nplt.xlabel(\"Predicted Labels\")\nplt.ylabel(\"True Labels\")\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-18T22:22:43.364407Z","iopub.execute_input":"2023-05-18T22:22:43.364762Z","iopub.status.idle":"2023-05-18T22:22:43.709096Z","shell.execute_reply.started":"2023-05-18T22:22:43.364734Z","shell.execute_reply":"2023-05-18T22:22:43.708196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Assuming 'predicted_scores' contains the predicted scores ranging from 0 to 5\nplt.hist(predictions, bins=6, range=(0, 6), edgecolor='black')\nplt.xlabel('Predicted Scores')\nplt.ylabel('Count')\nplt.title('Histogram of Predicted Scores')\nplt.xticks(range(6))\nplt.show()\nplt.hist(test.isup_grade.values, bins=6, range=(0, 6), edgecolor='black')\nplt.xlabel('Predicted Scores')\nplt.ylabel('Count')\nplt.title('Histogram of Predicted Scores')\nplt.xticks(range(6))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-18T22:28:25.330284Z","iopub.execute_input":"2023-05-18T22:28:25.330739Z","iopub.status.idle":"2023-05-18T22:28:25.842436Z","shell.execute_reply.started":"2023-05-18T22:28:25.330705Z","shell.execute_reply":"2023-05-18T22:28:25.841359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# END NOTES\nI will keep on updating this kernel with my new findings and learning in order to help everyone who has just started in this competition.\n\n**<span style=\"color:Red\">Please upvote this kernel if you like it . It motivates me to produce more quality content :)**","metadata":{}}]}