我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用numpy.trim_zeros()。
def trim_zeros_frames(x, eps=1e-7): """Remove trailling zeros frames. Similar to :func:`numpy.trim_zeros`, trimming trailing zeros features. Args: x (numpy.ndarray): Feature matrix, shape (``T x D``) eps (float): Values smaller than ``eps`` considered as zeros. Returns: numpy.ndarray: Trimmed 2d feature matrix, shape (``T' x D``) Examples: >>> import numpy as np >>> from nnmnkwii.preprocessing import trim_zeros_frames >>> x = np.random.rand(100,10) >>> y = trim_zeros_frames(x) """ T, D = x.shape s = np.sum(np.abs(x), axis=1) s[s < eps] = 0. return x[: len(np.trim_zeros(s))]
def predict_one_component(self, team_1, team_2, neutral=False): """ Returns team 1's probability of winning """ if self.latent_variables.estimated is False: raise Exception("No latent variables estimated!") else: if type(team_1) == str: team_1_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values()).T[self.team_dict[team_1]], trim='b')[-1] team_2_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values()).T[self.team_dict[team_2]], trim='b')[-1] else: team_1_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values()).T[team_1], trim='b')[-1] team_2_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values()).T[team_2], trim='b')[-1] t_z = self.transform_z() if neutral is False: return self.link(t_z[0] + team_1_ability - team_2_ability) else: return self.link(team_1_ability - team_2_ability)
def predict_two_components(self, team_1, team_2, team_1b, team_2b, neutral=False): """ Returns team 1's probability of winning """ if self.latent_variables.estimated is False: raise Exception("No latent variables estimated!") else: if type(team_1) == str: team_1_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[0].T[self.team_dict[team_1]], trim='b')[-1] team_2_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[0].T[self.team_dict[team_2]], trim='b')[-1] team_1_b_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[1].T[self.team_dict[team_1]], trim='b')[-1] team_2_b_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[1].T[self.team_dict[team_2]], trim='b')[-1] else: team_1_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[0].T[team_1], trim='b')[-1] team_2_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[0].T[team_2], trim='b')[-1] team_1_b_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[1].T[team_1_b], trim='b')[-1] team_2_b_ability = np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[1].T[team_2_b], trim='b')[-1] t_z = self.transform_z() if neutral is False: return self.link(t_z[0] + team_1_ability - team_2_ability + team_1_b_ability - team_2_b_ability) else: return self.link(team_1_ability - team_2_ability + team_1_b_ability - team_2_b_ability)
def bdnn_prediction(bdnn_batch_size, logits, threshold=th): result = np.zeros((bdnn_batch_size, 1)) indx = np.arange(bdnn_batch_size) + 1 indx = indx.reshape((bdnn_batch_size, 1)) indx = utils.bdnn_transform(indx, w, u) indx = indx[w:(bdnn_batch_size-w), :] indx_list = np.arange(w, bdnn_batch_size - w) for i in indx_list: indx_temp = np.where((indx-1) == i) pred = logits[indx_temp] pred = np.sum(pred)/pred.shape[0] result[i] = pred result = np.trim_zeros(result) result = result >= threshold return result.astype(int)
def _get_ids_from_cutout( self, cutout_fcn, resource, resolution, corner, extent, t_range=[0, 1], version=0): """ Do a cutout and return the unique ids within the specified region. 0 is never returned as an id. Args: cutout_fcn (function): SpatialDB's cutout method. Provided for naive search of ids in sub-regions resource (project.BossResource): Data model info based on the request or target resource resolution (int): the resolution level corner ((int, int, int)): xyz location of the corner of the region extent ((int, int, int)): xyz extents of the region t_range (optional[list[int]]): time range, defaults to [0, 1] version (optional[int]): Reserved for future use. Defaults to 0 Returns: (numpy.array): unique ids in a numpy array. """ cube = cutout_fcn(resource, corner, extent, resolution, t_range) id_arr = np.unique(cube.data) # 0 is not a valid id. id_arr_no_zero = np.trim_zeros(id_arr, trim='f') return id_arr_no_zero
def get_audio_analysis(song_url): if(song_url is None): return None, None, None, None, None urlretrieve(song_url, "current.mp3") y, sr = librosa.load("./current.mp3") # Tempo = beats/minute tempo, beats = librosa.beat.beat_track(y=y, sr=sr) # pitch = Frequency pitches, magnitudes = librosa.piptrack(y=y, sr=sr, fmax=1000, hop_length=1000) pitches, magnitudes = extract_max(pitches, magnitudes, pitches.shape) y[abs(y) < 10**-2] = 0 y = np.trim_zeros(y) json = { 'sound_wave': np.array(y[:len(pitches)]).tolist(), 'pitch': pitches } y_harm, y_per = librosa.effects.hpss(y) harm, perc = audio_fingerprint(y_harm), audio_fingerprint(y_per) pitch_ave = np.average(pitches) return float(tempo), float(pitch_ave), float(harm), float(perc), json
def adjust_length(pred_line, lineN, max_length): """ Messy function that handles problems that arise if predictions for the same example have different lengths which may happen due to using a different batch size for each model. Normally it shouldn't be needed. :param pred_line: :param lineN: :param max_length: :return: """ pred_line = numpy.trim_zeros(pred_line, trim='b') # The following takes care of lines that are shorter than the ones for previous files due to 0-trimming if lineN > len(max_length): maxLen = numpy.append(max_length, len(pred_line)) while len(pred_line) < maxLen[lineN - 1]: pred_line = numpy.append(pred_line, 0) # print "Tail zero added to line "+str(lineN)+" of "+pred_file if len(pred_line) > maxLen[lineN - 1]: print '!!! Warning: Line ' + str(lineN) + ' is longer than the corresponding lines of previous files.' maxLen[lineN - 1] = len(pred_line) return pred_line, max_length
def predictions_from_csv(fh, max_length): """ Loads single model predictions from a csv file where lines may differ in length :param fh: file handle to the csv file :return: list of numpy arrays representing the predictions of individual examples """ preds = list() lineN = 0 for line in fh: lineN += 1 pred_line = numpy.fromstring(line, sep=', ') if (args.trim_zeros): # If different batch sizes are used for the fused models, the prediction vectors need to be adjusted pred_line, max_length = adjust_length(pred_line, lineN, max_length) preds.append(pred_line) return preds, max_length
def get_max_width(char): max_width = 0 for byte in char: trimmed = np.trim_zeros(byte, 'b') max_width = max(len(trimmed), max_width) return max_width
def avgEpisodeVValue(self): """ Returns the average V value on the episode (on time steps where a non-random action has been taken) """ if (len(self._Vs_on_last_episode) == 0): return -1 if(np.trim_zeros(self._Vs_on_last_episode)!=[]): return np.average(np.trim_zeros(self._Vs_on_last_episode)) else: return 0
def train(self, t_x, t_y, v_x, v_y, lrv, char2idx, sess, epochs, batch_size=10): idx2char = {k: v for v, k in char2idx.items()} v_y_g = [np.trim_zeros(v_y_t) for v_y_t in v_y] gold_out = [toolbox.generate_trans_out(v_y_t, idx2char) for v_y_t in v_y_g] best_score = 0 for epoch in range(epochs): Batch.train_seq2seq(sess, model=self.en_vec + self.trans_labels, decoding=self.feed_previous, batch_size=batch_size, config=self.trans_train, lr=self.trans_l_rate, lrv=lrv, data=[t_x] + [t_y]) pred = Batch.predict_seq2seq(sess, model=self.en_vec + self.de_vec + self.trans_output, decoding=self.feed_previous, decode_len=self.decode_step, data=[v_x], argmax=True, batch_size=100) pred_out = [toolbox.generate_trans_out(pre_t, idx2char) for pre_t in pred] c_scores = evaluation.trans_evaluator(gold_out, pred_out) print 'epoch: %d' % (epoch + 1) print 'ACC: %f' % c_scores[0] print 'Token F score: %f' % c_scores[1] if c_scores[1] > best_score: best_score = c_scores[1] self.saver.save(sess, self.trained + '_weights', write_meta_graph=False) if best_score > 0: self.saver.restore(sess, self.trained + '_weights')
def unpad_zeros(l): out = [] for tags in l: out.append([np.trim_zeros(line) for line in tags]) return out
def decode_chars(idx, idx2chars): out = [] for line in idx: line = np.trim_zeros(line) out.append([idx2chars[item] for item in line]) return out
def predict_seq2seq(sess, model, decoding, data, decode_len, dr=None, argmax=True, batch_size=100, ensemble=False, verbose=False): num_items = len(data) in_len = len(data[0][0]) input_v = model[:num_items*in_len + decode_len] input_v.append(decoding) if dr is not None: input_v.append(dr) predictions = model[num_items*in_len + decode_len:] output = [] samples = zip(*data) start_idx = 0 n_samples = len(samples) while start_idx < n_samples: if verbose: print '%d' % (start_idx * 100 / n_samples) + '%' next_batch_input = samples[start_idx:start_idx + batch_size] batch_size = len(next_batch_input) holders = [] next_batch_input = zip(*next_batch_input) for n_batch in next_batch_input: n_batch = np.asarray(n_batch).T for b in n_batch: holders.append(b) for i in range(decode_len): holders.append(np.zeros(batch_size, dtype='int32')) holders.append(True) if dr is not None: holders.append(0.0) if argmax: pre = sess.run(predictions, feed_dict={i: h for i, h in zip(input_v, holders)}) pre = [np.argmax(pre_t, axis=1) for pre_t in pre] pre = np.asarray(pre).T.tolist() pre = [np.trim_zeros(pre_t) for pre_t in pre] output += pre else: pre = sess.run(predictions, feed_dict={i: h for i, h in zip(input_v, holders)}) output += pre start_idx += batch_size return output
def plot_abilities_one_components(self, team_ids, **kwargs): import matplotlib.pyplot as plt import seaborn as sns figsize = kwargs.get('figsize',(15,5)) if self.latent_variables.estimated is False: raise Exception("No latent variables estimated!") else: plt.figure(figsize=figsize) if type(team_ids) == type([]): if type(team_ids[0]) == str: for team_id in team_ids: plt.plot(np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values()).T[self.team_dict[team_id]], trim='b'), label=self.team_strings[self.team_dict[team_id]]) else: for team_id in team_ids: plt.plot(np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values()).T[team_id], trim='b'), label=self.team_strings[team_id]) else: if type(team_ids) == str: plt.plot(np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values()).T[self.team_dict[team_ids]], trim='b'), label=self.team_strings[self.team_dict[team_ids]]) else: plt.plot(np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values()).T[team_ids], trim='b'), label=self.team_strings[team_ids]) plt.legend() plt.ylabel("Power") plt.xlabel("Games") plt.show()
def plot_abilities_two_components(self, team_ids, component_id=0, **kwargs): import matplotlib.pyplot as plt import seaborn as sns figsize = kwargs.get('figsize',(15,5)) if component_id == 0: name_strings = self.team_strings name_dict = self.team_dict else: name_strings = self.team_strings_2 name_dict = self.team_dict_2 if self.latent_variables.estimated is False: raise Exception("No latent variables estimated!") else: plt.figure(figsize=figsize) if type(team_ids) == type([]): if type(team_ids[0]) == str: for team_id in team_ids: plt.plot(np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[component_id].T[name_dict[team_id]], trim='b'), label=name_strings[name_dict[team_id]]) else: for team_id in team_ids: plt.plot(np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[component_id].T[team_id], trim='b'), label=name_strings[team_id]) else: if type(team_ids) == str: plt.plot(np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[component_id].T[name_dict[team_ids]], trim='b'), label=name_strings[name_dict[team_ids]]) else: plt.plot(np.trim_zeros(self._model_abilities(self.latent_variables.get_z_values())[component_id].T[team_ids], trim='b'), label=name_strings[team_ids]) plt.legend() plt.ylabel("Power") plt.xlabel("Games") plt.show()
def reverse(self, x): """Reverses output of transform back to text. Args: x: iterator or matrix of integers. Document representation in bytes. Yields: Iterators of utf-8 strings. """ for data in x: document = np.trim_zeros(data.astype(np.int8), trim='b').tostring() try: yield document.decode('utf-8') except UnicodeDecodeError: yield ''
def fetch_int_data_from_LA(self,bytes,chan=1): """ fetches the data stored by DMA. integer address increments .. tabularcolumns:: |p{3cm}|p{11cm}| ============== ============================================================================================ **Arguments** ============== ============================================================================================ bytes: number of readings(integers) to fetch chan: channel number (1-4) ============== ============================================================================================ """ try: self.H.__sendByte__(CP.TIMING) self.H.__sendByte__(CP.FETCH_INT_DMA_DATA) self.H.__sendInt__(bytes) self.H.__sendByte__(chan-1) ss = self.H.fd.read(int(bytes*2)) t = np.zeros(bytes*2) for a in range(int(bytes)): t[a] = CP.ShortInt.unpack(ss[a*2:a*2+2])[0] self.H.__get_ack__() except Exception as ex: self.raiseException(ex, "Communication Error , Function : "+inspect.currentframe().f_code.co_name) t=np.trim_zeros(t) b=1;rollovers=0 while b<len(t): if(t[b]<t[b-1] and t[b]!=0): rollovers+=1 t[b:]+=65535 b+=1 return t
def fetch_long_data_from_LA(self,bytes,chan=1): """ fetches the data stored by DMA. long address increments .. tabularcolumns:: |p{3cm}|p{11cm}| ============== ============================================================================================ **Arguments** ============== ============================================================================================ bytes: number of readings(long integers) to fetch chan: channel number (1,2) ============== ============================================================================================ """ try: self.H.__sendByte__(CP.TIMING) self.H.__sendByte__(CP.FETCH_LONG_DMA_DATA) self.H.__sendInt__(bytes) self.H.__sendByte__(chan-1) ss = self.H.fd.read(int(bytes*4)) self.H.__get_ack__() tmp = np.zeros(bytes) for a in range(int(bytes)): tmp[a] = CP.Integer.unpack(ss[a*4:a*4+4])[0] tmp = np.trim_zeros(tmp) return tmp except Exception as ex: self.raiseException(ex, "Communication Error , Function : "+inspect.currentframe().f_code.co_name)
def trim_zeros(filt, trim='fb'): """ Trim the leading and/or trailing zeros from a 1-D array or sequence. Parameters ---------- filt : 1-D array or sequence Input array. trim : str, optional A string with 'f' representing trim from front and 'b' to trim from back. Default is 'fb', trim zeros from both front and back of the array. Returns ------- trimmed : 1-D array or sequence The result of trimming the input. The input data type is preserved. Examples -------- >>> a = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0)) >>> np.trim_zeros(a) array([1, 2, 3, 0, 2, 1]) >>> np.trim_zeros(a, 'b') array([0, 0, 0, 1, 2, 3, 0, 2, 1]) The input data type is preserved, list/tuple in means list/tuple out. >>> np.trim_zeros([0, 1, 2, 0]) [1, 2] """ first = 0 trim = trim.upper() if 'F' in trim: for i in filt: if i != 0.: break else: first = first + 1 last = len(filt) if 'B' in trim: for i in filt[::-1]: if i != 0.: break else: last = last - 1 return filt[first:last]
def write_string(string, offset_x=0, offset_y=0, kerning=True): """Write a string to the buffer :returns: The length, in pixels, of the written string. :param string: The text string to write :param offset_x: Position the text along x (default 0) :param offset_y: Position the text along y (default 0) :param kerning: Whether to kern the characters closely together or display one per matrix (default True) :Examples: Write a string to the buffer, aligning one character per dislay, This is ideal for displaying still messages up to 6 characters long:: microdotphat.write_string("Bilge!", kerning=False) Write a string to buffer, with the characters as close together as possible. This is ideal for writing text which you intend to scroll:: microdotphat.write_string("Hello World!") """ str_buf = [] space = [0x00] * 5 gap = [0x00] * 3 if kerning: space = [0x00] * 2 gap = [0x00] for char in string: if char == ' ': str_buf += space else: char_data = numpy.array(_get_char(char)) if kerning: char_data = numpy.trim_zeros(char_data) str_buf += list(char_data) str_buf += gap # Gap between chars for x in range(len(str_buf)): for y in range(7): p = (str_buf[x] & (1 << y)) > 0 set_pixel(offset_x + x, offset_y + y, p) l = len(str_buf) del str_buf return l