Skip to content

Instantly share code, notes, and snippets.

@JeffreyMFarley
Last active November 16, 2015 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyMFarley/effdebb216880326e8f3 to your computer and use it in GitHub Desktop.
Save JeffreyMFarley/effdebb216880326e8f3 to your computer and use it in GitHub Desktop.
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