Last active
November 16, 2015 16:23
-
-
Save JeffreyMFarley/effdebb216880326e8f3 to your computer and use it in GitHub Desktop.
This file contains 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
def similar_songs(): | |
import csv | |
vector = ['acousticness', 'danceability', 'energy', | |
'instrumentalness', 'liveness', 'speechiness', | |
'valence'] # minus 'bpm & 'key' | |
underworld = 'underworld.txt' | |
mbm = 'mbm.txt' | |
orbital = 'orbital.txt' | |
cut_copy = 'cut_copy.txt' | |
all = [] | |
for fileName in [cut_copy, mbm, orbital]: | |
with open(fileName) as f: | |
reader = csv.DictReader(f, dialect=csv.excel_tab) | |
all.extend(list(reader)) | |
tree = KDTree.fromTable(all, vector, ['title', 'artist']) | |
with open(underworld) as f: | |
reader = csv.DictReader(f, dialect=csv.excel_tab) | |
for song in reader: | |
feature = [float(song[v]) if v in song else 0. for v in vector] | |
points, labels, distance = tree.nearest_neighbor(feature) | |
s = '{0} -> {1}\n {2}\n {3}\n'.format(song['title'], labels, feature, points) | |
print(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment