Skip to content

Instantly share code, notes, and snippets.

@ellisbrown
ellisbrown / song2playlist.py
Last active February 17, 2019 19:40
Finds which of your Spotify playlists a song is in.
import sys
from collections import defaultdict
from tqdm import tqdm
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
CLIENT_ID = "9f64faf742764cf48d556ecc64ea2b1e"
CLIENT_SECRET = "56c58af2d3d845beb7102a466dd4f8a7"
USERNAME = ""
@ellisbrown
ellisbrown / name2tensor.py
Last active April 17, 2019 07:35
Name2Gender Char-RNN name tensor converter
def name_to_tensor(name, cuda=False):
"""converts a name to a vectorized numerical input for use with a nn
each character is converted to a one hot (n, 1, 26) tensor
Args:
name (string): first name (e.g., "Ellis")
Return:
tensor (torch.tensor)
"""
name = clean_str(name)
tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
@ellisbrown
ellisbrown / rnn.py
Last active December 26, 2017 19:56
Name2Gender Char-RNN model definition
class RNN(nn.Module):
"""Recurrent Neural Network
original source: https://goo.gl/12wiKB
Simple implementation of an RNN with two linear layers and a LogSoftmax
layer on the output
Args:
input_size: (int) size of data
hidden_size: (int) number of hidden units
output_size: (int) size of output
"""
@ellisbrown
ellisbrown / gender_features.py
Last active April 18, 2018 20:52
Name2Gender Naïve-Bayes
def gender_features(name):
features = {}
features["last_letter"] = name[-1].lower()
features["first_letter"] = name[0].lower()
# names ending in -yn are mostly female, names ending in -ch are mostly male, so add 3 more features
features["suffix2"] = name[-2:]
features["suffix3"] = name[-3:]
features["suffix4"] = name[-4:]
return features