Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取genfromtxt/loadtxt以忽略被忽略列/行中的数据类型_Python_Numpy - Fatal编程技术网

Python 获取genfromtxt/loadtxt以忽略被忽略列/行中的数据类型

Python 获取genfromtxt/loadtxt以忽略被忽略列/行中的数据类型,python,numpy,Python,Numpy,我有一个包含整数数据的文件,其中前几行/列用于名称 我希望能够使用genfromtxt或loadtxt并且仍然能够让numpy将其作为一个同质数组来读取。为此,我使用了选项skiprows和usecols,但没有帮助。 在下面的(工作)示例中,我希望print(test\u array.shape)给出(3,3)和print(test.array)给出 [[0 0 0] [0 1 0] [1 0 0]] 在尝试加载文件之前,是否有任何方法可以在不修剪第一行/第一列的情况下实现我想要的结果?

我有一个包含整数数据的文件,其中前几行/列用于名称

我希望能够使用
genfromtxt
loadtxt
并且仍然能够让numpy将其作为一个同质数组来读取。为此,我使用了选项
skiprows
usecols
,但没有帮助。 在下面的(工作)示例中,我希望
print(test\u array.shape)
给出(3,3)和
print(test.array)
给出

[[0 0 0]
 [0 1 0]
 [1 0 0]]
在尝试加载文件之前,是否有任何方法可以在不修剪第一行/第一列的情况下实现我想要的结果?请注意,我要加载的实际文件是B-I-G(~6 gigs),因此任何解决方案都不应该过于计算密集

from __future__ import print_function
from StringIO import StringIO #use io.StringIO with py3
import numpy as np

example_file = StringIO("FID 1 2 3\n11464_ATCACG 0 0 0\n11465_CGATGT 0 1 0\n11466_TTAGGC 1 0 0")
test_array = np.loadtxt(example_file, skiprows=1, usecols=(1,), dtype=int)

print(test_array.shape) #(3,)
print(test_array) #[0 0 1]

您可以使用
np.genfromtxt
中的
usecols
skip_header
标志。那么它的工作原理是:

test_array = np.genfromtxt(example_file, skip_header=1, usecols=(1,2,3))
>>> print(test_array)
[[ 0.  0.  0.]
 [ 0.  1.  0.]
 [ 1.  0.  0.]]

您可以使用
np.genfromtxt
中的
usecols
skip_header
标志。那么它的工作原理是:

test_array = np.genfromtxt(example_file, skip_header=1, usecols=(1,2,3))
>>> print(test_array)
[[ 0.  0.  0.]
 [ 0.  1.  0.]
 [ 1.  0.  0.]]