挖掘暗信息:分析用户收藏的歌曲的歌手对产生推荐列表产生的影响

缘由

如果某个用户偏爱某一位歌手的歌曲,那么我们调高这位歌手的歌曲在推荐列表里的比重。显然是非常有意义的。
那么,我怎么知道用户是否偏爱某一位歌手呢?就是计算这个用户收藏的歌曲的歌手的熵。
熵是无序程度,值越大混乱程度越大,如果值为0的话,就表示没有混乱,用户只喜欢一个歌手的歌。那么我来看看熵值到底和歌手有什么关系。


计算熵

比如用户收藏了10首刘德华的歌曲,计算熵的代码:
singer={'刘德华':10}
#该函数接受一个字典数据集
#字典以歌手为键,以其出现的次数为值
#如:{刘德华:0}
#,然后计算其混杂程度。使用熵来计算
#熵遍历所有可能的结果的概率除以总次数的概率p,然后将所有的p做计算:p*log(p),再将所有的这个结果加起来
def entropy(results):
    from math import log
    log2=lambda x:log(x)/log(2)
    #计算熵
    counts=sum(singer.values())
    ent=0.0
    for r in results.keys():
        p=float(results[r])/counts
        ent=ent-p*log2(p)
    return ent#熵越大,混乱度越高,如此,一个集合都的结果都一样的话,那么熵应该为0
执行代码该函数得到结果:
0.0
这是一种非常专一的情况。说明所有为0的用户,都对某一歌手有着偏爱。


再看,十位不同的歌手:
singer={'刘0':1,
        '刘1':1,
        '刘2':1,
        '刘3':1,
        '刘4':1,
        '刘5':1,
        '刘6':1,
        '刘7':1,
        '刘8':1,
        '刘9':1
        } 
结果:
>>> 
3.32192809489
>>> 
再看,五位不同的歌手:
singer={'刘0':1,
        '刘1':1,
        '刘2':1,
        '刘3':1,
        '刘4':1,
        } 
结果:
>>> 
2.32192809489
>>> 
再看,两位歌手,但次数不同
singer={'刘0':9,
        '刘1':1,
        }  
结果:
>>> 
0.468995593589
>>> 

那么我大概有点感觉了,比如貌似小于1的还是很有偏爱的感觉哈。
那么我觉得小实验这样做下去是无穷无尽的,具体熵为多少才能体现对歌手的偏爱呢?我觉得我现在可以直接把计算了收藏了那一万首歌曲的每个用户的熵算出来之后,我再确定熵值为多少才表示对歌手有偏爱,这样比较科学。


熵值存储

既然每个用户存储这样一个熵值,而且我们做实验的时候,用户收集的歌曲是不会发生变化的。所以我决定在在user表里增加一个float字段。而且我为该列取名为:FSSE:favorties-song-singer-entropy,即为喜欢的歌曲的歌手的熵。
当然,我不会算所有用户的熵值,我只会算:收藏了1万歌曲的用户的收藏的歌的歌手的熵值。那一万首歌就是计算了相似度的歌。经过我查询那样用户也太多了,我决定只计算收藏歌曲数量在70首以上的,那么用户数量为2297
SQL语句如下:
SELECT userid
FROM moresmallfavorites
GROUP BY userid
HAVING COUNT( userid ) >70

整体步骤

好的,到这里想法就已经比较成型了,我觉得简单叙述一下步骤:

  1. 为user表添加一列,字段名为FSSE,float型
  2. 找出需要计算熵值的用户,供2297位。
  3. 找到这些用户的收藏列表
  4. 从收藏列表中获得歌曲id
  5. 通过歌曲id拿到歌手名
  6. 针对每一个用户计算其收藏歌曲的歌手的熵值
  7. 将熵值插入user表的FSSE字段
  8. 然后我再根据这2297个数据确定,一个数值,低于此数值表示对某歌手有偏爱
  9. 再产生推荐列表时,计算出该用户熵值,低于上一条的数值,那么就会调高某歌手的歌曲在推荐列表中的比例。

此外,还可以做些额外的分析

  • 再将这2297位用户,以其收藏的歌数为x轴,以其熵值为y轴,做出点画出一幅图来,这幅图应该能够观察一点有利用价值的东西。

执行


首先第一步为user表添加一列,字段名为FSSE,float型:
sql语句如下:
alter table user add column FSSE float
查询某一个用户收藏的歌曲的歌手,以用户id为3为例:
select singer from music inner join (
select musicid as mid from moresmallfavorites where userid=3
) a 
ON music.id = a.mid

这里我还制作了singer表。见 总结部分。有singer表后,我们就可以用singer的id为键,而不是歌手名了,而且对歌手名做了更好地统一和管理。
剩余步骤直接用代码完成:
def countEntropy():
    try:
        singer={}
        conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='musicrecomsys',port=3306,charset="gb2312") # charset="gb2312"为了能够打印出中文而添加的
        #用拿到执行sql语句的对象  
        cur1=conn.cursor()
        cur2=conn.cursor()
        count1=cur1.execute('select userid from moresmallfavorites GROUP BY userid HAVING count(userid)>70')  
        results1=cur1.fetchall()
        count=0
        print '读取用户数据....'
        for r1 in results1:
            singer.clear()
            count2=cur2.execute('select singerid from music inner join (select musicid as mid from moresmallfavorites where userid=%s) a ON music.id = a.mid',[r1[0]])
            results2=cur2.fetchall()
            for r2 in results2:
                flag=singer.setdefault(r2[0],0)
                #如果等于0,说明第一次出现这个字段。不等于0说明已经有了这个字段了,所以多加一个
                if flag==0:singer[r2[0]]=1
                else:singer[r2[0]]=flag+1
            count2=cur2.execute('UPDATE user SET FSSE = %s WHERE id = %s',[entropy(singer),r1[0]])
            count+=1
            if count%100==0:print '剩余',(count1-count),'总共:',count
        cur1.close()
        cur2.close()
        conn.commit()#必须要有这个提交事务,否则不能正确插入
        conn.close()  
    except MySQLdb.Error,e:  
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])

结果

我们直接来在数据库里看了一下结果,如下图所示:


结果发现数据库里看很不方便,所以我写个函数方便查看结果,我也思考是否方便用sql语句来写,但是有点麻烦:

import MySQLdb 
import operator#为了让字典以值排序
#写一个函数方便查看熵的结果
def showEntropyResults():
    try:
        singer={}
        conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='musicrecomsys',port=3306,charset="gb2312") #charset="gb2312"为了能够打印出中文而添加的
        #用拿到执行sql语句的对象  
        cur1=conn.cursor()
        cur2=conn.cursor()
        count1=cur1.execute('SELECT fsse, id FROM user WHERE fsse <5 ORDER BY fsse')#决定输出熵为多少以下,或者什么范围
        results1=cur1.fetchall()
        count=0
        print '读取用户数据....'
        for r1 in results1:
            singer.clear()
            count2=cur2.execute('select singerid from music inner join (select musicid as mid from moresmallfavorites where userid=%s) a ON music.id = a.mid',[r1[1]])
            results2=cur2.fetchall()
            for r2 in results2:
                flag=singer.setdefault(r2[0],0)
                #如果等于0,说明第一次出现这个字段。不等于0说明已经有了这个字段了,所以多加一个
                if flag==0:singer[r2[0]]=1
                else:singer[r2[0]]=flag+1
            sortedSinger=sorted(singer.iteritems(), key=operator.itemgetter(1),reverse=True)#对字典以value进行排序
            print '--------------------------------------------------'
            print '熵为:',r1[0],'用户id:',r1[1],'singer列表如下:'
            print sortedSinger
        cur1.close()
        cur2.close()
        conn.commit()#必须要有这个提交事务,否则不能正确插入
        conn.close()  
    except MySQLdb.Error,e:  
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])  

