coursera公开课——recommender system作业(第二周)

写这么丑的代码我也是醉了,继续学习。
第二周的assignment:

  1. Mean Rating: Calculate the mean rating for each movie, order with the highest rating listed first, and submit the top 5.
  2. % of ratings 4+: Calculate the percentage of ratings for each movie that are 4 or higher. Order with the highest percentage first, and submit the top 5.
  3. Rating Count: Count the number of ratings for each movie, order with the most number of ratings first, and submit the top 5.
  4. Top 5 Star Wars: Calculate movies that most often occur with Star Wars: Episode IV - A New Hope (1977) using the (x+y)/x method described in class. In other words, for each movie, calculate the percentage of Star Wars raters who also rated that movie. Order with the highest percentage first, and submit the top 5.
#coding:utf-8
import csv
#top n function
def topn(name,scores,n=5):
    tmpscores=scores[:] #create a new array
    tmpscores.sort()
    flag=[1 for i in range(len(name))] # flags 
    for i in range(n):
        for j in range(len(name)):
            if scores[j]==tmpscores[-1-i]:
                if flag[j]:
                    flag[j]=0
                    print name[j],scores[j]
def caldiv(name,array1,array2):
    result=[0.0 for i in range(len(name))]
    for i in range(len(name)):
        if i!=0:
            result[i]=array1[i]*1.0/array2[i]
    return result
star_level=4
csvfile=file('A1Ratings.csv','rU')
reader=csv.reader(csvfile,dialect='excel')
for line in reader:
    if reader.line_num==1:
        name=line
        scores=[0 for i in range(len(name))]
        totalcount=[0 for i in range(len(name))]
        star_count=[0 for i in range(len(name))]
    if reader.line_num!=1:
        for num in name:
            ff=name.index(num)
            if ff>0:
                temp=1
                item=line[ff]
                if not item.strip(): # to solve the proble of ""
                    item=0
                    temp=0
                scores[ff]=scores[ff]+int(item)
                totalcount[ff]=totalcount[ff]+temp
                if int(item)>=star_level:
                    star_count[ff]=star_count[ff]+1
average=caldiv(name,scores,totalcount)
average1=caldiv(name,star_count,totalcount)
topn(name,average)
topn(name,average1)
csvfile.close()
csvfile=file('A1Ratings.csv','rU')
reader=csv.reader(csvfile,dialect='excel')
sit=1
count=[0.0 for i in range(len(name))]
for line in reader:
    if reader.line_num!=1:
        for i in range(len(name)):
            if not line[i].strip():
                line[i]=0
            if i>0 and i!=sit:
                if  int(line[sit])*int(line[i]):
                    count[i]=count[i]+1.0/15
topn(name,count,5)

中间遇到的问题:
1. new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
initial code:
csvfile1=file(‘A1Ratings.csv’,’rb’)
update code:
csvfile1=file(‘A1Ratings.csv’,’rU’)
2. 问题代码如下:

for line in reader:
    print line
    if reader.line_num==1:
        name=line
        scores=[0 for i in range(len(name))]
        totalcount=[0 for i in range(len(name))]
    if reader.line_num!=1:
        for num in name:
            if name.index(num)>0:
                for item in line:
                    if name.index(num)==line.index(item):
                        temp=1
#       print item,num
                        if not item.strip():                
                            item=0
                            temp=0
                        scores[name.index(num)]=scores[name.index(num)]+int(item)
                        totalcount[name.index(num)]=totalcount[name.index(num)]+temp
print scores
print totalcount
csvfile.close()

打flag问题,如果没有打flag,当有相同的分数时(如4分),会定位到第一个打4分的位置。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值