def affine_backward(dout, cache): """ Computes the backward pass for an affine layer. Inputs: - dout: Upstream derivative, of shape (N, M) - cache: Tuple of: - x: Input data, of shape (N, d_1, ... d_k) - w: Weights, of shape (D, M) Returns a tuple of: - dx: Gradient with respect to x, of shape (N, d1, ..., d_k) - dw: Gradient with respect to w, of shape (D, M) - db: Gradient with respect to b, of shape (M,) """ x, w, b = cache x_plain = np.reshape(x, (x.shape[0], -1)) db = np.sum(dout, axis=0) dx_plain = np.dot(dout, np.transpose(w)) dx = np.reshape(dx_plain, x.shape) dw = np.dot(np.transpose(x_plain), dout) return dx, dw, db
def svm_loss(x, y): """ Computes the loss and gradient using for multiclass SVM classification. Inputs: - x: Input data, of shape (N, C) where x[i, j] is the score for the jth class for the ith input. - y: Vector of labels, of shape (N,) where y[i] is the label for x[i] and 0 <= y[i] < C Returns a tuple of: - loss: Scalar giving the loss - dx: Gradient of the loss with respect to x """ N = x.shape[0] correct_class_scores = x[np.arange(N), y] #TODO: Support broadcast case: (X,) (X, Y) #shape(x) is (d0, d1) #shape(correct_class_scores) is (d0,) #margins = np.maximum(0, x - correct_class_scores + 1.0) margins = np.transpose(np.maximum(0, np.transpose(x) - np.transpose(correct_class_scores) + 1.0)) loss = (np.sum(margins) - np.sum(margins[np.arange(N), y])) / N return loss
def svm_loss(x, y): """ Computes the loss and gradient using for multiclass SVM classification. Inputs: - x: Input data, of shape (N, C) where x[i, j] is the score for the jth class for the ith input. - y: Vector of labels, of shape (N,) where y[i] is the label for x[i] and 0 <= y[i] < C Returns a tuple of: - loss: Scalar giving the loss - dx: Gradient of the loss with respect to x """ N = x.shape[0] correct_class_scores = x[np.arange(N), y] #TODO: Support broadcast case: (X,) (X, Y) #shape(x) is (d0, d1) #shape(correct_class_scores) is (d0,) #margins = np.maximum(0, x - correct_class_scores + 1.0) margins = np.transpose( np.maximum(0, np.transpose(x) - np.transpose(correct_class_scores) + 1.0)) loss = (np.sum(margins) - np.sum(margins[np.arange(N), y])) / N return loss
def qz_mjk_m(self, mj, m): mmat = np.tile(self._theta_m_k[m, :], self._vocab_n) mmat = np.mat(mmat.reshape(self._vocab_n, self._k)) p = np.multiply(mmat, np.mat(self._psi_k_j).T) p = self._p_dm * p mm = np.tile(mj[m, :], self._k).reshape(self._k, self._vocab_n) mm = np.transpose(mm) p /= mm return p
def set_param(self): self.params = {} c_cnt, height, width = self.input_dim f_cnt = self.num_filters f_h, f_w = self.filter_size, self.filter_size self.params['conv1_weight'] = random.randn(f_cnt, c_cnt, f_h, f_w) * self.weight_scale self.params['conv1_bias'] = np.zeros(f_cnt) #TODO(Haoran): whole stuff about all dimension calculations #should be substituted by quering symbol.arg_list conv_stride = 1 conv_pad = (f_h - 1) / 2 Hc, Wc = 1 + (height + 2 * conv_pad - f_h) / conv_stride, 1 + ( width + 2 * conv_pad - f_w) / conv_stride pool_height, pool_width = 2, 2 pool_stride = 2 Hp, Wp = (Hc - pool_height) / pool_stride + 1, ( Wc - pool_width) / pool_stride + 1 # weight has to be tranposed to fit mxnet's symbol self.params['fc1_weight'] = np.transpose( random.randn(5408, self.hidden_dim) * self.weight_scale) self.params['fc1_bias'] = np.zeros((self.hidden_dim)) # weight has to be tranposed to fit mxnet's symbol self.params['fc2_weight'] = np.transpose( random.randn(self.hidden_dim, self.num_classes) * self.weight_scale) self.params['fc2_bias'] = np.zeros((self.num_classes)) #TODO(Haoran): move following into parent structured model class self.param_keys = self.params.keys() # Build key's index in loss func's arglist self.key_args_index = {} for i, key in enumerate(self.param_keys): # data, targets would be the first two elments in arglist self.key_args_index[key] = self.data_target_cnt + i
def set_param(self): self.params = {} c_cnt, height, width = self.input_dim f_cnt = self.num_filters f_h, f_w = self.filter_size, self.filter_size self.params['conv1_weight'] = random.randn(f_cnt, c_cnt, f_h, f_w) * self.weight_scale self.params['conv1_bias'] = np.zeros(f_cnt) #TODO(Haoran): whole stuff about all dimension calculations #should be substituted by quering symbol.arg_list conv_stride = 1 conv_pad = (f_h - 1) / 2 Hc, Wc = 1 + (height + 2 * conv_pad - f_h) / conv_stride, 1 + ( width + 2 * conv_pad - f_w) / conv_stride pool_height, pool_width = 2, 2 pool_stride = 2 Hp, Wp = (Hc - pool_height) / pool_stride + 1, (Wc - pool_width ) / pool_stride + 1 # weight has to be tranposed to fit mxnet's symbol self.params['fc1_weight'] = np.transpose(random.randn( 5408, self.hidden_dim) * self.weight_scale) self.params['fc1_bias'] = np.zeros((self.hidden_dim)) # weight has to be tranposed to fit mxnet's symbol self.params['fc2_weight'] = np.transpose(random.randn( self.hidden_dim, self.num_classes) * self.weight_scale) self.params['fc2_bias'] = np.zeros((self.num_classes)) #TODO(Haoran): move following into parent structured model class self.param_keys = self.params.keys() # Build key's index in loss func's arglist self.key_args_index = {} for i, key in enumerate(self.param_keys): # data, targets would be the first two elments in arglist self.key_args_index[key] = self.data_target_cnt + i
def svm_loss(x, y, mode): """ Computes the loss and gradient using for multiclass SVM classification. Inputs: - x: Input data, of shape (N, C) where x[i, j] is the score for the jth class for the ith input. - y: Vector of labels, of shape (N,) where y[i] is the label for x[i] and 0 <= y[i] < C Returns a tuple of: - loss: Scalar giving the loss - dx: Gradient of the loss with respect to x """ if mode == 'cpu': np.set_policy(policy.OnlyNumpyPolicy()) else: np.set_policy(policy.PreferMXNetPolicy()) N = x.shape[0] correct_class_scores = x[np.arange(N), y] #TODO: Support broadcast case: (X,) (X, Y) #margins = np.maximum(0, x - correct_class_scores + 1.0) margins = np.transpose( np.maximum(0, np.transpose(x) - np.transpose(correct_class_scores) + 1.0)) #margins[np.arange(N), y] = 0 #loss = np.sum(margins) / N loss = (np.sum(margins) - np.sum(margins[np.arange(N), y])) / N margins[np.arange(N), y] = 0 num_pos = np.sum(margins > 0, axis=1) dx = np.zeros_like(x) dx[margins > 0] = 1 dx[np.arange(N), y] -= num_pos dx /= N return loss, dx
def svm_loss(x, y, mode): """ Computes the loss and gradient using for multiclass SVM classification. Inputs: - x: Input data, of shape (N, C) where x[i, j] is the score for the jth class for the ith input. - y: Vector of labels, of shape (N,) where y[i] is the label for x[i] and 0 <= y[i] < C Returns a tuple of: - loss: Scalar giving the loss - dx: Gradient of the loss with respect to x """ if mode == 'cpu': np.set_policy(policy.OnlyNumpyPolicy()) else: np.set_policy(policy.PreferMXNetPolicy()) N = x.shape[0] correct_class_scores = x[np.arange(N), y] #TODO: Support broadcast case: (X,) (X, Y) #margins = np.maximum(0, x - correct_class_scores + 1.0) margins = np.transpose(np.maximum(0, np.transpose(x) - np.transpose( correct_class_scores) + 1.0)) #margins[np.arange(N), y] = 0 #loss = np.sum(margins) / N loss = (np.sum(margins) - np.sum(margins[np.arange(N), y])) / N margins[np.arange(N), y] = 0 num_pos = np.sum(margins > 0, axis=1) dx = np.zeros_like(x) dx[margins > 0] = 1 dx[np.arange(N), y] -= num_pos dx /= N return loss, dx
def parse(self, tokens, oracle_actions=None): def _valid_actions(stack, buffer): valid_actions = [] if len(buffer) > 0: valid_actions += [SHIFT] if len(stack) >= 2: valid_actions += [REDUCE_L, REDUCE_R] return valid_actions if oracle_actions: oracle_actions = list(oracle_actions) buffer = StackRNN(self.buffRNN, self.params['empty_buffer_emb']) stack = StackRNN(self.stackRNN) # Put the parameters in the cg W_comp = self.params['pW_comp'] # syntactic composition b_comp = self.params['pb_comp'] W_s2h = self.params['pW_s2h'] # state to hidden b_s2h = self.params['pb_s2h'] W_act = self.params['pW_act'] # hidden to action b_act = self.params['pb_act'] emb = self.params['wemb'] # We will keep track of all the losses we accumulate during parsing. # If some decision is unambiguous because it's the only thing valid given # the parser state, we will not model it. We only model what is ambiguous. loss = 0. # push the tokens onto the buffer (tokens is in reverse order) for tok in tokens: # TODO: I remember numpy ndarray supports python built-in list indexing tok_embedding = emb[np.array([tok])] buffer.push(tok_embedding, (tok_embedding, self.vocab.i2w[tok])) while not (len(stack) == 1 and len(buffer) == 0): # compute probability of each of the actions and choose an action # either from the oracle or if there is no oracle, based on the model valid_actions = _valid_actions(stack, buffer) log_probs = None action = valid_actions[0] if len(valid_actions) > 1: p_t = np.transpose( np.concatenate([buffer.top(), stack.top()], axis=1)) h = np.tanh(np.dot(W_s2h, p_t) + b_s2h) logits = np.dot(W_act, h) + b_act log_probs = logsoftmax(logits, valid_actions) if oracle_actions is None: # Temporary work around by manually back-off to numpy https://github.com/dmlc/minpy/issues/15 action = numpy.argmax(map(lambda x: x[0], list(log_probs))) if oracle_actions is not None: action = oracle_actions.pop() if log_probs is not None: # append the action-specific loss # print action, log_probs[action], map(lambda x: x[0], list(log_probs)) loss += log_probs[action] # execute the action to update the parser state if action == SHIFT: tok_embedding, token = buffer.pop() stack.push(tok_embedding, (tok_embedding, token)) else: # one of the REDUCE actions right = stack.pop() # pop a stack state left = stack.pop() # pop another stack state # figure out which is the head and which is the modifier head, modifier = (left, right) if action == REDUCE_R else (right, left) # compute composed representation head_rep, head_tok = head mod_rep, mod_tok = modifier composed_rep = np.tanh( np.dot( W_comp, np.transpose( np.concatenate([head_rep, mod_rep], axis=1))) + b_comp) composed_rep = np.transpose(composed_rep) stack.push(composed_rep, (composed_rep, head_tok)) if oracle_actions is None: print('{0} --> {1}'.format(head_tok, mod_tok)) # the head of the tree that remains at the top of the stack is the root if oracle_actions is None: head = stack.pop()[1] print('ROOT --> {0}'.format(head)) return -loss
def test_fromnumeric(): # Functions # 'alen', 'all', 'alltrue', 'amax', 'amin', 'any', 'argmax', # 'argmin', 'argpartition', 'argsort', 'around', 'choose', 'clip', # 'compress', 'cumprod', 'cumproduct', 'cumsum', 'diagonal', 'mean', # 'ndim', 'nonzero', 'partition', 'prod', 'product', 'ptp', 'put', # 'rank', 'ravel', 'repeat', 'reshape', 'resize', 'round_', # 'searchsorted', 'shape', 'size', 'sometrue', 'sort', 'squeeze', # 'std', 'sum', 'swapaxes', 'take', 'trace', 'transpose', 'var', a = [4, 3, 5, 7, 6, 8] indices = [0, 1, 4] np.take(a, indices) a = np.array(a) # a[indices] np.take(a, [[0, 1], [2, 3]]) a = np.zeros((10, 2)) b = a.T a = np.arange(6).reshape((3, 2)) np.reshape(a, (2, 3)) # C-like index ordering np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape np.reshape(a, (2, 3), order='F') # Fortran-like index ordering np.reshape(np.ravel(a, order='F'), (2, 3), order='F') a = np.array([[1, 2, 3], [4, 5, 6]]) np.reshape(a, 6) np.reshape(a, 6, order='F') np.reshape(a, (3, -1)) # the unspecified value is inferred to be 2 choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]] np.choose([2, 3, 1, 0], choices) np.choose([2, 4, 1, 0], choices, mode='clip') # 4 goes to 3 (4-1) np.choose([2, 4, 1, 0], choices, mode='wrap') # 4 goes to (4 mod 4) a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]] choices = [-10, 10] np.choose(a, choices) a = np.array([0, 1]).reshape((2, 1, 1)) c1 = np.array([1, 2, 3]).reshape((1, 3, 1)) c2 = np.array([-1, -2, -3, -4, -5]).reshape((1, 1, 5)) np.choose(a, (c1, c2)) # result is 2x3x5, res[0,:,:]=c1, res[1,:,:]=c2 np.repeat(3, 4) x = np.array([[1, 2], [3, 4]]) np.repeat(x, 2) np.repeat(x, 3, axis=1) np.repeat(x, [1, 2], axis=0) a = np.arange(5) np.put(a, [0, 2], [-44, -55]) a = np.arange(5) np.put(a, 22, -5, mode='clip') x = np.array([[1, 2, 3]]) np.swapaxes(x, 0, 1) x = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) np.swapaxes(x, 0, 2) x = np.arange(4).reshape((2, 2)) np.transpose(x) x = np.ones((1, 2, 3)) np.transpose(x, (1, 0, 2)).shape a = np.array([3, 4, 2, 1]) np.partition(a, 3) np.partition(a, (1, 3)) x = np.array([3, 4, 2, 1]) x[np.argpartition(x, 3)] x[np.argpartition(x, (1, 3))] x = [3, 4, 2, 1] np.array(x)[np.argpartition(x, 3)] a = np.array([[1, 4], [3, 1]]) np.sort(a) # sort along the last axis np.sort(a, axis=None) # sort the flattened array np.sort(a, axis=0) # sort along the first axis dtype = [('name', 'S10'), ('height', float), ('age', int)] values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38), ('Galahad', 1.7, 38)] a = np.array(values, dtype=dtype) # create a structured array np.sort(a, order='height') # doctest: +SKIP np.sort(a, order=['age', 'height']) # doctest: +SKIP x = np.array([3, 1, 2]) np.argsort(x) x = np.array([[0, 3], [2, 2]]) np.argsort(x, axis=0) np.argsort(x, axis=1) x = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')]) np.argsort(x, order=('x', 'y')) np.argsort(x, order=('y', 'x')) a = np.arange(6).reshape(2, 3) np.argmax(a) np.argmax(a, axis=0) np.argmax(a, axis=1) b = np.arange(6) b[1] = 5 np.argmax(b) # Only the first occurrence is returned. a = np.arange(6).reshape(2, 3) np.argmin(a) np.argmin(a, axis=0) np.argmin(a, axis=1) b = np.arange(6) b[4] = 0 np.argmin(b) # Only the first occurrence is returned. np.searchsorted([1, 2, 3, 4, 5], 3) np.searchsorted([1, 2, 3, 4, 5], 3, side='right') np.searchsorted([1, 2, 3, 4, 5], [-10, 10, 2, 3]) a = np.array([[0, 1], [2, 3]]) np.resize(a, (2, 3)) np.resize(a, (1, 4)) np.resize(a, (2, 4)) x = np.array([[[0], [1], [2]]]) x.shape np.squeeze(x).shape np.squeeze(x, axis=(2, )).shape a = np.arange(4).reshape(2, 2) a = np.arange(8).reshape(2, 2, 2) a a[:, :, 0] # main diagonal is [0 6] a[:, :, 1] # main diagonal is [1 7] np.trace(np.eye(3)) a = np.arange(8).reshape((2, 2, 2)) np.trace(a) a = np.arange(24).reshape((2, 2, 2, 3)) np.trace(a).shape x = np.array([[1, 2, 3], [4, 5, 6]]) np.ravel(x) x.reshape(-1) np.ravel(x, order='F') np.ravel(x.T) np.ravel(x.T, order='A') a = np.arange(3)[::-1] a # a = np.arange(12).reshape(2,3,2).swapaxes(1,2); a x = np.eye(3) np.nonzero(x) x[np.nonzero(x)] np.transpose(np.nonzero(x)) a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) a > 3 np.nonzero(a > 3) np.shape(np.eye(3)) np.shape([[1, 2]]) np.shape([0]) np.shape(0) a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) np.shape(a) a.shape a = np.array([[1, 2], [3, 4], [5, 6]]) np.compress([0, 1], a, axis=0) np.compress([False, True, True], a, axis=0) np.compress([False, True], a, axis=1) np.compress([False, True], a) a = np.arange(10) np.clip(a, 1, 8) np.clip(a, 3, 6, out=a) a = np.arange(10) np.clip(a, [3, 4, 1, 1, 1, 4, 4, 4, 4, 4], 8) np.sum([]) np.sum([0.5, 1.5]) np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32) np.sum([[0, 1], [0, 5]]) np.sum([[0, 1], [0, 5]], axis=0) np.sum([[0, 1], [0, 5]], axis=1) # np.ones(128, dtype=np.int8).sum(dtype=np.int8) # np.any([[True, False], [True, True]]) # np.any([[True, False], [False, False]], axis=0) # np.any([-1, 0, 5]) # np.any(np.nan) # np.all([[True,False],[True,True]]) # np.all([[True,False],[True,True]], axis=0) # np.all([-1, 4, 5]) # np.all([1.0, np.nan]) a = np.array([[1, 2, 3], [4, 5, 6]]) np.cumsum(a) np.cumsum(a, dtype=float) # specifies type of output value(s) np.cumsum(a, axis=0) # sum over rows for each of the 3 columns np.cumsum(a, axis=1) # sum over columns for each of the 2 rows x = np.arange(4).reshape((2, 2)) np.ptp(x, axis=0) np.ptp(x, axis=1) a = np.arange(4).reshape((2, 2)) np.amax(a) # Maximum of the flattened array np.amax(a, axis=0) # Maxima along the first axis np.amax(a, axis=1) # Maxima along the second axis b = np.arange(5, dtype=np.float) # b[2] = np.NaN np.amax(b) np.nanmax(b) a = np.arange(4).reshape((2, 2)) np.amin(a) # Minimum of the flattened array np.amin(a, axis=0) # Minima along the first axis np.amin(a, axis=1) # Minima along the second axis b = np.arange(5, dtype=np.float) # b[2] = np.NaN np.amin(b) np.nanmin(b) a = np.zeros((7, 4, 5)) a.shape[0] np.alen(a) x = np.array([536870910, 536870910, 536870910, 536870910]) np.prod(x) #random np.prod([]) np.prod([1., 2.]) np.prod([[1., 2.], [3., 4.]]) np.prod([[1., 2.], [3., 4.]], axis=1) x = np.array([1, 2, 3], dtype=np.uint8) # np.prod(x).dtype == np.uint x = np.array([1, 2, 3], dtype=np.int8) # np.prod(x).dtype == np.int a = np.array([1, 2, 3]) np.cumprod(a) # intermediate results 1, 1*2 a = np.array([[1, 2, 3], [4, 5, 6]]) np.cumprod(a, dtype=float) # specify type of output np.cumprod(a, axis=0) np.cumprod(a, axis=1) np.ndim([[1, 2, 3], [4, 5, 6]]) np.ndim(np.array([[1, 2, 3], [4, 5, 6]])) np.ndim(1) a = np.array([[1, 2, 3], [4, 5, 6]]) np.size(a) np.size(a, 1) np.size(a, 0) np.around([0.37, 1.64]) np.around([0.37, 1.64], decimals=1) np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value np.around([1, 2, 3, 11], decimals=1) # ndarray of ints is returned np.around([1, 2, 3, 11], decimals=-1) a = np.array([[1, 2], [3, 4]]) np.mean(a) np.mean(a, axis=0) np.mean(a, axis=1) a = np.zeros((2, 512 * 512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.mean(a) np.mean(a, dtype=np.float64) a = np.array([[1, 2], [3, 4]]) np.std(a) np.std(a, axis=0) np.std(a, axis=1) a = np.zeros((2, 512 * 512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.std(a) np.std(a, dtype=np.float64) a = np.array([[1, 2], [3, 4]]) np.var(a) np.var(a, axis=0) np.var(a, axis=1) a = np.zeros((2, 512 * 512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.var(a) np.var(a, dtype=np.float64)
def test_numeric(): # 'newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', # 'arange', 'array', 'zeros', 'count_nonzero', 'empty', 'broadcast', # 'dtype', 'fromstring', 'fromfile', 'frombuffer', 'int_asbuffer', # 'where', 'argwhere', 'copyto', 'concatenate', 'fastCopyAndTranspose', # 'lexsort', 'set_numeric_ops', 'can_cast', 'promote_types', # 'min_scalar_type', 'result_type', 'asarray', 'asanyarray', # 'ascontiguousarray', 'asfortranarray', 'isfortran', 'empty_like', # 'zeros_like', 'ones_like', 'correlate', 'convolve', 'inner', 'dot', # 'einsum', 'outer', 'vdot', 'alterdot', 'restoredot', 'roll', # 'rollaxis', 'moveaxis', 'cross', 'tensordot', 'array2string', # 'get_printoptions', 'set_printoptions', 'array_repr', 'array_str', # 'set_string_function', 'little_endian', 'require', 'fromiter', # 'array_equal', 'array_equiv', 'indices', 'fromfunction', 'isclose', 'load', # 'loads', 'isscalar', 'binary_repr', 'base_repr', 'ones', 'identity', # 'allclose', 'compare_chararrays', 'putmask', 'seterr', 'geterr', # 'setbufsize', 'getbufsize', 'seterrcall', 'geterrcall', 'errstate', # 'flatnonzero', 'Inf', 'inf', 'infty', 'Infinity', 'nan', 'NaN', 'False_', # 'True_', 'bitwise_not', 'full', 'full_like', 'matmul' x = np.arange(6) x = x.reshape((2, 3)) np.zeros_like(x) y = np.arange(3, dtype=np.float) np.zeros_like(y) np.ones(5) np.ones((5, ), dtype=np.int) np.ones((2, 1)) s = (2, 2) np.ones(s) x = np.arange(6) x = x.reshape((2, 3)) np.ones_like(x) y = np.arange(3, dtype=np.float) np.ones_like(y) np.full((2, 2), np.inf) x = np.arange(6, dtype=np.int) np.full_like(x, 1) np.full_like(x, 0.1) np.full_like(y, 0.1) np.count_nonzero(np.eye(4)) np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]]) np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=0) np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=1) a = [1, 2] np.asarray(a) a = np.array([1, 2]) np.asarray(a) is a a = np.array([1, 2], dtype=np.float32) np.asarray(a, dtype=np.float32) is a np.asarray(a, dtype=np.float64) is a np.asarray(a) is a np.asanyarray(a) is a a = [1, 2] np.asanyarray(a) np.asanyarray(a) is a x = np.arange(6).reshape(2, 3) np.ascontiguousarray(x, dtype=np.float32) x = np.arange(6).reshape(2, 3) y = np.asfortranarray(x) x = np.arange(6).reshape(2, 3) y = np.require(x, dtype=np.float32, requirements=['A', 'O', 'W', 'F']) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = np.array([[1, 2, 3], [4, 5, 6]], order='FORTRAN') np.isfortran(b) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = a.T np.isfortran(b) np.isfortran(np.array([1, 2], order='FORTRAN')) x = np.arange(6).reshape(2, 3) np.argwhere(x > 1) x = np.arange(-2, 3) np.flatnonzero(x) np.correlate([1, 2, 3], [0, 1, 0.5]) np.correlate([1, 2, 3], [0, 1, 0.5], "same") np.correlate([1, 2, 3], [0, 1, 0.5], "full") np.correlate([1 + 1j, 2, 3 - 1j], [0, 1, 0.5j], 'full') np.correlate([0, 1, 0.5j], [1 + 1j, 2, 3 - 1j], 'full') np.convolve([1, 2, 3], [0, 1, 0.5]) np.convolve([1, 2, 3], [0, 1, 0.5], 'same') np.convolve([1, 2, 3], [0, 1, 0.5], 'valid') rl = np.outer(np.ones((5, )), np.linspace(-2, 2, 5)) # im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,))) # grid = rl + im x = np.array(['a', 'b', 'c'], dtype=object) np.outer(x, [1, 2, 3]) a = np.arange(60.).reshape(3, 4, 5) b = np.arange(24.).reshape(4, 3, 2) c = np.tensordot(a, b, axes=([1, 0], [0, 1])) c.shape # A slower but equivalent way of computing the same... d = np.zeros((5, 2)) a = np.array(range(1, 9)) A = np.array(('a', 'b', 'c', 'd'), dtype=object) x = np.arange(10) np.roll(x, 2) x2 = np.reshape(x, (2, 5)) np.roll(x2, 1) np.roll(x2, 1, axis=0) np.roll(x2, 1, axis=1) a = np.ones((3, 4, 5, 6)) np.rollaxis(a, 3, 1).shape np.rollaxis(a, 2).shape np.rollaxis(a, 1, 4).shape x = np.zeros((3, 4, 5)) np.moveaxis(x, 0, -1).shape np.moveaxis(x, -1, 0).shape np.transpose(x).shape np.moveaxis(x, [0, 1], [-1, -2]).shape np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape x = [1, 2, 3] y = [4, 5, 6] np.cross(x, y) x = [1, 2] y = [4, 5, 6] np.cross(x, y) x = [1, 2, 0] y = [4, 5, 6] np.cross(x, y) x = [1, 2] y = [4, 5] np.cross(x, y) x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.array([[4, 5, 6], [1, 2, 3]]) np.cross(x, y) np.cross(x, y, axisc=0) x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) y = np.array([[7, 8, 9], [4, 5, 6], [1, 2, 3]]) np.cross(x, y) np.cross(x, y, axisa=0, axisb=0) # np.array_repr(np.array([1,2])) # np.array_repr(np.ma.array([0.])) # np.array_repr(np.array([], np.int32)) x = np.array([1e-6, 4e-7, 2, 3]) # np.array_repr(x, precision=6, suppress_small=True) # np.array_str(np.arange(3)) a = np.arange(10) x = np.arange(4) np.set_string_function(lambda x: 'random', repr=False) grid = np.indices((2, 3)) grid.shape grid[0] # row indices grid[1] # column indices x = np.arange(20).reshape(5, 4) row, col = np.indices((2, 3)) x[row, col] np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) np.isscalar(3.1) np.isscalar([3.1]) np.isscalar(False) # np.binary_repr(3) # np.binary_repr(-3) # np.binary_repr(3, width=4) # np.binary_repr(-3, width=3) # np.binary_repr(-3, width=5) # np.base_repr(5) # np.base_repr(6, 5) # np.base_repr(7, base=5, padding=3) # np.base_repr(10, base=16) # np.base_repr(32, base=16) np.identity(3) np.allclose([1e10, 1e-7], [1.00001e10, 1e-8]) np.allclose([1e10, 1e-8], [1.00001e10, 1e-9]) np.allclose([1e10, 1e-8], [1.0001e10, 1e-9]) # np.allclose([1.0, np.nan], [1.0, np.nan]) # np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.isclose([1e10, 1e-7], [1.00001e10, 1e-8]) np.isclose([1e10, 1e-8], [1.00001e10, 1e-9]) np.isclose([1e10, 1e-8], [1.0001e10, 1e-9]) # np.isclose([1.0, np.nan], [1.0, np.nan]) # np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.array_equal([1, 2], [1, 2]) np.array_equal(np.array([1, 2]), np.array([1, 2])) np.array_equal([1, 2], [1, 2, 3]) np.array_equal([1, 2], [1, 4]) np.array_equiv([1, 2], [1, 2]) np.array_equiv([1, 2], [1, 3]) np.array_equiv([1, 2], [[1, 2], [1, 2]]) np.array_equiv([1, 2], [[1, 2, 1, 2], [1, 2, 1, 2]]) np.array_equiv([1, 2], [[1, 2], [1, 3]])
def test_fromnumeric(): # Functions # 'alen', 'all', 'alltrue', 'amax', 'amin', 'any', 'argmax', # 'argmin', 'argpartition', 'argsort', 'around', 'choose', 'clip', # 'compress', 'cumprod', 'cumproduct', 'cumsum', 'diagonal', 'mean', # 'ndim', 'nonzero', 'partition', 'prod', 'product', 'ptp', 'put', # 'rank', 'ravel', 'repeat', 'reshape', 'resize', 'round_', # 'searchsorted', 'shape', 'size', 'sometrue', 'sort', 'squeeze', # 'std', 'sum', 'swapaxes', 'take', 'trace', 'transpose', 'var', a = [4, 3, 5, 7, 6, 8] indices = [0, 1, 4] np.take(a, indices) a = np.array(a) # a[indices] np.take(a, [[0, 1], [2, 3]]) a = np.zeros((10, 2)) b = a.T a = np.arange(6).reshape((3, 2)) np.reshape(a, (2, 3)) # C-like index ordering np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape np.reshape(a, (2, 3), order='F') # Fortran-like index ordering np.reshape(np.ravel(a, order='F'), (2, 3), order='F') a = np.array([[1,2,3], [4,5,6]]) np.reshape(a, 6) np.reshape(a, 6, order='F') np.reshape(a, (3,-1)) # the unspecified value is inferred to be 2 choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]] np.choose([2, 3, 1, 0], choices) np.choose([2, 4, 1, 0], choices, mode='clip') # 4 goes to 3 (4-1) np.choose([2, 4, 1, 0], choices, mode='wrap') # 4 goes to (4 mod 4) a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]] choices = [-10, 10] np.choose(a, choices) a = np.array([0, 1]).reshape((2,1,1)) c1 = np.array([1, 2, 3]).reshape((1,3,1)) c2 = np.array([-1, -2, -3, -4, -5]).reshape((1,1,5)) np.choose(a, (c1, c2)) # result is 2x3x5, res[0,:,:]=c1, res[1,:,:]=c2 np.repeat(3, 4) x = np.array([[1,2],[3,4]]) np.repeat(x, 2) np.repeat(x, 3, axis=1) np.repeat(x, [1, 2], axis=0) a = np.arange(5) np.put(a, [0, 2], [-44, -55]) a = np.arange(5) np.put(a, 22, -5, mode='clip') x = np.array([[1,2,3]]) np.swapaxes(x,0,1) x = np.array([[[0,1],[2,3]],[[4,5],[6,7]]]) np.swapaxes(x,0,2) x = np.arange(4).reshape((2,2)) np.transpose(x) x = np.ones((1, 2, 3)) np.transpose(x, (1, 0, 2)).shape a = np.array([3, 4, 2, 1]) np.partition(a, 3) np.partition(a, (1, 3)) x = np.array([3, 4, 2, 1]) x[np.argpartition(x, 3)] x[np.argpartition(x, (1, 3))] x = [3, 4, 2, 1] np.array(x)[np.argpartition(x, 3)] a = np.array([[1,4],[3,1]]) np.sort(a) # sort along the last axis np.sort(a, axis=None) # sort the flattened array np.sort(a, axis=0) # sort along the first axis dtype = [('name', 'S10'), ('height', float), ('age', int)] values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38), ('Galahad', 1.7, 38)] a = np.array(values, dtype=dtype) # create a structured array np.sort(a, order='height') # doctest: +SKIP np.sort(a, order=['age', 'height']) # doctest: +SKIP x = np.array([3, 1, 2]) np.argsort(x) x = np.array([[0, 3], [2, 2]]) np.argsort(x, axis=0) np.argsort(x, axis=1) x = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')]) np.argsort(x, order=('x','y')) np.argsort(x, order=('y','x')) a = np.arange(6).reshape(2,3) np.argmax(a) np.argmax(a, axis=0) np.argmax(a, axis=1) b = np.arange(6) b[1] = 5 np.argmax(b) # Only the first occurrence is returned. a = np.arange(6).reshape(2,3) np.argmin(a) np.argmin(a, axis=0) np.argmin(a, axis=1) b = np.arange(6) b[4] = 0 np.argmin(b) # Only the first occurrence is returned. np.searchsorted([1,2,3,4,5], 3) np.searchsorted([1,2,3,4,5], 3, side='right') np.searchsorted([1,2,3,4,5], [-10, 10, 2, 3]) a=np.array([[0,1],[2,3]]) np.resize(a,(2,3)) np.resize(a,(1,4)) np.resize(a,(2,4)) x = np.array([[[0], [1], [2]]]) x.shape np.squeeze(x).shape np.squeeze(x, axis=(2,)).shape a = np.arange(4).reshape(2,2) a = np.arange(8).reshape(2,2,2); a a[:,:,0] # main diagonal is [0 6] a[:,:,1] # main diagonal is [1 7] np.trace(np.eye(3)) a = np.arange(8).reshape((2,2,2)) np.trace(a) a = np.arange(24).reshape((2,2,2,3)) np.trace(a).shape x = np.array([[1, 2, 3], [4, 5, 6]]) np.ravel(x) x.reshape(-1) np.ravel(x, order='F') np.ravel(x.T) np.ravel(x.T, order='A') a = np.arange(3)[::-1]; a # a = np.arange(12).reshape(2,3,2).swapaxes(1,2); a x = np.eye(3) np.nonzero(x) x[np.nonzero(x)] np.transpose(np.nonzero(x)) a = np.array([[1,2,3],[4,5,6],[7,8,9]]) a > 3 np.nonzero(a > 3) np.shape(np.eye(3)) np.shape([[1, 2]]) np.shape([0]) np.shape(0) a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) np.shape(a) a.shape a = np.array([[1, 2], [3, 4], [5, 6]]) np.compress([0, 1], a, axis=0) np.compress([False, True, True], a, axis=0) np.compress([False, True], a, axis=1) np.compress([False, True], a) a = np.arange(10) np.clip(a, 1, 8) np.clip(a, 3, 6, out=a) a = np.arange(10) np.clip(a, [3,4,1,1,1,4,4,4,4,4], 8) np.sum([]) np.sum([0.5, 1.5]) np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32) np.sum([[0, 1], [0, 5]]) np.sum([[0, 1], [0, 5]], axis=0) np.sum([[0, 1], [0, 5]], axis=1) # np.ones(128, dtype=np.int8).sum(dtype=np.int8) # np.any([[True, False], [True, True]]) # np.any([[True, False], [False, False]], axis=0) # np.any([-1, 0, 5]) # np.any(np.nan) # np.all([[True,False],[True,True]]) # np.all([[True,False],[True,True]], axis=0) # np.all([-1, 4, 5]) # np.all([1.0, np.nan]) a = np.array([[1,2,3], [4,5,6]]) np.cumsum(a) np.cumsum(a, dtype=float) # specifies type of output value(s) np.cumsum(a,axis=0) # sum over rows for each of the 3 columns np.cumsum(a,axis=1) # sum over columns for each of the 2 rows x = np.arange(4).reshape((2,2)) np.ptp(x, axis=0) np.ptp(x, axis=1) a = np.arange(4).reshape((2,2)) np.amax(a) # Maximum of the flattened array np.amax(a, axis=0) # Maxima along the first axis np.amax(a, axis=1) # Maxima along the second axis b = np.arange(5, dtype=np.float) # b[2] = np.NaN np.amax(b) np.nanmax(b) a = np.arange(4).reshape((2,2)) np.amin(a) # Minimum of the flattened array np.amin(a, axis=0) # Minima along the first axis np.amin(a, axis=1) # Minima along the second axis b = np.arange(5, dtype=np.float) # b[2] = np.NaN np.amin(b) np.nanmin(b) a = np.zeros((7,4,5)) a.shape[0] np.alen(a) x = np.array([536870910, 536870910, 536870910, 536870910]) np.prod(x) #random np.prod([]) np.prod([1.,2.]) np.prod([[1.,2.],[3.,4.]]) np.prod([[1.,2.],[3.,4.]], axis=1) x = np.array([1, 2, 3], dtype=np.uint8) # np.prod(x).dtype == np.uint x = np.array([1, 2, 3], dtype=np.int8) # np.prod(x).dtype == np.int a = np.array([1,2,3]) np.cumprod(a) # intermediate results 1, 1*2 a = np.array([[1, 2, 3], [4, 5, 6]]) np.cumprod(a, dtype=float) # specify type of output np.cumprod(a, axis=0) np.cumprod(a,axis=1) np.ndim([[1,2,3],[4,5,6]]) np.ndim(np.array([[1,2,3],[4,5,6]])) np.ndim(1) a = np.array([[1,2,3],[4,5,6]]) np.size(a) np.size(a,1) np.size(a,0) np.around([0.37, 1.64]) np.around([0.37, 1.64], decimals=1) np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value np.around([1,2,3,11], decimals=1) # ndarray of ints is returned np.around([1,2,3,11], decimals=-1) a = np.array([[1, 2], [3, 4]]) np.mean(a) np.mean(a, axis=0) np.mean(a, axis=1) a = np.zeros((2, 512*512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.mean(a) np.mean(a, dtype=np.float64) a = np.array([[1, 2], [3, 4]]) np.std(a) np.std(a, axis=0) np.std(a, axis=1) a = np.zeros((2, 512*512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.std(a) np.std(a, dtype=np.float64) a = np.array([[1, 2], [3, 4]]) np.var(a) np.var(a, axis=0) np.var(a, axis=1) a = np.zeros((2, 512*512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.var(a) np.var(a, dtype=np.float64)
def test_numeric(): # 'newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', # 'arange', 'array', 'zeros', 'count_nonzero', 'empty', 'broadcast', # 'dtype', 'fromstring', 'fromfile', 'frombuffer', 'int_asbuffer', # 'where', 'argwhere', 'copyto', 'concatenate', 'fastCopyAndTranspose', # 'lexsort', 'set_numeric_ops', 'can_cast', 'promote_types', # 'min_scalar_type', 'result_type', 'asarray', 'asanyarray', # 'ascontiguousarray', 'asfortranarray', 'isfortran', 'empty_like', # 'zeros_like', 'ones_like', 'correlate', 'convolve', 'inner', 'dot', # 'einsum', 'outer', 'vdot', 'alterdot', 'restoredot', 'roll', # 'rollaxis', 'moveaxis', 'cross', 'tensordot', 'array2string', # 'get_printoptions', 'set_printoptions', 'array_repr', 'array_str', # 'set_string_function', 'little_endian', 'require', 'fromiter', # 'array_equal', 'array_equiv', 'indices', 'fromfunction', 'isclose', 'load', # 'loads', 'isscalar', 'binary_repr', 'base_repr', 'ones', 'identity', # 'allclose', 'compare_chararrays', 'putmask', 'seterr', 'geterr', # 'setbufsize', 'getbufsize', 'seterrcall', 'geterrcall', 'errstate', # 'flatnonzero', 'Inf', 'inf', 'infty', 'Infinity', 'nan', 'NaN', 'False_', # 'True_', 'bitwise_not', 'full', 'full_like', 'matmul' x = np.arange(6) x = x.reshape((2, 3)) np.zeros_like(x) y = np.arange(3, dtype=np.float) np.zeros_like(y) np.ones(5) np.ones((5,), dtype=np.int) np.ones((2, 1)) s = (2,2) np.ones(s) x = np.arange(6) x = x.reshape((2, 3)) np.ones_like(x) y = np.arange(3, dtype=np.float) np.ones_like(y) np.full((2, 2), np.inf) x = np.arange(6, dtype=np.int) np.full_like(x, 1) np.full_like(x, 0.1) np.full_like(y, 0.1) np.count_nonzero(np.eye(4)) np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]]) np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=0) np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=1) a = [1, 2] np.asarray(a) a = np.array([1, 2]) np.asarray(a) is a a = np.array([1, 2], dtype=np.float32) np.asarray(a, dtype=np.float32) is a np.asarray(a, dtype=np.float64) is a np.asarray(a) is a np.asanyarray(a) is a a = [1, 2] np.asanyarray(a) np.asanyarray(a) is a x = np.arange(6).reshape(2,3) np.ascontiguousarray(x, dtype=np.float32) x = np.arange(6).reshape(2,3) y = np.asfortranarray(x) x = np.arange(6).reshape(2,3) y = np.require(x, dtype=np.float32, requirements=['A', 'O', 'W', 'F']) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = np.array([[1, 2, 3], [4, 5, 6]], order='FORTRAN') np.isfortran(b) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = a.T np.isfortran(b) np.isfortran(np.array([1, 2], order='FORTRAN')) x = np.arange(6).reshape(2,3) np.argwhere(x>1) x = np.arange(-2, 3) np.flatnonzero(x) np.correlate([1, 2, 3], [0, 1, 0.5]) np.correlate([1, 2, 3], [0, 1, 0.5], "same") np.correlate([1, 2, 3], [0, 1, 0.5], "full") np.correlate([1+1j, 2, 3-1j], [0, 1, 0.5j], 'full') np.correlate([0, 1, 0.5j], [1+1j, 2, 3-1j], 'full') np.convolve([1, 2, 3], [0, 1, 0.5]) np.convolve([1,2,3],[0,1,0.5], 'same') np.convolve([1,2,3],[0,1,0.5], 'valid') rl = np.outer(np.ones((5,)), np.linspace(-2, 2, 5)) # im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,))) # grid = rl + im x = np.array(['a', 'b', 'c'], dtype=object) np.outer(x, [1, 2, 3]) a = np.arange(60.).reshape(3,4,5) b = np.arange(24.).reshape(4,3,2) c = np.tensordot(a,b, axes=([1,0],[0,1])) c.shape # A slower but equivalent way of computing the same... d = np.zeros((5,2)) a = np.array(range(1, 9)) A = np.array(('a', 'b', 'c', 'd'), dtype=object) x = np.arange(10) np.roll(x, 2) x2 = np.reshape(x, (2,5)) np.roll(x2, 1) np.roll(x2, 1, axis=0) np.roll(x2, 1, axis=1) a = np.ones((3,4,5,6)) np.rollaxis(a, 3, 1).shape np.rollaxis(a, 2).shape np.rollaxis(a, 1, 4).shape x = np.zeros((3, 4, 5)) np.moveaxis(x, 0, -1).shape np.moveaxis(x, -1, 0).shape np.transpose(x).shape np.moveaxis(x, [0, 1], [-1, -2]).shape np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape x = [1, 2, 3] y = [4, 5, 6] np.cross(x, y) x = [1, 2] y = [4, 5, 6] np.cross(x, y) x = [1, 2, 0] y = [4, 5, 6] np.cross(x, y) x = [1,2] y = [4,5] np.cross(x, y) x = np.array([[1,2,3], [4,5,6]]) y = np.array([[4,5,6], [1,2,3]]) np.cross(x, y) np.cross(x, y, axisc=0) x = np.array([[1,2,3], [4,5,6], [7, 8, 9]]) y = np.array([[7, 8, 9], [4,5,6], [1,2,3]]) np.cross(x, y) np.cross(x, y, axisa=0, axisb=0) # np.array_repr(np.array([1,2])) # np.array_repr(np.ma.array([0.])) # np.array_repr(np.array([], np.int32)) x = np.array([1e-6, 4e-7, 2, 3]) # np.array_repr(x, precision=6, suppress_small=True) # np.array_str(np.arange(3)) a = np.arange(10) x = np.arange(4) np.set_string_function(lambda x:'random', repr=False) grid = np.indices((2, 3)) grid.shape grid[0] # row indices grid[1] # column indices x = np.arange(20).reshape(5, 4) row, col = np.indices((2, 3)) x[row, col] np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) np.isscalar(3.1) np.isscalar([3.1]) np.isscalar(False) # np.binary_repr(3) # np.binary_repr(-3) # np.binary_repr(3, width=4) # np.binary_repr(-3, width=3) # np.binary_repr(-3, width=5) # np.base_repr(5) # np.base_repr(6, 5) # np.base_repr(7, base=5, padding=3) # np.base_repr(10, base=16) # np.base_repr(32, base=16) np.identity(3) np.allclose([1e10,1e-7], [1.00001e10,1e-8]) np.allclose([1e10,1e-8], [1.00001e10,1e-9]) np.allclose([1e10,1e-8], [1.0001e10,1e-9]) # np.allclose([1.0, np.nan], [1.0, np.nan]) # np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.isclose([1e10,1e-7], [1.00001e10,1e-8]) np.isclose([1e10,1e-8], [1.00001e10,1e-9]) np.isclose([1e10,1e-8], [1.0001e10,1e-9]) # np.isclose([1.0, np.nan], [1.0, np.nan]) # np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.array_equal([1, 2], [1, 2]) np.array_equal(np.array([1, 2]), np.array([1, 2])) np.array_equal([1, 2], [1, 2, 3]) np.array_equal([1, 2], [1, 4]) np.array_equiv([1, 2], [1, 2]) np.array_equiv([1, 2], [1, 3]) np.array_equiv([1, 2], [[1, 2], [1, 2]]) np.array_equiv([1, 2], [[1, 2, 1, 2], [1, 2, 1, 2]]) np.array_equiv([1, 2], [[1, 2], [1, 3]])
def qz_mjk_k(self, mj, k): m = np.tile(np.transpose(self._theta_m_k[:, k]), self._vocab_n) j = np.repeat(self._psi_k_j[k, :], self._n_docs) tmj = np.multiply(m, j).reshape(self._vocab_n, self._n_docs) tmj = np.transpose(tmj) return tmj / mj