Created
June 11, 2018 19:41
-
-
Save netskink/f1a305215d6a3454e9b6a77a5241b94a to your computer and use it in GitHub Desktop.
sample code from web for machine learning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# In[1]: | |
# Start of code for librosa walkthrough | |
# add autocomplete with TAB | |
# get_ipython().run_line_magic('config', 'IPCompleter.greedy=True') | |
# In[3]: | |
import IPython.display as ipd | |
# In[4]: | |
#ipd.Audio('../data/Train/2022.wav') | |
# | |
# OSX does not have splay but it does have | |
# afplay | |
ipd.Audio('../samples/UrbanSound/data/gun_shot/93139.wav') | |
# In[9]: | |
ipd.Audio('../samples/UrbanSound/data/gun_shot/87562.wav') | |
# In[10]: | |
ipd.Audio('../samples/UrbanSound/data/gun_shot/77247.wav') | |
# In[11]: | |
ipd.Audio('../samples/UrbanSound/data/gun_shot/7060.wav') | |
# In[5]: | |
import librosa | |
# In[6]: | |
data, sampling_rate = librosa.load('../samples/Demo/train/Train/2022.wav') | |
# In[7]: | |
data | |
#sampling_rate | |
# sampling_rate is 22050 | |
# In[9]: | |
get_ipython().run_line_magic('pylab', 'inline') | |
import os | |
import pandas as pd | |
import librosa | |
import librosa.display | |
import glob | |
plt.figure(figsize=(12, 4)) | |
librosa.display.waveplot(data, sr=sampling_rate) | |
# In[14]: | |
train = pd.read_csv('../samples/Demo/train/train.csv') | |
# In[84]: | |
#train | |
# In[17]: | |
i = random.choice(train.index) | |
i | |
# In[21]: | |
data_dir = '../samples/Demo/' | |
# In[19]: | |
audio_name = train.ID[i] | |
# In[23]: | |
path = os.path.join(data_dir, 'Train', str(audio_name) + '.wav') | |
path | |
# In[24]: | |
print('Class: ', train.Class[i]) | |
# In[32]: | |
# orig | |
# x, sr = librosa.load('../data/Train/' + str(train.ID[i]) + '.wav') | |
the_filename = '../samples/Demo/train/Train/' + str(train.ID[i]) + '.wav' | |
x, sr = librosa.load(the_filename) | |
# In[30]: | |
plt.figure(figsize=(12, 4)) | |
# In[31]: | |
librosa.display.waveplot(x, sr=sr) | |
# In[33]: | |
ipd.Audio(the_filename) | |
# In[34]: | |
train.Class.value_counts() | |
# In[35]: | |
test = pd.read_csv('../samples/Demo/test/test.csv') | |
# In[36]: | |
test['Class'] = 'jackhammer' | |
# In[38]: | |
test.to_csv('sub01.csv', index=False) | |
# In[42]: | |
data_dir = '../samples/Demo/train' | |
# In[60]: | |
def parser(row): | |
# function to load files and extract features | |
file_name = os.path.join(os.path.abspath(data_dir), 'Train', str(row.ID) + '.wav') | |
# print(file_name) | |
print('.', end='') | |
# handle exception to check if there isn't a file which is corrupted | |
try: | |
# here kaiser_fast is a technique used for faster extraction | |
X, sample_rate = librosa.load(file_name, res_type='kaiser_fast') | |
# we extract mfcc feature from data | |
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0) | |
except Exception as e: | |
print("Error encountered while parsing file: ", file) | |
return None, None | |
feature = mfccs | |
label = row.Class | |
return [feature, label] | |
# In[61]: | |
temp = train.apply(parser, axis=1) | |
# In[52]: | |
# train | |
# is a dataframe, here is a preview | |
# ID Class | |
# 0 0 siren | |
# 1 1 street_music | |
# 2 2 drilling | |
# In[85]: | |
#str(temp) | |
# In[64]: | |
#temp | |
type(temp) | |
# In[76]: | |
temp.columns = ['feature', 'label'] | |
# In[83]: | |
temp.label | |
# In[77]: | |
temp.head() | |
# In[78]: | |
from sklearn.preprocessing import LabelEncoder | |
# In[79]: | |
X = np.array(temp.feature.tolist()) | |
# In[75]: | |
y = np.array(temp.label.tolist()) | |
# In[ ]: | |
lb = LabelEncoder() | |
# In[ ]: | |
y = np_utils.to_categorical(lb.fit_transform(y)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment