我试图使用AWS上提供的Million Song数据集来查找轨道的响度与其流行度之间的关联。我遵循一个基本教程(http://musicmachinery.com/2011/09/04/how-to-process-a-million-songs-in-20-minutes/)来获取每个音轨的数据,并使用MRJob和Python构建了我的项目。现在我迷失了如何在使用映射器和缩减器时找到轨道之间的相关性。这是到目前为止我的代码:如何使用MapReduce计算python中两个变量之间的相关性
from mrjob.job import MRJob
import track
YIELD_ALL = True
class MRDensity(MRJob):
def mapper(self, _, line):
t = track.load_track(line)
if t:
if t['tempo'] > 0:
loudness = t['loudness']
#print loudness
hotness = t['song_hotttnesss']
xy = loudness * hotness
x2 = loudness * loudness
y2 = hotness * hotness
counter = counter + 1
yield (counter, (loudness, hotness, xy,x2,y2))
def reducer(self, key, val):
sumx2 = 0
sumy2 = 0
sumxy = 0
sumh = 0
suml = 0
for l, h, xy, x2, y2 in val:
suml = suml + l
sumh += h
sumxy += xy
sumx2 += x2
sumy2 += y2
yield key, suml
if __name__ == '__main__':
MRDensity.run()
此代码是不是真的工作,因为它产生这样的:
1 -10.142
1 -10.212
1 -11.137
1 -11.197
1 -13.496
1 -15.568
1 -15.607
1 -17.302
1 -22.262
1 -3.383
1 -3.809
1 -5.816
1 -5.902
1 -6.671
1 -7.24
1 -7.591
1 -8.729
1 -9.689
1 -9.738
1 -9.863
我需要编写代码的其余部分来计算loudness
之间的相关性帮助和hotness
变量为MSD数据集。谢谢!
根本不使用计数器变量是错误的。 – 2013-03-21 20:18:48
@JohnWiseman为何如此? – Atanu 2014-03-22 18:27:56
因为在使用MRJob时,您的代码可能在多台机器上的多个进程中运行。计数器变量不会在不同的进程中保持同步。 – 2015-04-25 16:30:36