Example #1
0
def getRecDict(playlistDict,songDict,recType = 0,scale = 0,topN = const.TOP_N
               ,maxLength = -1):
  """Get recommendations of all playlists using different method.
     Input:
       playlistDict - dict of playlist of specific scale.
       songDict - all songs dicts.
       recType - type of recommender.
       scale - scale of current playlist.
       topN - count of recommendations.
       maxLength - max window size.
     Output:
       recDict - dict of recommendations 
                 with pid as key and recommendation list as value.
  """
  recDict = {}
  # reuse arima results
  if recType == const.ARIMA or recType == const.ARIMA_SIMILAR \
                or recType == const.ARIMA_AVG or recType == const.ALL_HYBRID:
    arimaDict = persist.readPredictedTopicDictOfArima(playlistDict,songDict,
                                                      scale,maxLength)
  index = 0
  count = len(playlistDict)
  typeName = util.getMethodName(recType)
  # predict next song of each playlist
  for pid in playlistDict.keys():
    print 'length = %d:scale = %d >> %s:%d/%d' \
          % (maxLength,scale,typeName,index,count)
    playlist = playlistDict[pid]
    if recType == const.SIMILAR:
      tarDict = topicDictForNextSongByMostSimilar(playlist,songDict)
    elif recType == const.AVG:
      tarDict = topicDictForNextSongByAverage(playlist,songDict)
    elif recType == const.ARIMA:
      tarDict = arimaDict[pid]
    elif recType == const.ARIMA_SIMILAR:
      tarDict = topicDictForNextSongByMostSimilarHybrid(playlist,
                                                        songDict,
                                                        arimaDict)
    elif recType == const.ARIMA_AVG:
      tarDict = topicDictForNextSongByAverageHybrid(playlist,
                                                    songDict,
                                                    arimaDict)
    elif recType == const.ALL_HYBRID:
      tarDict = topicDictForNextSongByAllHybrid(playlist,
                                                songDict,
                                                arimaDict)
    else:
      print '%d is an Error Type......' % recType
      return
    if recType != const.ALL_HYBRID:
      recSong = getRecSongs(songDict,topN,tarDict)
    else:
      length = len(playlist.getTrainingList())
      simlarDict = topicDictForNextSongByMostSimilar(playlist,songDict)
      avgDict = topicDictForNextSongByAverage(playlist,songDict)
      recSong = getRecSongsForAllHybrid(songDict,topN,arimaDict[pid],simlarDict,avgDict,length)
    recDict[pid] = recSong
    index = index + 1
  return recDict
Example #2
0
def showResult():
  """Using pyplot to plot the experiment results.
     Input:
       None.
     Output:
       None.
  """
  logging.info('I am in showResult......')
  filename = "../txt/%s_testall_%d_%d.txt" % (const.DATASET_NAME,const.TOPIC_NUM,const.TOP_N)
  x = range(1,const.TOP_N,1)
  result = [[[] for i in range(5)] for i in range(3)]
  #read result from file to result
  if os.path.exists(filename):
    print '%s is existing......' % filename    
    rFile = open(filename,"r")
    lines = rFile.readlines()
    for line in lines:
      line = line.rstrip('\n')
      items = line.split("INFO:")
      line = items[1]
      items = line.split(":")
      ids = items[0]
      values = items[1]
      idItems = ids.split(">")
      mid = int(idItems[0])
      topN = int(idItems[1])
      valueItems = values.split()
      result[mid][0].append(float(valueItems[0]))
      result[mid][1].append(float(valueItems[1]))
      result[mid][2].append(float(valueItems[2]))
      result[mid][3].append(float(valueItems[3]))
      result[mid][4].append(float(valueItems[4]))
    rFile.close()
  else:
    rFile = open(filename,"w")
    rFile.close()
  #if some method is not in file, recreate it
  for mid in range(3):
    if len(result[mid][0]) == 0:
      recalls,precisions,f1s,maes,rmses = getErrorOfRecMethod(mid)
      result[mid][0] = recalls
      result[mid][1] = precisions
      result[mid][2] = f1s
      result[mid][3] = maes
      result[mid][4] = rmses

  #plt img of comparing with pure method
  for index in range(5):
    plt.figure(index)
    indexName = util.getIndexName(index)
    mids = [const.ARIMA,const.SIMILAR,const.AVG]
    marker = ['k','k-.','k:']
    markerIndex = 0
    for mid in mids:
      if index == 1 or index == 2:
        plt.plot(x[20:],result[mid][index][20:],marker[markerIndex],label=util.getMethodName(mid))
      else:
        plt.plot(x,result[mid][index],marker[markerIndex],label=util.getMethodName(mid))
      markerIndex += 1
    plt.title("%s of Different Recommend Algorithms" % indexName)
    plt.xlabel("Number of recommendations")
    plt.ylabel(indexName)
    plt.legend()
    plt.xlim(1,160)
    plt.savefig("../img/pure_%s_%s_%d_%d.png" % (const.DATASET_NAME,indexName,const.TOPIC_NUM,const.TOP_N))
    #plt.show()
  logging.info('I am out showResult......')
Example #3
0
def testRecMethod(recType = 0):
  """Test single recommender.
     Input:
       recType - id of recommender.
     Output:
       None.
  """
  info = '############%s#############' % util.getMethodName(recType)
  start_time = time.time()
  songDict = persist.readSongFromFile()
  allPlaylist = persist.readPlaylistFromFile()
  recallTotal = 0.0
  precisionTotal = 0.0
  f1Total = 0.0
  maeTotal = 0.0
  rmseTotal = 0.0
  if recType == const.KNN:
    simFilename = '../txt/simMatrix.txt'
    pid2Index,index2Pid,countMatrix = util.getUserSongMatrix(allPlaylist,songDict)
    if os.path.exists(simFilename):
      print '......'
      simFile = open(simFilename,'r')
      line = simFile.readline().rstrip('\n')
      simMatrix = eval(line)
      print len(simMatrix)
      print len(simMatrix[1234])
      simFile.close()
    else:
      print '++++++'
      simMatrix = util.getUserSimMatrix(countMatrix,pid2Index)
  for scale in range(10):
    playlistDict = allPlaylist[scale]
    playlistDict = allPlaylist[scale]
    if recType == const.ARIMA:
      recDict = predict.getRecDict(playlistDict,songDict,recType,scale)
    elif recType == const.SIMILAR:
      recDict = predict.getRecDict(playlistDict,songDict,recType,scale)
    elif recType == const.AVG:
      recDict = predict.getRecDict(playlistDict,songDict,recType,scale)
    elif recType == const.ARIMA_SIMILAR:
      recDict = predict.getRecDict(playlistDict,songDict,recType,scale)
    elif recType == const.ARIMA_AVG:
      recDict = predict.getRecDict(playlistDict,songDict,recType,scale)
    elif recType == const.KNN:
      recDict = predict.getRecDictOfUserKNN(playlistDict,songDict,scale,pid2Index,countMatrix,simMatrix)
    elif recType == const.MARKOV:
      recDict = predict.getRecDictOfFirstMarkov(allPlaylist,songDict,scale)
    elif recType == const.MARKOV_3:
      recDict = predict.getRecDictOfThreeOrderMarkov(allPlaylist,songDict,scale)
    elif recType == const.PATTERN:
      recDict = predict.getRecDictOfMostPattern(allPlaylist,songDict,scale)
    recall,precision,f1 = util.getTopNIndex(recDict,playlistDict)
    mae,rmse = util.getMAEandRMSE(recDict,playlistDict,songDict)
    recallTotal += recall
    precisionTotal += precision
    f1Total += f1
    maeTotal += mae
    rmseTotal += rmse

  recall = recallTotal / 10
  precision = precisionTotal / 10
  f1 = f1Total / 10
  mae = maeTotal / 10
  rmse = rmseTotal / 10

  print info
  logging.info(info)
  print 'Recall = ',recall
  logging.info('Recall = %f' % recall)
  print 'Precision = ',precision
  logging.info('Precision = %f' % precision)
  print 'F1-Score = ',f1
  logging.info('F1-Score = %f' % f1)
  print 'MAE = ',mae
  logging.info('MAE = %f' % mae)
  print 'RMSE = ',rmse
  logging.info('RMSE = %f' % rmse)
  print 'Consumed: %ds' % (time.time()-start_time)
  logging.info('Consumed: %ds' % (time.time()-start_time))
Example #4
0
def showResult():
  """Using pyplot to plot the experiment results.
     Input:
       None.
     Output:
       None.
  """
  logging.info('I am in showResult......')
  filename = "../txt/%s_testall_%d_%d.txt" % (const.DATASET_NAME,const.TOPIC_NUM,const.TOP_N)
  x = range(1,const.TOP_N,1)
  result = [[[] for i in range(5)] for i in range(const.METHOD_SIZE)]
  #read result from file to result
  if os.path.exists(filename):
    print '%s is existing......' % filename    
    rFile = open(filename,"r")
    lines = rFile.readlines()
    for line in lines:
      line = line.rstrip('\n')
      items = line.split("INFO:")
      line = items[1]
      items = line.split(":")
      ids = items[0]
      values = items[1]
      idItems = ids.split(">")
      mid = int(idItems[0])
      topN = int(idItems[1])
      valueItems = values.split()
      result[mid][0].append(float(valueItems[0]))
      result[mid][1].append(float(valueItems[1]))
      result[mid][2].append(float(valueItems[2]))
      result[mid][3].append(float(valueItems[3]))
      result[mid][4].append(float(valueItems[4]))
    rFile.close()
  else:
    rFile = open(filename,"w")
    rFile.close()
  #if some method is not in file, recreate it
  for mid in range(const.METHOD_SIZE):
    if len(result[mid][0]) == 0:
      recalls,precisions,f1s,maes,rmses = getErrorOfRecMethod(mid)
      result[mid][0] = recalls
      result[mid][1] = precisions
      result[mid][2] = f1s
      result[mid][3] = maes
      result[mid][4] = rmses

  #plt img of comparing with pure method
  for index in range(5):
    plt.figure(index)
    indexName = util.getIndexName(index)
    mids = [const.ARIMA,const.SIMILAR,const.AVG]
    markerIndex = 0
    for mid in mids:
      if index == 1 or index == 2:
        plt.plot(x[10:],result[mid][index][10:],const.marker[markerIndex],label=util.getMethodName(mid))
      else:
        plt.plot(x,result[mid][index],const.marker[markerIndex],label=util.getMethodName(mid))
      markerIndex += 1
    plt.title("%s of Different Recommend Algorithms(Pure)" % indexName)
    plt.xlabel("Number of recommendations")
    plt.ylabel(indexName)
    plt.legend()
    plt.xlim(1,160)
    plt.savefig("../img/pure_%s_%s_%d_%d.png" % (const.DATASET_NAME,indexName,const.TOPIC_NUM,const.TOP_N))
    #plt.show()

  #plt img of comparing with hybrid method
  for index in range(5):
    plt.figure(index+5)
    indexName = util.getIndexName(index)
    mids = [const.ARIMA,const.ARIMA_SIMILAR,const.ARIMA_AVG,const.ALL_HYBRID]
    markerIndex = 0
    for mid in mids:
      if index == 1 or index == 2:
        plt.plot(x[10:],result[mid][index][10:],const.marker[markerIndex],label=util.getMethodName(mid))
      else:
        plt.plot(x,result[mid][index],const.marker[markerIndex],label=util.getMethodName(mid))
      markerIndex += 1
    plt.title("%s of Different Recommend Algorithms(Hybrid)" % indexName)
    plt.xlabel("Number of recommendations")
    plt.ylabel(indexName)
    plt.legend()
    plt.xlim(1,160)
    plt.savefig("../img/hybrid_%s_%s_%d_%d.png" % (const.DATASET_NAME,indexName,const.TOPIC_NUM,const.TOP_N))
    #plt.show()


  #plt img of comparing with sequential method
  for index in range(5):
    plt.figure(index+10)
    indexName = util.getIndexName(index)
    mids = [const.ARIMA,const.KNN,const.PATTERN,const.MARKOV,const.MARKOV_3]
    markerIndex = 0
    for mid in mids:
      if index == 1 or index == 2:
        plt.plot(x[10:],result[mid][index][10:],const.marker[markerIndex],label=util.getMethodName(mid))
      else:
        plt.plot(x,result[mid][index],const.marker[markerIndex],label=util.getMethodName(mid))
      markerIndex += 1
    plt.title("%s of Different Recommend Methods" % indexName)
    plt.xlabel("Number of recommendations")
    plt.ylabel(indexName)
    plt.legend()
    plt.xlim(1,160)
    plt.savefig("../img/seq_%s_%s_%d_%d.png" % (const.DATASET_NAME,indexName,const.TOPIC_NUM,const.TOP_N))
    #plt.show()
  
  plt.figure(30)
  plt.plot(x,result[const.ARIMA_SIMILAR][3],'k-.',label=util.getMethodName(const.ARIMA_SIMILAR)) 
  plt.plot(x,result[const.ARIMA_AVG][3],'k+',label=util.getMethodName(const.ARIMA_AVG)) 
  plt.plot(x,result[const.ALL_HYBRID][3],'k',label=util.getMethodName(const.ALL_HYBRID)) 
  plt.title("MAE of Hybrid Music Recommendation Methods")
  plt.xlabel("Number of recommendations")
  plt.ylabel("MAE")
  plt.legend()
  plt.xlim(1,160)
  plt.savefig("../img/local_global_%s_%s_%d_%d.png" % (const.DATASET_NAME,"MAE",const.TOPIC_NUM,const.TOP_N))

  plt.figure(31)
  plt.plot(x,result[const.ARIMA_SIMILAR][4],'k-.',label=util.getMethodName(const.ARIMA_SIMILAR)) 
  plt.plot(x,result[const.ARIMA_AVG][4],'k+',label=util.getMethodName(const.ARIMA_AVG)) 
  plt.plot(x,result[const.ALL_HYBRID][4],'k',label=util.getMethodName(const.ALL_HYBRID)) 
  plt.title("RMSE of Hybrid Music Recommendation Methods")
  plt.xlabel("Number of recommendations")
  plt.ylabel("RMSE")
  plt.legend()
  plt.xlim(1,160)
  plt.savefig("../img/local_global_%s_%s_%d_%d.png" % (const.DATASET_NAME,"RMSE",const.TOPIC_NUM,const.TOP_N))

  plt.figure(19)
  improvement = []
  for i in range(len(result[const.ARIMA][1])):
    improvement.append((result[const.ARIMA][1][i]-result[const.KNN][1][i]) / result[const.KNN][1][i])
  plt.plot(x[10:],improvement[10:],'k',label='Improvement over UserKNN Recommender')
  plt.title('Average Precision Improvement over UserKNN Recommender')
  plt.xlabel('Number of recommendations')
  plt.ylabel('Improvement in Average Precision (times)')
  plt.legend()
  indexName = util.getIndexName(1)
  plt.savefig("../img/improvement_knn_%s_%s_%d_%d.png" % (const.DATASET_NAME,indexName,const.TOPIC_NUM,const.TOP_N))

  plt.figure(20)
  improvement = []
  for i in range(len(result[const.ARIMA][1])):
    improvement.append((result[const.ARIMA][1][i]-result[const.KNN][1][i]) / result[const.KNN][1][i])
  plt.plot(x[10:],improvement[10:],'k',label='Improvement over UserKNN Recommender')
  plt.title('Average Precision Improvement over UserKNN Recommender')
  plt.xlabel('Number of recommendations')
  plt.ylabel('Improvement in Average Precision (times)')
  plt.legend()
  indexName = util.getIndexName(1)
  plt.savefig("../img/improvement_knn_%s_%s_%d_%d.png" % (const.DATASET_NAME,indexName,const.TOPIC_NUM,const.TOP_N))

  plt.figure(21)
  improvement = []
  for i in range(len(result[const.ARIMA][2])):
    improvement.append((result[const.ARIMA][2][i]-result[const.KNN][2][i]) / result[const.KNN][2][i])
  plt.plot(x[10:],improvement[10:],'k',label='Improvement over UserKNN Recommender')
  plt.title('Average F1-Score Improvement over UserKNN Recommender')
  plt.xlabel('Number of recommendations')
  plt.ylabel('Improvement in Average F1-Score (times)')
  plt.legend()
  indexName = util.getIndexName(2)
  plt.savefig("../img/improvement_knn_%s_%s_%d_%d.png" % (const.DATASET_NAME,indexName,const.TOPIC_NUM,const.TOP_N))
  #plt.show()
  logging.info('I am out showResult......')

  #plt img of comparing with pure method
  for index in range(5):
    plt.figure(index+50)
    indexName = util.getIndexName(index)
    print indexName
    mids = [const.ARIMA,const.SIMILAR,const.KNN,const.AVG,const.PATTERN,const.MARKOV]
    markerIndex = 0
    for mid in mids:
      if index == 1 or index == 2:
        plt.plot(x[10:],result[mid][index][10:],const.marker1[markerIndex],label=util.getMethodName(mid))
      else:
        plt.plot(x,result[mid][index],const.marker1[markerIndex],label=util.getMethodName(mid))
      markerIndex += 1
    plt.title("%s of Different Recommend Algorithms" % indexName)
    plt.xlabel("Number of recommendations")
    plt.ylabel(indexName)
    plt.legend()
    plt.xlim(1,160)
    plt.savefig("../img/all_%s_%s_%d_%d.png" % (const.DATASET_NAME,indexName,const.TOPIC_NUM,const.TOP_N))

  #plt img of comparing with hybrid method
  for index in range(5):
    plt.figure(index+75)
    indexName = util.getIndexName(index)
    mids = [const.ARIMA,const.ALL_HYBRID]
    markerIndex = 0
    for mid in mids:
      if index == 1 or index == 2:
        plt.plot(x[10:],result[mid][index][10:],const.marker[markerIndex],label=util.getMethodName(mid))
      else:
        plt.plot(x,result[mid][index],const.marker[markerIndex],label=util.getMethodName(mid))
      markerIndex += 1
    plt.title("%s of Different Recommend Algorithms" % indexName)
    plt.xlabel("Number of recommendations")
    plt.ylabel(indexName)
    plt.legend()
    plt.xlim(1,160)
    plt.savefig("../img/hybrid_only_%s_%s_%d_%d.png" % (const.DATASET_NAME,indexName,const.TOPIC_NUM,const.TOP_N))
Example #5
0
def testRecMethod(recType=0):
    """Test single recommender.
     Input:
       recType - id of recommender.
     Output:
       None.
  """
    info = '############%s#############' % util.getMethodName(recType)
    start_time = time.time()
    songDict = persist.readSongFromFile()
    allPlaylist = persist.readPlaylistFromFile()
    recallTotal = 0.0
    precisionTotal = 0.0
    f1Total = 0.0
    maeTotal = 0.0
    rmseTotal = 0.0
    if recType == const.KNN:
        simFilename = '../txt/simMatrix.txt'
        pid2Index, index2Pid, countMatrix = util.getUserSongMatrix(
            allPlaylist, songDict)
        if os.path.exists(simFilename):
            print '......'
            simFile = open(simFilename, 'r')
            line = simFile.readline().rstrip('\n')
            simMatrix = eval(line)
            print len(simMatrix)
            print len(simMatrix[1234])
            simFile.close()
        else:
            print '++++++'
            simMatrix = util.getUserSimMatrix(countMatrix, pid2Index)
    for scale in range(10):
        playlistDict = allPlaylist[scale]
        playlistDict = allPlaylist[scale]
        if recType == const.ARIMA:
            recDict = predict.getRecDict(playlistDict, songDict, recType,
                                         scale)
        elif recType == const.SIMILAR:
            recDict = predict.getRecDict(playlistDict, songDict, recType,
                                         scale)
        elif recType == const.AVG:
            recDict = predict.getRecDict(playlistDict, songDict, recType,
                                         scale)
        elif recType == const.ARIMA_SIMILAR:
            recDict = predict.getRecDict(playlistDict, songDict, recType,
                                         scale)
        elif recType == const.ARIMA_AVG:
            recDict = predict.getRecDict(playlistDict, songDict, recType,
                                         scale)
        elif recType == const.KNN:
            recDict = predict.getRecDictOfUserKNN(playlistDict, songDict,
                                                  scale, pid2Index,
                                                  countMatrix, simMatrix)
        elif recType == const.MARKOV:
            recDict = predict.getRecDictOfFirstMarkov(allPlaylist, songDict,
                                                      scale)
        elif recType == const.MARKOV_3:
            recDict = predict.getRecDictOfThreeOrderMarkov(
                allPlaylist, songDict, scale)
        elif recType == const.PATTERN:
            recDict = predict.getRecDictOfMostPattern(allPlaylist, songDict,
                                                      scale)
        recall, precision, f1 = util.getTopNIndex(recDict, playlistDict)
        mae, rmse = util.getMAEandRMSE(recDict, playlistDict, songDict)
        recallTotal += recall
        precisionTotal += precision
        f1Total += f1
        maeTotal += mae
        rmseTotal += rmse

    recall = recallTotal / 10
    precision = precisionTotal / 10
    f1 = f1Total / 10
    mae = maeTotal / 10
    rmse = rmseTotal / 10

    print info
    logging.info(info)
    print 'Recall = ', recall
    logging.info('Recall = %f' % recall)
    print 'Precision = ', precision
    logging.info('Precision = %f' % precision)
    print 'F1-Score = ', f1
    logging.info('F1-Score = %f' % f1)
    print 'MAE = ', mae
    logging.info('MAE = %f' % mae)
    print 'RMSE = ', rmse
    logging.info('RMSE = %f' % rmse)
    print 'Consumed: %ds' % (time.time() - start_time)
    logging.info('Consumed: %ds' % (time.time() - start_time))
Example #6
0
def showResult():
    """Using pyplot to plot the experiment results.
     Input:
       None.
     Output:
       None.
  """
    logging.info('I am in showResult......')
    filename = "../txt/%s_testall_%d_%d.txt" % (const.DATASET_NAME,
                                                const.TOPIC_NUM, const.TOP_N)
    x = range(1, const.TOP_N, 1)
    result = [[[] for i in range(5)] for i in range(const.METHOD_SIZE)]
    #read result from file to result
    if os.path.exists(filename):
        print '%s is existing......' % filename
        rFile = open(filename, "r")
        lines = rFile.readlines()
        for line in lines:
            line = line.rstrip('\n')
            items = line.split("INFO:")
            line = items[1]
            items = line.split(":")
            ids = items[0]
            values = items[1]
            idItems = ids.split(">")
            mid = int(idItems[0])
            topN = int(idItems[1])
            valueItems = values.split()
            result[mid][0].append(float(valueItems[0]))
            result[mid][1].append(float(valueItems[1]))
            result[mid][2].append(float(valueItems[2]))
            result[mid][3].append(float(valueItems[3]))
            result[mid][4].append(float(valueItems[4]))
        rFile.close()
    else:
        rFile = open(filename, "w")
        rFile.close()
    #if some method is not in file, recreate it
    for mid in range(const.METHOD_SIZE):
        if len(result[mid][0]) == 0:
            recalls, precisions, f1s, maes, rmses = getErrorOfRecMethod(mid)
            result[mid][0] = recalls
            result[mid][1] = precisions
            result[mid][2] = f1s
            result[mid][3] = maes
            result[mid][4] = rmses

    #plt img of comparing with pure method
    for index in range(5):
        plt.figure(index)
        indexName = util.getIndexName(index)
        mids = [const.ARIMA, const.SIMILAR, const.AVG]
        markerIndex = 0
        for mid in mids:
            if index == 1 or index == 2:
                plt.plot(x[10:],
                         result[mid][index][10:],
                         const.marker[markerIndex],
                         label=util.getMethodName(mid))
            else:
                plt.plot(x,
                         result[mid][index],
                         const.marker[markerIndex],
                         label=util.getMethodName(mid))
            markerIndex += 1
        plt.title("%s of Different Recommend Algorithms(Pure)" % indexName)
        plt.xlabel("Number of recommendations")
        plt.ylabel(indexName)
        plt.legend()
        plt.xlim(1, 160)
        plt.savefig(
            "../img/pure_%s_%s_%d_%d.png" %
            (const.DATASET_NAME, indexName, const.TOPIC_NUM, const.TOP_N))
        #plt.show()

    #plt img of comparing with hybrid method
    for index in range(5):
        plt.figure(index + 5)
        indexName = util.getIndexName(index)
        mids = [
            const.ARIMA, const.ARIMA_SIMILAR, const.ARIMA_AVG, const.ALL_HYBRID
        ]
        markerIndex = 0
        for mid in mids:
            if index == 1 or index == 2:
                plt.plot(x[10:],
                         result[mid][index][10:],
                         const.marker[markerIndex],
                         label=util.getMethodName(mid))
            else:
                plt.plot(x,
                         result[mid][index],
                         const.marker[markerIndex],
                         label=util.getMethodName(mid))
            markerIndex += 1
        plt.title("%s of Different Recommend Algorithms(Hybrid)" % indexName)
        plt.xlabel("Number of recommendations")
        plt.ylabel(indexName)
        plt.legend()
        plt.xlim(1, 160)
        plt.savefig(
            "../img/hybrid_%s_%s_%d_%d.png" %
            (const.DATASET_NAME, indexName, const.TOPIC_NUM, const.TOP_N))
        #plt.show()

    #plt img of comparing with sequential method
    for index in range(5):
        plt.figure(index + 10)
        indexName = util.getIndexName(index)
        mids = [
            const.ARIMA, const.KNN, const.PATTERN, const.MARKOV, const.MARKOV_3
        ]
        markerIndex = 0
        for mid in mids:
            if index == 1 or index == 2:
                plt.plot(x[10:],
                         result[mid][index][10:],
                         const.marker[markerIndex],
                         label=util.getMethodName(mid))
            else:
                plt.plot(x,
                         result[mid][index],
                         const.marker[markerIndex],
                         label=util.getMethodName(mid))
            markerIndex += 1
        plt.title("%s of Different Recommend Methods" % indexName)
        plt.xlabel("Number of recommendations")
        plt.ylabel(indexName)
        plt.legend()
        plt.xlim(1, 160)
        plt.savefig(
            "../img/seq_%s_%s_%d_%d.png" %
            (const.DATASET_NAME, indexName, const.TOPIC_NUM, const.TOP_N))
        #plt.show()

    plt.figure(30)
    plt.plot(x,
             result[const.ARIMA_SIMILAR][3],
             'k-.',
             label=util.getMethodName(const.ARIMA_SIMILAR))
    plt.plot(x,
             result[const.ARIMA_AVG][3],
             'k+',
             label=util.getMethodName(const.ARIMA_AVG))
    plt.plot(x,
             result[const.ALL_HYBRID][3],
             'k',
             label=util.getMethodName(const.ALL_HYBRID))
    plt.title("MAE of Hybrid Music Recommendation Methods")
    plt.xlabel("Number of recommendations")
    plt.ylabel("MAE")
    plt.legend()
    plt.xlim(1, 160)
    plt.savefig("../img/local_global_%s_%s_%d_%d.png" %
                (const.DATASET_NAME, "MAE", const.TOPIC_NUM, const.TOP_N))

    plt.figure(31)
    plt.plot(x,
             result[const.ARIMA_SIMILAR][4],
             'k-.',
             label=util.getMethodName(const.ARIMA_SIMILAR))
    plt.plot(x,
             result[const.ARIMA_AVG][4],
             'k+',
             label=util.getMethodName(const.ARIMA_AVG))
    plt.plot(x,
             result[const.ALL_HYBRID][4],
             'k',
             label=util.getMethodName(const.ALL_HYBRID))
    plt.title("RMSE of Hybrid Music Recommendation Methods")
    plt.xlabel("Number of recommendations")
    plt.ylabel("RMSE")
    plt.legend()
    plt.xlim(1, 160)
    plt.savefig("../img/local_global_%s_%s_%d_%d.png" %
                (const.DATASET_NAME, "RMSE", const.TOPIC_NUM, const.TOP_N))

    plt.figure(19)
    improvement = []
    for i in range(len(result[const.ARIMA][1])):
        improvement.append(
            (result[const.ARIMA][1][i] - result[const.KNN][1][i]) /
            result[const.KNN][1][i])
    plt.plot(x[10:],
             improvement[10:],
             'k',
             label='Improvement over UserKNN Recommender')
    plt.title('Average Precision Improvement over UserKNN Recommender')
    plt.xlabel('Number of recommendations')
    plt.ylabel('Improvement in Average Precision (times)')
    plt.legend()
    indexName = util.getIndexName(1)
    plt.savefig("../img/improvement_knn_%s_%s_%d_%d.png" %
                (const.DATASET_NAME, indexName, const.TOPIC_NUM, const.TOP_N))

    plt.figure(20)
    improvement = []
    for i in range(len(result[const.ARIMA][1])):
        improvement.append(
            (result[const.ARIMA][1][i] - result[const.KNN][1][i]) /
            result[const.KNN][1][i])
    plt.plot(x[10:],
             improvement[10:],
             'k',
             label='Improvement over UserKNN Recommender')
    plt.title('Average Precision Improvement over UserKNN Recommender')
    plt.xlabel('Number of recommendations')
    plt.ylabel('Improvement in Average Precision (times)')
    plt.legend()
    indexName = util.getIndexName(1)
    plt.savefig("../img/improvement_knn_%s_%s_%d_%d.png" %
                (const.DATASET_NAME, indexName, const.TOPIC_NUM, const.TOP_N))

    plt.figure(21)
    improvement = []
    for i in range(len(result[const.ARIMA][2])):
        improvement.append(
            (result[const.ARIMA][2][i] - result[const.KNN][2][i]) /
            result[const.KNN][2][i])
    plt.plot(x[10:],
             improvement[10:],
             'k',
             label='Improvement over UserKNN Recommender')
    plt.title('Average F1-Score Improvement over UserKNN Recommender')
    plt.xlabel('Number of recommendations')
    plt.ylabel('Improvement in Average F1-Score (times)')
    plt.legend()
    indexName = util.getIndexName(2)
    plt.savefig("../img/improvement_knn_%s_%s_%d_%d.png" %
                (const.DATASET_NAME, indexName, const.TOPIC_NUM, const.TOP_N))
    #plt.show()
    logging.info('I am out showResult......')

    #plt img of comparing with pure method
    for index in range(5):
        plt.figure(index + 50)
        indexName = util.getIndexName(index)
        print indexName
        mids = [
            const.ARIMA, const.SIMILAR, const.KNN, const.AVG, const.PATTERN,
            const.MARKOV
        ]
        markerIndex = 0
        for mid in mids:
            if index == 1 or index == 2:
                plt.plot(x[10:],
                         result[mid][index][10:],
                         const.marker1[markerIndex],
                         label=util.getMethodName(mid))
            else:
                plt.plot(x,
                         result[mid][index],
                         const.marker1[markerIndex],
                         label=util.getMethodName(mid))
            markerIndex += 1
        plt.title("%s of Different Recommend Algorithms" % indexName)
        plt.xlabel("Number of recommendations")
        plt.ylabel(indexName)
        plt.legend()
        plt.xlim(1, 160)
        plt.savefig(
            "../img/all_%s_%s_%d_%d.png" %
            (const.DATASET_NAME, indexName, const.TOPIC_NUM, const.TOP_N))

    #plt img of comparing with hybrid method
    for index in range(5):
        plt.figure(index + 75)
        indexName = util.getIndexName(index)
        mids = [const.ARIMA, const.ALL_HYBRID]
        markerIndex = 0
        for mid in mids:
            if index == 1 or index == 2:
                plt.plot(x[10:],
                         result[mid][index][10:],
                         const.marker[markerIndex],
                         label=util.getMethodName(mid))
            else:
                plt.plot(x,
                         result[mid][index],
                         const.marker[markerIndex],
                         label=util.getMethodName(mid))
            markerIndex += 1
        plt.title("%s of Different Recommend Algorithms" % indexName)
        plt.xlabel("Number of recommendations")
        plt.ylabel(indexName)
        plt.legend()
        plt.xlim(1, 160)
        plt.savefig(
            "../img/hybrid_only_%s_%s_%d_%d.png" %
            (const.DATASET_NAME, indexName, const.TOPIC_NUM, const.TOP_N))
Example #7
0
def getRecDict(playlistDict,
               songDict,
               recType=0,
               scale=0,
               topN=const.TOP_N,
               maxLength=-1):
    """Get recommendations of all playlists using different method.
     Input:
       playlistDict - dict of playlist of specific scale.
       songDict - all songs dicts.
       recType - type of recommender.
       scale - scale of current playlist.
       topN - count of recommendations.
       maxLength - max window size.
     Output:
       recDict - dict of recommendations 
                 with pid as key and recommendation list as value.
  """
    recDict = {}
    # reuse arima results
    if recType == const.ARIMA or recType == const.ARIMA_SIMILAR \
                  or recType == const.ARIMA_AVG or recType == const.ALL_HYBRID:
        arimaDict = persist.readPredictedTopicDictOfArima(
            playlistDict, songDict, scale, maxLength)
    index = 0
    count = len(playlistDict)
    typeName = util.getMethodName(recType)
    # predict next song of each playlist
    for pid in playlistDict.keys():
        print 'length = %d:scale = %d >> %s:%d/%d' \
              % (maxLength,scale,typeName,index,count)
        playlist = playlistDict[pid]
        if recType == const.SIMILAR:
            tarDict = topicDictForNextSongByMostSimilar(playlist, songDict)
        elif recType == const.AVG:
            tarDict = topicDictForNextSongByAverage(playlist, songDict)
        elif recType == const.ARIMA:
            tarDict = arimaDict[pid]
        elif recType == const.ARIMA_SIMILAR:
            tarDict = topicDictForNextSongByMostSimilarHybrid(
                playlist, songDict, arimaDict)
        elif recType == const.ARIMA_AVG:
            tarDict = topicDictForNextSongByAverageHybrid(
                playlist, songDict, arimaDict)
        elif recType == const.ALL_HYBRID:
            tarDict = topicDictForNextSongByAllHybrid(playlist, songDict,
                                                      arimaDict)
        else:
            print '%d is an Error Type......' % recType
            return
        if recType != const.ALL_HYBRID:
            recSong = getRecSongs(songDict, topN, tarDict)
        else:
            length = len(playlist.getTrainingList())
            simlarDict = topicDictForNextSongByMostSimilar(playlist, songDict)
            avgDict = topicDictForNextSongByAverage(playlist, songDict)
            recSong = getRecSongsForAllHybrid(songDict, topN, arimaDict[pid],
                                              simlarDict, avgDict, length)
        recDict[pid] = recSong
        index = index + 1
    return recDict