Commit dfc7a04b authored by Steve Tjoa's avatar Steve Tjoa

edits to day 2 exercise: knn

parent d2d27546
{
"metadata": {
"name": "",
"signature": "sha256:c18acfb58e847f8024390a467d0b4ecd17445242efddf05612ef24a19c90e563"
"signature": "sha256:eaf3c7682d4b7de77074a247b83c31a5dd8257441eb59f23d9951f0ae352f13e"
},
"nbformat": 3,
"nbformat_minor": 0,
......@@ -344,7 +344,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
"prompt_number": 1
},
{
"cell_type": "markdown",
......@@ -363,7 +363,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
"prompt_number": 2
},
{
"cell_type": "code",
......@@ -375,7 +375,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
"prompt_number": 3
},
{
"cell_type": "markdown",
......@@ -397,13 +397,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"prompt_number": 5,
"text": [
"'https://ccrma.stanford.edu/workshops/mir2014/audio/drum%20samples/snares/SNARE_01_01.WAV'"
]
}
],
"prompt_number": 13
"prompt_number": 5
},
{
"cell_type": "markdown",
......@@ -418,11 +418,13 @@
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"from essentia.standard import ZeroCrossingRate\n",
"zcr = ZeroCrossingRate()\n",
"\n",
"\n",
"number_of_files = 20\n",
"number_of_features = 5\n",
"features_snare = np.zeros([number_of_files, number_of_features])"
"features_snare = []"
],
"language": "python",
"metadata": {},
......@@ -456,16 +458,193 @@
"collapsed": false,
"input": [
"import urllib\n",
"from essentia.standard import MonoLoader\n",
"from essentia.standard import MonoLoader, ZeroCrossingRate, Centroid, CentralMoments, DistributionShape, Spectrum, Windowing\n",
"hamming_window = Windowing(type='hamming')\n",
"zcr = ZeroCrossingRate()\n",
"centroid = Centroid()\n",
"spectrum = Spectrum()\n",
"centralmoments = CentralMoments()\n",
"distributionshape = DistributionShape()\n",
"\n",
"fs = 44100\n",
"features_table = []\n",
"for url in snare_file_list + kick_file_list:\n",
" urllib.urlretrieve(url, filename='/tmp/localfile.wav')\n",
" audio = MonoLoader(filename = '/tmp/localfile.wav', sampleRate = fs)() \n",
" frame = audio[:int(0.2*fs)]\n",
" feature_vector = []\n",
" feature_vector.append( zcr(frame) )\n",
" spectral_magnitude = spectrum(hamming_window(frame))\n",
" feature_vector.append( centroid(spectral_magnitude) )\n",
" spectral_moments = distributionshape(centralmoments(spectral_magnitude))\n",
" feature_vector.extend( spectral_moments )\n",
" features_table.append( feature_vector )\n",
"\n",
"\n",
"sample_rate = 44100\n",
"urllib.urlretrieve(snare_URL, filename='/tmp/localfile.wav')\n",
"audio = MonoLoader(filename = '/tmp/localfile.wav', sampleRate = sample_rate)()"
"features_table = array(features_table)\n",
"print features_table"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[ 1.62471652e-01 2.36405745e-01 4.00112346e-02 7.53566146e-01\n",
" -1.77525520e-01]\n",
" [ 1.16666667e-01 2.21813351e-01 3.49240005e-02 6.74486637e-01\n",
" -4.22939777e-01]\n",
" [ 1.21882088e-01 2.16630742e-01 3.44936140e-02 6.97950125e-01\n",
" -4.12385702e-01]\n",
" [ 9.18367356e-02 2.04460323e-01 3.14886980e-02 8.07589829e-01\n",
" -6.42979145e-02]\n",
" [ 1.61904767e-01 2.71532953e-01 4.19509038e-02 5.50324261e-01\n",
" -4.77055550e-01]\n",
" [ 1.30045354e-01 2.28676051e-01 3.80903184e-02 8.45433593e-01\n",
" -2.10921764e-02]\n",
" [ 1.40249431e-01 2.51602560e-01 4.26780917e-02 7.59979486e-01\n",
" -2.06123590e-01]\n",
" [ 1.52721092e-01 2.53082067e-01 4.44976576e-02 7.05894053e-01\n",
" -2.62273788e-01]\n",
" [ 1.33673474e-01 2.60320574e-01 4.03219834e-02 6.03728771e-01\n",
" -3.55994463e-01]\n",
" [ 1.17913835e-01 2.16710851e-01 3.01928632e-02 8.90169322e-01\n",
" 4.26502228e-01]\n",
" [ 6.80272095e-03 5.47488108e-02 1.58221014e-02 3.10297251e+00\n",
" 1.05381603e+01]\n",
" [ 5.44217695e-03 3.13566811e-02 7.53624039e-03 4.57224989e+00\n",
" 2.59836254e+01]\n",
" [ 4.08163248e-03 1.71935484e-02 2.87185842e-03 7.22230673e+00\n",
" 7.60733871e+01]\n",
" [ 4.30838997e-03 4.21788730e-02 1.12768942e-02 3.60826254e+00\n",
" 1.51811485e+01]\n",
" [ 6.12244895e-03 3.37548926e-02 1.01857409e-02 4.76424742e+00\n",
" 2.65034351e+01]\n",
" [ 5.55555569e-03 2.49018725e-02 6.05471013e-03 5.12525940e+00\n",
" 3.25076065e+01]\n",
" [ 5.32879820e-03 2.85752080e-02 7.12108333e-03 5.20154762e+00\n",
" 3.39053345e+01]\n",
" [ 4.30838997e-03 3.50905433e-02 9.30536166e-03 4.14755917e+00\n",
" 2.00502682e+01]\n",
" [ 5.89569146e-03 3.92675437e-02 8.93500820e-03 3.75893021e+00\n",
" 1.77870655e+01]\n",
" [ 5.78231271e-03 2.72752997e-02 7.65201543e-03 5.09223413e+00\n",
" 3.01892776e+01]]\n"
]
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from sklearn import preprocessing\n",
"import numpy as np\n",
"\n",
"scaler = preprocessing.MinMaxScaler(feature_range = (-1, 1))\n",
"training_features = scaler.fit_transform(features_table)\n",
"\n",
"scaler.get_params()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 33,
"text": [
"{'copy': True, 'feature_range': (-1, 1)}"
]
}
],
"prompt_number": 33
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from sklearn.neighbors import KNeighborsClassifier\n",
"\n",
"model_snare = KNeighborsClassifier(n_neighbors = 1)\n",
"model_snare.fit(training_features, concatenate([zeros(10), ones(10)]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 29,
"text": [
"KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
" n_neighbors=1, p=2, weights='uniform')"
]
}
],
"prompt_number": 29
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print model_snare.predict(training_features[-1])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 1.]\n"
]
}
],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
......
{
"metadata": {
"name": "",
"signature": "sha256:6bc45d7d702f477a92150907ff7b603393084a9cb41c05f77a201d080fa1e30f"
"signature": "sha256:54325d7e3e47937b6b8ae7289c0ca91f6c55e20581cdc44be41e66bf323c0907"
},
"nbformat": 3,
"nbformat_minor": 0,
......@@ -217,6 +217,14 @@
"\n",
"Now, test out with the examples using https://ccrma.stanford.edu/workshops/mir2014/TestKicksCorpus.txt and https://ccrma.stanford.edu/workshops/mir2014/TestSnaresCorpus.txt. These are real-world testing samples\u2026"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment