In [2]:
%load_ext watermark
%watermark -d -v -a 'Sebastian Raschka'


Sebastian Raschka 10/12/2014 

CPython 3.4.2
IPython 2.3.1



Assigning Majority Mood Labels

After classification (regarding the web app), a user can leave feedback whether he or she agrees with or not to add a new mood label to the databse. The goal is to collect multiple opinions for particular songs and update the ground truth label by majority rule.


In [ ]:
# Adding a new majority rule column to the database
import sqlite3

conn = sqlite3.connect('../../dataset/training/growing_training.sqlite')
c = conn.cursor()
c.execute('ALTER TABLE moodtable ADD COLUMN majoritymood TEXT;')
conn.commit()

In [24]:
# Get the majority-rule-based mood label

from collections import Counter

test1 = 'happy,happy,sad,happy,sad'

count = Counter(test1.split(','))
print(count.most_common(1))


[('happy', 3)]

In [33]:
# Update the database

c.execute('SELECT rowid FROM moodtable')
rowids = c.fetchall()
for i in rowids:
    c.execute('SELECT mood from moodtable WHERE rowid=?;', i)
    mood = c.fetchone()
    mood = Counter(mood[0].split(',')).most_common(1)[0][0]
    query = 'UPDATE moodtable SET majoritymood=? WHERE rowid=?;'
    data = [mood, i[0]]
    c.execute(query, data)
conn.commit()
conn.close()

In [4]:
# Show the results

import pandas as pd
import sqlite3

conn = sqlite3.connect('../../dataset/training/growing_training.sqlite')
print(pd.read_sql('SELECT * FROM moodtable', conn).tail())
conn.close()


                    artist                   title  \
1330  florence the machine       dog days are over   
1331            ed sheeran                    fire   
1332         dream theater  breaking all illusions   
1333              coldplay                     ink   
1334                    u2    it's a beautiful day   

                                                 lyrics   mood majoritymood  
1330  Happiness, it hurt like a train on a track\nCo...  happy        happy  
1331  The rain won't stop falling\nIt's harder than ...    sad          sad  
1332  With the sun in place, there's a test of faith...  happy        happy  
1333  Got a tattoo that said "2gether thru life"\r\n...  happy        happy  
1334  The heart is a bloom\nShoots up through stony ...  happy        happy  



Randomly select a happy or sad song


In [12]:
conn = sqlite3.connect('../../dataset/training/growing_training.sqlite')

sql = "SELECT artist,title FROM moodtable WHERE majoritymood='happy' ORDER BY RANDOM() LIMIT 1;"
cursor = conn.cursor()
cursor.execute(sql)
result = cursor.fetchone()
artistname = result[0]#result[1].decode('utf-8')
songtitle = result[1]#result[2].decode('utf-8')
print(artistname, songtitle)

conn.close()


faith hill red umbrella

In [ ]:
my_dir = '/Users/sebastian/github/www.sebastianraschka.com/Webapps/musicmood_pythonanywhere_local/

le = pickle.load(open(os.path.join(my_dir, 'lyrics_label_encoder.p'), 'rb'))
tfidf = pickle.load(open(os.path.join(my_dir, 'lyrics_tfidf_noporter.p'), 'rb'))
clf = pickle.load(open(os.path.join(my_dir, 'lyrics_clf.p'), 'rb')) # trained on no porter