{"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":"code","source":"#! pip install librosa\nimport pandas as pd\nimport os\nimport librosa\n#import IPython.display as ipd\n#import matplotlib.pyplot as plt\nimport numpy as np\n#import sklearn\n#import tensorflow as tf\nimport scipy\nimport gc\n\nfrom scipy.io import wavfile\nfrom scipy.stats import skew\nfrom tqdm import tqdm, tqdm_pandas\n\ntqdm.pandas()\n\n#from sklearn.model_selection import train_test_split\n#from sklearn.preprocessing import LabelEncoder, StandardScaler\n#from sklearn.model_selection import GridSearchCV\n\nfrom warnings import simplefilter","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-25T23:59:12.601704Z","iopub.execute_input":"2022-08-25T23:59:12.602337Z","iopub.status.idle":"2022-08-25T23:59:40.836851Z","shell.execute_reply.started":"2022-08-25T23:59:12.602307Z","shell.execute_reply":"2022-08-25T23:59:40.836123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"simplefilter(action='ignore', category=FutureWarning)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T23:59:40.838167Z","iopub.execute_input":"2022-08-25T23:59:40.838449Z","iopub.status.idle":"2022-08-25T23:59:40.842106Z","shell.execute_reply.started":"2022-08-25T23:59:40.838424Z","shell.execute_reply":"2022-08-25T23:59:40.841582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_path=\"../input/freesound-audio-tagging/train.csv\"\ntest = pd.read_csv(\"../input/freesound-audio-tagging/sample_submission.csv\")\n\ntrain_path = '../input/freesound-audio-tagging/audio_train/'\ntest_path = '../input/freesound-audio-tagging/audio_test/'","metadata":{"execution":{"iopub.status.busy":"2022-08-26T00:00:02.679463Z","iopub.execute_input":"2022-08-26T00:00:02.680003Z","iopub.status.idle":"2022-08-26T00:00:02.691957Z","shell.execute_reply.started":"2022-08-26T00:00:02.679978Z","shell.execute_reply":"2022-08-26T00:00:02.691390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{}},{"cell_type":"code","source":"def audio_magic(fileName, path):\n    data, _ = librosa.core.load(path + fileName, sr = 44100)\n    try:\n        mfcc = librosa.feature.mfcc(data, sr = 44100, n_mfcc=20)\n        rms = librosa.feature.rms(data)[0]\n        chroma_stft = librosa.feature.chroma_stft(data)[0]\n        zcr = librosa.feature.zero_crossing_rate(data)[0]\n        rolloff = librosa.feature.spectral_rolloff(data)[0]\n        spectral_centroid = librosa.feature.spectral_centroid(data)[0]\n        spectral_bandwidth = librosa.feature.spectral_bandwidth(data)[0]\n        mfcc_trunc = np.hstack((np.mean(mfcc, axis=1), np.std(mfcc, axis=1), skew(mfcc, axis = 1), np.max(mfcc, axis = 1), np.median(mfcc, axis = 1), np.min(mfcc, axis = 1)))\n        return pd.Series(np.hstack((mfcc_trunc, rms, chroma_stft, zcr, rolloff, spectral_centroid, spectral_bandwidth)))\n    except:\n        print('Error with audio')\n        return pd.Series([0]*20)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T23:59:40.874328Z","iopub.execute_input":"2022-08-25T23:59:40.874589Z","iopub.status.idle":"2022-08-25T23:59:40.882725Z","shell.execute_reply.started":"2022-08-25T23:59:40.874567Z","shell.execute_reply":"2022-08-25T23:59:40.882221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def driver(train_path,label_path):\n    train_df = pd.DataFrame()\n    l_df=pd.read_csv(label_path)\n    l_df['fname']=l_df['fname']\n    train_df = l_df['fname'].progress_apply(audio_magic, path=train_path)\n    train_df['label']=l_df['label']\n    return train_df\n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-25T23:59:40.883709Z","iopub.execute_input":"2022-08-25T23:59:40.883991Z","iopub.status.idle":"2022-08-25T23:59:40.896784Z","shell.execute_reply.started":"2022-08-25T23:59:40.883969Z","shell.execute_reply":"2022-08-25T23:59:40.896277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=driver(train_path,label_path)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-26T00:00:10.400637Z","iopub.execute_input":"2022-08-26T00:00:10.400983Z","iopub.status.idle":"2022-08-26T00:15:31.653596Z","shell.execute_reply.started":"2022-08-26T00:00:10.400957Z","shell.execute_reply":"2022-08-26T00:15:31.652871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"        ","metadata":{},"execution_count":null,"outputs":[]}]}