{"cells":[{"metadata":{"_uuid":"a8b0d69faf1564d2a1357b40f6c9d66b4a8d64aa"},"cell_type":"markdown","source":"### As far as we know f1 score is highly dependent from true positive rate. If a class is not present then it will have true positive rate and f1 score both equal to 0, even though all predictions are correct. That could have a stong effect******** on small batches when not all classes are present."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.metrics import f1_score","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"84320b7c65d6a1f109a8da11a0fcefbbbc3dbe07"},"cell_type":"markdown","source":"### Read all true labels from training set to demonstate the idea on them"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"num_classes = 28\n\ndf = pd.read_csv('../input/train.csv')\n\ny_true = np.zeros((len(df), num_classes))\n\nfor i, row in df.iterrows():\n    for lblIndex in row['Target'].split():\n        y_true[i][int(lblIndex)] = 1\n        \nprint(y_true.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"04a48735e9b7f5143ca68370061db649cc1268da"},"cell_type":"markdown","source":"### We can calculate f1 score for all true labels with themselves. This should give f1 equal to 1 since all classes are present in training set."},{"metadata":{"trusted":true,"_uuid":"dbc2ec2fd7513e981d6e489fdc4667cd4f4189dc"},"cell_type":"code","source":"print(f1_score(y_true, y_true, average='macro'))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1f357d3aec3353a5d54f124a533a44b5d1e6dd94"},"cell_type":"markdown","source":"### After that we will calculate f1 score for true labels with themselves for small batches to see the effect"},{"metadata":{"trusted":true,"_uuid":"8777222ac86f6cf4215260786ced81238e444e01"},"cell_type":"code","source":"for batch_size in [64, 32, 16]:\n    print(\"Batch size:\", batch_size, \"F1 macro:\", f1_score(y_true[:batch_size], y_true[:batch_size], average='macro'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b8b9d500b732bbb74c6d75a9d9c112d5cee20ef0"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}