scipy.io.arff.

loadarff#

scipy.io.arff.loadarff(f)[原始碼]#

讀取 arff 檔案。

資料會以紀錄陣列 (record array) 的形式回傳,可以像 NumPy 陣列的字典一樣存取。例如,如果其中一個屬性稱為 'pressure',則可以從 data 紀錄陣列中存取其前 10 個資料點,如下所示:data['pressure'][0:10]

參數:
f類檔案物件或字串

要讀取的類檔案物件,或是要開啟的檔案名稱。

回傳值:
data紀錄陣列

arff 檔案的資料,可透過屬性名稱存取。

metaMetaData

包含關於 arff 檔案的資訊,例如屬性的名稱和類型、關聯性 (資料集的名稱) 等。

引發:
ParseArffError

如果給定的檔案不是 ARFF 格式,則會引發此錯誤。

NotImplementedError

ARFF 檔案具有目前尚不支援的屬性。

註解

此函數應能讀取大多數 arff 檔案。未實作的功能包含

  • 日期類型屬性

  • 字串類型屬性

它可以讀取具有數值和名義屬性的檔案。它無法讀取具有稀疏資料(檔案中為 {})的檔案。但是,此函數可以讀取具有遺失資料(檔案中為 ?)的檔案,將資料點表示為 NaN。

範例

>>> from scipy.io import arff
>>> from io import StringIO
>>> content = """
... @relation foo
... @attribute width  numeric
... @attribute height numeric
... @attribute color  {red,green,blue,yellow,black}
... @data
... 5.0,3.25,blue
... 4.5,3.75,green
... 3.0,4.00,red
... """
>>> f = StringIO(content)
>>> data, meta = arff.loadarff(f)
>>> data
array([(5.0, 3.25, 'blue'), (4.5, 3.75, 'green'), (3.0, 4.0, 'red')],
      dtype=[('width', '<f8'), ('height', '<f8'), ('color', '|S6')])
>>> meta
Dataset: foo
    width's type is numeric
    height's type is numeric
    color's type is nominal, range is ('red', 'green', 'blue', 'yellow', 'black')