执行该函数并可查看结果,我抽取了部分:
--------------------------------------------------
熵为: 0.146094 用户id: 12798 singer列表如下:
[(35L, 94), (1070L, 2)]
--------------------------------------------------
熵为: 0.319501 用户id: 7417 singer列表如下:
[(2L, 68), (17L, 1), (437L, 1), (4527L, 1)]
--------------------------------------------------
熵为: 4.98225 用户id: 2538 singer列表如下:
[(6L, 14), (129L, 8), (1117L, 3), (1555L, 3), (73L, 2), (1015L, 2), (516L, 1), (8L, 1), (523L, 1), (268L, 1), (275L, 1), (9623L, 1), (408L, 1), (4L, 1), (27L, 1), (1564L, 1), (798L, 1), (416L, 1), (801L, 1), (1315L, 1), (166L, 1), (41L, 1), (1324L, 1), (430L, 1), (1200L, 1), (30L, 1), (318L, 1), (65L, 1), (322L, 1), (267L, 1), (586L, 1), (207L, 1), (336L, 1), (213L, 1), (1753L, 1), (474L, 1), (603L, 1), (864L, 1), (425L, 1), (355L, 1), (1638L, 1), (9062L, 1), (681L, 1), (1900L, 1), (3317L, 1), (122L, 1), (4987L, 1), (507L, 1)]
--------------------------------------------------
熵为: 4.98475 用户id: 11146 singer列表如下:
[(44L, 39), (35L, 8), (220L, 7), (175L, 6), (56L, 5), (319L, 5), (95L, 5), (2L, 4), (798L, 4), (93L, 4), (6L, 3), (14L, 3), (32L, 3), (206L, 3), (324L, 3), (78L, 3), (3L, 2), (5L, 2), (43L, 2), (172L, 2), (84L, 2), (129L, 1), (130L, 1), (392L, 1), (138L, 1), (783L, 1), (912L, 1), (17L, 1), (147L, 1), (149L, 1), (1176L, 1), (68L, 1), (410L, 1), (7076L, 1), (156L, 1), (29L, 1), (36L, 1), (134L, 1), (38L, 1), (221L, 1), (606L, 1), (310L, 1), (300L, 1), (1216L, 1), (71L, 1), (2504L, 1), (1356L, 1), (333L, 1), (846L, 1), (207L, 1), (336L, 1), (1773L, 1), (88L, 1), (1753L, 1), (475L, 1), (222L, 1), (272L, 1), (749L, 1), (104L, 1), (194L, 1), (107L, 1), (493L, 1), (2550L, 1)]
--------------------------------------------------
熵为: 6.00173 用户id: 13935 singer列表如下:
[(94L, 13), (78L, 10), (66L, 8), (550L, 7), (13L, 6), (23L, 6), (44L, 6), (74L, 6), (93L, 6), (6L, 5), (245L, 5), (198L, 4), (2L, 3), (5L, 3), (9L, 3), (35L, 3), (91L, 3), (95L, 3), (600L, 3), (1172L, 3), (152L, 3), (319L, 3), (344L, 3), (3L, 2), (21L, 2), (73L, 2), (83L, 2), (104L, 2), (114L, 2), (115L, 2), (161L, 2), (698L, 2), (190L, 2), (237L, 2), (284L, 2), (10L, 1), (17L, 1), (534L, 1), (25L, 1), (4634L, 1), (28L, 1), (33L, 1), (39L, 1), (41L, 1), (47L, 1), (54L, 1), (56L, 1), (58L, 1), (65L, 1), (67L, 1), (68L, 1), (82L, 1), (598L, 1), (88L, 1), (102L, 1), (106L, 1), (111L, 1), (139L, 1), (153L, 1), (9394L, 1), (696L, 1), (189L, 1), (2248L, 1), (202L, 1), (216L, 1), (38L, 1), (249L, 1), (2815L, 1), (271L, 1), (798L, 1), (1324L, 1), (306L, 1), (310L, 1), (329L, 1), (332L, 1), (338L, 1), (355L, 1), (9069L, 1), (367L, 1), (373L, 1), (386L, 1), (749L, 1), (402L, 1), (430L, 1), (451L, 1), (9165L, 1), (978L, 1), (476L, 1), (2545L, 1), (505L, 1)]
--------------------------------------------------
熵为: 6.00033 用户id: 12301 singer列表如下:
[(2L, 9), (271L, 5), (16L, 4), (3L, 3), (6L, 3), (12L, 3), (174L, 3), (78L, 3), (1172L, 2), (22L, 2), (91L, 2), (80L, 2), (724L, 2), (624L, 2), (603L, 2), (7821L, 1), (389L, 1), (1641L, 1), (9L, 1), (4531L, 1), (272L, 1), (401L, 1), (402L, 1), (403L, 1), (8329L, 1), (537L, 1), (795L, 1), (798L, 1), (31L, 1), (672L, 1), (33L, 1), (1606L, 1), (177L, 1), (3116L, 1), (45L, 1), (3376L, 1), (561L, 1), (94L, 1), (179L, 1), (606L, 1), (219L, 1), (55L, 1), (440L, 1), (698L, 1), (489L, 1), (319L, 1), (322L, 1), (195L, 1), (324L, 1), (197L, 1), (198L, 1), (15L, 1), (140L, 1), (2935L, 1), (463L, 1), (81L, 1), (82L, 1), (1763L, 1), (5635L, 1), (1324L, 1), (600L, 1), (1113L, 1), (527L, 1), (93L, 1), (1630L, 1), (479L, 1), (864L, 1), (1506L, 1), (483L, 1), (615L, 1), (232L, 1), (361L, 1), (2855L, 1), (117L, 1), (1014L, 1), (1015L, 1), (123L, 1), (765L, 1)]
--------------------------------------------------
熵为: 8.42014 用户id: 9091 singer列表如下:
[(175L, 8), (144L, 7), (499L, 7), (70L, 6), (232L, 6), (307L, 6), (479L, 6), (6L, 5), (106L, 5), (194L, 5), (264L, 5), (474L, 5), (591L, 5), (58L, 4), (84L, 4), (168L, 4), (195L, 4), (386L, 4), (452L, 4), (476L, 4), (749L, 4), (1088L, 4), (9L, 3), (17L, 3), (18L, 3), (117L, 3), (219L, 3), (221L, 3), (291L, 3), (315L, 3), (361L, 3), (369L, 3), (416L, 3), (419L, 3), (430L, 3), (488L, 3), (525L, 3), (550L, 3), (613L, 3), (641L, 3), (645L, 3), (652L, 3), (731L, 3), (750L, 3), (878L, 3), (2892L, 3), (991L, 3), (1140L, 3), (1506L, 3), (1804L, 3), (2L, 2), (5L, 2), (12L, 2), (21L, 2), (30L, 2), (44L, 2), (64L, 2), (65L, 2), (66L, 2), (69L, 2), (73L, 2), (85L, 2), (91L, 2), (102L, 2), (134L, 2), (141L, 2), (188L, 2), (192L, 2), (213L, 2), (222L, 2), (224L, 2), (41L, 2), (284L, 2), (287L, 2), (296L, 2), (300L, 2), (319L, 2), (321L, 2), (324L, 2), (333L, 2), (336L, 2), (353L, 2), (403L, 2), (389L, 2), (409L, 2), (411L, 2), (439L, 2), (442L, 2), (447L, 2), (448L, 2), (456L, 2), (489L, 2), (505L, 2), (507L, 2), (517L, 2), (541L, 2), (553L, 2), (590L, 2), (681L, 2), (682L, 2), (713L, 2), (782L, 2), (805L, 2), (844L, 2), (869L, 2), (973L, 2), (1003L, 2), (1015L, 2), (1024L, 2), (3097L, 2), (1053L, 2), (1077L, 2), (1113L, 2), (1117L, 2), (1172L, 2), (1180L, 2), (1262L, 2), (1334L, 2), (1341L, 2), (1425L, 2), (1445L, 2), (1555L, 2), (1613L, 2), (1658L, 2), (1719L, 2), (1756L, 2), (1788L, 2), (1809L, 2), (13L, 1), (19L, 1), (23L, 1), (25L, 1), (26L, 1), (4123L, 1), (38L, 1), (2089L, 1), (47L, 1), (48L, 1), (53L, 1), (56L, 1), (61L, 1), (68L, 1), (71L, 1), (74L, 1), (79L, 1), (80L, 1), (82L, 1), (87L, 1), (95L, 1), (100L, 1), (105L, 1), (10258L, 1), (110L, 1), (120L, 1), (126L, 1), (127L, 1), (131L, 1), (137L, 1), (138L, 1), (139L, 1), (140L, 1), (146L, 1), (147L, 1), (148L, 1), (152L, 1), (8350L, 1), (2210L, 1), (166L, 1), (2417L, 1), (169L, 1), (177L, 1), (182L, 1), (184L, 1), (185L, 1), (190L, 1), (193L, 1), (205L, 1), (210L, 1), (214L, 1), (227L, 1), (230L, 1), (231L, 1), (241L, 1), (2296L, 1), (249L, 1), (250L, 1), (251L, 1), (256L, 1), (259L, 1), (261L, 1), (262L, 1), (2434L, 1), (270L, 1), (272L, 1), (276L, 1), (285L, 1), (288L, 1), (289L, 1), (297L, 1), (301L, 1), (2350L, 1), (313L, 1), (325L, 1), (330L, 1), (8526L, 1), (367L, 1), (372L, 1), (381L, 1), (395L, 1), (2444L, 1), (400L, 1), (402L, 1), (4499L, 1), (412L, 1), (420L, 1), (969L, 1), (438L, 1), (440L, 1), (2499L, 1), (2510L, 1), (472L, 1), (475L, 1), (490L, 1), (493L, 1), (2547L, 1), (504L, 1), (513L, 1), (2563L, 1), (2567L, 1), (520L, 1), (2569L, 1), (2579L, 1), (532L, 1), (534L, 1), (542L, 1), (549L, 1), (556L, 1), (559L, 1), (2608L, 1), (568L, 1), (574L, 1), (587L, 1), (594L, 1), (596L, 1), (600L, 1), (603L, 1), (604L, 1), (4705L, 1), (615L, 1), (2679L, 1), (632L, 1), (642L, 1), (644L, 1), (646L, 1), (651L, 1), (661L, 1), (2715L, 1), (2718L, 1), (2719L, 1), (674L, 1), (675L, 1), (2734L, 1), (687L, 1), (704L, 1), (717L, 1), (720L, 1), (735L, 1), (759L, 1), (813L, 1), (785L, 1), (793L, 1), (798L, 1), (2848L, 1), (801L, 1), (804L, 1), (806L, 1), (6957L, 1), (815L, 1), (2878L, 1), (834L, 1), (835L, 1), (838L, 1), (842L, 1), (852L, 1), (864L, 1), (2930L, 1), (885L, 1), (2935L, 1), (892L, 1), (907L, 1), (9102L, 1), (912L, 1), (913L, 1), (2966L, 1), (921L, 1), (937L, 1), (945L, 1), (951L, 1), (952L, 1), (959L, 1), (9152L, 1), (962L, 1), (964L, 1), (974L, 1), (977L, 1), (981L, 1), (1001L, 1), (1004L, 1), (3053L, 1), (9206L, 1), (1019L, 1), (1028L, 1), (7175L, 1), (1033L, 1), (1035L, 1), (3085L, 1), (1039L, 1), (1054L, 1), (3117L, 1), (9263L, 1), (3121L, 1), (1078L, 1), (521L, 1), (1084L, 1), (1100L, 1), (1110L, 1), (1149L, 1), (1150L, 1), (1152L, 1), (1157L, 1), (1168L, 1), (5270L, 1), (1178L, 1), (1185L, 1), (9379L, 1), (1191L, 1), (882L, 1), (1200L, 1), (1207L, 1), (1208L, 1), (1214L, 1), (1215L, 1), (1216L, 1), (887L, 1), (1236L, 1), (7390L, 1), (1247L, 1), (5361L, 1), (1274L, 1), (1276L, 1), (2261L, 1), (5409L, 1), (1315L, 1), (1324L, 1), (1325L, 1), (3376L, 1), (1336L, 1), (1338L, 1), (1343L, 1), (1375L, 1), (3448L, 1), (1412L, 1), (3466L, 1), (1432L, 1), (7579L, 1), (1438L, 1), (1440L, 1), (1447L, 1), (3512L, 1), (1491L, 1), (1494L, 1), (1503L, 1), (9707L, 1), (1538L, 1), (3600L, 1), (1561L, 1), (1572L, 1), (1573L, 1), (5696L, 1), (1630L, 1), (1650L, 1), (960L, 1), (1671L, 1), (1681L, 1), (3777L, 1), (7881L, 1), (1752L, 1), (1760L, 1), (1779L, 1), (3842L, 1), (1823L, 1), (3039L, 1), (1873L, 1), (6678L, 1), (1917L, 1), (6028L, 1), (1940L, 1), (1947L, 1), (1958L, 1), (1973L, 1), (4028L, 1), (4041L, 1), (2017L, 1), (3067L, 1), (2023L, 1), (4074L, 1), (2047L, 1)]
--------------------------------------------------

