本文整理汇总了Python中numpy.lib.arraysetops.unique函数的典型用法代码示例。如果您正苦于以下问题:Python unique函数的具体用法?Python unique怎么用?Python unique使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了unique函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: findCoords
def findCoords(gs, candidates=None):
if candidates == None:
candidates=[]
# List all the possible z-level (heights)
zRange = list(takewhile(lambda x : x < gs.boardSize[2], \
sort(unique(flatten(gs.heightMap())))))
if zRange==[]:
print "Board is full, cannot find legal coordinates !"
return None
else:
zRange = sort(unique(map(third,candidates)))
# Do we have a choice on the z-level ?
if len(zRange)==1:
z = zRange[0]
else:
print "\n",gs.boardToASCII(markedCubes=candidates)
# Discard the z height max
if zRange[-1]==gs.boardSize[2]:
zRange = zRange[:-1]
z = -1+input("Which z-level ? (%d-%d)\n> " \
% (zRange[0]+1,zRange[-1]+1))
candidates = filter(lambda c: c[2]==z, candidates)
if len(candidates)>1:
# Display the z-level with xy coordinates as letter-number
print ' '+''.join(chr(97+x) for x in xrange(gs.boardSize[0]))
print ' +'+'-'*gs.boardSize[0]
lines = gs.boardToASCII(zRange=[z],markedCubes=candidates)\
.split('\n')
for y in xrange(gs.boardSize[1]):
print '%s |%s' % (str(y+1).zfill(2),lines[y])
print "\n"
xy = raw_input("Which xy coordinates ?\n> ")
return array([ord(xy[0])-97,int(xy[1:])-1,z])
else:
return candidates[0]
开发者ID:didmar,项目名称:blokus3d-python,代码行数:35,代码来源:interface.py
示例2: findMove
def findMove(gs, askApply=True):
moves = gs.legalMoves()
if len(moves)==1:
print "Only one move possible :\n", moveToASCII(moves[0])
else:
ok = False
while not ok:
# First, pick a block
blkId = findBlock(gs,candidates=unique(map(snd,moves)))
assert blkId != None # since we checked that len(lm) was > 0
# Filter the moves that have the selected block id
moves = filter(lambda m : m[1]==blkId, moves)
# Then, find the coordinates on the board
coords = findCoords(gs,candidates=unik(map(fst,moves)))
# Filter the moves that have the selected coordinates
moves = filter(lambda m : (m[0]==coords).all(), moves)
# Finally, find its variation
blkVarId = findVariation(gs,blkId, \
candidates=unique(map(third,moves)))
move = (coords,blkId,blkVarId)
print "You have selected :\n", moveToASCII(moves[0])
print "Is this the move you wanted ? [Y/n]"
if raw_input("") in ["n","N"]:
# Will start again with all legal moves possibles
moves = gs.legalMoves()
else:
ok = True
if askApply:
print "Do you want to play this move over the current gamestate ?",\
" [Y/n]"
if raw_input("") not in ["n","N"]:
gs.playMove(move)
return move
开发者ID:didmar,项目名称:blokus3d-python,代码行数:33,代码来源:interface.py
示例3: uniqueIdx
def uniqueIdx(L):
"""
Find indexes of unique elements in L
based on their string representation
(works both for cubes and blocks)
"""
return list(snd(unique([str(x) for x in L], return_index=True)))
开发者ID:didmar,项目名称:blokus3d-python,代码行数:7,代码来源:utils.py
示例4: test_unique_axis
def test_unique_axis(self):
types = []
types.extend(np.typecodes['AllInteger'])
types.extend(np.typecodes['AllFloat'])
types.append('datetime64[D]')
types.append('timedelta64[D]')
types.append([('a', int), ('b', int)])
types.append([('a', int), ('b', float)])
for dtype in types:
self._run_axis_tests(dtype)
msg = 'Non-bitwise-equal booleans test failed'
data = np.arange(10, dtype=np.uint8).reshape(-1, 2).view(bool)
result = np.array([[False, True], [True, True]], dtype=bool)
assert_array_equal(unique(data, axis=0), result, msg)
msg = 'Negative zero equality test failed'
data = np.array([[-0.0, 0.0], [0.0, -0.0], [-0.0, 0.0], [0.0, -0.0]])
result = np.array([[-0.0, 0.0]])
assert_array_equal(unique(data, axis=0), result, msg)
开发者ID:ChrisBarker-NOAA,项目名称:numpy,代码行数:21,代码来源:test_arraysetops.py
示例5: check_all
def check_all(a, b, i1, i2, c, dt):
base_msg = 'check {0} failed for type {1}'
msg = base_msg.format('values', dt)
v = unique(a)
assert_array_equal(v, b, msg)
msg = base_msg.format('return_index', dt)
v, j = unique(a, 1, 0, 0)
assert_array_equal(v, b, msg)
assert_array_equal(j, i1, msg)
msg = base_msg.format('return_inverse', dt)
v, j = unique(a, 0, 1, 0)
assert_array_equal(v, b, msg)
assert_array_equal(j, i2, msg)
msg = base_msg.format('return_counts', dt)
v, j = unique(a, 0, 0, 1)
assert_array_equal(v, b, msg)
assert_array_equal(j, c, msg)
msg = base_msg.format('return_index and return_inverse', dt)
v, j1, j2 = unique(a, 1, 1, 0)
assert_array_equal(v, b, msg)
assert_array_equal(j1, i1, msg)
assert_array_equal(j2, i2, msg)
msg = base_msg.format('return_index and return_counts', dt)
v, j1, j2 = unique(a, 1, 0, 1)
assert_array_equal(v, b, msg)
assert_array_equal(j1, i1, msg)
assert_array_equal(j2, c, msg)
msg = base_msg.format('return_inverse and return_counts', dt)
v, j1, j2 = unique(a, 0, 1, 1)
assert_array_equal(v, b, msg)
assert_array_equal(j1, i2, msg)
assert_array_equal(j2, c, msg)
msg = base_msg.format(('return_index, return_inverse '
'and return_counts'), dt)
v, j1, j2, j3 = unique(a, 1, 1, 1)
assert_array_equal(v, b, msg)
assert_array_equal(j1, i1, msg)
assert_array_equal(j2, i2, msg)
assert_array_equal(j3, c, msg)
开发者ID:ymarfoq,项目名称:outilACVDesagregation,代码行数:47,代码来源:test_arraysetops.py
示例6: _run_axis_tests
def _run_axis_tests(self, dtype):
data = np.array([[0, 1, 0, 0],
[1, 0, 0, 0],
[0, 1, 0, 0],
[1, 0, 0, 0]]).astype(dtype)
msg = 'Unique with 1d array and axis=0 failed'
result = np.array([0, 1])
assert_array_equal(unique(data), result.astype(dtype), msg)
msg = 'Unique with 2d array and axis=0 failed'
result = np.array([[0, 1, 0, 0], [1, 0, 0, 0]])
assert_array_equal(unique(data, axis=0), result.astype(dtype), msg)
msg = 'Unique with 2d array and axis=1 failed'
result = np.array([[0, 0, 1], [0, 1, 0], [0, 0, 1], [0, 1, 0]])
assert_array_equal(unique(data, axis=1), result.astype(dtype), msg)
msg = 'Unique with 3d array and axis=2 failed'
data3d = np.dstack([data] * 3)
result = data3d[..., :1]
assert_array_equal(unique(data3d, axis=2), result, msg)
uniq, idx, inv, cnt = unique(data, axis=0, return_index=True,
return_inverse=True, return_counts=True)
msg = "Unique's return_index=True failed with axis=0"
assert_array_equal(data[idx], uniq, msg)
msg = "Unique's return_inverse=True failed with axis=0"
assert_array_equal(uniq[inv], data)
msg = "Unique's return_counts=True failed with axis=0"
assert_array_equal(cnt, np.array([2, 2]), msg)
uniq, idx, inv, cnt = unique(data, axis=1, return_index=True,
return_inverse=True, return_counts=True)
msg = "Unique's return_index=True failed with axis=1"
assert_array_equal(data[:, idx], uniq)
msg = "Unique's return_inverse=True failed with axis=1"
assert_array_equal(uniq[:, inv], data)
msg = "Unique's return_counts=True failed with axis=1"
assert_array_equal(cnt, np.array([2, 1, 1]), msg)
开发者ID:ChrisBarker-NOAA,项目名称:numpy,代码行数:40,代码来源:test_arraysetops.py
示例7: test_unique
def test_unique(self):
def check_all(a, b, i1, i2, c, dt):
base_msg = 'check {0} failed for type {1}'
msg = base_msg.format('values', dt)
v = unique(a)
assert_array_equal(v, b, msg)
msg = base_msg.format('return_index', dt)
v, j = unique(a, 1, 0, 0)
assert_array_equal(v, b, msg)
assert_array_equal(j, i1, msg)
msg = base_msg.format('return_inverse', dt)
v, j = unique(a, 0, 1, 0)
assert_array_equal(v, b, msg)
assert_array_equal(j, i2, msg)
msg = base_msg.format('return_counts', dt)
v, j = unique(a, 0, 0, 1)
assert_array_equal(v, b, msg)
assert_array_equal(j, c, msg)
msg = base_msg.format('return_index and return_inverse', dt)
v, j1, j2 = unique(a, 1, 1, 0)
assert_array_equal(v, b, msg)
assert_array_equal(j1, i1, msg)
assert_array_equal(j2, i2, msg)
msg = base_msg.format('return_index and return_counts', dt)
v, j1, j2 = unique(a, 1, 0, 1)
assert_array_equal(v, b, msg)
assert_array_equal(j1, i1, msg)
assert_array_equal(j2, c, msg)
msg = base_msg.format('return_inverse and return_counts', dt)
v, j1, j2 = unique(a, 0, 1, 1)
assert_array_equal(v, b, msg)
assert_array_equal(j1, i2, msg)
assert_array_equal(j2, c, msg)
msg = base_msg.format(('return_index, return_inverse '
'and return_counts'), dt)
v, j1, j2, j3 = unique(a, 1, 1, 1)
assert_array_equal(v, b, msg)
assert_array_equal(j1, i1, msg)
assert_array_equal(j2, i2, msg)
assert_array_equal(j3, c, msg)
a = [5, 7, 1, 2, 1, 5, 7]*10
b = [1, 2, 5, 7]
i1 = [2, 3, 0, 1]
i2 = [2, 3, 0, 1, 0, 2, 3]*10
c = np.multiply([2, 1, 2, 2], 10)
# test for numeric arrays
types = []
types.extend(np.typecodes['AllInteger'])
types.extend(np.typecodes['AllFloat'])
types.append('datetime64[D]')
types.append('timedelta64[D]')
for dt in types:
aa = np.array(a, dt)
bb = np.array(b, dt)
check_all(aa, bb, i1, i2, c, dt)
# test for object arrays
dt = 'O'
aa = np.empty(len(a), dt)
aa[:] = a
bb = np.empty(len(b), dt)
bb[:] = b
check_all(aa, bb, i1, i2, c, dt)
# test for structured arrays
dt = [('', 'i'), ('', 'i')]
aa = np.array(list(zip(a, a)), dt)
bb = np.array(list(zip(b, b)), dt)
check_all(aa, bb, i1, i2, c, dt)
# test for ticket #2799
aa = [1. + 0.j, 1 - 1.j, 1]
assert_array_equal(np.unique(aa), [1. - 1.j, 1. + 0.j])
# test for ticket #4785
a = [(1, 2), (1, 2), (2, 3)]
unq = [1, 2, 3]
inv = [0, 1, 0, 1, 1, 2]
a1 = unique(a)
assert_array_equal(a1, unq)
a2, a2_inv = unique(a, return_inverse=True)
assert_array_equal(a2, unq)
assert_array_equal(a2_inv, inv)
开发者ID:ymarfoq,项目名称:outilACVDesagregation,代码行数:94,代码来源:test_arraysetops.py
示例8: test_unique_axis_list
def test_unique_axis_list(self):
msg = "Unique failed on list of lists"
inp = [[0, 1, 0], [0, 1, 0]]
inp_arr = np.asarray(inp)
assert_array_equal(unique(inp, axis=0), unique(inp_arr, axis=0), msg)
assert_array_equal(unique(inp, axis=1), unique(inp_arr, axis=1), msg)
开发者ID:ChrisBarker-NOAA,项目名称:numpy,代码行数:6,代码来源:test_arraysetops.py
示例9: print_unique_counts
def print_unique_counts(d):
column_list = d.columns.tolist()
print "number of rows: {}".format(len(d[column_list[0]]))
print ""
for c in column_list:
print "number of unique {}: {}".format(c,len(arraysetops.unique(d[c])))
开发者ID:yz-,项目名称:ut,代码行数:6,代码来源:bull_shit_hack_because_imports_dont_work_WTF.py
示例10: fit
def fit(self, X, y, sample_mask=None, X_argsorted=None, check_input=True,
sample_weight=None):
random_state = check_random_state(self.random_state)
# Deprecations
if sample_mask is not None:
warn("The sample_mask parameter is deprecated as of version 0.14 "
"and will be removed in 0.16.", DeprecationWarning)
if X_argsorted is not None:
warn("The X_argsorted parameter is deprecated as of version 0.14 "
"and will be removed in 0.16.", DeprecationWarning)
# Convert data
if check_input:
X = check_array(X, dtype=DTYPE, accept_sparse="csc")
if issparse(X):
X.sort_indices()
if X.indices.dtype != np.intc or X.indptr.dtype != np.intc:
raise ValueError("No support for np.int64 index based "
"sparse matrices")
# Determine output settings
n_samples, self.n_features_ = X.shape
is_classification = isinstance(self, ClassifierMixin)
y = np.atleast_1d(y)
if y.ndim == 1:
# reshape is necessary to preserve the data contiguity against vs
# [:, np.newaxis] that does not.
y = np.reshape(y, (-1, 1))
self.n_outputs_ = y.shape[1]
if is_classification:
y = np.copy(y)
self.classes_ = []
self.n_classes_ = []
for k in six.moves.range(self.n_outputs_):
classes_k, y[:, k] = unique(y[:, k], return_inverse=True)
self.classes_.append(classes_k)
self.n_classes_.append(classes_k.shape[0])
else:
self.classes_ = [None] * self.n_outputs_
self.n_classes_ = [1] * self.n_outputs_
self.n_classes_ = np.array(self.n_classes_, dtype=np.intp)
max_depth = 1
max_features = 10
if getattr(y, "dtype", None) != DOUBLE or not y.flags.contiguous:
y = np.ascontiguousarray(y, dtype=DOUBLE)
if len(y) != n_samples:
raise ValueError("Number of labels=%d does not match "
"number of samples=%d" % (len(y), n_samples))
if self.min_samples_split <= 0:
raise ValueError("min_samples_split must be greater than zero.")
if self.min_samples_leaf <= 0:
raise ValueError("min_samples_leaf must be greater than zero.")
if max_depth <= 0:
raise ValueError("max_depth must be greater than zero. ")
if not (0 < max_features <= self.n_features_):
raise ValueError("max_features must be in (0, n_features]")
if sample_weight is not None:
if (getattr(sample_weight, "dtype", None) != DOUBLE or
not sample_weight.flags.contiguous):
sample_weight = np.ascontiguousarray(
sample_weight, dtype=DOUBLE)
if len(sample_weight.shape) > 1:
raise ValueError("Sample weights array has more "
"than one dimension: %d" %
len(sample_weight.shape))
if len(sample_weight) != n_samples:
raise ValueError("Number of weights=%d does not match "
"number of samples=%d" %
(len(sample_weight), n_samples))
if self.method == 'default':
self.tree_ = _fit_regressor_stump(X, y, sample_weight, X_argsorted)
elif self.method == 'threaded':
self.tree_ = _fit_regressor_stump_threaded(X, y, sample_weight, X_argsorted)
elif self.method == 'c':
self.tree_ = _fit_regressor_stump_c_ext(X, y, sample_weight, X_argsorted)
elif self.method == 'c_threaded':
self.tree_ = _fit_regressor_stump_c_ext_threaded(X, y, sample_weight, X_argsorted)
else:
self.tree_ = _fit_regressor_stump(X, y, sample_weight, X_argsorted)
if self.n_outputs_ == 1:
self.n_classes_ = self.n_classes_[0]
self.classes_ = self.classes_[0]
return self
开发者ID:hbldh,项目名称:skboost,代码行数:99,代码来源:regression_stump.py
注:本文中的numpy.lib.arraysetops.unique函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论