本文整理汇总了Python中numpy.ndfromtxt函数的典型用法代码示例。如果您正苦于以下问题:Python ndfromtxt函数的具体用法?Python ndfromtxt怎么用?Python ndfromtxt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ndfromtxt函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_usecols
def test_usecols(self):
"Test the selection of columns"
# Select 1 column
control = np.array( [[1, 2], [3, 4]], float)
data = StringIO.StringIO()
np.savetxt(data, control)
data.seek(0)
test = np.ndfromtxt(data, dtype=float, usecols=(1,))
assert_equal(test, control[:, 1])
#
control = np.array( [[1, 2, 3], [3, 4, 5]], float)
data = StringIO.StringIO()
np.savetxt(data, control)
data.seek(0)
test = np.ndfromtxt(data, dtype=float, usecols=(1, 2))
assert_equal(test, control[:, 1:])
# Testing with arrays instead of tuples.
data.seek(0)
test = np.ndfromtxt(data, dtype=float, usecols=np.array([1, 2]))
assert_equal(test, control[:, 1:])
# Checking with dtypes defined converters.
data = StringIO.StringIO("""JOE 70.1 25.3\nBOB 60.5 27.9""")
names = ['stid', 'temp']
dtypes = ['S4', 'f8']
test = np.ndfromtxt(data, usecols=(0, 2), dtype=zip(names, dtypes))
assert_equal(test['stid'], ["JOE", "BOB"])
assert_equal(test['temp'], [25.3, 27.9])
开发者ID:GunioRobot,项目名称:numpy-refactor,代码行数:27,代码来源:test_io.py
示例2: test_dtype_with_converters
def test_dtype_with_converters(self):
dstr = "2009; 23; 46"
test = np.ndfromtxt(StringIO(dstr), delimiter=";", dtype=float, converters={0: bytes})
control = np.array([("2009", 23.0, 46)], dtype=[("f0", "|S4"), ("f1", float), ("f2", float)])
assert_equal(test, control)
test = np.ndfromtxt(StringIO(dstr), delimiter=";", dtype=float, converters={0: float})
control = np.array([2009.0, 23.0, 46])
assert_equal(test, control)
开发者ID:hector1618,项目名称:numpy,代码行数:8,代码来源:test_io.py
示例3: test_unused_converter
def test_unused_converter(self):
"Test whether unused converters are forgotten"
data = StringIO("1 21\n 3 42\n")
test = np.ndfromtxt(data, usecols=(1,), converters={0: lambda s: int(s, 16)})
assert_equal(test, [21, 42])
#
data.seek(0)
test = np.ndfromtxt(data, usecols=(1,), converters={1: lambda s: int(s, 16)})
assert_equal(test, [33, 66])
开发者ID:hector1618,项目名称:numpy,代码行数:9,代码来源:test_io.py
示例4: test_autostrip
def test_autostrip(self):
"Test autostrip"
data = "01/01/2003 , 1.3, abcde"
kwargs = dict(delimiter=",", dtype=None)
mtest = np.ndfromtxt(StringIO(data), **kwargs)
ctrl = np.array([("01/01/2003 ", 1.3, " abcde")], dtype=[("f0", "|S12"), ("f1", float), ("f2", "|S8")])
assert_equal(mtest, ctrl)
mtest = np.ndfromtxt(StringIO(data), autostrip=True, **kwargs)
ctrl = np.array([("01/01/2003", 1.3, "abcde")], dtype=[("f0", "|S10"), ("f1", float), ("f2", "|S5")])
assert_equal(mtest, ctrl)
开发者ID:hector1618,项目名称:numpy,代码行数:10,代码来源:test_io.py
示例5: test_1D
def test_1D(self):
"Test squeezing to 1D"
control = np.array([1, 2, 3, 4], int)
#
data = StringIO.StringIO('1\n2\n3\n4\n')
test = np.ndfromtxt(data, dtype=int)
assert_array_equal(test, control)
#
data = StringIO.StringIO('1,2,3,4\n')
test = np.ndfromtxt(data, dtype=int, delimiter=',')
assert_array_equal(test, control)
开发者ID:GunioRobot,项目名称:numpy-refactor,代码行数:11,代码来源:test_io.py
示例6: test_comments
def test_comments(self):
"Test the stripping of comments"
control = np.array([1, 2, 3, 5], int)
# Comment on its own line
data = StringIO.StringIO('# comment\n1,2,3,5\n')
test = np.ndfromtxt(data, dtype=int, delimiter=',', comments='#')
assert_equal(test, control)
# Comment at the end of a line
data = StringIO.StringIO('1,2,3,5# comment\n')
test = np.ndfromtxt(data, dtype=int, delimiter=',', comments='#')
assert_equal(test, control)
开发者ID:GunioRobot,项目名称:numpy-refactor,代码行数:11,代码来源:test_io.py
示例7: test_comments
def test_comments(self):
"Test the stripping of comments"
control = np.array([1, 2, 3, 5], int)
# Comment on its own line
data = StringIO("# comment\n1,2,3,5\n")
test = np.ndfromtxt(data, dtype=int, delimiter=asbytes(","), comments=asbytes("#"))
assert_equal(test, control)
# Comment at the end of a line
data = StringIO("1,2,3,5# comment\n")
test = np.ndfromtxt(data, dtype=int, delimiter=asbytes(","), comments=asbytes("#"))
assert_equal(test, control)
开发者ID:hector1618,项目名称:numpy,代码行数:11,代码来源:test_io.py
示例8: test_incomplete_names
def test_incomplete_names(self):
"Test w/ incomplete names"
data = "A,,C\n0,1,2\n3,4,5"
kwargs = dict(delimiter=",", names=True)
# w/ dtype=None
ctrl = np.array([(0, 1, 2), (3, 4, 5)], dtype=[(_, int) for _ in ("A", "f0", "C")])
test = np.ndfromtxt(StringIO(data), dtype=None, **kwargs)
assert_equal(test, ctrl)
# w/ default dtype
ctrl = np.array([(0, 1, 2), (3, 4, 5)], dtype=[(_, float) for _ in ("A", "f0", "C")])
test = np.ndfromtxt(StringIO(data), **kwargs)
开发者ID:hector1618,项目名称:numpy,代码行数:11,代码来源:test_io.py
示例9: test_dtype_with_converters
def test_dtype_with_converters(self):
dstr = "2009; 23; 46"
test = np.ndfromtxt(StringIO.StringIO(dstr,),
delimiter=";", dtype=float, converters={0:str})
control = np.array([('2009', 23., 46)],
dtype=[('f0','|S4'), ('f1', float), ('f2', float)])
assert_equal(test, control)
test = np.ndfromtxt(StringIO.StringIO(dstr,),
delimiter=";", dtype=float, converters={0:float})
control = np.array([2009., 23., 46],)
assert_equal(test, control)
开发者ID:GunioRobot,项目名称:numpy-refactor,代码行数:11,代码来源:test_io.py
示例10: test_fixed_width_names
def test_fixed_width_names(self):
"Test fix-width w/ names"
data = " A B C\n 0 1 2.3\n 45 67 9."
kwargs = dict(delimiter=(5, 5, 4), names=True, dtype=None)
ctrl = np.array([(0, 1, 2.3), (45, 67, 9.0)], dtype=[("A", int), ("B", int), ("C", float)])
test = np.ndfromtxt(StringIO(data), **kwargs)
assert_equal(test, ctrl)
#
kwargs = dict(delimiter=5, names=True, dtype=None)
ctrl = np.array([(0, 1, 2.3), (45, 67, 9.0)], dtype=[("A", int), ("B", int), ("C", float)])
test = np.ndfromtxt(StringIO(data), **kwargs)
assert_equal(test, ctrl)
开发者ID:hector1618,项目名称:numpy,代码行数:12,代码来源:test_io.py
示例11: test_autostrip
def test_autostrip(self):
"Test autostrip"
data = "01/01/2003 , 1.3, abcde"
kwargs = dict(delimiter=",", dtype=None)
mtest = np.ndfromtxt(StringIO(data), **kwargs)
ctrl = np.array([('01/01/2003 ', 1.3, ' abcde')],
dtype=[('f0', '|S12'), ('f1', float), ('f2', '|S8')])
assert_equal(mtest, ctrl)
mtest = np.ndfromtxt(StringIO(data), autostrip=True, **kwargs)
ctrl = np.array([('01/01/2003', 1.3, 'abcde')],
dtype=[('f0', '|S10'), ('f1', float), ('f2', '|S5')])
assert_equal(mtest, ctrl)
开发者ID:andyk,项目名称:load_gen,代码行数:12,代码来源:test_io.py
示例12: test_record
def test_record(self):
"Test w/ explicit dtype"
data = StringIO(asbytes("1 2\n3 4"))
# data.seek(0)
test = np.ndfromtxt(data, dtype=[("x", np.int32), ("y", np.int32)])
control = np.array([(1, 2), (3, 4)], dtype=[("x", "i4"), ("y", "i4")])
assert_equal(test, control)
#
data = StringIO("M 64.0 75.0\nF 25.0 60.0")
# data.seek(0)
descriptor = {"names": ("gender", "age", "weight"), "formats": ("S1", "i4", "f4")}
control = np.array([("M", 64.0, 75.0), ("F", 25.0, 60.0)], dtype=descriptor)
test = np.ndfromtxt(data, dtype=descriptor)
assert_equal(test, control)
开发者ID:hector1618,项目名称:numpy,代码行数:14,代码来源:test_io.py
示例13: test_converters_cornercases
def test_converters_cornercases(self):
"Test the conversion to datetime."
converter = {"date": lambda s: strptime(s, "%Y-%m-%d %H:%M:%SZ")}
data = StringIO("2009-02-03 12:00:00Z, 72214.0")
test = np.ndfromtxt(data, delimiter=",", dtype=None, names=["date", "stid"], converters=converter)
control = np.array((datetime(2009, 02, 03), 72214.0), dtype=[("date", np.object_), ("stid", float)])
assert_equal(test, control)
开发者ID:hector1618,项目名称:numpy,代码行数:7,代码来源:test_io.py
示例14: test_fancy_dtype
def test_fancy_dtype(self):
"Check that a nested dtype isn't MIA"
data = StringIO("1,2,3.0\n4,5,6.0\n")
fancydtype = np.dtype([("x", int), ("y", [("t", int), ("s", float)])])
test = np.ndfromtxt(data, dtype=fancydtype, delimiter=",")
control = np.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype)
assert_equal(test, control)
开发者ID:hector1618,项目名称:numpy,代码行数:7,代码来源:test_io.py
示例15: test_filling_values
def test_filling_values(self):
"Test missing values"
data = "1, 2, 3\n1, , 5\n0, 6, \n"
kwargs = dict(delimiter=",", dtype=None, filling_values=-999)
ctrl = np.array([[1, 2, 3], [1, -999, 5], [0, 6, -999]], dtype=int)
test = np.ndfromtxt(StringIO(data), **kwargs)
assert_equal(test, ctrl)
开发者ID:hector1618,项目名称:numpy,代码行数:7,代码来源:test_io.py
示例16: test_single_dtype_wo_names
def test_single_dtype_wo_names(self):
"Test single dtype w/o names"
data = "0, 1, 2.3\n4, 5, 6.7"
mtest = np.ndfromtxt(StringIO(data),
delimiter=",", dtype=float, defaultfmt="f%02i")
ctrl = np.array([[0., 1., 2.3], [4., 5., 6.7]], dtype=float)
assert_equal(mtest, ctrl)
开发者ID:andyk,项目名称:load_gen,代码行数:7,代码来源:test_io.py
示例17: from_txt
def from_txt(self, filename, nlines=None, dtype=float, **kwargs):
"""
Extract data from file to a single big ndarray.
"""
with open(filename, 'rb') as f:
delimiter = kwargs.get('delimiter', ',')
skiprows = kwargs.get('skiprows', 0)
# Total number of columns.
num_cols = len(f.readline().split(delimiter))
# Use columns defined by indexes.
usecols = kwargs.get('usecols', None)
if not usecols:
# Skip columns from left side.
skipcols = kwargs.get('skipcols', 0)
usecols=range(skipcols, num_cols)
# Ensure list.
if isinstance(usecols, int):
usecols = [usecols]
# Number of examples.
f.seek(0)
if nlines > 0:
f = itertools.islice(f, nlines + skiprows)
data = np.ndfromtxt(f,
dtype=dtype,
skiprows=skiprows,
delimiter=delimiter,
usecols=usecols)
return np.nan_to_num(data)
开发者ID:deniskolokol,项目名称:feature-norm,代码行数:33,代码来源:featurenorm.py
示例18: test_fancy_dtype
def test_fancy_dtype(self):
"Check that a nested dtype isn't MIA"
data = StringIO.StringIO('1,2,3.0\n4,5,6.0\n')
fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])])
test = np.ndfromtxt(data, dtype=fancydtype, delimiter=',')
control = np.array([(1,(2,3.0)),(4,(5,6.0))], dtype=fancydtype)
assert_equal(test, control)
开发者ID:GunioRobot,项目名称:numpy-refactor,代码行数:7,代码来源:test_io.py
示例19: test_spacedelimiter
def test_spacedelimiter(self):
"Test space delimiter"
data = StringIO.StringIO("1 2 3 4 5\n6 7 8 9 10")
test = np.ndfromtxt(data)
control = np.array([[ 1., 2., 3., 4., 5.],
[ 6., 7., 8., 9.,10.]])
assert_equal(test, control)
开发者ID:GunioRobot,项目名称:numpy-refactor,代码行数:7,代码来源:test_io.py
示例20: test_record
def test_record(self):
"Test w/ explicit dtype"
data = StringIO.StringIO('1 2\n3 4')
# data.seek(0)
test = np.ndfromtxt(data, dtype=[('x', np.int32), ('y', np.int32)])
control = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
assert_equal(test, control)
#
data = StringIO.StringIO('M 64.0 75.0\nF 25.0 60.0')
# data.seek(0)
descriptor = {'names': ('gender','age','weight'),
'formats': ('S1', 'i4', 'f4')}
control = np.array([('M', 64.0, 75.0), ('F', 25.0, 60.0)],
dtype=descriptor)
test = np.ndfromtxt(data, dtype=descriptor)
assert_equal(test, control)
开发者ID:GunioRobot,项目名称:numpy-refactor,代码行数:16,代码来源:test_io.py
注:本文中的numpy.ndfromtxt函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论