观察数据得到以下几点事实:
  1. 结果中的最后一个,熵为8.42014是我这次计算中的最大的一个了。而上述结果中的第一个,也就是熵为0.146094,是非常有意义的,因为该用户就收藏了2个人的歌,而其中一位的歌手居然占到了97.9%,显然这个非常有利于我们的产生推荐列表。
  2. 就算是熵最大的用户,他还是会偏爱几个歌手的歌。纵观全部数据,所无论熵的大小,所有的用户都会偏爱几位歌手的歌曲。只是占全部收藏歌曲的多与少而已。

结论

  1. 我认为不必刻意确定一个熵的值,低于这个熵的值的时候会启动歌曲的歌手偏爱这套体系,而高于这个值就不启动。这套体系是指:我们会在产生推荐列表的时候,加大用户收藏歌曲的歌手的歌曲的比重。
  2. 我觉得可以使用某一个数学公式,比如倒函数这样的,倒函数的特点就是x越接近于0,那么y值就越大。如果我将y设置成这套体系中的比重,然后x值就熵。熵越低,比重越大,也可以使用高斯函数,因为具体时候哪一个函数更适合,我们也要经过计算、对比。
综上,产生了一种推荐歌曲的辅助策略。当然,这种的策略和用户直接收藏的歌手是类似的。

