【问题标题】:How can I turn record array from arff file into ndarray?如何将记录数组从 arff 文件转换为 ndarray?
【发布时间】:2019-05-25 23:01:39
【问题描述】:

ARFF documentation 告诉我我的文件正在作为记录数组读取,但我似乎无法像普通记录数组那样将其转换为 ndarray。应该有 31 个特征的 11055 个示例。

>>> dataset.shape
(11055,)
>>> dataset[0]
(b'1', b'1', b'1', b'1', b'1', b'-1', b'1', b'1', b'-1', b'1', b'1', b'1', b'1', b'0', b'0', b'-1', b'1', b'1', b'0', b'1', b'1', b'1', b'1', b'1', b'1', b'1', b'1', b'1', b'0', b'1', b'1')
>>> dataset.dtype
dtype([('having_IP_Address', 'S2'), ('URL_Length', 'S2'), ('Shortining_Service', 'S2'), ('having_At_Symbol', 'S2'), ('double_slash_redirecting', 'S2'), ('Prefix_Suffix', 'S2'), ('having_Sub_Domain', 'S2'), ('SSLfinal_State', 'S2'), ('Domain_registeration_length', 'S2'), ('Favicon', 'S2'), ('port', 'S2'), ('HTTPS_token', 'S2'), ('Request_URL', 'S2'), ('URL_of_Anchor', 'S2'), ('Links_in_tags', 'S2'), ('SFH', 'S2'), ('Submitting_to_email', 'S2'), ('Abnormal_URL', 'S2'), ('Redirect', 'S1'), ('on_mouseover', 'S2'), ('RightClick', 'S2'), ('popUpWidnow', 'S2'), ('Iframe', 'S2'), ('age_of_domain', 'S2'), ('DNSRecord', 'S2'), ('web_traffic', 'S2'), ('Page_Rank', 'S2'), ('Google_Index', 'S2'), ('Links_pointing_to_page', 'S2'), ('Statistical_report', 'S2'), ('Result', 'S2')])

基本上,我正在尝试将存储在dataset 中的这个记录数组转换为 ndarray 并对其进行整形以匹配向量维度。问题似乎是我留下的 ndarray 是具有该长记录 dtype 的对象列表,而不是列表列表。我只是不确定如何将该 dtype 转换为列表。

from scipy.io import arff
import urllib.request
import io
import numpy as np

# this just reads the arff from its URL 
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/00327/Training%20Dataset.arff"
ftpstream = urllib.request.urlopen(url)
dataset, meta = arff.loadarff(io.StringIO(ftpstream.read().decode('utf-8')))

num_features = len(meta.names())
num_examples = dataset.shape[0]
dataset.view(np.ndarray).reshape(num_examples, num_features)

最后一行导致错误ValueError: cannot reshape array of size 11055 into shape (11055,31)。

我最终希望得到一个带有 shape(11055,31) 和数字 dtype 的 ndarray。

您可以找到数据here。但这是文件的样子:

@relation phishing

@attribute having_IP_Address  { -1,1 }
@attribute URL_Length   { 1,0,-1 }
@attribute Shortining_Service { 1,-1 }
@attribute having_At_Symbol   { 1,-1 }
@attribute double_slash_redirecting { -1,1 }
@attribute Prefix_Suffix  { -1,1 }
@attribute having_Sub_Domain  { -1,0,1 }
@attribute SSLfinal_State  { -1,1,0 }
@attribute Domain_registeration_length { -1,1 }
@attribute Favicon { 1,-1 }
@attribute port { 1,-1 }
@attribute HTTPS_token { -1,1 }
@attribute Request_URL  { 1,-1 }
@attribute URL_of_Anchor { -1,0,1 }
@attribute Links_in_tags { 1,-1,0 }
@attribute SFH  { -1,1,0 }
@attribute Submitting_to_email { -1,1 }
@attribute Abnormal_URL { -1,1 }
@attribute Redirect  { 0,1 }
@attribute on_mouseover  { 1,-1 }
@attribute RightClick  { 1,-1 }
@attribute popUpWidnow  { 1,-1 }
@attribute Iframe { 1,-1 }
@attribute age_of_domain  { -1,1 }
@attribute DNSRecord   { -1,1 }
@attribute web_traffic  { -1,0,1 }
@attribute Page_Rank { -1,1 }
@attribute Google_Index { 1,-1 }
@attribute Links_pointing_to_page { 1,0,-1 }
@attribute Statistical_report { -1,1 }
@attribute Result  { -1,1 }


