python - 如何将记录数组从 arff 文件转换为 ndarray?

标签 python python-3.x numpy arff

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

>>> 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 并 reshape 它以匹配向量维度。问题似乎是我留下的 ndarray 是具有长记录 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:无法将大小为11055的数组 reshape 为形状(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,0,1,1,1,-1,-1,-1,1,1,1,-1,-1,0,0,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,您的数组是一个具有复杂数据类型的常规 ndarray 。由于这不是您可以更改的内容,因此您必须转换数组的结构和数据类型。最简洁的方法(尽管不是最有效的)是

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

tolist 会将数组转换为元组列表,然后简单的数据类型 int8 会将其重新组装成常规数组。

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

关于python - 如何将记录数组从 arff 文件转换为 ndarray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953116/

相关文章:

python - 警告 :lib openflow _01 fields ignored due to unspecified prerequisites nw_src nw _dst

python - 是否有一个 python 模块可以将值和错误转换为科学记数法?

python - 为什么嵌套循环比扁平循环执行得快得多?

python - 使用 Spark 对分组数据进行快速行间计算

python - 语句溢出超过一行

python-3.x - Windows 上的 Tensorflow 安装问题 "tensorflow_gpu-0.12.0rc0-cp35-cp35m-win_amd64.whl is not a supported wheel on th is platform."

python - AWS Lambda 和 MySQL 连接处理

python - 添加两个列表然后 sort = None(?)

python - 调整 PIL 像素值的更快方法

python - numpy.any() 返回 True 但 "is True"比较失败