def test_numpy_one(self):
o = Identity()
n = np.r_[2., 3]
assert np.all(n+1 == n+o)
assert np.all(1+n == o+n)
assert np.all(n-1 == n-o)
assert np.all(1-n == o-n)
assert np.all(n/1 == n/o)
assert np.all(n/-1 == n/-o)
assert np.all(1/n == o/n)
assert np.all(-1/n == -o/n)
assert np.all(n*1 == n*o)
assert np.all(n*-1 == n*-o)
assert np.all(1*n == o*n)
assert np.all(-1*n == -o*n)
python类r_()的实例源码
def test_float64_pass(self):
# The number of units of least precision
# In this case, use a few places above the lowest level (ie nulp=1)
nulp = 5
x = np.linspace(-20, 20, 50, dtype=np.float64)
x = 10**x
x = np.r_[-x, x]
# Addition
eps = np.finfo(x.dtype).eps
y = x + x*eps*nulp/2.
assert_array_almost_equal_nulp(x, y, nulp)
# Subtraction
epsneg = np.finfo(x.dtype).epsneg
y = x - x*epsneg*nulp/2.
assert_array_almost_equal_nulp(x, y, nulp)
def test_complex64_pass(self):
nulp = 5
x = np.linspace(-20, 20, 50, dtype=np.float32)
x = 10**x
x = np.r_[-x, x]
xi = x + x*1j
eps = np.finfo(x.dtype).eps
y = x + x*eps*nulp/2.
assert_array_almost_equal_nulp(xi, x + y*1j, nulp)
assert_array_almost_equal_nulp(xi, y + x*1j, nulp)
y = x + x*eps*nulp/4.
assert_array_almost_equal_nulp(xi, y + y*1j, nulp)
epsneg = np.finfo(x.dtype).epsneg
y = x - x*epsneg*nulp/2.
assert_array_almost_equal_nulp(xi, x + y*1j, nulp)
assert_array_almost_equal_nulp(xi, y + x*1j, nulp)
y = x - x*epsneg*nulp/4.
assert_array_almost_equal_nulp(xi, y + y*1j, nulp)
def test_2d(self):
# Tests mr_ on 2D arrays.
a_1 = np.random.rand(5, 5)
a_2 = np.random.rand(5, 5)
m_1 = np.round_(np.random.rand(5, 5), 0)
m_2 = np.round_(np.random.rand(5, 5), 0)
b_1 = masked_array(a_1, mask=m_1)
b_2 = masked_array(a_2, mask=m_2)
# append columns
d = mr_['1', b_1, b_2]
self.assertTrue(d.shape == (5, 10))
assert_array_equal(d[:, :5], b_1)
assert_array_equal(d[:, 5:], b_2)
assert_array_equal(d.mask, np.r_['1', m_1, m_2])
d = mr_[b_1, b_2]
self.assertTrue(d.shape == (10, 5))
assert_array_equal(d[:5,:], b_1)
assert_array_equal(d[5:,:], b_2)
assert_array_equal(d.mask, np.r_[m_1, m_2])
def from_points(cls, p, q, r):
abc = (p-r).cross(q-r)
hp = np.r_[abc, -abc.dot(r)]
return cls(hp)
def generateAllGaussianCenters(self, referenceSamples=None) :
"""
Generates kernels in the region where the P(X_reference) takes large values
"""
self.kernelBasis = referenceSamples.shape[1]
return referenceSamples[:, numpy.r_[0:self.kernelBasis]]
def generateFirstNGaussianCenters(self, referenceSamples=None) :
"""
Chooses the firts N samples as Gaussian centers as an optimization
"""
numcols = referenceSamples.shape[1]
self.kernelBasis = min(self.kernelBasis, numcols)
return referenceSamples[:, numpy.r_[0:self.kernelBasis]]
def predict(self, dataframe):
def is_invalid(row):
valid_cpf = cpf.validate(str(row['recipient_id']).zfill(11))
valid_cnpj = cnpj.validate(str(row['recipient_id']).zfill(14))
good_doctype = row['document_type'] in ('bill_of_sale', 'simple_receipt', 'unknown')
return good_doctype and (not (valid_cpf or valid_cnpj))
return np.r_[dataframe.apply(is_invalid, axis=1)]
def predict(self, X=None):
self._X['is_over_monthly_subquota_limit'] = False
for metadata in self.limits:
data, monthly_limit = metadata['data'], metadata['monthly_limit']
if len(data):
surplus_reimbursements = self.__find_surplus_reimbursements(data, monthly_limit)
self._X.loc[surplus_reimbursements.index,
'is_over_monthly_subquota_limit'] = True
results = self._X.loc[self.X.index, 'is_over_monthly_subquota_limit']
return np.r_[results]
def predict(self, X):
statuses = ['BAIXADA', 'NULA', 'SUSPENSA', 'INAPTA']
self._X = X.apply(self.__compare_date, axis=1)
return np.r_[self._X & X['situation'].isin(statuses)]
def disp(name=None, idx=None):
"""displays selected data from (files written by) the class
`CMADataLogger`.
The call ``cma.disp(name, idx)`` is a shortcut for
``cma.CMADataLogger(name).disp(idx)``.
Arguments
---------
`name`
name of the logger, filename prefix, `None` evaluates to
the default ``'outcmaes'``
`idx`
indices corresponding to rows in the data file; by
default the first five, then every 100-th, and the last
10 rows. Too large index values are removed.
The best ever observed iteration is also printed by default.
Examples
--------
::
import cma
from numpy import r_
# assume some data are available from previous runs
cma.disp(None, r_[0, -1]) # first and last
cma.disp(None, r_[0:int(1e9):100, -1]) # every 100-th and last
cma.disp(idx=r_[0, -10:0]) # first and ten last
cma.disp(idx=r_[0:int(1e9):1000, -10:0])
:See also: `CMADataLogger.disp`
"""
return CMADataLogger(name if name else CMADataLogger.default_prefix
).disp(idx)
# END cmaplt.py
def vec(self):
return np.r_[self.translation, self.quaternion]
def matrix(self):
""":obj:`numpy.ndarray` of float: The canonical 4x4 matrix
representation of this transform.
The first three columns contain the columns of the rotation matrix
followed by a zero, and the last column contains the translation vector
followed by a one.
"""
return np.r_[np.c_[self._rotation, self._translation], [[0,0,0,1]]]
def test_inverse(self):
R_a_b = RigidTransform.random_rotation()
t_a_b = RigidTransform.random_translation()
T_a_b = RigidTransform(R_a_b, t_a_b, 'a', 'b')
T_b_a = T_a_b.inverse()
# multiple with numpy arrays
M_a_b = np.r_[np.c_[R_a_b, t_a_b], [[0,0,0,1]]]
M_b_a = np.linalg.inv(M_a_b)
self.assertTrue(np.sum(np.abs(T_b_a.matrix - M_b_a)) < 1e-5, msg='Inverse gave incorrect transformation')
# check frames
self.assertEqual(T_b_a.from_frame, 'b', msg='Inverse has incorrect input frame')
self.assertEqual(T_b_a.to_frame, 'a', msg='Inverse has incorrect output frame')
def rank_bank_topic(bank_doc_map, doc_topic_dist):
"""Rank topics for banks
"""
bank_topic_ranks = {}
for each_bank in bank_doc_map:
rank = []
for each_doc in bank_doc_map[each_bank]:
rank.append(calc_ranks(doc_topic_dist[each_doc]))
rank = np.r_[rank]
# compute ranking score
bank_topic_ranks[each_bank] = np.mean(1. / rank, axis=0)
return bank_topic_ranks
def train(args):
corpus = load_corpus(args.input)
n_vocab, docs = len(corpus['vocab']), corpus['docs']
corpus.clear() # save memory
X_docs = []
for k in docs.keys():
X_docs.append(vecnorm(doc2vec(docs[k], n_vocab), 'logmax1', 0))
del docs[k]
np.random.seed(0)
np.random.shuffle(X_docs)
# X_docs_noisy = corrupted_matrix(np.r_[X_docs], 0.1)
n_val = args.n_val
# X_train = np.r_[X_docs[:-n_val]]
# X_val = np.r_[X_docs[-n_val:]]
X_train = np.r_[X_docs[:-n_val]]
del X_docs[:-n_val]
X_val = np.r_[X_docs]
del X_docs
start = timeit.default_timer()
vae = VarAutoEncoder(n_vocab, args.n_dim, comp_topk=args.comp_topk, ctype=args.ctype, save_model=args.save_model)
vae.fit([X_train, X_train], [X_val, X_val], nb_epoch=args.n_epoch, batch_size=args.batch_size)
print 'runtime: %ss' % (timeit.default_timer() - start)
def plot_tsne(doc_codes, doc_labels, classes_to_visual, save_file):
# markers = ["D", "p", "*", "s", "d", "8", "^", "H", "v", ">", "<", "h", "|"]
markers = ["o", "v", "8", "s", "p", "*", "h", "H", "+", "x", "D"]
plt.rc('legend',**{'fontsize':30})
classes_to_visual = list(set(classes_to_visual))
C = len(classes_to_visual)
while True:
if C <= len(markers):
break
markers += markers
class_ids = dict(zip(classes_to_visual, range(C)))
if isinstance(doc_codes, dict) and isinstance(doc_labels, dict):
codes, labels = zip(*[(code, doc_labels[doc]) for doc, code in doc_codes.items() if doc_labels[doc] in classes_to_visual])
else:
codes, labels = doc_codes, doc_labels
X = np.r_[list(codes)]
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
np.set_printoptions(suppress=True)
X = tsne.fit_transform(X)
plt.figure(figsize=(10, 10), facecolor='white')
for c in classes_to_visual:
idx = np.array(labels) == c
# idx = get_indices(labels, c)
plt.plot(X[idx, 0], X[idx, 1], linestyle='None', alpha=1, marker=markers[class_ids[c]],
markersize=10, label=c)
legend = plt.legend(loc='upper right', shadow=True)
# plt.title("tsne")
# plt.savefig(save_file)
plt.savefig(save_file, format='eps', dpi=2000)
plt.show()
def plot_tsne_3d(doc_codes, doc_labels, classes_to_visual, save_file, maker_size=None, opaque=None):
markers = ["D", "p", "*", "s", "d", "8", "^", "H", "v", ">", "<", "h", "|"]
plt.rc('legend',**{'fontsize':20})
colors = ['r', 'b', 'g', 'c', 'm', 'y', 'k']
C = len(classes_to_visual)
while True:
if C <= len(markers):
break
markers += markers
while True:
if C <= len(colors):
break
colors += colors
class_ids = dict(zip(classes_to_visual, range(C)))
if isinstance(doc_codes, dict) and isinstance(doc_labels, dict):
codes, labels = zip(*[(code, doc_labels[doc]) for doc, code in doc_codes.items() if doc_labels[doc] in classes_to_visual])
else:
codes, labels = doc_codes, doc_labels
X = np.r_[list(codes)]
tsne = TSNE(perplexity=30, n_components=3, init='pca', n_iter=5000)
np.set_printoptions(suppress=True)
X = tsne.fit_transform(X)
fig = plt.figure(figsize=(10, 10), facecolor='white')
ax = fig.add_subplot(111, projection='3d')
# The problem is that the legend function don't support the type returned by a 3D scatter.
# So you have to create a "dummy plot" with the same characteristics and put those in the legend.
scatter_proxy = []
for i in range(C):
cls = classes_to_visual[i]
idx = np.array(labels) == cls
ax.scatter(X[idx, 0], X[idx, 1], X[idx, 2], c=colors[i], alpha=opaque[i] if opaque else 1, s=maker_size[i] if maker_size else 20, marker=markers[i], label=cls)
scatter_proxy.append(mpl.lines.Line2D([0],[0], linestyle="none", c=colors[i], marker=markers[i], label=cls))
ax.legend(scatter_proxy, classes_to_visual, numpoints=1)
plt.savefig(save_file)
plt.show()
def DBN_visualize_pca_2d(doc_codes, doc_labels, classes_to_visual, save_file):
"""
Visualize the input data on a 2D PCA plot. Depending on the number of components,
the plot will contain an X amount of subplots.
@param doc_codes:
@param number_of_components: The number of principal components for the PCA plot.
"""
# markers = ["p", "s", "h", "H", "+", "x", "D"]
markers = ["o", "v", "8", "s", "p", "*", "h", "H", "+", "x", "D"]
C = len(classes_to_visual)
while True:
if C <= len(markers):
break
markers += markers
class_ids = dict(zip(classes_to_visual.keys(), range(C)))
codes, labels = doc_codes, doc_labels
X = np.r_[list(codes)]
X = PCA(n_components=3).fit_transform(X)
plt.figure(figsize=(10, 10), facecolor='white')
x_pc, y_pc = 1, 2
for c in classes_to_visual.keys():
idx = np.array(labels) == c
# idx = get_indices(labels, c)
plt.plot(X[idx, x_pc], X[idx, y_pc], linestyle='None', alpha=0.6, marker=markers[class_ids[c]],
markersize=6, label=classes_to_visual[c])
# plt.legend(c)
plt.title('Projected on the first 2 PCs')
plt.xlabel('PC %s' % x_pc)
plt.ylabel('PC %s' % y_pc)
# legend = plt.legend(loc='upper center', shadow=True)
plt.savefig(save_file)
plt.show()
def reuters_visualize_tsne(doc_codes, doc_labels, classes_to_visual, save_file):
"""
Visualize the input data on a 2D PCA plot. Depending on the number of components,
the plot will contain an X amount of subplots.
@param doc_codes:
@param number_of_components: The number of principal components for the PCA plot.
"""
# markers = ["p", "s", "h", "H", "+", "x", "D"]
markers = ["o", "v", "8", "s", "p", "*", "h", "H", "+", "x", "D"]
C = len(classes_to_visual)
while True:
if C <= len(markers):
break
markers += markers
class_names = classes_to_visual.keys()
class_ids = dict(zip(class_names, range(C)))
class_names = set(class_names)
codes, labels = zip(*[(code, doc_labels[doc]) for doc, code in doc_codes.items() if class_names.intersection(set(doc_labels[doc]))])
X = np.r_[list(codes)]
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
np.set_printoptions(suppress=True)
X = tsne.fit_transform(X)
plt.figure(figsize=(10, 10), facecolor='white')
for c in classes_to_visual.keys():
idx = get_indices(labels, c)
plt.plot(X[idx, 0], X[idx, 1], linestyle='None', alpha=0.6, marker=markers[class_ids[c]],
markersize=6, label=classes_to_visual[c])
legend = plt.legend(loc='upper center', shadow=True)
plt.title("tsne")
plt.savefig(save_file)
plt.show()