python 的 numpy 读取文件的问题

importpandasaspdimportnumpyasnpfrompandasimportSeries,DataFramedefiris_type(s):it={'I... import pandas as pd
import numpy as np
from pandas import Series ,DataFrame
def iris_type(s):
it = {'Iris-setosa': 0, 'Iris-versicolor': 1, 'Iris-virginica': 2}
return it[s]
path = u'E:/data/flower.data'
data = np.loadtxt(path, dtype=float, delimiter=',', converters={4: iris_type})

然后报了下面的错误 :
D:\Users\Administrator\PycharmProjects\untitled3\venv\Scripts\python.exe D:/Users/Administrator/PycharmProjects/untitled3/tree.py
Traceback (most recent call last):
File "D:/Users/Administrator/PycharmProjects/untitled3/tree.py", line 8, in <module>
data = np.loadtxt(path, dtype=float, delimiter=',', converters={4: iris_type})
File "D:\Users\Administrator\PycharmProjects\untitled3\venv\lib\site-packages\numpy\lib\npyio.py", line 1092, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "D:\Users\Administrator\PycharmProjects\untitled3\venv\lib\site-packages\numpy\lib\npyio.py", line 1019, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "D:\Users\Administrator\PycharmProjects\untitled3\venv\lib\site-packages\numpy\lib\npyio.py", line 1019, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "D:\Users\Administrator\PycharmProjects\untitled3\venv\lib\site-packages\numpy\lib\npyio.py", line 1078, in tobytes_first
return conv(x.encode("latin1"))
File "D:/Users/Administrator/PycharmProjects/untitled3/tree.py", line 6, in iris_type
return it[s]
KeyError: b'Iris-setosa'

Process finished with exit code 1
展开
 我来答
yslr123456
2018-04-25
知道答主
回答量:4
采纳率:0%
帮助的人:3956
展开全部
你这样改下:def iris_type(s): it = {b'Iris-setosa': 0, b'Iris-versicolor': 1, b'Iris-virginica': 2} return it[s]
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友c8c8fb385e
2018-04-04 · TA获得超过248个赞
知道小有建树答主
回答量:428
采纳率:62%
帮助的人:137万
展开全部
return it[s]是什么意思呀
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式