鱼C论坛

 找回密码
 立即注册
查看: 2114|回复: 5

[已解决]求大神帮我看下这个程序 哪错了

[复制链接]
发表于 2017-4-14 21:25:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 没有土豆的洋芋 于 2017-4-15 11:22 编辑

import urllib2
url = 'http://aima.cs.berkeley.edu/data/iris.csv'
u = urllib2.urlopen(url)
localFile = open('iris.csv'', 'w')
localFile.write(u.read())
localFile.close()
from numpy import genfromtxt, zeros

data = genfromtxt('iris.csv',delimiter=',',usecols=(0,1,2,3))

target = genfromtxt('iris.csv',delimiter=',',usecols=(4),dtype=str)
from pylab import figure, subplot, hist, xlim, show
xmin = min(data[:,0])
xmax = max(data[:,0])
figure()
subplot(411)
hist(data[target='setosa',0],color='b',alpha=.7)
xlim(xmin,xmax)
subplot(412)
hist(data[target=='versicolor',0],color='r',alpha=.7)
xlim(xmin,xmax)
subplot(413)
hist(data[target=='virginica',0],color='g',alpha=.7)
xlim(xmin,xmax)
subplot(414)
hist(data[:,0],color='y',alpha=.7)
xlim(xmin,xmax)
show()
显示错误:invalid syntax
源程序 网址http://chuansong.me/n/2675694
谢谢
最佳答案
2017-4-15 17:00:47
  1. import urllib
  2. import requests
  3. # from urllib import request
  4. url = 'http://aima.cs.berkeley.edu/data/iris.csv'
  5. u = requests.get(url)
  6. localFile = open('iris.csv', 'w')
  7. localFile.write(u.text)
  8. localFile.close()
  9. from numpy import genfromtxt, zeros

  10. data = genfromtxt('iris.csv',delimiter=',',usecols=(0,1,2,3))

  11. target = genfromtxt('iris.csv',delimiter=',',usecols=(4),dtype=str)
  12. from pylab import figure, subplot, hist, xlim, show
  13. xmin = min(data[:,0])
  14. xmax = max(data[:,0])
  15. figure()
  16. subplot(411)
  17. hist(data[target=='setosa',0],color='b',alpha=.7)
  18. xlim(xmin,xmax)
  19. subplot(412)
  20. hist(data[target=='versicolor',0],color='r',alpha=.7)
  21. xlim(xmin,xmax)
  22. subplot(413)
  23. hist(data[target=='virginica',0],color='g',alpha=.7)
  24. xlim(xmin,xmax)
  25. subplot(414)
  26. hist(data[:,0],color='y',alpha=.7)
  27. xlim(xmin,xmax)
  28. show()
复制代码


给你改了下,
  1. hist(data[target='setosa',0],color='b',alpha=.7)
复制代码

少个=
QQ图片20170415112123.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-14 21:51:10 | 显示全部楼层
显示你的哪里invalid syntax
截图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-15 11:23:13 | 显示全部楼层
新手·ing 发表于 2017-4-14 21:51
显示你的哪里invalid syntax
截图

伐啦 麻烦看一下 大神
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-15 11:27:47 | 显示全部楼层
第十三十四行
还有倒数第三行
[]里的内容都去掉逗号
是切片吗,切片没有逗号
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-15 14:46:56 | 显示全部楼层
新手·ing 发表于 2017-4-15 11:27
第十三十四行
还有倒数第三行
[]里的内容都去掉逗号

还是不可以 啊大神
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-15 17:00:47 | 显示全部楼层    本楼为最佳答案   
  1. import urllib
  2. import requests
  3. # from urllib import request
  4. url = 'http://aima.cs.berkeley.edu/data/iris.csv'
  5. u = requests.get(url)
  6. localFile = open('iris.csv', 'w')
  7. localFile.write(u.text)
  8. localFile.close()
  9. from numpy import genfromtxt, zeros

  10. data = genfromtxt('iris.csv',delimiter=',',usecols=(0,1,2,3))

  11. target = genfromtxt('iris.csv',delimiter=',',usecols=(4),dtype=str)
  12. from pylab import figure, subplot, hist, xlim, show
  13. xmin = min(data[:,0])
  14. xmax = max(data[:,0])
  15. figure()
  16. subplot(411)
  17. hist(data[target=='setosa',0],color='b',alpha=.7)
  18. xlim(xmin,xmax)
  19. subplot(412)
  20. hist(data[target=='versicolor',0],color='r',alpha=.7)
  21. xlim(xmin,xmax)
  22. subplot(413)
  23. hist(data[target=='virginica',0],color='g',alpha=.7)
  24. xlim(xmin,xmax)
  25. subplot(414)
  26. hist(data[:,0],color='y',alpha=.7)
  27. xlim(xmin,xmax)
  28. show()
复制代码


给你改了下,
  1. hist(data[target='setosa',0],color='b',alpha=.7)
复制代码

少个=
figure_1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 00:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表