总结

制作singer表

发现了一个问题,那就是我在制作这个表的时候忘了给singer也作一张表,那么在music表内的的singer列,就应该是引用singer表中的id。从长远来看这样非常有利,因为singer还有自己的生日、年龄、专辑等等一系列信息。此外还有一个原因也迫使我完成这个工作,那就是python貌似无法用引用的方法来以中文为键,比如代码如下:
a='刘德华'
dictionary={}
dictionary.setdefault(a,0)
print dictionary
dictionary.clear()
dictionary.setdefault('a',0)
print dictionary

结果:
>>> 
{'\xc1\xf5\xb5\xc2\xbb\xaa': 0}
{'a': 0}
>>> 


我就想让键为刘德华而已,百度了半天也搞不定,想了想就不找了。


所以现在我要制作一张singer表,流程也非常简单(但是非常笨,我在写代码的时候又想到了更好地流程),如下:
  1. 遍历music表的singer列
  2. 如果是没有出现的歌手,在singer表中新建一个条目,把singer表的id赋值给music表的singerid列
  3. 如果singer表中已经有了的歌手,那么就把歌手的id赋值给music表的singerid列(新建一个singerid列)
  4. 遍历完成后,删除music的singer列,保留singerid列
先建singer表吧,sql语句如下:
CREATE TABLE singer(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(255)
)

