<jupyter_start><jupyter_text>Extract features from the dataset and dump these features into a binary .dat file **my.dat**<jupyter_code>directory = "./archive/Data/genres_original/"
f= open("my.dat" ,'wb')
i=0
for folder in os.listdir(directory):
    i+=1
    if i==11 :
        break   
    for file in os.listdir(directory+folder):
        try:
            (rate,sig) = wav.read(directory+folder+"/"+file)
            mfcc_feat = mfcc(sig,rate ,winlen=0.020, appendEnergy = False)
            covariance = np.cov(np.matrix.transpose(mfcc_feat))
            mean_matrix = mfcc_feat.mean(0)
            feature = (mean_matrix , covariance , i)
            pickle.dump(feature , f)
        except:
            print(file, "is a corrupt file.")
f.close()
dataset = []
def loadDataset(filename , split , trSet , teSet):
    with open("my.dat" , 'rb') as f:
        while True:
            try:
                dataset.append(pickle.load(f))
            except EOFError:
                f.close()
                break  

    for x in range(len(dataset)):
        if random.random() <split :      
            trSet.append(dataset[x])
        else:
            teSet.append(dataset[x])  
trainingSet = []
testSet = []
loadDataset("my.dat" , 0.66, trainingSet, testSet)
leng = len(testSet)
predictions = []
for x in range (leng):
    predictions.append(nearestClass(getNeighbors(trainingSet ,testSet[x] , 10))) 

accuracy1 = getAccuracy(testSet , predictions)
print(accuracy1)
leng = len(testSet)
predictions = []
for x in range (leng):
    predictions.append(nearestClass(getNeighbors(trainingSet ,testSet[x] , 8))) 

accuracy1 = getAccuracy(testSet , predictions)
print(accuracy1)
leng = len(testSet)
predictions = []
for x in range (leng):
    predictions.append(nearestClass(getNeighbors(trainingSet ,testSet[x] , 6))) 

accuracy1 = getAccuracy(testSet , predictions)
print(accuracy1)
leng = len(testSet)
predictions = []
for x in range (leng):
    predictions.append(nearestClass(getNeighbors(trainingSet ,testSet[x] , 4))) 

accuracy1 = getAccuracy(testSet , predictions)
print(accuracy1)
leng = len(testSet)
predictions = []
for x in range (leng):
    predictions.append(nearestClass(getNeighbors(trainingSet ,testSet[x] , 5))) 

accuracy1 = getAccuracy(testSet , predictions)
print(accuracy1)<jupyter_output>0.7026239067055393
{'fraction_non_alphanumeric': 0.12757731958762886, 'fraction_numerical': 0.019329896907216496, 'mean_word_length': 3.257769652650823, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '575184', 'license': 'MIT', 'n_tokens_mistral': 816, 'n_tokens_neox': 778, 'n_words': 167, 'path': 'KNearestNeighbour.ipynb', 'repo_name': 'ashutosh321607/Music-Genre-Classification', 'size': '2328'}