[英]Slicing Cross-section of 2D NumPy array
I'm looking to print a cross-section of a numpy array. 我正在寻找打印numpy数组的横截面。 I am looking to pull the first 15 rows and only data from the 5th index column. 我想拉前15行,仅从第5个索引列中提取数据。
import csv as csv
import numpy as np
csv_file_object = open('train.csv', 'rU')
header = next(csv_file_object)
data = []
for row in csv_file_object:
data.append(row)
data = np.array(data)
print(data[0:15,5])
When I run the above code, I receive the following error: IndexError: too many indices for array. 当我运行上面的代码时,我收到以下错误:IndexError:数组的索引过多。
Running the code without adding the column filter works as expected. 运行代码而不添加列过滤器按预期工作。 The csv file is a 12x892 (x,y) dataset. csv文件是12x892(x,y)数据集。
The csv_file_object
is a file object contains all lines of your csv file without splitting. csv_file_object
是一个文件对象,包含csv文件的所有行,但不拆分。 You have to use csv
module to read them properly: 您必须使用csv
模块正确阅读它们:
with open('train.csv', 'rU') as csv_file_object:
reader = csv.DictReader(csv_file_object)
data = np.array([row.values() for row in reader])
But since you want to convert them to a numpy array you better to use genfromtxt
function: 但是由于您要将它们转换为numpy数组,因此最好使用genfromtxt
函数:
data = np.genfromtxt('train.csv')
You can pass arguments like dtype
, delimiter
and names
(a boolean for keeping the headers). 您可以传递dtype
, delimiter
和names
类的参数(用于保留标题的布尔值)。
UPDATE: I did not call csv.reader() prior to opening the file 更新:打开文件之前,我没有调用csv.reader()
import csv as csv
import numpy as np
csv_file_object = csv.reader(open('train.csv', 'rU'))
header = next(csv_file_object)
data = []
for row in csv_file_object:
data.append(row)
data = np.array(data)
print(data.shape)
print(data[0:15,5])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.