首先標(biāo)注一下報錯內(nèi)容:
In [11]: data = np.loadtxt(path, dtype=float, delimiter=',', converters={4: iris_type})
In [12]: Traceback (most recent call last):
File "<ipython-input-11-ec8ed0fbe8e2>", line 1, in <module>
data = np.loadtxt(path, dtype=float, delimiter=',', converters={4: iris_type})
File "C:\Users\Data Engineer\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1024, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Users\Data Engineer\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1024, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "<ipython-input-8-c822de30841a>", line 3, in iris_type
return it[s]
KeyError: b'Iris-setosa'
報錯key words: b'Iris-setosa'
通過搜索原因辣恋,發(fā)現(xiàn)有可能是在對文件讀取是編譯出現(xiàn)了問題获高,并且Keyword中提示b'Iris-setosa'檐薯,而我們的string轉(zhuǎn)float函數(shù)中沒有字母b,很奇怪沸久。所以嘗試將轉(zhuǎn)換函數(shù)所有的string前加b。結(jié)果發(fā)現(xiàn)數(shù)據(jù)讀取正常余蟹。
下邊附上轉(zhuǎn)換函數(shù):
def iris_type(s):
it={b'Iris-setosa':0, b'Iris-versicolor':1, b'Iris-virginica':2}
return it[s]
->markdown用的還不熟卷胯,怎么轉(zhuǎn)換字體顏色啊,強迫癥要犯了<Face with Tears of Joy>威酒。
生活中我們需要一些勇氣去追尋自己的理想窑睁。#Never give up