{
  "id": 239239,
  "title": "How to properly add weights to screw data?",
  "url": "/competitions/seti-breakthrough-listen/discussion/239239",
  "author_name": "Last Scene",
  "post_date": "2021-05-15T12:12:14.242000",
  "votes": 2,
  "comment_count": 4,
  "views": 0,
  "content": "<p>My model doesn't perform well in class 1 due to data screwed.<br>\nI've tried <a href=\"https://github.com/ufoym/imbalanced-dataset-sampler\" target=\"_blank\">Imbalanced Sampler</a> but this doesn't seem improve much for my model.</p>\n<p>Now I'm thinking of adding weights to loss function.<br>\nI wonder is there a proper way(like a formula) to calculate what weights should I add?</p>\n<p>Edit:<br>\nAfter doing some research, I've got [(n_0 + n_1) / (2.0 * n_0), (n_0 + n_1) / (2.0 * n_1)] as weight. And it works a little. The positive class's f1 score reached 0.88, but can't go higher(I even added weights heavily to the positive class). Any reminds could be of help.</p>",
  "messages": [
    {
      "id": 1308785,
      "postDate": "2021-05-15T12:59:04.140Z",
      "content": "<p>If you are Using Tensorflow, Add this in you model.fit function  <code>class_weight={0: 0.55106693, 1: 5.39553643}</code>. What this basically does that it if your model predicts the class 1 as wrong it will be penalized  5.39553643 Time more thus making it weighted Bce</p>",
      "rawMarkdown": "If you are Using Tensorflow, Add this in you model.fit function  `class_weight={0: 0.55106693, 1: 5.39553643}`. What this basically does that it if your model predicts the class 1 as wrong it will be penalized  5.39553643 Time more thus making it weighted Bce",
      "votes": 1,
      "replies": [
        {
          "id": 1308798,
          "postDate": "2021-05-15T13:05:51.670Z",
          "content": "<p>Thanks for sharing. I'm using pytorch, I think the clue is the same.</p>",
          "rawMarkdown": "Thanks for sharing. I'm using pytorch, I think the clue is the same."
        },
        {
          "id": 1308881,
          "postDate": "2021-05-15T14:19:11.240Z",
          "content": "<p>You can sklearn.utils.class_weight.compute_class_weight to calculate the weights i you want use them</p>",
          "rawMarkdown": "You can sklearn.utils.class_weight.compute_class_weight to calculate the weights i you want use them\n",
          "votes": 1
        },
        {
          "id": 1309064,
          "postDate": "2021-05-15T16:27:12.583Z",
          "content": "<p>Thanks again, this is helpful.</p>",
          "rawMarkdown": "Thanks again, this is helpful."
        }
      ]
    },
    {
      "id": 1308712,
      "postDate": "2021-05-15T12:12:14.243Z",
      "content": "<p>My model doesn't perform well in class 1 due to data screwed.<br>\nI've tried <a href=\"https://github.com/ufoym/imbalanced-dataset-sampler\" target=\"_blank\">Imbalanced Sampler</a> but this doesn't seem improve much for my model.</p>\n<p>Now I'm thinking of adding weights to loss function.<br>\nI wonder is there a proper way(like a formula) to calculate what weights should I add?</p>\n<p>Edit:<br>\nAfter doing some research, I've got [(n_0 + n_1) / (2.0 * n_0), (n_0 + n_1) / (2.0 * n_1)] as weight. And it works a little. The positive class's f1 score reached 0.88, but can't go higher(I even added weights heavily to the positive class). Any reminds could be of help.</p>",
      "rawMarkdown": "My model doesn't perform well in class 1 due to data screwed.\nI've tried [Imbalanced Sampler](https://github.com/ufoym/imbalanced-dataset-sampler) but this doesn't seem improve much for my model.\n\nNow I'm thinking of adding weights to loss function.\nI wonder is there a proper way(like a formula) to calculate what weights should I add?\n\nEdit:\nAfter doing some research, I've got [(n_0 + n_1) / (2.0 * n_0), (n_0 + n_1) / (2.0 * n_1)] as weight. And it works a little. The positive class's f1 score reached 0.88, but can't go higher(I even added weights heavily to the positive class). Any reminds could be of help.",
      "votes": 2
    }
  ],
  "comments": [
    {
      "id": 1308785,
      "author_name": "Mithil Salunkhe",
      "author_url": "",
      "post_date": "2021-05-15T12:59:04.140000",
      "content": "<p>If you are Using Tensorflow, Add this in you model.fit function  <code>class_weight={0: 0.55106693, 1: 5.39553643}</code>. What this basically does that it if your model predicts the class 1 as wrong it will be penalized  5.39553643 Time more thus making it weighted Bce</p>",
      "votes": 1,
      "replies": [
        {
          "id": 1308798,
          "author_name": "Last Scene",
          "author_url": "",
          "post_date": "2021-05-15T13:05:51.670000",
          "content": "<p>Thanks for sharing. I'm using pytorch, I think the clue is the same.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1308881,
          "author_name": "Mithil Salunkhe",
          "author_url": "",
          "post_date": "2021-05-15T14:19:11.240000",
          "content": "<p>You can sklearn.utils.class_weight.compute_class_weight to calculate the weights i you want use them</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 1309064,
          "author_name": "Last Scene",
          "author_url": "",
          "post_date": "2021-05-15T16:27:12.583000",
          "content": "<p>Thanks again, this is helpful.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1308785": "If you are Using Tensorflow, Add this in you model.fit function  `class_weight={0: 0.55106693, 1: 5.39553643}`. What this basically does that it if your model predicts the class 1 as wrong it will be penalized  5.39553643 Time more thus making it weighted Bce",
    "1308712": "My model doesn't perform well in class 1 due to data screwed.\nI've tried [Imbalanced Sampler](https://github.com/ufoym/imbalanced-dataset-sampler) but this doesn't seem improve much for my model.\n\nNow I'm thinking of adding weights to loss function.\nI wonder is there a proper way(like a formula) to calculate what weights should I add?\n\nEdit:\nAfter doing some research, I've got [(n_0 + n_1) / (2.0 * n_0), (n_0 + n_1) / (2.0 * n_1)] as weight. And it works a little. The positive class's f1 score reached 0.88, but can't go higher(I even added weights heavily to the positive class). Any reminds could be of help."
  }
}