{"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":"##### Audio Classification Using Machine Learning\n#####dataset url https://urbansounddataset.weebly.com/urbansound8k.html","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:50:56.621130Z","iopub.execute_input":"2022-02-25T12:50:56.621387Z","iopub.status.idle":"2022-02-25T12:50:56.624601Z","shell.execute_reply.started":"2022-02-25T12:50:56.621358Z","shell.execute_reply":"2022-02-25T12:50:56.623936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install librosa","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:50:57.028242Z","iopub.execute_input":"2022-02-25T12:50:57.028938Z","iopub.status.idle":"2022-02-25T12:51:05.565271Z","shell.execute_reply.started":"2022-02-25T12:50:57.028900Z","shell.execute_reply":"2022-02-25T12:51:05.564275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:05.567407Z","iopub.execute_input":"2022-02-25T12:51:05.567685Z","iopub.status.idle":"2022-02-25T12:51:05.574266Z","shell.execute_reply.started":"2022-02-25T12:51:05.567632Z","shell.execute_reply":"2022-02-25T12:51:05.573583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename='../input/birdclef-2022/train_audio/akiapo/XC122401.ogg'","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:05.575943Z","iopub.execute_input":"2022-02-25T12:51:05.576524Z","iopub.status.idle":"2022-02-25T12:51:05.585298Z","shell.execute_reply.started":"2022-02-25T12:51:05.576480Z","shell.execute_reply":"2022-02-25T12:51:05.584551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import IPython.display as ipd\nimport librosa\nimport librosa.display","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:05.588649Z","iopub.execute_input":"2022-02-25T12:51:05.588866Z","iopub.status.idle":"2022-02-25T12:51:07.995516Z","shell.execute_reply.started":"2022-02-25T12:51:05.588839Z","shell.execute_reply":"2022-02-25T12:51:07.994743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!dir","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:07.996802Z","iopub.execute_input":"2022-02-25T12:51:07.997072Z","iopub.status.idle":"2022-02-25T12:51:08.664348Z","shell.execute_reply.started":"2022-02-25T12:51:07.997038Z","shell.execute_reply":"2022-02-25T12:51:08.663491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Dog Sound\nplt.figure(figsize=(14,5))\ndata,sample_rate=librosa.load(filename)\nlibrosa.display.waveshow(data,sr=sample_rate)\nipd.Audio(filename)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:08.666302Z","iopub.execute_input":"2022-02-25T12:51:08.666672Z","iopub.status.idle":"2022-02-25T12:51:11.251321Z","shell.execute_reply.started":"2022-02-25T12:51:08.666608Z","shell.execute_reply":"2022-02-25T12:51:11.250556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Sound\nfilename='../input/birdclef-2022/train_audio/akekee/XC174953.ogg'\nplt.figure(figsize=(14,5))\ndata,sample_rate=librosa.load(filename)\nlibrosa.display.waveshow(data,sr=sample_rate)\nipd.Audio(filename)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:11.252519Z","iopub.execute_input":"2022-02-25T12:51:11.252881Z","iopub.status.idle":"2022-02-25T12:51:11.760495Z","shell.execute_reply.started":"2022-02-25T12:51:11.252842Z","shell.execute_reply":"2022-02-25T12:51:11.759557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_rate\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:11.762100Z","iopub.execute_input":"2022-02-25T12:51:11.762374Z","iopub.status.idle":"2022-02-25T12:51:11.768612Z","shell.execute_reply.started":"2022-02-25T12:51:11.762335Z","shell.execute_reply":"2022-02-25T12:51:11.767687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from scipy.io import wavfile as wav\n# wave_sample_rate, wave_audio=wav.read(filename)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:11.770138Z","iopub.execute_input":"2022-02-25T12:51:11.770462Z","iopub.status.idle":"2022-02-25T12:51:11.777612Z","shell.execute_reply.started":"2022-02-25T12:51:11.770424Z","shell.execute_reply":"2022-02-25T12:51:11.776647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# wave_sample_rate","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:11.781535Z","iopub.execute_input":"2022-02-25T12:51:11.781876Z","iopub.status.idle":"2022-02-25T12:51:11.786576Z","shell.execute_reply.started":"2022-02-25T12:51:11.781838Z","shell.execute_reply":"2022-02-25T12:51:11.785683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# wave_audio","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:11.788243Z","iopub.execute_input":"2022-02-25T12:51:11.788522Z","iopub.status.idle":"2022-02-25T12:51:11.794963Z","shell.execute_reply.started":"2022-02-25T12:51:11.788485Z","shell.execute_reply":"2022-02-25T12:51:11.794130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:11.796835Z","iopub.execute_input":"2022-02-25T12:51:11.797069Z","iopub.status.idle":"2022-02-25T12:51:11.807101Z","shell.execute_reply.started":"2022-02-25T12:51:11.797044Z","shell.execute_reply":"2022-02-25T12:51:11.806201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\nmetadata1=pd.read_csv('../input/birdclef-2022/eBird_Taxonomy_v2021.csv')\nmetadata1.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:11.808737Z","iopub.execute_input":"2022-02-25T12:51:11.809326Z","iopub.status.idle":"2022-02-25T12:51:11.903298Z","shell.execute_reply.started":"2022-02-25T12:51:11.809286Z","shell.execute_reply":"2022-02-25T12:51:11.902528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata1.isnull().sum()\nmetadata1['REPORT_AS'].isnull().round()*100","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:11.904495Z","iopub.execute_input":"2022-02-25T12:51:11.904758Z","iopub.status.idle":"2022-02-25T12:51:11.931230Z","shell.execute_reply.started":"2022-02-25T12:51:11.904722Z","shell.execute_reply":"2022-02-25T12:51:11.930571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Check whether the dataset is imbalanced\nmetadata1['REPORT_AS'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:25.314541Z","iopub.execute_input":"2022-02-25T12:51:25.315300Z","iopub.status.idle":"2022-02-25T12:51:25.330594Z","shell.execute_reply.started":"2022-02-25T12:51:25.315262Z","shell.execute_reply":"2022-02-25T12:51:25.329756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Let's read a sample audio using librosa\nimport librosa\naudio_file_path='../input/birdclef-2022/train_audio/afrsil1/XC125458.ogg'\nlibrosa_audio_data,librosa_sample_rate=librosa.load(audio_file_path)\nmfccs = librosa.feature.mfcc(y=librosa_audio_data, sr=librosa_sample_rate, n_mfcc=40)\nprint(mfccs.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:26.292860Z","iopub.execute_input":"2022-02-25T12:51:26.293358Z","iopub.status.idle":"2022-02-25T12:51:26.642983Z","shell.execute_reply.started":"2022-02-25T12:51:26.293319Z","shell.execute_reply":"2022-02-25T12:51:26.639849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfccs","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:28.570841Z","iopub.execute_input":"2022-02-25T12:51:28.571580Z","iopub.status.idle":"2022-02-25T12:51:28.577649Z","shell.execute_reply.started":"2022-02-25T12:51:28.571542Z","shell.execute_reply":"2022-02-25T12:51:28.576843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### Extracting MFCC's For every audio file\nimport pandas as pd\nimport os\nimport librosa\n\naudio_dataset_path='../input/birdclef-2022/train_audio/'\nmetadata=pd.read_csv('../input/birdclef-2022/train_metadata.csv')\nmetadata.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:34.577299Z","iopub.execute_input":"2022-02-25T12:51:34.577577Z","iopub.status.idle":"2022-02-25T12:51:34.687877Z","shell.execute_reply.started":"2022-02-25T12:51:34.577539Z","shell.execute_reply":"2022-02-25T12:51:34.687092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:51:36.100231Z","iopub.execute_input":"2022-02-25T12:51:36.101111Z","iopub.status.idle":"2022-02-25T12:51:36.124356Z","shell.execute_reply.started":"2022-02-25T12:51:36.101045Z","shell.execute_reply":"2022-02-25T12:51:36.123685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"master = pd.merge(metadata,metadata1,how=\"inner\",left_on=\"primary_label\",right_on=\"REPORT_AS\")\nmaster.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:52:01.077962Z","iopub.execute_input":"2022-02-25T12:52:01.078226Z","iopub.status.idle":"2022-02-25T12:52:01.120055Z","shell.execute_reply.started":"2022-02-25T12:52:01.078196Z","shell.execute_reply":"2022-02-25T12:52:01.119117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"master.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:52:34.579336Z","iopub.execute_input":"2022-02-25T12:52:34.579616Z","iopub.status.idle":"2022-02-25T12:52:34.585583Z","shell.execute_reply.started":"2022-02-25T12:52:34.579587Z","shell.execute_reply":"2022-02-25T12:52:34.584678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"master.columns","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:53:38.815996Z","iopub.execute_input":"2022-02-25T12:53:38.816559Z","iopub.status.idle":"2022-02-25T12:53:38.822549Z","shell.execute_reply.started":"2022-02-25T12:53:38.816519Z","shell.execute_reply":"2022-02-25T12:53:38.821829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"master.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:54:04.319527Z","iopub.execute_input":"2022-02-25T12:54:04.320120Z","iopub.status.idle":"2022-02-25T12:54:04.380224Z","shell.execute_reply.started":"2022-02-25T12:54:04.320081Z","shell.execute_reply":"2022-02-25T12:54:04.379498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ","metadata":{}},{"cell_type":"code","source":"master.drop(['SPECIES_GROUP','REPORT_AS'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:56:07.814788Z","iopub.execute_input":"2022-02-25T12:56:07.815588Z","iopub.status.idle":"2022-02-25T12:56:07.858474Z","shell.execute_reply.started":"2022-02-25T12:56:07.815538Z","shell.execute_reply":"2022-02-25T12:56:07.857601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"def features_extractor(file):\n    audio, sample_rate = librosa.load(file_name, res_type='kaiser_fast') \n    mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)\n    mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)\n    \n    return mfccs_scaled_features","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:57:12.175859Z","iopub.execute_input":"2022-02-25T12:57:12.176722Z","iopub.status.idle":"2022-02-25T12:57:12.182293Z","shell.execute_reply.started":"2022-02-25T12:57:12.176560Z","shell.execute_reply":"2022-02-25T12:57:12.181518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom tqdm import tqdm\n### Now we iterate through every audio file and extract features \n### using Mel-Frequency Cepstral Coefficients\nextracted_features=[]\nfor index_num,row in tqdm(metadata.iterrows()):\n    file_name = os.path.join(os.path.abspath(audio_dataset_path)+'/',str(row[\"filename\"]))\n    final_class_labels=row[\"primary_label\"]\n    data=features_extractor(file_name)\n    extracted_features.append([data,final_class_labels])","metadata":{"execution":{"iopub.status.busy":"2022-02-25T12:57:12.373961Z","iopub.execute_input":"2022-02-25T12:57:12.374455Z","iopub.status.idle":"2022-02-25T15:02:02.614358Z","shell.execute_reply.started":"2022-02-25T12:57:12.374421Z","shell.execute_reply":"2022-02-25T15:02:02.610097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### converting extracted_features to Pandas dataframe\nextracted_features_df=pd.DataFrame(extracted_features,columns=['feature','primary_label'])\nextracted_features_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:02.616265Z","iopub.execute_input":"2022-02-25T15:02:02.616548Z","iopub.status.idle":"2022-02-25T15:02:02.645929Z","shell.execute_reply.started":"2022-02-25T15:02:02.616512Z","shell.execute_reply":"2022-02-25T15:02:02.645001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Split the dataset into independent and dependent dataset\nX=np.array(extracted_features_df['feature'].tolist())\ny=np.array(extracted_features_df['primary_label'].tolist())","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:02.647324Z","iopub.execute_input":"2022-02-25T15:02:02.647732Z","iopub.status.idle":"2022-02-25T15:02:02.684251Z","shell.execute_reply.started":"2022-02-25T15:02:02.647697Z","shell.execute_reply":"2022-02-25T15:02:02.683507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:02.689206Z","iopub.execute_input":"2022-02-25T15:02:02.691485Z","iopub.status.idle":"2022-02-25T15:02:02.700287Z","shell.execute_reply.started":"2022-02-25T15:02:02.691434Z","shell.execute_reply":"2022-02-25T15:02:02.699490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:02.701953Z","iopub.execute_input":"2022-02-25T15:02:02.702365Z","iopub.status.idle":"2022-02-25T15:02:02.708966Z","shell.execute_reply.started":"2022-02-25T15:02:02.702325Z","shell.execute_reply":"2022-02-25T15:02:02.708252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Label Encoding\n###y=np.array(pd.get_dummies(y))\n### Label Encoder\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.preprocessing import LabelEncoder\nlabelencoder=LabelEncoder()\ny=to_categorical(labelencoder.fit_transform(y))","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:02.710220Z","iopub.execute_input":"2022-02-25T15:02:02.710569Z","iopub.status.idle":"2022-02-25T15:02:07.265147Z","shell.execute_reply.started":"2022-02-25T15:02:02.710536Z","shell.execute_reply":"2022-02-25T15:02:07.264380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.266601Z","iopub.execute_input":"2022-02-25T15:02:07.266876Z","iopub.status.idle":"2022-02-25T15:02:07.277279Z","shell.execute_reply.started":"2022-02-25T15:02:07.266839Z","shell.execute_reply":"2022-02-25T15:02:07.275816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Train Test Split\nfrom sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.279376Z","iopub.execute_input":"2022-02-25T15:02:07.280202Z","iopub.status.idle":"2022-02-25T15:02:07.294782Z","shell.execute_reply.started":"2022-02-25T15:02:07.280018Z","shell.execute_reply":"2022-02-25T15:02:07.294079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.297310Z","iopub.execute_input":"2022-02-25T15:02:07.297932Z","iopub.status.idle":"2022-02-25T15:02:07.304532Z","shell.execute_reply.started":"2022-02-25T15:02:07.297894Z","shell.execute_reply":"2022-02-25T15:02:07.303759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.307952Z","iopub.execute_input":"2022-02-25T15:02:07.308159Z","iopub.status.idle":"2022-02-25T15:02:07.315939Z","shell.execute_reply.started":"2022-02-25T15:02:07.308135Z","shell.execute_reply":"2022-02-25T15:02:07.315193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.317386Z","iopub.execute_input":"2022-02-25T15:02:07.317869Z","iopub.status.idle":"2022-02-25T15:02:07.323263Z","shell.execute_reply.started":"2022-02-25T15:02:07.317833Z","shell.execute_reply":"2022-02-25T15:02:07.322475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.324668Z","iopub.execute_input":"2022-02-25T15:02:07.325130Z","iopub.status.idle":"2022-02-25T15:02:07.332458Z","shell.execute_reply.started":"2022-02-25T15:02:07.325078Z","shell.execute_reply":"2022-02-25T15:02:07.331634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.333748Z","iopub.execute_input":"2022-02-25T15:02:07.334066Z","iopub.status.idle":"2022-02-25T15:02:07.340923Z","shell.execute_reply.started":"2022-02-25T15:02:07.334023Z","shell.execute_reply":"2022-02-25T15:02:07.340088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.342385Z","iopub.execute_input":"2022-02-25T15:02:07.342667Z","iopub.status.idle":"2022-02-25T15:02:07.349084Z","shell.execute_reply.started":"2022-02-25T15:02:07.342621Z","shell.execute_reply":"2022-02-25T15:02:07.348313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\nmodel = DecisionTreeClassifier()\nmodel.fit(X_train, y_train)\npredictions = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:15:00.770054Z","iopub.execute_input":"2022-02-25T15:15:00.770553Z","iopub.status.idle":"2022-02-25T15:15:14.329665Z","shell.execute_reply.started":"2022-02-25T15:15:00.770510Z","shell.execute_reply":"2022-02-25T15:15:14.328887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Measure the performance of the decision tree model\nfrom sklearn.metrics import classification_report\nfrom sklearn.metrics import confusion_matrix\nprint(classification_report(y_test,predictions))\nnp.argmax(y_test, axis=1)\n# print(confusion_matrix(y_test,predictions))\nconfusion_matrix(y_test.argmax(axis=1), predictions.argmax(axis=1))","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:23:44.094452Z","iopub.execute_input":"2022-02-25T15:23:44.094723Z","iopub.status.idle":"2022-02-25T15:23:44.150575Z","shell.execute_reply.started":"2022-02-25T15:23:44.094692Z","shell.execute_reply":"2022-02-25T15:23:44.149863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.score(X_test, y_test)\nscore","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:26:17.986378Z","iopub.execute_input":"2022-02-25T15:26:17.986678Z","iopub.status.idle":"2022-02-25T15:26:18.036883Z","shell.execute_reply.started":"2022-02-25T15:26:17.986642Z","shell.execute_reply":"2022-02-25T15:26:18.035929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Random Forest","metadata":{}},{"cell_type":"code","source":"# Random forest\n# Fit\n# Import model object\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import classification_report, confusion_matrix, accuracy_score,f1_score, precision_score, recall_score\n\n# Create model object\nrfc = RandomForestClassifier(n_estimators = 200)\n\n# Fit model to training data\nrfc.fit(X_train,y_train)\n# Predict\ny_pred_rfc = rfc.predict(X_test)\n\n# Classification report\nprint('--'*40)\nprint('Classification Report')\n# print(classification_report(y_test, y_pred_rfc))\nconfusion_matrix(y_test.argmax(axis=1), y_pred_rfc.argmax(axis=1))\n# Accuracy\nprint('--'*40)\nrf_accuracy = round(accuracy_score(y_test, y_pred_rfc)*100,2)\nprint('Accuracy', rf_accuracy,'%')","metadata":{"execution":{"iopub.status.busy":"2022-02-25T16:47:17.118411Z","iopub.execute_input":"2022-02-25T16:47:17.119124Z","iopub.status.idle":"2022-02-25T16:47:18.437354Z","shell.execute_reply.started":"2022-02-25T16:47:17.119023Z","shell.execute_reply":"2022-02-25T16:47:18.436114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Score It\nprint('Random Forest')\n# Confusion matrix\nprint('\\n')\nprint('Confusion Matrix')\nprint(confusion_matrix(y_test, y_pred_rfc))\n\n# Classification report\nprint('--'*40)\nprint('Classification Report')\n# print(classification_report(y_test, y_pred_rfc))\nprint(confusion_matrix(y_test.argmax(axis=1), y_pred_rfc.argmax(axis=1)))\ny_\n# Accuracy\nprint('--'*40)\nrf_accuracy = round(accuracy_score(y_test, y_pred_rfc)*100,2)\nprint('Accuracy', rf_accuracy,'%')\n# Predict\ny_pred_rfc = rfc.predict(X_test)\n# Score It\nprint('Random Forest')\n# Confusion matrix\nprint('\\n')\nprint('Confusion Matrix')\nprint(confusion_matrix(y_test, y_pred_rfc))\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:49:24.108315Z","iopub.status.idle":"2022-02-25T15:49:24.108978Z","shell.execute_reply.started":"2022-02-25T15:49:24.108735Z","shell.execute_reply":"2022-02-25T15:49:24.108762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit new KNN\n# Create model object\nknn = KNeighborsClassifier(n_neighbors = 13)\n\n# Fit new KNN on training data\nknn.fit(X_train, y_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict new KNN\ny_pred_knn_op = knn.predict(X_test)\n# Score it with new KNN\nprint('K-Nearest Neighbors(KNN)')\nprint('k = 13')\n\n# Confusion Matrix\nprint('\\n')\nprint(confusion_matrix(y_test, y_pred_knn_op))\n\n# Classification Report\nprint('--'*40)\nprint('Classfication Report',classification_report(y_test, y_pred_knn_op))\n\n# Accuracy\nprint('--'*40)\nknn_op_accuracy =round(accuracy_score(y_test, y_pred_knn_op)*100,2)\nprint('Accuracy',knn_op_accuracy,'%')\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.350600Z","iopub.execute_input":"2022-02-25T15:02:07.350930Z","iopub.status.idle":"2022-02-25T15:02:07.356488Z","shell.execute_reply.started":"2022-02-25T15:02:07.350897Z","shell.execute_reply":"2022-02-25T15:02:07.355752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense,Dropout,Activation,Flatten\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn import metrics\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.358187Z","iopub.execute_input":"2022-02-25T15:02:07.358438Z","iopub.status.idle":"2022-02-25T15:02:07.366373Z","shell.execute_reply.started":"2022-02-25T15:02:07.358405Z","shell.execute_reply":"2022-02-25T15:02:07.365555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### No of classes\nnum_labels=y.shape[1]","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.367376Z","iopub.execute_input":"2022-02-25T15:02:07.367908Z","iopub.status.idle":"2022-02-25T15:02:07.376295Z","shell.execute_reply.started":"2022-02-25T15:02:07.367871Z","shell.execute_reply":"2022-02-25T15:02:07.375657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\n###first layer\nmodel.add(Dense(1000,input_shape=(40,)))\nmodel.add(Activation('relu'))\n# model.add(Dropout(0.5))\n\n###final layer\nmodel.add(Dense(num_labels))\nmodel.add(Activation('softmax'))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:07.379071Z","iopub.execute_input":"2022-02-25T15:02:07.379278Z","iopub.status.idle":"2022-02-25T15:02:09.761490Z","shell.execute_reply.started":"2022-02-25T15:02:07.379250Z","shell.execute_reply":"2022-02-25T15:02:09.760658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:09.762969Z","iopub.execute_input":"2022-02-25T15:02:09.763413Z","iopub.status.idle":"2022-02-25T15:02:09.775536Z","shell.execute_reply.started":"2022-02-25T15:02:09.763370Z","shell.execute_reply":"2022-02-25T15:02:09.774656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',metrics=['accuracy'],optimizer='adam')","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:02:09.777610Z","iopub.execute_input":"2022-02-25T15:02:09.778095Z","iopub.status.idle":"2022-02-25T15:02:09.802233Z","shell.execute_reply.started":"2022-02-25T15:02:09.778053Z","shell.execute_reply":"2022-02-25T15:02:09.797803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Trianing my model\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom datetime import datetime \n\nnum_epochs = 10\nnum_batch_size = 8\n\ncheckpointer = ModelCheckpoint(filepath='saved_models/audio_classification.hdf5', \n                               verbose=1, save_best_only=True)\nstart = datetime.now()\n\nmodel.fit(X_train, y_train, batch_size=num_batch_size, epochs=num_epochs, validation_data=(X_test, y_test), callbacks=[checkpointer], verbose=1)\n\n\nduration = datetime.now() - start\nprint(\"Training completed in time: \", duration)","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:10:20.546025Z","iopub.execute_input":"2022-02-25T15:10:20.546307Z","iopub.status.idle":"2022-02-25T15:11:01.551724Z","shell.execute_reply.started":"2022-02-25T15:10:20.546276Z","shell.execute_reply":"2022-02-25T15:11:01.550799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_accuracy=model.evaluate(X_test,y_test,verbose=0)\nprint(test_accuracy[1])","metadata":{"execution":{"iopub.status.busy":"2022-02-25T15:11:01.654077Z","iopub.execute_input":"2022-02-25T15:11:01.654704Z","iopub.status.idle":"2022-02-25T15:11:01.839185Z","shell.execute_reply.started":"2022-02-25T15:11:01.654658Z","shell.execute_reply":"2022-02-25T15:11:01.838434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_feature.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-24T21:38:45.703516Z","iopub.execute_input":"2022-02-24T21:38:45.703968Z","iopub.status.idle":"2022-02-24T21:38:45.92563Z","shell.execute_reply.started":"2022-02-24T21:38:45.703931Z","shell.execute_reply":"2022-02-24T21:38:45.924444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test[1]","metadata":{"execution":{"iopub.status.busy":"2022-02-24T21:38:46.416319Z","iopub.execute_input":"2022-02-24T21:38:46.416587Z","iopub.status.idle":"2022-02-24T21:38:46.422579Z","shell.execute_reply.started":"2022-02-24T21:38:46.416556Z","shell.execute_reply":"2022-02-24T21:38:46.421837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict_classes(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T21:40:21.569492Z","iopub.execute_input":"2022-02-24T21:40:21.569828Z","iopub.status.idle":"2022-02-24T21:40:21.600469Z","shell.execute_reply.started":"2022-02-24T21:40:21.569789Z","shell.execute_reply":"2022-02-24T21:40:21.598422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename=\"../input/birdclef-2022/test_soundscapes/soundscape_453028782.ogg\"\naudio, sample_rate = librosa.load(filename, res_type='kaiser_fast') \nmfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)\nmfccs_scaled_features = np.mean(mfccs_features.T,axis=0)\n\nprint(mfccs_scaled_features)\nmfccs_scaled_features=mfccs_scaled_features.reshape(1,-1)\nprint(mfccs_scaled_features)\nprint(mfccs_scaled_features.shape)\npredicted_label=model.predict_classes(mfccs_scaled_features)\nprint(predicted_label)\nprediction_class = labelencoder.inverse_transform(predicted_label) \nprediction_class","metadata":{"execution":{"iopub.status.busy":"2022-02-24T21:40:23.913536Z","iopub.execute_input":"2022-02-24T21:40:23.913885Z","iopub.status.idle":"2022-02-24T21:40:24.508547Z","shell.execute_reply.started":"2022-02-24T21:40:23.913832Z","shell.execute_reply":"2022-02-24T21:40:24.505247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-02-24T21:30:29.283264Z","iopub.status.idle":"2022-02-24T21:30:29.284027Z","shell.execute_reply.started":"2022-02-24T21:30:29.283789Z","shell.execute_reply":"2022-02-24T21:30:29.283813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-02-24T21:30:29.277183Z","iopub.status.idle":"2022-02-24T21:30:29.278028Z","shell.execute_reply.started":"2022-02-24T21:30:29.277785Z","shell.execute_reply":"2022-02-24T21:30:29.27781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-02-24T21:30:29.281259Z","iopub.status.idle":"2022-02-24T21:30:29.282218Z","shell.execute_reply.started":"2022-02-24T21:30:29.281913Z","shell.execute_reply":"2022-02-24T21:30:29.281942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}