Ch2.Making Reconmmendation in PCI

做《Programing Collective Intelligence》中chapter 2.Making Recommendation的实例,有3个问题花了好长时间:

1. 遇到报错"UnicodeDecodeError: ‘gbk’ codec can’t decode bytes in position 2-3: illegal multibyte sequence"。看了http://www.tuicool.com/articles/nEjiEv这篇文章confirm了是编解码的问题,明显Python是用gbk解码数据文件的,而数据文件是用其他方式编码。用Notepad++能看到文件是encoding in ANSI,是用ANSI编码的。http://zhidao.baidu.com/link?url=JWUp5uFuU0lhUXVclozgbZybW9jVu8SfYbUHqQyGakr-27hhoy5xYrMSgtjuMEHZQLDy2HEe-DN6SKmekbUE2q这篇知道里提及open()有encoding这个argument可以设置解码方式,于是把encoding设置成utf-8,把数据文件u.item也在notepad里保存成encoding in utf-8。结果呢,还是不行,还是报gbk给您解不了码。后来就用help(open)查open这个属性到底怎么用,没道理啊,设成utf-8怎么还gkb在解啊。终于,才知道,当encoding=utf-8时只能解码用utf-8编码的text file,而我的数据文件是item类型(具体item是个什么文件类型,不详)。就用notepad把.item另存为.txt,这才可以顺利解码读取数据文件了。

 

2. 第二个问题是在用pydelicious API时报了很多invalid syntax的错,因为里面有许多语法没有见过,没法下判断,没法针对性去搜solution,就先放放了。

 

3. 第三个遇到的是keyerror,http://scnjl.iteye.com/blog/905177这篇文章提到“用dict[key]这个读取会报KeyError异常”,在知道不是数据不是自己操作有问题,果断用了try..except pass把异常pass掉,证明也没用影响结果。

 

除了上述python程序语法上的问题,其实关于做推荐的逻辑还没有好好看透,只是先把程序代码抄下来走通了,还要再程序再看上几遍。

转载于:https://www.cnblogs.com/ffan/p/3588465.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class Account: """An account has a balance and a holder. >>> a = Account('John') >>> a.deposit(12) 12 >>> a.balance 12 >>> a.interest 0.02 >>> a.time_to_retire(20) 26 >>> a.balance # balance 保持不变 12 >>> a.withdraw(11) # 每次取款额度不超过10,超过无法取出 "Can't withdraw that amount" >>> a.withdraw(10) 2 >>> a.withdraw(10) 'Insufficient funds' """ def __init__(self, account_holder): self.holder = account_holder self.balance = 0 self.interest = 0.02 def deposit(self, amount): self.balance += amount return self.balance def withdraw(self, amount): if amount > 10: return "Can't withdraw that amount" elif self.balance < amount: return 'Insufficient funds' else: self.balance -= amount return self.balance def time_to_retire(self, amount): assert self.balance > 0 and amount > 0 and self.interest > 0 years = 0 while self.balance < amount: self.balance *= (1 + self.interest) years += 1 return years class FreeChecking(Account): """A bank account that charges for withdrawals, but the first two are free! >>> ch = FreeChecking('Jack') >>> ch.balance = 20 >>> ch.withdraw(100) # 首次取款免费,不论是否成功,免费次数减少1次 'Insufficient funds' >>> ch.withdraw(3) # 第二次取款免费 17 >>> ch.balance 17 >>> ch.withdraw(3) # 2次免费用完,开始收费 13 >>> ch.withdraw(3) 9 >>> ch2 = FreeChecking('John') >>> ch2.balance = 10 >>> ch2.withdraw(3) # 首次免费 7 >>> ch.withdraw(3) # 2次免费 5 >>> ch.withdraw(5) # 余额不足 'Insufficient funds' """ def __init__(self, account_holder): super().__init__(account_holder) self.free_withdrawals = 2 self.withdrawal_fee = 1 def withdraw(self, amount): if self.free_withdrawals > 0: self.free_withdrawals -= 1 return super().withdraw(amount) else: return super().withdraw(amount + self.withdrawal_fee) import doctest doctest.testmod()

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值