再为music新加一列singerid:
alter table music add column singerid INT

到这里突然想到了更好的流程:
  1. 使用sql语句group by可以拿到所有的singer列的歌手名,还是不重复的那种。
  2. 先把上面的歌手名插入到singer表中,并且出现次数的,先插入,这样遍历更快
  3. 再将music表中singer转换为singerid
拿到所有的singer列的歌手名的sql代码:
select singer,count(singer) from music group by singer order by count(singer) desc

使用python代码完成剩下的转换工作:
#将music表中的singer,先插入到singer表中的id和name
def createSingerID():
    try:
        conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='musicrecomsys',port=3306,charset="gb2312") # charset="gb2312"为了能够打印出中文而添加的
        #用拿到执行sql语句的对象  
        cur1=conn.cursor()
        cur2=conn.cursor()
        count1=cur1.execute('select singer,count(singer) from music group by singer order by count(singer) desc')
        results1=cur1.fetchall()
        print '开始....'
        count=0
        for r1 in results1:
            count2=cur2.execute('INSERT INTO singer(name) VALUES (%s)',[r1[0]])
            conn.commit()#必须要有这个提交事务,否则不能正确插入
            count+=1
            if count%100==0:print '剩余:',count1-count#妈的,我还以为要搞多久,做了个计时器,结果4.5秒就完了。
        cur1.close()
        cur2.close()
        conn.close()  
    except MySQLdb.Error,e:  
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])
        
