{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":1,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import IPython.display as ipd  # To play sound in the notebook\nfrom tqdm import tqdm_notebook\nimport wave\n\nfrom sklearn.linear_model import LogisticRegression\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()","execution_count":2,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"5272d869-2d4b-433b-9db7-c46ce1afb118","_uuid":"9d44f9a36ef645ad67ccf7f965a49da33a11dcff","trusted":true},"cell_type":"code","source":"audio_train_files = os.listdir('../input/audio_train')\naudio_test_files = os.listdir('../input/audio_test')\n\ntrain = pd.read_csv('../input/train.csv', index_col='fname')\nsubmission = pd.read_csv('../input/sample_submission.csv', index_col='fname')","execution_count":3,"outputs":[]},{"metadata":{"_cell_guid":"e76dbf21-cdd4-4fa9-841c-b8c931fd84ac","_uuid":"2db0260a1cdc1b897a38b0e61078cd291321b418","trusted":true},"cell_type":"code","source":"train.head()","execution_count":4,"outputs":[]},{"metadata":{"_cell_guid":"badb80e9-fdad-4dac-aac3-b3b2c4052610","_uuid":"d29e266c958380c69c566fcaca2e36933eaaeb1a","trusted":true},"cell_type":"code","source":"submission.head()","execution_count":5,"outputs":[]},{"metadata":{"_cell_guid":"9b599a35-2716-4fd4-a817-28bca9f480a1","_uuid":"200710669c3cfa368e90af633ac8f3d176e33a6f","trusted":true},"cell_type":"code","source":"ipd.Audio('../input/audio_train/' + '00044347.wav') # Hi-hat","execution_count":6,"outputs":[]},{"metadata":{"_cell_guid":"1f8e5eed-95a7-4876-a737-b92646a5bbad","_uuid":"65eecf0ed8a97554e85841d9c7bc6d0bc7c08683","trusted":true},"cell_type":"code","source":"ipd.Audio('../input/audio_train/' + '001ca53d.wav') # Saxophone","execution_count":7,"outputs":[]},{"metadata":{"_cell_guid":"4b2e8909-d382-42db-a167-48da94cd7f05","_uuid":"f999673271a7faf1202895ba9b72046013e00d8e","trusted":true},"cell_type":"code","source":"ipd.Audio('../input/audio_train/' + '00c82919.wav') # Can you guess?","execution_count":8,"outputs":[]},{"metadata":{"_uuid":"19c6e842cc9abfd109d017c039f26bef298e2cd0"},"cell_type":"markdown","source":"## Some basic audio file processing (in this case, reading the number of frames in each file)"},{"metadata":{"_cell_guid":"f7e7f049-da70-4532-ab60-ba87fc24bd61","_uuid":"a7ecff70ecf2327d23a2aa51c1c975a64d7dcbeb","trusted":true},"cell_type":"code","source":"train['nframes'] = 0\n\nfor e, fname in enumerate(tqdm_notebook(train.index)):\n    try:\n        w = wave.open('../input/audio_train/' + fname)\n        p = w.getparams()\n        train.loc[fname, 'nframes'] = p.nframes\n    except:\n        print(f'Failed: {e} - {fname}')","execution_count":9,"outputs":[]},{"metadata":{"_cell_guid":"239e6073-0f49-4e81-a0f8-d19511fb3b94","_uuid":"82e1b9c6852e0c36e5f63379253f510bea9e5c41","trusted":true},"cell_type":"code","source":"_, ax = plt.subplots(figsize=(16, 4))\nsns.violinplot(ax=ax, x=\"label\", y=\"nframes\", data=train)\nplt.xticks(rotation=90)\nplt.title('Distribution of audio frames, per label', fontsize=16)\nplt.show()","execution_count":10,"outputs":[]},{"metadata":{"_cell_guid":"8660d7fd-fd12-496b-81ba-6b67268683cc","_uuid":"34dacfffb719f9ce6a47503bbff9827ba30624dc","trusted":true},"cell_type":"code","source":"test = pd.DataFrame(index=submission.index, columns=['nframes'], data=0)\n\nfor e, fname in enumerate(tqdm_notebook(test.index)):\n    try:\n        w = wave.open('../input/audio_test/' + fname)\n        p = w.getparams()\n        test.loc[fname, 'nframes'] = p.nframes\n    except:\n        print(f'Failed: {e} - {fname}')","execution_count":11,"outputs":[]},{"metadata":{"_cell_guid":"80321f5a-d79e-4606-9214-687421db2f69","_uuid":"17f2ee299f2d4220511ed76ef801445a9dccab8c","trusted":true},"cell_type":"code","source":"test.head()","execution_count":12,"outputs":[]},{"metadata":{"_cell_guid":"a3e18706-8766-4ab3-8274-71071372f876","_uuid":"2e1bd14f7d108551be4fcacb00519cb3c7b7ea86"},"cell_type":"markdown","source":"## Can we make a classifier based on the number of audio frames?"},{"metadata":{"_cell_guid":"d2c8592a-0507-47b2-8222-398000d44613","_uuid":"f352e2c1adabfb026227b0b8fc22709898936e70","trusted":true},"cell_type":"code","source":"clf = LogisticRegression()\nclf.fit(train['nframes'].values.reshape(-1,1), train['label'].values)","execution_count":13,"outputs":[]},{"metadata":{"_cell_guid":"27b5b09c-6bf1-418b-9ece-c62171cc372a","_uuid":"2fcc8beb1e5a25260ac5273b3628b7a1b3a4128a","trusted":true,"collapsed":true},"cell_type":"code","source":"preds = clf.predict_proba(test.values)\ntop_3 = clf.classes_[np.argsort(preds, axis=1)[:, -3:]]\nsubmission['label'] = [' '.join(list(x)) for x in top_3]","execution_count":14,"outputs":[]},{"metadata":{"_cell_guid":"936397c8-be85-41e7-bcc9-4fdd86a09d85","_uuid":"bd464aaa2ce834b64a6c2c6ca2e1b53f07e0eafa","trusted":true},"cell_type":"code","source":"submission.sample(10)","execution_count":15,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"b7bae05b-919b-48a3-9df9-79a2caf43e66","_uuid":"c39494c92d74ba7dd5cc3ba84ad44c5c497e73a8","trusted":true},"cell_type":"code","source":"submission.to_csv('audio_frame_lr.csv')","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"f8b64c79-e610-4809-a6e3-1c132940e1ec","_uuid":"f6d6ff166a3500be3c9a99031ba17ba90ce0da3d","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"bbc8e1fc-16ca-4ae8-8b35-09402673d7c9","_uuid":"d264272aa39a427643e420caa8e79fe11db468a7","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"54e2e58e-88a4-46cb-a264-f3b982c6517f","_uuid":"790a9ce8e17a0cf0549259b3c81cbc6d7f91dad6","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"86b6ec4e-1092-4013-bf93-6ab686b43bd3","_uuid":"cfe6d208af54adb02fa60ba792e04dc96511ae0f","trusted":false},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}