{"metadata":{"language_info":{"mimetype":"text/x-python","version":"3.6.3","pygments_lexer":"ipython3","codemirror_mode":{"version":3,"name":"ipython"},"nbconvert_exporter":"python","file_extension":".py","name":"python"},"kernelspec":{"name":"python3","language":"python","display_name":"Python 3"}},"nbformat_minor":1,"cells":[{"metadata":{"_cell_guid":"25640178-a293-46aa-be36-84cf8296db65","collapsed":true,"_uuid":"b6802625a8b65a130a8bcd758ab4acd42ca68fc0"},"outputs":[],"execution_count":null,"cell_type":"code","source":"import os\nimport pandas as pd\nfrom os.path import join\nfrom scipy.fftpack import fft\nfrom scipy.io import wavfile\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport IPython.display as ipd"},{"metadata":{"_cell_guid":"4c69ee2e-2738-4024-9bb7-f8fc17b2c13c","collapsed":true,"_uuid":"fb97a3f63e118999118e820d5292286fd6e00892"},"outputs":[],"execution_count":null,"cell_type":"code","source":"train_audio_path = '../input/train/audio/'\nlabels = os.listdir(train_audio_path)\ndel labels[0]"},{"metadata":{"_cell_guid":"5af2f623-9a87-4bc1-b603-5c99727320e6","_uuid":"34d07101fd2cac0d3d67be62a80b6f03cac49451"},"outputs":[],"execution_count":null,"cell_type":"code","source":"samples = dict()\nfor label in labels:\n    samples[label] = [f for f in os.listdir(join(train_audio_path, label)) if f.endswith('.wav')]\n    print(label)\n    print(len(samples[label]))"},{"metadata":{"_cell_guid":"4a9431d9-0c2b-4b14-a1a4-517e81f7c780","collapsed":true,"_uuid":"9627d61f5f1a46fa15c5583cf1dee3c4a05d8913"},"outputs":[],"execution_count":null,"cell_type":"code","source":"def custom_fft(y, fs):\n    T = 1.0 / fs\n    N = y.shape[0]\n    yf = fft(y)\n    xf = np.linspace(0.0, 1.0/(2.0*T), N//2)\n    vals = 2.0/N * np.abs(yf[0:N//2])\n    return xf, vals"},{"metadata":{"scrolled":true,"_cell_guid":"543c08a7-673e-42cf-bca3-e4f850389a29","_uuid":"cc2f830b49e0868dab0935502e1520a3c575adff","_kg_hide-input":false,"_kg_hide-output":false},"outputs":[],"execution_count":null,"cell_type":"code","source":"import time\nX = []\ny = []\ncount = 0\nfor label, samp in samples.items():\n    if label == '_background_noise_':\n        continue\n    start = time.time()\n    for wav in samp:\n        rate, row =  wavfile.read(train_audio_path + label + '/' + wav)\n        if rate != 16000 or len(row) !=16000:\n            continue\n        else:\n            xf, vals = custom_fft(row, 16000)\n            X.append(vals)\n            y.append(label)\n    print(label)"},{"metadata":{"scrolled":true,"_cell_guid":"dde1417b-d8f6-4b72-8e5c-6e411766b24f","collapsed":true,"_uuid":"ed44fe3ec24b15e9620de167f717e9799dd6c517"},"outputs":[],"execution_count":null,"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nXf = np.array([x for x,yy in zip(X, y) if yy in labels])\nyf = np.array([yy for x,yy in zip(X, y) if yy in labels])\nXtrain, Xtest, ytrain, ytest = train_test_split(Xf, yf, test_size=0.4)"},{"metadata":{"_cell_guid":"40622913-d2d4-462f-8ce0-4c8f19fe3ccd","collapsed":true,"_uuid":"c7e4bd75165a2f80fa6134535bc53fa9905ff854"},"outputs":[],"execution_count":null,"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nfrom sklearn.decomposition import PCA\nfrom sklearn.svm import LinearSVC\ncomponents=101\nscaler = StandardScaler()\npca = PCA(n_components=components)\nsvm = LinearSVC()"},{"metadata":{"_cell_guid":"1eb4b4f9-cd9c-4053-ba47-304431e55432","_uuid":"c87e3000dfba9b8345182a16f8a0b40dda0237fd"},"outputs":[],"execution_count":null,"cell_type":"code","source":"import time\nfrom sklearn.pipeline import Pipeline\npipe=dict()\nfor label in labels:\n    if label == '_background_noise_':\n            continue\n    print(label)\n    start = time.time()\n    y_transformed = [1 if yy==label else 0 for yy in ytrain]\n    y_transformed_test=np.array(ytest==label, dtype=int)\n    pipe[label] = Pipeline([('scaler', StandardScaler()),\n                ('pca', PCA(n_components=components)),\n                ('svm', LinearSVC())])\n    pipe[label].fit(Xtrain, y_transformed)\n    print(time.time() - start)\n    print(pipe[label].score(Xtrain, y_transformed))\n    print(pipe[label].score(Xtest, y_transformed_test))"},{"metadata":{"_cell_guid":"6def6940-caaf-40ce-a07c-ba99bcd07d9a","collapsed":true,"_uuid":"0fb16d7a3128fe8e9abff14177427f9279278fb5"},"outputs":[],"execution_count":null,"cell_type":"code","source":"from sklearn.base import BaseEstimator\nfrom sklearn.base import ClassifierMixin"},{"metadata":{"_cell_guid":"378af82a-4e10-45c2-b3f1-56788cef2aa3","collapsed":true,"_uuid":"1e32523aa40bdad457c7745fc24af7b523153786"},"outputs":[],"execution_count":null,"cell_type":"code","source":"class Ensemble(BaseEstimator, ClassifierMixin):\n    \n    def __init__(self, clfs, weights, labels):\n        self.clfs = clfs\n        self.w = np.array(weights)\n        self.labels = labels\n        \n    def R(self, x):\n        if sum(x) == 0:\n            return np.argmin(self.w)\n        else:\n            return np.argmax(self.w*x)\n        \n    def fit(self, x, y):\n        \n        pass\n    \n    def predict(self, x):\n        res = []\n        for label in self.labels:\n            res.append(self.clfs[label].predict(x))\n        yy = []\n        for up, zero, one, bird, yes, on, off, house, six, sheila, bed, marvin,\n        nine, stop, cat, down, dog, four, tree, happy, three, five, go, seven,\n        left, wow, two, eight, right in zip(*res):\n            yy.append(self.R([up, zero, one, bird, yes, on, off, house, six, sheila,\n                              bed, marvin, nine, stop, cat, down, dog, four, tree,\n                              happy, three, five, go, seven, left, wow, two, eight, right]))\n        return np.array([self.labels[y] for y in yy])\n"},{"metadata":{"_cell_guid":"389275f0-1b1e-4884-aec6-1728ab3af020","collapsed":true,"_uuid":"f0a6ae7726ee2ce4eae5b8ae79f98f0dd4d95f04"},"outputs":[],"execution_count":null,"cell_type":"code","source":"labels_check= labels\ndel labels_check[labels.index['_background_noise_']]\nclas = Ensemble(pipe, np.ones(len(labels_check)),labels_check)"},{"metadata":{"_cell_guid":"31cb5fe1-7850-4473-a337-6fff135da676","collapsed":true,"_uuid":"9451836484348ec8abcc24710db713aa9057bc2b"},"outputs":[],"execution_count":null,"cell_type":"code","source":"clas.predict(Xtest)"},{"metadata":{"_cell_guid":"7ea3246b-40f0-4451-8402-a039bf6dd196","collapsed":true,"_uuid":"5d15aff30f2ca47ddf0d98b2e5272161299a9a6c"},"outputs":[],"execution_count":null,"cell_type":"code","source":"clas.score(Xtest, ytest)"},{"metadata":{"_cell_guid":"5b128535-bfee-43ee-8255-5e1a665b3c92","collapsed":true,"_uuid":"4315dc5f7117db50f89813b56a266eda8017183f"},"outputs":[],"execution_count":null,"cell_type":"code","source":""}],"nbformat":4}