【机器学习实战】Logistic回归例程调试(1)

直接调用源码。调试过程如下

1.在Python Shell下调用open函数打开txt文件的文件路径写法:

f=open("d:/tmp.txt")
或
f=open("d://tmp.txt")
或
f=open("d:\\tmp.txt")

包含中文的路径:

srcfile = r"D:/测试路径/测试文件.txt"
f = open(srcfile.decode('utf8').encode('gbk'))
注意路径是用'/'。

2.在shell中导入py源码

>>> import sysort sys
>>> sys.path.append('F:\temp')
>>> import logRegres

error1 :

No module named logRegres

error1 cleared by:

>>>sys.path.append('F:\\temp')

new error2 occured:

SyntaxError: Non-ASCII character '\xe8' in file F:\temp\logRegres.py on line 14, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

error2 cleared by:

去除第14行的中文注释。

go on:

>>>dataArr,labelMat=logRegres.loadDataSet()

error3 occured:

IOError: [Errno 2] No such file or directory: 'testSet.txt'

error3 cleared by:

读 testSet.txt 的原代码:

fr = open(‘testSet.txt’)

改为:

dataMat = []; labelMat = []
BASE_DIR = os.path.dirname(file)
print BASE_DIR
file_path = os.path.join(BASE_DIR, ‘testSet.txt’)
fr = open(file_path)

记得在开头增加一句:import os

tips:

改动过py文件中的代码后,在shell中调试新版本代码应该先运行一句:

reload(py文件名不带后缀)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值