本文整理汇总了Python中numpy.ndfromtxt函数的典型用法代码示例。如果您正苦于以下问题:Python ndfromtxt函数的具体用法?Python ndfromtxt怎么用?Python ndfromtxt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ndfromtxt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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])
示例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)
示例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])
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)