dcase-2019/Get Weighted Classes.ipynb

94 строки
2.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from collections import defaultdict\n",
"\n",
"from load_dataset import load_dataset_from_path #, make_weights_for_balanced_classes\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_dir = r\"D:\\repos\\Data-Processing\\audio\\train\"\n",
"test_dir = r\"D:\\repos\\Data-Processing\\audio\\test\"\n",
"\n",
"X_train, y_train = load_dataset_from_path(train_dir)\n",
"X_test, y_test = load_dataset_from_path(test_dir)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(np.where(y_train[:,9] == 1)[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_weights(label_array, n_classes):\n",
"# weight_dict = defaultdict(int)\n",
" weights = []\n",
" for i in range(n_classes):\n",
" negs = len(np.where(label_array[:,i] == 0)[0])\n",
" pos = len(np.where(label_array[:,i] == 1)[0])\n",
" if pos > 0:\n",
" weights.append(negs/pos)\n",
" else:\n",
" weights.append(0)\n",
"# weight_dict[i] += len(np.where(label_array[:,i] == 1)[0])\n",
"# weights = [0.] * n_classes\n",
"# for k, v in weight_dict.items():\n",
"# if v:\n",
"# weights[k] = len(label_array) / v \n",
"# return weights\n",
" return weights"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"get_weights(y_train, 37)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}