def next(self, batch_size): if self.current_batch + batch_size < self.size: self.current_batch += batch_size return self.x_title[ self.current_batch:self.current_batch + batch_size], self.x_body[ self.current_batch:self.current_batch + batch_size], self.y[ self.current_batch:self.current_batch + batch_size], self.seqlen_title[ self.current_batch:self.current_batch + batch_size], self.seqlen_body[ self.current_batch:self.current_batch + batch_size] else: self.current_batch = self.current_batch + batch_size - self.size batch_x_title = np.concatentate(self.x_title[self.current_batch:], self.x_title[:self.current_batch]) batch_x_body = np.concatentate(self.x_body[self.current_batch:], self.x_body[:self.current_batch]) batch_y = np.concatenate(self.y[self.current_batch:], self.y[:self.current_batch]) batch_seqlen_title = np.concatenate( self.seqlen_title[self.current_batch:], self.seqlen_title[:self.current_batch]) batch_seqlen_body = np.concatenate( self.seqlen_body[self.current_batch:], self.seqlen_body[:self.current_batch]) return batch_x_title, batch_x_body, batch_y, batch_seqlen_title, batch_seqlen_body
def plot_both_errors(trainCEs, trainREs, testCE=None, testRE=None, pad=None, block=True): plt.figure(2) plt.clf() if pad is None: pad = max(len(trainCEs), len(trainREs)) else: trainCEs = np.concatentate((trainCEs, [None] * (pad - len(trainCEs)))) trainREs = np.concatentate((trainREs, [None] * (pad - len(trainREs)))) plt.subplot(2, 1, 1) plt.title('Classification accuracy') plt.plot(100 * np.array(trainCEs)) if testCE is not None: plt.plot([100 * testCE] * pad) plt.subplot(2, 1, 2) plt.title('Model loss (MSE)') plt.plot(trainREs) if testRE is not None: plt.plot([testRE] * pad) plt.tight_layout() plt.gcf().canvas.set_window_title('Errors') plt.show(block=block)
def plot_both_errors(trainCEs, trainREs, testCE=None, testRE=None, pad=None, block=True, save_path=None): plt.figure(2).canvas.mpl_connect('key_press_event', keypress) plt.clf() if pad is None: pad = max(len(trainCEs), len(trainREs)) else: trainCEs = np.concatentate((trainCEs, [None]*(pad-len(trainCEs)))) trainREs = np.concatentate((trainREs, [None]*(pad-len(trainREs)))) plt.subplot(2,1,1) plt.title('Classification error [%]') plt.plot(100*np.array(trainCEs)) if testCE is not None: plt.plot([100*testCE]*pad) plt.subplot(2,1,2) plt.title('Model loss [MSE/sample]') plt.plot(trainREs) if testRE is not None: plt.plot([testRE]*pad) plt.tight_layout() plt.gcf().canvas.set_window_title('Error metrics') if save_path: plt.savefig(save_path) plt.show(block=block)
def triangulationMatting(self): """ success, errorMessage = triangulationMatting(self) Perform triangulation matting. Returns True if successful (ie. all inputs and outputs are valid) and False if not. When success=False an explanatory error message should be returned. """ success = False msg = 'Placeholder' ######################################### ## PLACE YOUR CODE BETWEEN THESE LINES ## ######################################### backA = self._images['backA'] backB = self._images['backB'] compA = self._images['compA'] compB = self._images['compB'] if ((backA is not None) and (backB is not None) and (compA is not None) and (compB is not None)): if ((backA.shape == backB.shape) and (backA.shape == compA.shape) and (backA.shape == compB.shape)): self._images['colOut'] = np.zeros( [backA.shape[0], backA.shape[1], 3]) self._images['alphaOut'] = np.zeros( [backA.shape[0], backA.shape[1]]) for i in range(backA.shape[0]): for j in range(backA.shape[1]): deltaA = compA - backA deltaB = compB - backB delta = np.concatenate((deltaA, deltaB), axis=0) identity = np.identity(3) twoIdentities = np.concatenate((identity, identity), axis=0) comps = np.concatentate(((-1) * compA, (-1) * compB), axis=0) A = np.concatentate((twoIdentities, comps.T), axis=1) x = np.dot(np.linalg.pinv(A), delta.T) self._images['colOut'][i, j] = np.clip( x[0:3], 0.0, 255.0) #if(x[3] <= 0.0): #self._images['alphaOut'][i, j] = 0.0 #else if(x[3] > 1.0): #self._images['alphaOut'][i, j] = 255.0 #else: #self._images['alphaOut'][i, j] = x[3] * 255.0 success = True else: success = False msg = "source error" ######################################### return success, msg
def moving_average_update(old_data, old_avg, new_data, period=20): ''' update the moving average :param data: :param period: :return: ''' if old_data.shape[0] < period: padded_data = np.concatenate((np.ones(period) * old_data[0], old_data)) working_data = old_data[-period:] else: working_data = old_data[-period:] working_data = np.concatentate((working_data, new_data)) cumulative_data = np.cumsum(working_data) working_moving_avg = (cumulative_data[period:] - cumulative_data[:-period]) / period moving_avg = np.concatenate((old_avg, working_moving_avg)) return moving_avg
def process_data( X, y, X_sub=None, y_sub=None, insuff=None, subset_frac=0.1): """ This function builds and returns the quadratic sufficient statistics, and stores a specified subset of the data. The sufficient statistics are: suff[0] = sum x suff[1] = sum y*x suff[2] = sum x*x^T suff[3] = sum y*x*x^T The subset of data is stored in X_sub and y_sub, and the fraction to be stored is given by subset_frac. """ if insuff is not None: suff = [x + y for x, y in zip(suff, insuff)] # num data T = np.shape(y)[0] # get random subset of data indices = np.random.choice(T, int(T*subset_frac), replace=False) if X_sub is None: X_sub = X[indices, :] y_sub = y[indices] else: X_sub = np.vstack([X_sub, X[indices, :]]) y_sub = np.concatentate([y_sub, y[indices]]) return suff, X_sub, y_sub
def ProvideWrappedS(sArray, index): s = sArray #shortcut smax = s[-1] sind = s[index] snewa = s[index:] snewa = snewa - sind snewb = s[:index] snewb = snewb + (smax - sind) snew = _np.concatentate((snewa, snewb)) return snew
def make_set_gauss(cov, start_state, num_particles): """ Initialize a gaussian distribution around start state """ sx = start_state.pos[0] sy = start_state.pos[1] #TODO: check this mean = np.concatentate((start_state, np.matrix([start_state.angle])), axis=0) def gen_pose(idx): sample = np.random.multivariate_normal(mean, cov) return t2d.Pose2D(sample[0,0], sample[1,0], sample[2,0]) return map(gen_pose, range(num_particles))
def make_set_gauss(cov, start_state, num_particles): """ Initialize a gaussian distribution around start state """ sx = start_state.pos[0] sy = start_state.pos[1] # TODO: check this mean = np.concatentate((start_state, np.matrix([start_state.angle])), axis=0) def gen_pose(idx): sample = np.random.multivariate_normal(mean, cov) return t2d.Pose2D(sample[0, 0], sample[1, 0], sample[2, 0]) return map(gen_pose, range(num_particles))
def cmap(clr1,clr2,clrType=None,name=None): ''' NAME: mmlcolors.cmap PURPOSE: To create a simple linear colormap from two colors. CALLING: cmapout=cmap(clr1,clr2[,clrType=None,name=None]) ARGUMENTS: clr1: 3x1 list of intial color values clr2: 3x1 list of final color values KEYWORDS: clrType: String specifying what type of colors clr1 & clr2 are "rgb" [DEFAULT], "hsv", or "hls" name: String to use to name colormap OUTPUT: cmapout: New instance of Colormap ''' import mmlpars,numpy import matplotlib.colors as colors import matplotlib.cm as cm # Pars input clr1=mmlpars.mml_pars(clr1,type=list) clr2=mmlpars.mml_pars(clr2,type=list) clrType=mmlpars.mml_pars(clrType,default='rgb',type=str,list=['rgb','hsv','hls']) # Handle rgb colors if clrType=='rgb': # Create dictionary for segmented color map cdict={'red': [(0.0,clr1[0],clr1[0]), (1.0,clr2[0],clr2[0])], 'green': [(0.0,clr1[1],clr1[1]), (1.0,clr2[1],clr2[1])], 'blue': [(0.0,clr1[2],clr1[2]), (1.0,clr2[2],clr2[2])]} # Create linear segmented colormap cmapout=colors.LinearSegmentedColormap(name,cdict) # Handle other color types else: # Create array in original colorspace clrlist_0=numpy.linspace(clr1[0],clr2[0],256) clrlist_1=numpy.linspace(clr1[1],clr2[1],256) clrlist_2=numpy.linspace(clr1[2],clr2[2],256) clrarr=numpy.concatentate(([clrlist_0],[clrlist_1],[clrlist_2]),axis=0).T # Convert to rgb space if clrType=='hsv': clrarr=colors.hsv_to_rgb(clrarr) elif clrType=='hls': clrarr=colors.hls_to_rgb(clrarr) # Create color map from array cmapout=colors.ListedColormap(clrarr,name=name) # Return colormap return cmapout
def square_plot(data, path): if type(data) == list: data = np.concatentate(data) data = (data - data.min()) / (data.max() - data.min()) n = int(np.ceil(np.sqrt(data.shape[0]))) padding = (((0, n**2 - data.shape[0]), (0, 1), (0, 1)) + ((0, 0), ) * (data.ndim - 3)) data = np.pad(data, padding, mode='constant', constant_values=1) data = data.reshape((n, n) + data.shape[1:]).transpose( (0, 2, 1, 3) + tuple(range(4, data.ndim + 1))) data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:]) plt.imsave(path, data, cmap='gray')
def get_samples(self, permuted=True): # outputs numpy matrix if permuted: temp_list = [] for chain in self.store_chains: temp_list.append(chain["chain_obj"].get_samples( warmup=self.warmup_per_chain)) output = temp_list[0] if len(temp_list) > 0: for i in range(1, len(temp_list)): output = numpy.concatentate([output, temp_list[i]], axis=0) return (output) else: raise ValueError("for now leave this")
def scan_callback(msg): global minMidDistanceVal global drive_forward global lastScan scan = np.asarray(msg.ranges) zeroInds = np.where(scan == 0)[0] scan[zeroInds.tolist()] = float('inf') radsPerSample = msg.angle_increment samplesLeftRight int(np.floor(.35/radsPerSample)) lInd = int(len(scan) - samplesLeftRight) lrvals = np.concatentate((scan[lInd:],scan[0:samplesLeftRight+1])) minMidDistanceVal = min(lrvals) if (minMidDistanceVal < minAcceptableDistance): drive_forward = False lastScan = scan
np.full_like(a,val) np.linspace() np.concatentate() ''' c = np.arange(6) d = np.ones((3, 6)) e = np.eye(5, dtype=np.int) f = np.zeros((3, 6), dtype=np.int32) g = np.full((2, 3), 4) np.zeros_like(a) g = np.linspace(1, 10, 5) h = np.concatentate((a, b)) ''' ###############ndarray数组的维度变换############### a.reshape(shape) #不改变数组 a.resize(shape) #改变数组 a.swapaxes(ax1,ax2) #n个维度中两个进行调换 a.flatten() #降维 new_a = a.astype(new_type) #创建新数组 ''' ''' ################ndarray数组向列表转换############ ls = a.tolist() ''' ''' ###############多维数组的索引和切片##############