#将music表中的singer,查询singer表,再把singer表的id插入到music表的singerid
def singerToSingerID():
    try:
        conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='musicrecomsys',port=3306,charset="gb2312") # charset="gb2312"为了能够打印出中文而添加的
        #用拿到执行sql语句的对象  
        cur1=conn.cursor()
        cur2=conn.cursor()
        count1=cur1.execute('select id,singer from music')
        results1=cur1.fetchall()
        print '开始....'
        count=0
        for r1 in results1:
            count2=cur2.execute('select id from singer where name=%s',[r1[1]])
            results2=cur2.fetchone()#歌手id
            count2=cur2.execute('UPDATE music SET singerid = %s WHERE id = %s',[results2[0],r1[0]])
            conn.commit()#必须要有这个提交事务,否则不能正确插入
            count+=1
            if count%1000==0:print '剩余:',count1-count#妈的,我还以为要搞多久,做了个计时器,结果4.5秒就完了。
        cur1.close()
        cur2.close()
        conn.close()  
    except MySQLdb.Error,e:  
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])

删除music表中的singer列,就搞定了,sql语句:
alter table music drop column singer


python控制台输出中文

为了能在python控制台打印出中文,所以我在代码下一句添加了一个字符:
conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='musicrecomsys',port=3306,charset="gb2312") # charset="gb2312"为了能够打印出中文而添加的

形成对比实验的思路

我觉得是时候把只用歌曲相似度产生的推荐列表(针对所有用户)算出来了,然后我再证明使用了熵这样的情况下推荐列表推荐的更准确。也就是对比关注和不关注用户收藏的歌曲的歌手的信息。

与收藏歌手的关系

我认为,在这个地方所谓的歌曲的歌手和用户收藏的喜爱歌手,还是有点不一样的。简单地说,显然用户收藏的歌手更能体现用户的偏爱了,这种数据可以用于计算用户相似度,也可以调高这位歌手的歌曲的在推荐列表里的比重,实际上不用计算,因为用户的收藏已经明确告诉了喜欢的歌手了。更多关于如何处理这两类歌手细节,现在暂时不讨论,现在主要研究收藏的歌曲的歌手。
收藏的歌曲的歌手,是暗信息(我自己发明的词),因为是隐藏的,不那么明显,但是又实际存在的,值得加以利用。


不同相似度算法的推荐列表的对比

下面这句话也我可以计算用不同的相似度算法来产生歌曲相似度的表,最后产生的推荐列表肯定有优劣之分,有了事实作为依据,我只需要自圆其说:某某相似度算法更时候做音乐推荐。也许作为一种发表论文的方式,非常不错哦。


windows下python按任意键继续

代码如下:
import os
os.system('pause')

摧毁表结构的sql语句

truncate table

源代码

# -*- coding: cp936 -*-
#encoding=utf-8
 
import copy
import os


#singer字典请以下面这种的方式组织
#要创建这样的形式其实一句sql语句都能够搞定,当然也可以用代码。
singerMODE={'刘0':9,
        '刘1':1,
        }
import MySQLdb 
import operator#为了让字典以值排序
#写一个函数方便查看熵的结果
def showEntropyResults():
    try:
        singer={}
        conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='musicrecomsys',port=3306,charset="gb2312") #charset="gb2312"为了能够打印出中文而添加的
        #用拿到执行sql语句的对象  
        cur1=conn.cursor()
        cur2=conn.cursor()
        count1=cur1.execute('SELECT fsse, id FROM user WHERE fsse <1000 ORDER BY fsse desc')#决定输出熵为多少以下,或者什么范围
        results1=cur1.fetchall()
        count=0
        print '读取用户数据....'
        for r1 in results1:
            singer.clear()
            count2=cur2.execute('select singerid from music inner join (select musicid as mid from moresmallfavorites where userid=%s) a ON music.id = a.mid',[r1[1]])
            results2=cur2.fetchall()
            for r2 in results2:
                flag=singer.setdefault(r2[0],0)
                #如果等于0,说明第一次出现这个字段。不等于0说明已经有了这个字段了,所以多加一个
                if flag==0:singer[r2[0]]=1
                else:singer[r2[0]]=flag+1
            sortedSinger=sorted(singer.iteritems(), key=operator.itemgetter(1),reverse=True)#对字典以value进行排序
            print '--------------------------------------------------'
            print '熵为:',r1[0],'用户id:',r1[1],'singer列表如下:'
            print sortedSinger
        cur1.close()
        cur2.close()
        conn.commit()#必须要有这个提交事务,否则不能正确插入
        conn.close()  
    except MySQLdb.Error,e:  
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])  
showEntropyResults()

