<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Speech recognition
Simple sppeech recognition system can be implemented using DTW + MFCC.
Step1:  We will use the data-speech-commands database composed of 105,000 WAVE audio files of people saying thirty different words. We will use only a subset of this database.
We assume that you have previously downloaded and extracted the database. You need to specify the path to the folder where you extracted it.
Step2:  Precompute all MFCCs
Step3:  Prepare train/val dataset
Step4:  Leave P Out Cross Validation
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import os
import glob
import librosa
import numpy as np
Explanation: Speech recognition
Simple sppeech recognition system can be implemented using DTW + MFCC.
End of explanation
DATABASE_PATH = '/Users/pierrerouanet/Downloads/data-speech_commands_v0.02'
labels = {'cat', 'dog', 'house', 'happy', 'zero'}
labels
# We will use only N occurences per word
N = 25
Explanation: We will use the data-speech-commands database composed of 105,000 WAVE audio files of people saying thirty different words. We will use only a subset of this database.
We assume that you have previously downloaded and extracted the database. You need to specify the path to the folder where you extracted it.
End of explanation
mfccs = []
true_labels = []
for l in labels:
    sounds = glob.glob(os.path.join(DATABASE_PATH, l, '*.wav'))
    np.random.shuffle(sounds)
    sounds = sounds[:N]
    
    for s in sounds:
        y, sr = librosa.load(s)
        mfcc = librosa.feature.mfcc(y, sr, n_mfcc=13)
        mfccs.append(mfcc.T)
        true_labels.append(l)
        
mfccs = np.array(mfccs)
true_labels = np.array(true_labels)
Explanation: Precompute all MFCCs
End of explanation
val_percent = 0.2
n_val = int(val_percent * len(true_labels))
I = np.random.permutation(len(true_labels))
I_val, I_train = I[:n_val], I[n_val:]
Explanation: Prepare train/val dataset
End of explanation
from dtw import dtw
def cross_validation(train_indices, val_indices):
    score = 0.0
    for i in val_indices:
        x = mfccs[i]
        dmin, jmin = np.inf, -1
        for j in train_indices:
            y = mfccs[j]
            d, _, _, _ = dtw(x, y, dist=lambda x, y: np.linalg.norm(x - y, ord=1))
            if d < dmin:
                dmin = d
                jmin = j
        score += 1.0 if (true_labels[i] == true_labels[jmin]) else 0.0
        
    return score / len(val_indices)
rec_rate = cross_validation(I_train, I_val)
print('Recognition rate {}%'.format(100. * rec_rate))
Explanation: Leave P Out Cross Validation
End of explanation
<END_TASK>