[英]Numpy genfromtxt doesn't seem to work when names=True for Python 3
I am using the Google Colab enviroment. 我正在使用Google Colab环境。
The file I am using can be found here. 我正在使用的文件可以在这里找到。 It is a csv file 这是一个CSV文件
https://drive.google.com/open?id=1v7Mm6S8BVtou1iIfobY43LRF8MgGdjfU https://drive.google.com/open?id=1v7Mm6S8BVtou1iIfobY43LRF8MgGdjfU
Warning: it has several million rows. 警告:它有几百万行。
This code runs within a minute in Google Colab Python 3 notebook. 此代码在一分钟内即可在Google Colab Python 3笔记本中运行。 I tried this several times with no problem. 我几次尝试都没有问题。
from numpy import genfromtxt
my_data = genfromtxt('DlRefinedRatings.csv', delimiter=',' , dtype=int)
print(my_data[0:50])
The code below, on the other hand, runs for several minutes before disconnecting from Google Colab's server. 另一方面,下面的代码要运行几分钟,然后才能与Google Colab的服务器断开连接。 I tried multiple times. 我尝试了多次。 Eventually colab gives me a 'running out of memory' warning. 最终,colab给了我“内存不足”的警告。
from numpy import genfromtxt
my_data = genfromtxt('DlRefinedRatings.csv', delimiter=',' , dtype=int, names=True)
print(my_data[0:50])
It seems that there used to be an issue with names=True in Python 3 but that issue was fixed https://github.com/numpy/numpy/issues/5411 似乎在Python 3中曾经有一个names = True的问题,但该问题已修复https://github.com/numpy/numpy/issues/5411
I check which version I was using in Colab and it was up to date 我检查了我在Colab中使用的版本,并且它是最新的
import numpy as np
print(np.version.version)
>1.14.3
With 同
my_data = genfromtxt('DlRefinedRatings.csv', delimiter=',' , dtype=int, max_rows=100)
I got a (100,4) int array. 我有一个(100,4)int数组。
With names=True
it took long, and then issued an long list of errors, all the same except for line number (even with the max_rows): 使用names=True
会花费很长时间,然后发出一长串错误,除了行号(即使使用max_rows)之外,其他所有错误都一样:
Line #4121986 (got 4 columns instead of 3)
The header line is screwy - with an initial blank name: 标题行是螺丝钉-带有初始空白名称:
In [753]: !head ../Downloads/refinedRatings.csv
,user_id,book_id,rating
0,1,258,5
1,2,4081,4
2,2,260,5
3,2,9296,5
5,2,26,4
7,2,33,4
8,2,301,5
9,2,2686,5
10,2,3753,5
So based on names it thinks there are 3 columns, but all data lines have 4. Hence the error. 因此,基于名称,它认为有3列,但所有数据行都有4列。因此出现错误。 I don't know why it ignores the max_rows
in this case. 我不知道为什么在这种情况下它会忽略max_rows
。
But with my own names 但是用我自己的名字
In [755]: np.genfromtxt('../Downloads/refinedRatings.csv',delimiter=',',dtype=in
...: t, max_rows=10, names='foo,bar,dat,me')
Out[755]:
array([(-1, -1, -1, -1), ( 0, 1, 258, 5), ( 1, 2, 4081, 4),
( 2, 2, 260, 5), ( 3, 2, 9296, 5), ( 5, 2, 26, 4),
( 7, 2, 33, 4), ( 8, 2, 301, 5), ( 9, 2, 2686, 5),
(10, 2, 3753, 5)],
dtype=[('foo', '<i8'), ('bar', '<i8'), ('dat', '<i8'), ('me', '<i8')])
The first record (-1,-1,-1,-1)
is the initial bad header line, with -1 inplace of strings it couldn't turn into ints. 第一条记录(-1,-1,-1,-1)
是最初的错误标头行,用-1代替字符串无法转换为整数。 So we should skip_header
as done below. 因此,我们应该按以下skip_header
进行skip_header
。
alternatively: 或者:
In [756]: np.genfromtxt('../Downloads/refinedRatings.csv',delimiter=',',dtype=in
...: t, max_rows=10, skip_header=1)
Out[756]:
array([[ 0, 1, 258, 5],
[ 1, 2, 4081, 4],
[ 2, 2, 260, 5],
[ 3, 2, 9296, 5],
[ 5, 2, 26, 4],
[ 7, 2, 33, 4],
[ 8, 2, 301, 5],
[ 9, 2, 2686, 5],
[ 10, 2, 3753, 5],
[ 11, 2, 8519, 5]])
In sum, skip the header, and use your own names if you want a structured array. 总之,跳过标题,如果需要结构化数组,请使用自己的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.