def countEntropy():
    try:
        singer={}
        conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='musicrecomsys',port=3306,charset="gb2312") # charset="gb2312"为了能够打印出中文而添加的
        #用拿到执行sql语句的对象  
        cur1=conn.cursor()
        cur2=conn.cursor()
        count1=cur1.execute('select userid from moresmallfavorites GROUP BY userid HAVING count(userid)>70')  
        results1=cur1.fetchall()
        count=0
        print '读取用户数据....'
        for r1 in results1:
            singer.clear()
            count2=cur2.execute('select singerid from music inner join (select musicid as mid from moresmallfavorites where userid=%s) a ON music.id = a.mid',[r1[0]])
            results2=cur2.fetchall()
            for r2 in results2:
                flag=singer.setdefault(r2[0],0)
                #如果等于0,说明第一次出现这个字段。不等于0说明已经有了这个字段了,所以多加一个
                if flag==0:singer[r2[0]]=1
                else:singer[r2[0]]=flag+1
            count2=cur2.execute('UPDATE user SET FSSE = %s WHERE id = %s',[entropy(singer),r1[0]])
            count+=1
            if count%100==0:print '剩余',(count1-count),'总共:',count
        cur1.close()
        cur2.close()
        conn.commit()#必须要有这个提交事务,否则不能正确插入
        conn.close()  
    except MySQLdb.Error,e:  
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])  
#该函数接受一个字典数据集
#字典以歌手为键,以其出现的次数为值
#如:{刘德华:0}
#,然后计算其混杂程度。使用熵来计算
#熵遍历所有可能的结果的概率除以总次数的概率p,然后将所有的p做计算:p*log(p),再将所有的这个结果加起来
def entropy(results):
    from math import log
    log2=lambda x:log(x)/log(2)
    #计算熵
    counts=sum(results.values())
    ent=0.0
    for r in results.keys():
        p=float(results[r])/counts
        ent=ent-p*log2(p)
    return ent#熵越大,混乱度越高,如此,一个集合都的结果都一样的话,那么熵应该为0

#将music表中的singer,先插入到singer表中的id和name
def createSingerID():
    try:
        conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='musicrecomsys',port=3306,charset="gb2312") # charset="gb2312"为了能够打印出中文而添加的
        #用拿到执行sql语句的对象  
        cur1=conn.cursor()
        cur2=conn.cursor()
        count1=cur1.execute('select singer,count(singer) from music group by singer order by count(singer) desc')
        results1=cur1.fetchall()
        print '开始....'
        count=0
        for r1 in results1:
            count2=cur2.execute('INSERT INTO singer(name) VALUES (%s)',[r1[0]])
            conn.commit()#必须要有这个提交事务,否则不能正确插入
            count+=1
            if count%100==0:print '剩余:',count1-count#妈的,我还以为要搞多久,做了个计时器,结果4.5秒就完了。
        cur1.close()
        cur2.close()
        conn.close()  
    except MySQLdb.Error,e:  
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])
        
#将music表中的singer,查询singer表,再把singer表的id插入到music表的singerid
def singerToSingerID():
    try:
        conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='musicrecomsys',port=3306,charset="gb2312") # charset="gb2312"为了能够打印出中文而添加的
        #用拿到执行sql语句的对象  
        cur1=conn.cursor()
        cur2=conn.cursor()
        count1=cur1.execute('select id,singer from music')
        results1=cur1.fetchall()
        print '开始....'
        count=0
        for r1 in results1:
            count2=cur2.execute('select id from singer where name=%s',[r1[1]])
            results2=cur2.fetchone()#歌手id
            count2=cur2.execute('UPDATE music SET singerid = %s WHERE id = %s',[results2[0],r1[0]])
            conn.commit()#必须要有这个提交事务,否则不能正确插入
            count+=1
            if count%1000==0:print '剩余:',count1-count#妈的,我还以为要搞多久,做了个计时器,结果4.5秒就完了。
        cur1.close()
        cur2.close()
        conn.close()  
    except MySQLdb.Error,e:  
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])

singerToSingerID()

上传网盘:entropyforFSSE 2014-2-10

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值