-- 20180228
原书: 《Python机器学习 预测分析核心算法》 Michael Boeles 著
GitHub地址: https://github.com/atencra/mlip
本段基于源码3-2(在岩石水雷数据集上度量分类器性能), 解决以下问题:
1.urllib2 不存在
PyCharm中点击File -- Settings -- Project XXXXX -- Project Interpreter
点击Package 输入 urllib 安装urllib3 和yieldfrom.urllib.request 替代urllib2
在 .py中改使用以下命令使用url远程数据
import urllib.request
import urllib
target_url = ("https://archive.ics.uci.edu/ml/machine-learning-"
"databases/undocumented/connectionist-bench/sonar/sonar.all-data")
data = urllib.request.urlopen(target_url)
2. csv文件转list报错
row = line.strip().split(',')
TypeError: a bytes-like object is required, not 'str'
将
row = line.strip().split(',')
调整为
row = line.decode('utf8').strip().split(',')
3. 或者不使用(1)中直接调用远程数据的方法,将数据保存到本地并调用(注意将本地路径中的 \ 变成 \\)
target_url = ("https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data")
# save data to local disk
urllib.request.urlretrieve(target_url, 'C:\\Users\\000\\Desktop\\backup\\PY\\download_test.csv')
# then read data from local
data = pd.read_csv('C:\\Users\\000\\Desktop\\backup\\PY\\download_test.csv',header=None,prefix="V",sep=',')
print(data)
代码说明可查看如下链接:
https://www.cnblogs.com/lxnz/p/7117426.html
-- 20180301 add
4. 后续3-5实验中读取的数据有列索引,原书方法转第一行时会报错,转使用url远程数据. url如下:
target_url = ("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data")
如数据地址有变动,可直接去http://archive.ics.uci.edu/ml/index.php 查找
5. 输出训练模型的各项系数
print (rocksVMinesModel.coef_)