本文整理汇总了Python中numpy.chararray方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.chararray方法的具体用法?Python numpy.chararray怎么用?Python numpy.chararray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.chararray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: display_policy_grid
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def display_policy_grid(self, policy):
"""
prints a nice table of the policy in grid
input:
policy a dictionary of the optimal policy {<state, action_dist>}
"""
print "==Display policy grid=="
policy_grid = np.chararray((len(self.grid), len(self.grid[0])))
for k in self.get_states():
if self.is_terminal((k[0], k[1])) or self.grid[k[0]][k[1]] == 'x':
policy_grid[k[0]][k[1]] = '-'
else:
# policy_grid[k[0]][k[1]] = self.dirs[agent.get_action((k[0], k[1]))]
policy_grid[k[0]][k[1]] = self.dirs[policy[(k[0], k[1])][0][0]]
row_format = '{:>20}' * (len(self.grid[0]))
for row in policy_grid:
print row_format.format(*row)
示例2: test_slice
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def test_slice(self):
"""Regression test for https://github.com/numpy/numpy/issues/5982"""
arr = np.array([['abc ', 'def '], ['geh ', 'ijk ']],
dtype='S4').view(np.chararray)
sl1 = arr[:]
assert_array_equal(sl1, arr)
assert_(sl1.base is arr)
assert_(sl1.base.base is arr.base)
sl2 = arr[:, :]
assert_array_equal(sl2, arr)
assert_(sl2.base is arr)
assert_(sl2.base.base is arr.base)
assert_(arr[0, 0] == b'abc')
示例3: test_slice
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def test_slice(self):
"""Regression test for https://github.com/numpy/numpy/issues/5982"""
arr = np.array([['abc ', 'def '], ['geh ', 'ijk ']],
dtype='S4').view(np.chararray)
sl1 = arr[:]
assert_array_equal(sl1, arr)
assert_(sl1.base is arr)
assert_(sl1.base.base is arr.base)
sl2 = arr[:, :]
assert_array_equal(sl2, arr)
assert_(sl2.base is arr)
assert_(sl2.base.base is arr.base)
assert_(arr[0, 0] == asbytes('abc'))
示例4: min_max_years
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def min_max_years(config, image, before):
""" Exclude data outside of min and max year desired """
min_year = int(config['postprocessing']['minimum_year'])
if not min_year:
min_year = 1980
max_year = int(config['postprocessing']['maximum_year'])
if not max_year:
max_year = 2200
year_image = image[0,:,:].astype(np.str).view(np.chararray).ljust(4)
year_image = np.array(year_image).astype(np.float)
bad_indices = np.logical_or(year_image < min_year, year_image > max_year)
for i in range(image.shape[0] - 1):
image[i,:,:][bad_indices] = 0
image[image.shape[0]-1,:,:][bad_indices] = before[bad_indices]
return image
示例5: setup
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def setup(self):
self.A = np.array([['abc ', '123 '],
['789 ', 'xyz ']]).view(np.chararray)
self.B = np.array([['abc', '123'],
['789', 'xyz']]).view(np.chararray)
示例6: test_add
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def test_add(self):
AB = np.array([['abcefg', '123456'],
['789051', 'xyztuv']]).view(np.chararray)
assert_array_equal(AB, (self.A + self.B))
assert_(len((self.A + self.B)[0][0]) == 6)
示例7: test_radd
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def test_radd(self):
QA = np.array([['qabc', 'q123'],
['q789', 'qxyz']]).view(np.chararray)
assert_array_equal(QA, ('q' + self.A))
示例8: test_mul
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def test_mul(self):
A = self.A
for r in (2, 3, 5, 7, 197):
Ar = np.array([[A[0, 0]*r, A[0, 1]*r],
[A[1, 0]*r, A[1, 1]*r]]).view(np.chararray)
assert_array_equal(Ar, (self.A * r))
for ob in [object(), 'qrs']:
with assert_raises_regex(ValueError,
'Can only multiply by integers'):
A*ob
示例9: test_mod
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def test_mod(self):
"""Ticket #856"""
F = np.array([['%d', '%f'], ['%s', '%r']]).view(np.chararray)
C = np.array([[3, 7], [19, 1]])
FC = np.array([['3', '7.000000'],
['19', '1']]).view(np.chararray)
assert_array_equal(FC, F % C)
A = np.array([['%.3f', '%d'], ['%s', '%r']]).view(np.chararray)
A1 = np.array([['1.000', '1'], ['1', '1']]).view(np.chararray)
assert_array_equal(A1, (A % 1))
A2 = np.array([['1.000', '2'], ['3', '4']]).view(np.chararray)
assert_array_equal(A2, (A % [[1, 2], [3, 4]]))
示例10: test_rmod
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def test_rmod(self):
assert_(("%s" % self.A) == str(self.A))
assert_(("%r" % self.A) == repr(self.A))
for ob in [42, object()]:
with assert_raises_regex(
TypeError, "unsupported operand type.* and 'chararray'"):
ob % self.A
示例11: test_empty_indexing
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def test_empty_indexing():
"""Regression test for ticket 1948."""
# Check that indexing a chararray with an empty list/array returns an
# empty chararray instead of a chararray with a single empty string in it.
s = np.chararray((4,))
assert_(s[[]].size == 0)
示例12: test_chararray_rstrip
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def test_chararray_rstrip(self):
# Ticket #222
x = np.chararray((1,), 5)
x[0] = b'a '
x = x.rstrip()
assert_equal(x[0], b'a')
示例13: get_sequence_strings
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def get_sequence_strings(encoded_sequences):
"""
Converts encoded sequences into an array with sequence strings
"""
num_samples, _, _, seq_length = np.shape(encoded_sequences)
sequence_characters = np.chararray((num_samples, seq_length))
sequence_characters[:] = 'N'
for i, letter in enumerate(['A', 'C', 'G', 'T']):
letter_indxs = (encoded_sequences[:, :, i, :] == 1).squeeze()
sequence_characters[letter_indxs] = letter
# return 1D view of sequence characters
return sequence_characters.view('S%s' % (seq_length)).ravel()
示例14: setUp
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def setUp(self):
self.A = np.array([['abc ', '123 '],
['789 ', 'xyz ']]).view(np.chararray)
self.B = np.array([['abc', '123'],
['789', 'xyz']]).view(np.chararray)
示例15: test_mul
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import chararray [as 别名]
def test_mul(self):
A = self.A
for r in (2, 3, 5, 7, 197):
Ar = np.array([[A[0, 0]*r, A[0, 1]*r],
[A[1, 0]*r, A[1, 1]*r]]).view(np.chararray)
assert_array_equal(Ar, (self.A * r))
for ob in [object(), 'qrs']:
try:
A * ob
except ValueError:
pass
else:
self.fail("chararray can only be multiplied by integers")