def forward(self, src_batch, tgt_batch, is_train=False): #src_batch: (src_seq_len, batch_size) (35,80) src_embeded = self.embedding_s(src_batch) tgt_embeded = self.embedding_t(tgt_batch) #padding src_padded = np.zero((35, src_batch.size(-1), self.word_emb_size)) tgt_padded = np.zero((35, src_batch.size(-1), self.word_emb_size)) src_padded[:src_embeded.size(0), :src_embeded.size(1), :src_embeded. size(2)] = src_embeded.data tgt_padded[:tgt_embeded.size(0), :tgt_embeded.size(1), :tgt_embeded. size(2)] = tgt_embeded.data src_padded = np.transpose(np.expand_dims(src_padded, 2), (1, 3, 2, 0)) src_padded = np.concatenate([src_padded] * 35, axis=2) tgt_padded = np.transpose(np.expand_dims(tgt_padded, 2), (1, 3, 2, 0)) tgt_padded = np.concatenate([tgt_padded] * 35, axis=3) input = Variable( torch.from_numpy(np.concatenate([src_padded, tgt_padded], axis=1)).float()) if self.use_cuda == True: input = input.cuda() output = self.conv1(input) output = self.conv2(output) output = output.view(output.size(0), 1280) output = F.relu(self.mlp(output)) output = self.ll(output) output = self.sigmoid(output) return output
def __init__(self, input_dim=3*32*32, hidden_dim=100, num_classes=10, weight_scale=1e-3, reg=0.0): """ Initialize a new network. Inputs: - input_dim: An integer giving the size of the input - hidden_dim: An integer giving the size of the hidden layer - num_classes: An integer giving the number of classes to classify - dropout: Scalar between 0 and 1 giving dropout strength. - weight_scale: Scalar giving the standard deviation for random initialization of the weights. - reg: Scalar giving L2 regularization strength. """ self.params = {} self.reg = reg ############################################################################ # TODO: Initialize the weights and biases of the two-layer net. Weights # # should be initialized from a Gaussian with standard deviation equal to # # weight_scale, and biases should be initialized to zero. All weights and # # biases should be stored in the dictionary self.params, with first layer # # weights and biases using the keys 'W1' and 'b1' and second layer weights # # and biases using the keys 'W2' and 'b2'. # ############################################################################ self.params['W1'] = weight_scale * np.random.randn(input_dim, hidden_dim) / np.sqrt(input_dim) self.params['b1'] = np.zero((hidden_dim,)) self.params['W2'] = weight_scale * np.random.randn(hidden_dim,num_classes) / np.sqrt(input_dim) self.params['b2'] = np.zero((num_classes,)) self.params['reg'] pass
def generate_fix_data_batch(sess, pool_size=500): """ 使用队列生成pool_size大小的image, label样本池,供下一步做伪随机采样 :param sess: 当前图所在的session :param pool_size: 样本池大小,默认500 """ image_size = [28, 28, 1] image_batch = np.zero([pool_size] + image_size) label_batch = np.zero([pool_size, 1]) image, label = parse_tfrecords(tfrecord_type='train') image = tf.expand_dims(image, -1) coord = tf.train.Coordinator() try: threads = tf.train.start_queue_runners(coord) for i in range(pool_size): img, lbl = sess.run([image, label]) image_batch[i] = img label_batch[i] = lbl except Exception as e: coord.request_stop(e) coord.request_stop() coord.join(threads) return image_batch, label_batch
def adjustForMiniBatch(self, miniBatch, lr): weightGradients = [np.zero(w.shape) for w in self.weights] biasGradients = [np.zero(b.shape) for b in self.biases] for x, expected in miniBatch: inputWeightGradients, inputBiasGradients = self.backprop( x, expected) weightGradients = [ nw + dnw for nw, dnw in zip(weightGradients, inputWeightGradients) ] biasGradients = [ nb + dnb for nb, dnb in zip(biasGradients, inputBiasGradients) ] self.weights = [ w - lr * nw / len(miniBatch) for w, nw in zip(self.weights, weightGradients) ] self.biases = [ b - lr * nb / len(miniBatch) for b, nb in zip(self.biases, biasGradients) ]
def __init__(self, hiddenNeuroNum): self.p_hiddenNeuroNum = hiddenNeuroNum #记录隐藏层神经元数目 self.inputNNum = 728 #记录输入层神经元数目 self.outputNNum = 10 #记录输出层神经元数目 self.data #记录用来训练或者测试的样本输入 self.label #记录用来训练或者测试的标签输入 self.modelPar1=np.zero(self.p_hiddenNeuroNum, self.inputNNum + 1) #用来存放模型参数的矩阵,应该有两个 self.modelPar2=np.zero(self.outputNNum,self.p_hiddenNeuroNum + 1) self.sample_loc self.label_loc
def cycle(self, stop_at_center): # 1. Lift the first 3 legs self._move( primary=[ positions[0] + np.array([0, 0, LIFT_HEIGHT]), positions[1] + np.array([0, 0, LIFT_HEIGHT]), positions[2] + np.array([0, 0, LIFT_HEIGHT]) ], support=self.support, ) difference_ratio = math.sqrt((BUBBLE_RADIUS**2) - (LIFT_HEIGHT**2)) / BUBBLE_RADIUS begin_time = time.time() # 2. forward stride while True: if any( linalg.norm(p, 2) >= BUBBLE_RADIUS - 5.0 for p in self.positions): break delta_front = difference_ratio * DELTA_MOVEMENT delta_back = DELTA_MOVEMENT if stop_at_center: forward_target = np.zero([0, 0, LIFT_HEIGHT]) backward_target = np.zero([0, 0, 0]) else: forward_target = np.zero( [0, difference_ratio * BUBBLE_RADIUS, LIFT_HEIGHT]) backward_target = np.zero([0, -BUBBLE_RADIUS, 0]) # HACK(fyq14): This assumes all thre have exactly the same # position in their respective coordinates. delta_front = cap_magnitude(forward_target - self.primary[0], magnitude=delta_front) delta_back = cap_magnitude(backward_target - self.support[0], magnitude=delta_back) self._move(primary=[v + delta_front for v in self.primary], support=[v + delta_back for v in self.support]) artificial_sleep(begin_time=begin_time, amount=DELTA_TIME) begin_time = time.time() # 3. Put the legs down self._move( primary=[v - np.array([0, 0, LIFT_HEIGHT]) for v in self.primary], support=self.support) sleep(0.1) self.cycle = self.cycle.flip()
def update(self,batch,eta): #定义一个权重和偏置的list,盛放batch中所求梯度和偏置的和 sum_delta_biases=[np.zero(y) for y in self.biases] sum_delta_weights=[np.zero(y) for y in self.weights] for x,y in batch: one_biases,one_weights=self.backpropgation() sum_delta_biases=[sdb+ob for sdb ob in zip(sum_delta_biases,one_biases)] sum_delta_weights=[sdw+ow for sdw,ow in zip(sum_delta_weights,one_weights)] #更新参数 self.biases=[bs-eta/len(batch)*sdb for bs,sdb in zip(self.biases,sum_delta_biases)] self.weights=[ws-eta/len(batch)*sdw for ws,sdw in zip(self.weights,sum_delta_weights)]
def get_metrics_matrices( edge_scores, particles_df, truth_df, edge_cut_range=None, track_length_range=None, ROI=None, ): if edge_cut_range is None: edge_cut_range = np.arange(0.1, 0.9, 0.1) if track_length_range is None: track_length_range = np.arange(1, 12) if ROI is None: ROI = {"pt": {"min": 1}} efficiency_matrix = np.zero((len(edge_cut_range), len(track_length_range))) fake_rate_matrix = np.zero((len(edge_cut_range), len(track_length_range))) for i, edge_cut in enumerate(edge_cut_range): for j, track_length in enumerate(track_length_range): track_candidates = get_track_candidates(edge_scores, edge_cut) # TODO # track_candidates = clean_track_candidates(track_candidates) selected_tracks = get_selected_tracks(track_candidates, track_length) n_selected_tracks = len(selected_tracks) ( n_matched_tracks, n_trackable_particles, n_matched_tracks_ROI, n_trackable_particles_ROI, ) = evaluate_tracks( truth, particles, tracks, frac_reco_matched=0.5, frac_truth_matched=0.5, ROI=ROI, ) # IGNORE ROI (Region of Interest) FOR NOW... efficiency_matrix[i, j] = n_matched_tracks / n_trackable_particles fake_rate_matrix[i, j] = n_matched_tracks / n_selected_tracks return efficiency_matrix, fake_rate_matrix, edge_cut_range, track_length_range
def __init__(self, *args, **kwargs): super().__init__() self.weight = np.array((kwargs[input_dim], kwargs[output_dim])) if kwargs[bias]: self.b = np.array(kwargs[output_dim]) else: self.b = np.zero(kwargs[output_dim])
def pool_forward(A_prev, hparameters, mode='max'): # get shape of the input (m, n_H, n_W, n_C) = A_prev.shape #Get filter size and stride for pool layer pad = hparameters['pad'] stride = hparameters['stride'] f = hparameters['f'] # create output layer . Initialize to zeros A = np.zero((m, n_H, n_W, n_C)) for i in range(m): for h in range(n_H): for w in range(n_W): for c in range(n_C): v_begin = h * stride v_end = v_begin + f h_begin = w * stride h_end = h_begin + f a_slice = A_prev[i, v_begin:v_end, h_begin:h_end, c] if mode == 'max': A[i, h, w, c] = np.max(a_slice) elif mode == 'average': A[i, h, w, c] = np.mean(a_slice) cache = (A_prev, hparameters) return A, cache
def fit(self, X, y): """ 输入训练数据,培训神经元 :param x: 输入样本的向量 :param y: 对应的样本分类 :return: X:shape[n_samples, n_features] X:[[1,2,3], [4,5,6]] n_samples:2 n_features:3 y:[1:-1] """ # 初始化权重向量为零,1+是因为要引入w0,也就是步调函数的阈值 self.w_ = np.zero(1 + X.shape[1]) self.errors_ = [] for _ in range(self.n_iter): errors = 0 """ X:[[1,2,3], [4,5,6]] y:[1,-1] zip(X, y)=[[1,2,3,1], [4,5,6,-1]] """ for xi, target in zip(X, y): update = self.eta * (target - self.predict(xi)) self.w_[1:] += update * xi self.w_[0] += update errors += int(update != 0.0) self.errors_.append(errors) pass pass
def fit(self, x, y): """ 输入训练样本,培训神经元,x 输入样本向量,y对应样本分类 x:shape[n_samples,n_features] x:[[1,2,3],[4,5,6]] """ """ 初始化权重向量为零 加1 是w0 """ self.w_ = np.zero(1 + x.shape[1]) self.errors_ = [] for _ in range(self.n_iter): errors = 0 for xi, target in zip(x, y): update = self.eta * (target - self.predict(xi)) self.w_[1:] += update * xi self.w_[0] += update errors += int(update != 0.0) self.errors_.append(errors) pass pass pass
def __init_(self, LENGTH): self.board = np.zero((LENGTH, LENGTH)) self.x = -1 self.o = 1 self.winner = None self.ended = False self.num_estates = 3**(LENGTH*LENGTH)
def extract_features(data, dict_features): ''' 文章から特徴を抽出するのが目的 特徴ごとの存在するかどうかのベクトル ['特徴a', '特徴b', '特徴c', ・・・] (特徴が存在したら、各成分に1を格納。存在しなかったら0を格納。) を出力する return 先頭要素と該当素性の位置を1にしたベクトル ''' data_one_x = np.zero(len(dict_features) + 1, dtype=np.float64) data_one_x[0] = 1 # 先頭要素は固定で1(素性に対応しない重み) for word in data.split(): # 前後の空白削除 word.strip() # ストップワードを削除 if is_stopped(word): continue # ステミング word = stem.stemWord(word) # 素性のインデックス取得、行列の該当箇所を1に try: data_one_x[dict_features[word]] = 1 except: pass # ditc_featursにない素性は無視 return data_one_x
def on_epoch_end(epoch): # print generated text print('\n--- Generating text each epoch: %d' % epoch) start_index = random.randint(0, len(text) - maxlen - 1) for diversity in [0.2, 0.5, 1.0, 1.2]: print('--- diversity:', diversity) generated = '' sentence = text[start_index:start_index + maxlen] generated += sentences print('--- generating with:', sentence) sys.stdout.write(generated) for i in range(400): x_pred = np.zero((1, maxlen, len(chars))) for t, char in enumerate(sentence): x_pred[0, t, char_indices[char]] = 1.0 preds = model.predict(x_pred, verbose=0)[0] next_index = sample(preds, diversity) next_char = indices_char[next_index] generated += next_char sentence = sentence[1:] + next_char sys.stdout.write(next_char) sys.stdout.flush() print()
def validate_acceleration_value(acceleration_val, space_dimensions): """ This function validates that the acceleration value array or list is the right type and that the values of the list or array are valid. If it is not correct, or it cannot be turned into the correct format, bark at the user. If it can be, return the data correctly formatted. """ try: accel_val = np.array(acceleration_val) except Exception: raise Hokulani_TypeError(('The inputted position value cannot be ' 'turned into a numpy array.')) try: accel_len_val = len(accel_val) except Exception: raise Hokulani_TypeError(('The inputted acceleration value does not ' 'have a length element, and therefore ' 'cannot be processed by other functions.')) if not (isinstance(accel_val, (np.ndarray, list))): raise Hokulani_TypeError( ('Acceleration value input is not the right ' 'type. Expected: {type1} or {type2}.').format(type1=type( np.ndarray), type2=type(list))) elif (accel_len_val != space_dimensions): raise Hokulani_ShapeError( ('Acceleration value input is not the ' 'right shape. Expected: {size1}').format( size1=np.shape(np.zero([space_dimensions])))) else: return accel_val
def function7(): # Create an array of zeros with the same shape and type as X. Dont use reshape method x = np.arange(4, dtype=np.int64) arr = np.zero(4, dtype=int) return arr #write your code here """
def zero_or_one_or_empty_ndarray(shape, type=0, dtype=np.int): if type == 0: return np.zero(shape, dtype=np.int) elif type == 1: return np.ones(shape, dtype=np.int) else: return np.empty(shape, dtype=np.int)
def idft(inp_arr,x): inp_arr=numpy.zero(x) basis=numpy.exp(2*numpy.pi*i*inp_arr) y=funct(inp_arr,y) four_t +=basis*y print 'the invesre fourier transform is' + repr(four_t)
def findBestSignConventionCFS(data, aftershock_count): """ We want to check the best sign convention for CFS out of four CFS columns ARGS: data -- dictionary containing the data aftershock_count -- a list containing the total number of aftershocks RETURNS: a number between 1 - 4 specifying the column """ auc = np.zero(4) # as we have four column related to AUC in which #we have two distinct columns and the other two contain the same data but sign is different auc[0] = roc_auc_score(aftershock_count.transpose(), np.double(data['stresses_full_cfs_1'])) auc[1] = roc_auc_score(aftershock_count.transpose(), np.double(data['stresses_full_cfs_2'])) auc[2] = roc_auc_score(aftershock_count.transpose(), np.double(data['stresses_full_cfs_3'])) auc[3] = roc_auc_score(aftershock_count.transpose(), np.double(data['stresses_full_cfs_4'])) whichIsMax = np.argmax(auc) return whichIsMax + 1
def vectorise(talks, vocab_size): one_hot_talks = np.zero([len(talks),len(talks)[0], vocab_size]) for i, talk in enumerate(talks): for j, index in enumerate(talk): if index != -1: one_hot_talks[i][index] = 1 return one_hot_talks
def __init__(self, transformer, file_name, row_size): self.transformer = transformer self.file_name = file_name self.file_open = False self.file_desc = None self.row = np.zero(row_size) self.row_size = row_size self.cursor = 0
def function6(): # Create a null vector of size 10 but the fifth and eighth value which is 10,20 respectively arr = np.zero(10, dtype=int) arr[5] = 10 arr[6] = 20#wrtie your code here return arr
def fit(self, X, y): self.X = np.asanyarray(X) self.y = np.asanyarray(y) self.classes = np.unique(y) n_features = self.X.shape[1] n_classes = self.classes.shape[0] self.mean = np.zero((n_classes, n_features)) self.var = np.zero((n_classes, n_features)) self.prob_prior = np.zeros((n_classes)) for i, cls in enumerate(self.classes): xi = self.X[np.where(y == cls)] self.mean[i] = np.mean(xi, axis=0, keepdims=True) self.var[i] = np.var(xi, axis=0, keepdims=True) self.prob_prior[cls] = xi.shape[0] / self.X.shape[0]
def fit(self, x, y): ''' 输入训练数据,培训神经元,x输入样本向量,y对应样本分类 :param x: shape[n_samples,n_features] [[1,2,3],[4,5,6]] :param y: n_samples:2 n_features:3 y:[1,-1] """ 初始化权重向量0 加一是因为前面算法提到的w0,也就是不调函数阈值 """ :return: ''' self.w_ = np.zero(1 + x.sharpe[1]) self.errors_ = [] for _ in range(self.n_iter): errors = 0 ''' X:[[1,2,3],[4,5,6]] y:[1,-1] zip(x,y)=[[1,2,3,1],[4,5,6,-1]] ''' for xi, target in zip(x, y): ''' update=n*(y-y') ''' update = self.eta * (target - self.predict(xi)) ''' xi 是一个向量 update * xi等价 [ ▲w(1)=x[1]*update,▲w(2)=x[2]*update,▲w(2)=x[3]*uodate] ''' self.w_[1:] += update * xi self.w_[0] += update errors += int(update != 0.0) self.errprs_.append(errors) pass pass def net_input(self, x): ''' z=w0*1+w1*x2+...wn*xn :param self: :param x: :return: ''' return np.dot(x, self.w_[1:] + self.w_[0]) pass def predict(self, x): return np.where(self.net_input(x) >= 0.0, 1. - 1) pass
def autoNorm(dataSet): minVals = dataSet.min(0) maxVals = dataSet.max(0) ranges = maxVals - minVals normDataSet = np.zero(dataSet) m = dataSet.shape[0] normDataSet = dataSet - np.tile(minVals, (m, 1)) normDataSet = normDataSet / np.tile(ranges, (m, 1)) return normDataSet, ranges, minVals
def init_var(method, shape, name): if method == 'zero': v = np.zeros(shape) elif method == 'glorot_uniform': v = glorot_uniform(shape, shape[0], shape[1]) else: # defaults to zero v = np.zero(shape) return theano.shared(v, name=name)
def get_current_areas(self): # init np array in size of num of polys current_areas = np.zero(self.num_of_polys) for i in range(self.num_of_polys): current_areas[i] = self.calc_poly_area(self.polys_points[i]) return current_areas
def adaClassify(datToClass, classifierArr): dataMatrix = np.mat(datToClass) m = np.shape(dataMatrix)[0] aggClassEst = np.mat(np.zero((m, 1))) for i in range(len(classifierArr)): classEst = stumpClassify(dataMatrix, classifierArr[i]['dim'], classifierArr[i]['thresh'], \ classifierArr[i]['ineq']) aggClassEst += classifierArr[i]['alpha'] * classEst print(aggClassEst) return np.sign(aggClassEst)
def calculate_loglks(self, X): """ given a matrix X, will return a matrix M (len(X), len(groups)) containing the log likelihood """ loglk = np.zero((len(X), self.profile.group_size())) for i in range(len(X)): for j in range(self.profile.group_size()): loglk[i, j] = self.loglk(X[i], j) return loglk
def update_batch(self,batch,eta): nabla_b=[np.zeros(b.shape) for b in self.biases] nabla_w=[np.zero(w.shape) for w in slef.weights] for x,y in batch: delta_nabla_b,delta_nabla_w=self.bachprop(x,y) nabla_b=[nb+dnb for nb,dnb in zip(nabla_b,delta_nabla_b)] nabla_w=[nw+dnw for nw,dnw in zip(nabla_w,delta_nabla_w)] self.weights=[w-(eta/len(batch))*nw for w,nw in zip(self.weights,nabla_w)] self.biases=[b-(eta/len(batch))*nb for b,nb in zip(self.biases,nabla_b)]
def getAvgFeatureVecs( reviews, model, num_features): counter = 0. reviewFeatureVecs = np.zero(( len(reviews), num_features), dtype="float32") for review in reviews: if counter % 1000. == 0.: print "Review %d of %d" % (counter, len(reviews)) reviewFeatureVecs[counter] = makeFeatureVec(review, model, num_features) counter = counter + 1. return reviewFeatureVecs
def fit(self, X, Y): """ 输入训练数据,培训神经元,x输入样本向量,y对应样本分类 X:shape[n_samples, n_features] X:[[1, 2, 3], [4, 5, 6]] n_samples: 2 n_features: 3 Y:[1, -1] """ """ 初始化权重向量为0 加一是因为前面算法提到的W0,也就是步调函数阈值 """ self.w_ = np.zero(1 + X.shape[1]) self.errors_ = [] for _ in range(self.n_iter): errors = 0 """ X: [[1, 2, 3], [4, 5, 6]] Y: [1, -1] zip(X,Y) = [[1, 2, 3, 1], [4, 5, 6, -1]] """ for xi, target in zip(X, Y): """ update = n * (y - y') """ update = self.eta * (target - self.predict(xi)) """ xi 是一个向量 update * xi 等价: [W(1) = X[1]*update , W(2) = X[2]*update ,W(3) = X[3]*update ] """ self.w_[1:] += update * xi self._w[0] += update errors += int(update != 0.0) self.errors_.append(errors)
def buildModel(self): ''' ''' beta = 2 mu_u = np.zeros(self.numFactors) mu_m = np.zeros(self.numFactors) # parameters of Inv-Whishart distribution WI_u = np.eye(self.numFactors) b0_u = 2 df_u = self.numFactors mu0_u = np.zeros(self.numFactors) WI_m = np.eye(self.numFactors) b0_m = 2 df_m = self.numFactors mu0_m = np.zeros(self.numFactors) # initializing Bayesian PMF using MAP solution found by PMF P = np.zeros((self.numUsers, self.numFactors)) Q = np.zeros((self.numItems, self.numFactors)) P = self.initGaussian(P, 0, 1) Q = self.initGaussian(Q, 0, 1) mu_u = P.mean(axis=1) mu_m = Q.mean(axis=1) alpha_u = np.asarray(np.asmatrix(np.var(P, axis=1))**(-1)) alpha_m = np.asarray(np.asmatrix(np.var(Q, axis=1))**(-1)) # Iteration: x_bar = np.zero(self.numFactors) normalRdn = np.zero(self.numFactors) M = self.numUsers N = self.numItems for iter in range(1, self.MAX_Iteration): # Sample from user hyper parameters x_bar = P.mean(axis=1) S_bar = np.var(P, axis=1) mu0_u_x_bar = mu0_u - x_bar e1e2 = np.asmatrix(mu0_u_x_bar).transpose() * np.asmatrix(mu0_u_x_bar) e1e2 /= (M * b0_u / (b0_u + M + 0.0)) WI_post = np. WI_post = WI_u.inv().add(S_bar.scale(M)).add(e1e2); WI_post = WI_post.inv(); WI_post = WI_post.add(WI_post.transpose()).scale(0.5); df_upost = df_u + M; DenseMatrix wishrnd_u = wishart(WI_post, df_upost); if (wishrnd_u != null) alpha_u = wishrnd_u; mu_temp = mu0_u.scale(b0_u).add(x_bar.scale(M)).scale(1 / (b0_u + M + 0.0)); lam = alpha_u.scale(b0_u + M).inv().cholesky(); if (lam != null) { lam = lam.transpose(); for (int f = 0; f < numFactors; f++) normalRdn.set(f, Randoms.gaussian(0, 1)); mu_u = lam.mult(normalRdn).add(mu_temp); }
def CalcTestSetRMS(self,svmout, wtsout, yfile, svmcolumn=1, ycolumn=1): # To calculate the RMS. Test # svmout include the class number of each data in test set # wtsout include the weight of each class if (not self.test): raise ValueError("No test set!"); if (svmcolumn<1): raise ValueError("Column number for SVM class should be >=1!"); if (ycolumn<1): raise ValueError("Column number for y value should be >=1!"); f2=open(wtsout) f1=open(svmout) samefile=(yfile==svmout) if (not samefile): f3=open(yfile) svmc=[] wts=[] y=[] for line in f1: tmp=line.strip().split() if (len(tmp)>0): svmc.append(tmp[svmcolumn-1]) if samefile: y.append(tmp[ycolumn-1]) for line in f2: tmp=line.strip().split() if (len(tmp)>0): wts.append(tmp); if not samefile: for line in f3: tmp=line.strip().split() if (len(tmp)>0): y.append(tmp[ycolumn-1]) # check data: if (len(svmc) is 0 or len(wts) is 0 or len(y) is 0): raise ValueError("Size of svm output or weight or y is zero!") if (len(svmc)!=len(test) or len(svmc)!=len(y)): raise ValueError("Data size is not equal! test:" +str(len(test))+"; svm:"+str(len(svmc)) +"; y:"+str(len(y)) ) if (len(test[0])!=len(wts[0])): raise ValueError("Features number is not equal! test: " +str(len(test[0]))+"; weight:"+str(len(wts[0])) ) nclass=len(wts) svmcs=set(svmc) for i in svmcs: if (int(i)>nclass): raise ValueError("Class number in svm is off weight border! Nweight: " +str(nclass)+"; Class number in svm:"+str(i)) testR=np.array(self.test,dtype=np.float64) wtsR=np.array(wts,dtype=np.float64) errors=np.zero((len(svmc),1)) for i in range(len(svmc)): tmpx=testR[i,:] tmpwts=wtsR[svmc[i],:] errors[i]=(np.abs(float(y[i])-(tmpwts[0, :].dot(tmpx.T)).T)).T; rms=np.sqrt((errors.T.dot(errors)).ravel()[0]/errors.shape[0]) print rms return rms
def get_aa_value(self,aa): index = self.proteinalphabet.rindex(aa) value = numpy.zero([20]) value[index] = 1 return value
from PIL import Image import glob import numpy as np image_list = map (Image.open, glob.glob("c:/*ok*.png")) ok_image = np.zero(len(image_list), image_list[0].size[0]*image_list[0].size[1]) for (idx, im) in enumerate(image_list): ok_image[idx,:] = np.array(im,np.uint8).reshape(1,im.size[0]*im.size[1]) image_list = map (Image.open, glob.glob("c:/*cyst*.png")) cyst_image = np.zero(len(image_list), image_list[0].size[0]*image_list[0].size[1]) for (idx, im) in enumerate(image_list): cyst_image[idx,:] = np.array(im,np.uint8).reshape(1,im.size[0]*im.size[1]) #now 2 matrices: all_image = np.concatenation((ok_image,cyst_image)) #now tagging what is ok and what is not image_class = np.concatenation((np.zeros(ok_image.shape[0],1),np.ones(cyst_image.shape[0],1))) #now taking the tests: 20%, 10% from each "list" from sklearn.cross_validation import train_test_split x_train, x_test, y_train, y_test = train_test_split(all_image, image_class, test_size=0.2) #now we throw it to the "checker" classifier = NearestNeighbor(); #new "object" classifier.train(x_train,y_train) #we are training it with method "train"
datainfo = experiments[expname] sampling = datainfo.sampling #/ 6.0 Tw = int(2 * np.round(wtime*sampling/2)) f = h5py.File(datainfo.dpath + datainfo.name + '.hdf5', 'r+') for dfile in datainfo.datafiles: print datainfo.dpath + dfile d = f[dfile + '/Raw'] raw = d[()] peaks = cdp_identification(raw, wtime, datainfo) for i, s in enumerate(datainfo.sensors): print s, len(peaks[s]) dgroup = f.create_group(dfile + '/' + s) # Time of the peak dgroup.create_dataset('Time', peaks[s].shape[0], dtype='i', data=peaks[s], compression='gzip') rawpeaks = np.zero((peaks[s].shape[0], Tw)) # Extraction of the window around the peak maximum for j in range(peaks[s].shape[0]): tstart = peaks[s][j] - np.floor(Tw / 2) tstop = tstart + Tw rawpeaks[j, :] = raw[tstart:tstop, i] # Peak Data dgroup.create_dataset('Peaks', rawpeaks.shape, dtype='f', data=rawpeaks, compression='gzip')
import re from collections import namedtuple import numpy as np trmm_files = np.zero(1, dtype = [('year',a4), ('month',a2), ('day',a2), ('hour',a2)]) with open('/nfs/see-fs-01_users/eepdw/python_scripts/filenamelist/trmm_netcdffilelist', 'r') as f: netcdf_filelist=f.readlines() ncdfl2 = [elem.strip().split("/") for elem in netcdf_filelist] # #trmm_files = namedtuple("trmm_files", "year month day hour") #print s2[1:51] #print netcdf_filelist[1] #print ncdfl2[0][9] #print len(ncdfl2) for i, item in enumerate(ncdfl2): trmm_files.year[0][i]= item[9] print trmm_files.year #/nfs/a80/earceb/model_data/observations/satellite/TRMM/2011 # nappy.convertNAToCSV(na_file, annotation=True)
def kmeans(self, dataset, label_type, k): numSamples, dim = dataset.shape clusterAssment = np.mat(np.zeros(numSamples, 2)) clusterChanged = True ##初始化聚类中心 centroids = self.initcentroids(dataset, k) while clusterChanged: clusterChanged = False for i in xrange(numSamples): minDist = 1 minIndex = 0 for j in range(k): distance = self.curve_distance(dataset[i, :], centroids[j, :]) if distance < minDist: minDist = distance minIndex = j # 对第j类进行更新 if clusterAssment[i, 0] != minIndex: clusterChanged = True clusterAssment[i, :] = minIndex, minDist * minDist # 更新聚类中心 for j in range(k): poinsInCluster = dataset[np.nonzero(clusterAssment[:, 0].A == j)[0]] #TODO: WHAT 'S THIS? centroids[j, :] = np.mean(poinsInCluster, axis=0) #类别统计 mtemp = np.zero(k,classNum) type_rec = [0] * k max = [0] * k userNumber = [0] * k for i in range(numSamples): ele = clusterAssment[i,0] mtemp[ele,label_type[i]] += 1 userNumber[ele] += 1 if mtemp[ele,label_type[i]]>max[ele]: max[ele] = mtemp[ele,label_type[i]] type_rec[ele] = label_type[i] #更新用户类别数据 for i in range(numSamples): clusterAssment[i, 0] = type_rec[clusterAssment[i, 0]] #类别概率计算 # TODO: NOTICE TO MODIFY prob = [[0] for i in range(k)] prob = np.matrix(prob) for i in range(numSamples): for j in range(k): c = (clusterAssment[i,0] == label_type[i]) prob[i,j] = c+(1-c)*userNumber[ele]/numSamples # 改进部分,对于一个数据,分别计算他与每个簇中心的距离,然后计算一个数据该簇的概率 for i in xrange(numSamples): membership = np.zeros(k, 1) distance = np.zeros(k, 1) count = 0.0 for j in range(k): distance[j] = 1 - self.curve_distance(dataset[i, :], centroids[j, :]) membership.append(distance[j]) count += distance[j] for j in range(k): membership[j] = membership[j] / count centroidsNo = [x for x in range(0, k)] item = self.random_pick(centroids, membership) clusterAssment[i, :] = item, distance[item] # 计算出第i个数据对每个中心的隶属度概率 print "cluster complete!" return centroids, clusterAssment
IndicatorName=str(row['Indicator Name']) IndicatorCode=str(row['Indicator Code']) Year2005=str(row['2005 [YR2005]']) Year2006=str(row['2006 [YR2006]']) Year2007=str(row['2007 [YR2007]']) Year2008=str(row['2008 [YR2008]']) Year2009=str(row['2009 [YR2009]']) Year2010=str(row['2010 [YR2010]']) Year2011=str(row['2011 [YR2011]']) Year2012=str(row['2012 [YR2012]']) Year2013=str(row['2013 [YR2013]']) f.close() n=Year2005.size imputation=np.zero(shape=(n,1)) Delta11=np.zero(shape=(n,1)) Delta12=np.zero(shape=(n,1)) Delta13=np.zero(shape=(n,1)) for i in range(n): if (Year2012[i] = '#NA' && Year2011[i] = '#NA' && Year2010[i]= '#NA'): imputation[i]='1' else: imputation[i]='0' #change metrics for i in range(n): if (Year2010[i] != '#NA' && Year2011[i] != '#NA'):
#!/usr/bin/python import numpy # Task 1.i) # Generate a vector A with on column and 3 rows A = numpy.array([1, 2, 3]) # Task 1.ii) B = numpy.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Zeige alle Zeilen (:) und Spalten 1 bis exklusiv 3 (1:3) C = B[:,1:3] # Task 1.iii) # Erstelle einen Normalvektor 3x1 (3, 1) und Multipliziere ihn mit 5 D = numpy.ones( (3,1) ) * 5 # Task 1.iv) # Wir verwenden numpy.hstack um zwei Matrizen zu verketten E = numpy.hstack( (B,D) ) # Task i.v) # Wie 1.iii benutzen wir numpy.zeros und den Dimensionsgrenzen F = numpy.zero( (2,3) )
def gpffin_init(n): x=numpy.zero(n) for i in xrange(n): x[i]=i+-24.5; return x
df2 = df[(df['geoname']==ct)&(df['maprule']==mrid)]; nslist = numpy.unique(df2.netname); if len(nslist) == 0: # if the combination of GEO and MAP is missing break; data = []; lowerend = -0.1 chart = pygal.Bar(x_label_rotation = 20, zero = lowerend); chart.x_labels = map(str,nslist); str_for_name = ct+"_"+str(mrid)+'.svg'; count = 0 for nt in nslist: ss = numpy.array(df2[df2.netname==nt].loc[:,Q_set])[0,:]; if len(ss) == 0: ss = numpy.zero(4) if count == 0: tt = numpy.append(ss, sum(ss)); target_title = numpy.array(df2[df2.netname==nt].loc[:,T_set])[0,:]; else: tt = numpy.vstack((tt, numpy.append(ss, sum(ss)))); count += 1; # now data has the format: deficiency_type x network data = tt.T chart.title = 'Deficiency: '+ct+' MR '+str(mrid)+'\ntarget=['+str(target_title[0])+', '+str(target_title[1])+', ' +str(100-target_title[2])+', ' +str(100-target_title[3])+'] ' ; if count > 1: # Deficiency First 4 Types: for type_scan in range(len(Q_set)): chart.add(Legend[type_scan], [{'value': data[type_scan, i],