@data
-1,1,1,1,-1,-1,-1,-1,-1,1,1,-1,1,-1,1,-1,-1,-1,0,1,1,1,1,-1,-1,-1,-1,1,1,-1,-1
1,1,1,1,1,-1,0,1,-1,1,1,-1,1,0,-1,-1,1,1,0,1,1,1,1,-1,-1,0,-1,1,1,1,-1
1,0,1,1,1,-1,-1,-1,-1,1,1,-1,1,0,-1,-1,-1,-1,0,1,1,1,1,1,-1,1,-1,1,0,-1,-1
1,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

  • 您应该检查dataset.dtype 而不是type(dataset) 或type(dataset[0])。前者将为您提供记录的 numpy 数据类型。后两者只会告诉您 Python 类型,它是 ndarray 或标量。
  • 好的,所以 Python 将元素读取为 ndarray 中的标量,我猜这就是 void 的来源。 dataset.dtype 为我提供了该类元组结构中每个元素的类型和列名,就像在记录数组中一样。所以:dtype([('having_IP_Address', 'S2'),('URL_Length', 'S2'), ... ])。关于如何将这些对象中的每一个转换为 numpy 数组的任何想法?
  • 请使用完整的 dtype 编辑问题。您已经有一个 numpy 数组。每个元素都有在 dtype 中命名的字段。貌似有一堆两个字符串……
  • 好的。我没有投资于字段名称。我真的只是在寻找一个包含 array([[-1,1,1,1,-1,-1,-1,-1,-1,1,1,-1,1,-1,1,-1,-1,-1,0,1,1,1,1,-1,-1,-1,-1,1,1,-1,-1], ... [-1,-1,1,1,1,-1,-1,-1,1,1,1,1,-1,-1,0,-1,1,1,0,1,1,1,1,-1,1,-1,-1,-1,1,-1,-1]]) 作为数字 dtype 的 ndarray,而且 arff 的读取方式似乎真的没有什么灵活性。
  • 我问过这个问题:stackoverflow.com/q/53954147/2988730。现在我可以给你写一个正确的答案了。

标签: python python-3.x numpy arff


【解决方案1】:

查看文件,我们可以看到所有字段都是分类类型,而不是数字。除此之外,您的数组是具有复杂数据类型的常规ndarray。由于这不是您可以更改的,因此您必须转换数组的结构和数据类型。最简洁的方法(虽然不是最有效的)是

dataset = np.array(dataset.tolist(), dtype=np.int8)

tolist 会将数组转换为元组列表,然后简单的 dtype int8 将导致将其重新组合为常规数组。

这个问题是converting numpy array of string fields to numerical format 的基础。

【讨论】:

  • 出于好奇,你明白为什么 pandas 会让我做 .astype(int) 但 numpy 返回错误 Can't cast from structure to non-structure, except if the structure only has a single field. 和 .astype(int) 吗?
  • 因为在 numpy 中,每个元素都有一个复杂的结构。结构的所有元素都属于同一类型,这纯属巧合。 Pandas 可以将每个字段作为不同的列处理,并根据需要对其应用不同的转换。
猜你喜欢
  • 2015-06-10
  • 1970-01-01
  • 2013-08-14
  • 2019-08-21
  • 2014-06-11
  • 2018-03-06
  • 2014-02-10
  • 2020-08-24
  • 2012-02-29
相关资源
最近更新 更多