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 展开
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 展开
2个回答
展开全部
你这样改下:def iris_type(s): it = {b'Iris-setosa': 0, b'Iris-versicolor': 1, b'Iris-virginica': 2} return it[s]
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
return it[s]是什么意思呀
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询