def _transform(tensor): linear_trans = np.eyes(tensor.shape[-1]) linear_trans[:, i] = 0.5 temp = (tensor[:, i] + tensor[:, j]) / 2 tf.assign(tensor[:, i], temp) tf.assign(tensor[:, j], temp) return tensor
def fit(self, Xs, ys): interceptTerm = 0 if self.fit_intercept: interceptTerm = 1 M = np.zeros( (Xs[0].shape[1] + interceptTerm, Xs[0].shape[1] + interceptTerm)) N = np.zeros((Xs[0].shape[1] + interceptTerm, 1)) for i in range(len(Xs)): Xc = Xs[i] yc = ys[i] if self.fit_intercept: Xc = np.concatenate([Xc, np.ones((Xc.shape[0], 1))], axis=1) # Adding intercept column ones = np.ones((Xc.shape[0], 1)) M += np.linalg.multi_dot( [np.transpose(Xc), ones, np.transpose(ones), Xc]) N += yc * np.transpose(Xc).dot(ones) if self.ridge: M += self.lambda_ridge * np.eyes( M.shape[0]) # Integrating the Ridge term into the final matrix self.beta = np.linalg.solve(M, N) return self
def ekf_correction(self, z): # Observation Ht = np.ones((3 + 2 * self.lms, 5)) Ht[0:3, 0:3] = np.eyes(3) for i in range(z): mx = pos[0] + z[0] * np.cos(np.deg2rad(pos[2] + z[1])) my = pos[1] + z[0] * np.sin(np.deg2rad(pos[2] + z[1])) delta = np.array([[mx - pos[0]], [my - pos[1]]]) q = delta.T.dot(delta)[0, 0] sq = np.sqrt(q) H = np.array([[ -sq * delta[0], -sq * delta[1], 0, sq * delta[0], sq * delta[1] ], [delta[1], -delta[0], -q, -delta[1], delta[0]]]) / q K = P_pre @ H.T @ np.linalg.inv(H @ P_pre @ H.T + R) self.x = x_pre + K @ (z - H @ x_pre) temp = K @ H self.P = (np.eyes(temp.shape[0]) - temp) @ P_pre
def make_Dztstar(d, m, l): edges = d.size if l == 2: return np.eyes(edges) Ztstar = np.zeros(edges) if edges == 1: Ztstar[0] = d**(l - 2) else: for i in range(0, edges): Ztstar[i] = d[i]**(l - 2) return np.diag(Ztstar)
def transfert_study(dic_profils,dic_parts_genomes,dic_kmers,k): dic_profils_part = do_profils_all_genomes(dic_genomes,dic_kmers,k) matrice = np.eyes(len (dic_profils_part.keys()), len (dic_profils_part.keys())) listCles = {} cont = 0 count+=1 keys = dic_profils_part.keys() for i in keys: listCles[i] = cont for i in keys: for j in keys: if i!=j: matrice = distanceEuclidienne(genome[j], partieGenome[i]) return matrice
def build_community_graph(params, examples, labels, print_graph=False): num_neighbors, regular = params.num_neighbors, params.regular edges = edges_from_loss_fn(examples, cosine_loss, num_neighbors=num_neighbors, regular=regular, normalize_weights=True, substract_mean=True, exponent=True) if params.higher_order: num_nodes = len(set([int(edge[1][0]) for edge in edges])) adj = np.zeros(shape=(num_nodes, num_nodes)) for w, (a, b) in edges: adj[a, b] = adj adj = np.linalg.power(params.alpha * np.eyes(N=num_nodes) + adj, params.kappa) edges = [(a, b, w) for w, (a, b) in edges] graph = nx.Graph() graph.add_weighted_edges_from(edges) if print_graph: nx.draw_spring(graph) plt.show() return graph
# In[9]: arr # In[11]: np.ones((1,10))*5 # In[13]: np.eyes(1,2) # In[23]: arr = np.arange(9).reshape(3,3) arr # In[32]: np.eye(3)
d = 2 # dimensions n = 500 # number of x samples m = n # number of y samples T = 10000 # number of iterations eps = 1e-7 betaMax = 10000 betaCap = True # nstar = 50 # number of unassigned X samples X = np.random.uniform(0,1,(n,d)) #X = np.random.multivariate_normal([-1,-1],[[2,0],[0,2]],n) XMC = np.array(X)# XAn = np.array(X) mean = np.zeros(d) cov = np.eyes(d) Y = np.random.multivariate_normal(mean,cov,n) # Xstar = np.random.uniform(0,1,(nstar,d)) # Xstarold = np.array(Xstar) color = [norm(x - [0.5,0.5]) for x in X] color = np.array(color) Norm = matplotlib.colors.Normalize(vmin = np.min(color), vmax = np.max(color), clip = False) # Create lists for bandwidths of perturbation centers (aAn) # and associated coefficients (b,c,d) centers = 1 aAn = [0.]*centers ## aMC = [0.]*centers# B = [0.]*centers
downlinks = temp[5].split() ratio = temp[6] coeff = PathWeight[path]*ratio*saturationflow*lane_num for k in range(x_len): if path==x_R1[k][0] and x_R1[k][1] in uplinks: xL=k x_Eq[row:row+tc-1,xL*tc:(xL+1)*tc-1] = coeff*np.eye(tc-1) InitCoefficient[i]=InitCoefficient[i]+coeff*InitVehNum_x_R1[xL] elif path==x_R1[k][0] and x_R1[k][1] in downlinks: xL=k x_Eq[row:row+tc-1,xL*tc:(xL+1)*tc-1] = -1*coeff*np.eye(tc-1) InitCoefficient[i]=InitCoefficient[i]-coeff*InitVehNum_x_R1[xL] eL = i e_Eq[row:row+tc-1,eL*(tc-1):(eL+1)*(tc-1)]=-1*np.eyes(tc-1) b_Eq[row:row+tc-1] = np.zeros(tc-1) row = row+tc-1 print row ############################################################################################################################# ################################################ Inequition constraints ################################################# row = 0 ################################### minimum function ############################### for i in range(y_len): yL = i temp = y_R1['y'+str(j)] path = temp[0] junction = temp[1] pahsesequence = temp[2]
def GenTransMatrix(self, type_=None, trans_opts=None): return np.eyes(3, dtype=np.float)
def J_JADE(sig): M = np.eyes(sig.shape[0])
ufunc_reduce(np.add,a,b,c) def ufunc_reduce(ufct,*vectors): vs = np.ix_(*vectors) r = ufct.identity for v in vs: r = ufct(r,v) return r ufunc_reduce(np.add,a,b,c) np.add.identity np.identify() np.identify(2) np.identity(2) np.identity() np.identity(2) np.identity(4) np.eyes(4) np.eye(4) np.eye(3,4) np.identity(4,3) import numpy as np a = np.array([[1.0,2.0],[3.0,4.0]]) a print(a) a.T a.transpose() np.linalg.inv(a) u = np.eye(2) u j = np.array([[0.0,-1.0],[1.0,0.0]]) j j@j
def calculation(self): # Set odometry as first guess x0 = [t_x, t_y, cos(theta), sin(theta)] # TODO: check we should use odom in time 0 and then roto if self.is_first: self.points_old = self.points self.is_first = False self.is_second = True return elif self.is_second: x0 = np.array([ self.odom_position[0], self.odom_position[1], np.cos(self.odom_yaw), np.sin(self.odom_yaw) ]) x0 = x0.reshape((4, 1)) self.is_second = False else: x0 = self.roto_1 x = x0 # Solve x* = argmin_x x'M x + g'x s.t. x'Wx <= 1 and reproject to = 1 # Calculate all M_i. M_i_s = np.empty((len(self.points), 2, 4)) M_i_s[:, 0, 0] = 1 M_i_s[:, 0, 1] = 0 M_i_s[:, 0, 2] = self.points[:, 0] M_i_s[:, 0, 3] = -self.points[:, 1] M_i_s[:, 1, 0] = 0 M_i_s[:, 1, 1] = 1 M_i_s[:, 1, 2] = self.points[:, 1] M_i_s[:, 1, 3] = self.points[:, 0] # Calculate M. M = np.zeros((4, 4)) for M_i in M_i_s: M += np.matmul(np.transpose(M_i), M_i) # Get the points of the old scan. xs_old = self.points_old[:, 0] ys_old = self.points_old[:, 1] # https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.optimize.minimize.html def optimizer(x): x = x.reshape((4, 1)) return np.matmul(np.matmul(np.transpose(x), M), x) + np.matmul( np.transpose(g), x) # Guess x: while old x and new x are far, update the old x and repeat. k = 0 max_k = 50 epsilon = 1 while True: # Calculate g, projecting points into old frame using guess for x. g = np.zeros((1, 4)) for i, M_i in enumerate(M_i_s): # Project new points into old frame using guess for x. x_new, y_new = np.matmul(M_i, x) distances = np.sqrt( np.square(xs_old - x_new) + np.square(ys_old - y_new)) # Get the closest point. pi_i = self.points_old[np.argmin(distances)] pi_i = pi_i.reshape((2, 1)) # Update g. g += -2 * np.matmul(np.transpose(pi_i), M_i) g = np.transpose(g) # Transform sin(theta)^2 + cos(theta)^2 <= 1 to constraint >= 0. cons = ({ 'type': 'ineq', 'fun': lambda x: -(x[2]**2 + x[3]**2 - 1) }) res = minimize(optimizer, x, constraints=cons) x_new = res.x # Project onto the feasible set cos(theta)^2 + sin(theta)^2 = 1. intersection_polar = [1, np.arctan2(x_new[3], x_new[2])] x_new = np.array([ x_new[0], x_new[1], np.cos(intersection_polar[1]), np.sin(intersection_polar[1]) ]) # Check that the projection worked. # print(x_new[2]**2 + x_new[3]**2) # Project points using new guess. points_proj = np.array( [np.matmul(M_i, x_new) for i, M_i in enumerate(M_i_s)]) xs_proj, ys_proj = points_proj[:, 0], points_proj[:, 1] x = x_new guess_distance_mse = np.sqrt( np.sum( np.square(xs_old - xs_proj) + np.square(ys_old - ys_proj))) if guess_distance_mse < epsilon or k > max_k: break else: k += 1 # Calculate position print(x) roto = np.eyes((4, 4)) roto[0, 3] = x[0] roto[1, 3] = x[1] roto[0, 0] = x[2] roto[1, 1] = x[2] roto[1, 0] = x[3] roto[0, 1] = -x[3] self.roto_4 = np.matmul(self.roto_4, roto) self.roto_1 = np.array([ self.roto_4[0, 3], self.roto_4[1, 3], self.roto_4[0, 0], self.roto_4[1, 0] ]) self.roto_1 = self.roto_1.reshape((4, 1)) M_i = np.array([[1, 0, self.position[0], -self.position[1]], [0, 1, self.position[1], self.position[0]]]) position = np.matmul(M_i, self.roto_1) self.position = position # TODO: check topic being published to self.loc_tf.publish(Vector3(position[0], position[1], 0)) self.points_old = self.points
"""Array The number of dimensions is the rank of the array, the shape of an array is a tuple of integers giving the size of the array along each dimension """" a = np.array([1, 2, 3]) # Create a rank 1 array print(type(a)) # Prints "<class 'numpy.ndarray'>" print(a.shape) # Prints "(3,)" print(a) # Prints "[1, 2, 3]" print(a[0], a[1], a[2]) # Prints "1 2 3" # Numpy also provides many functions to create arrays a = np.zeros((2, 2)) # Creates an array of all zeros print(a) b = np.ones((1, 2)) # Creates an array of all ones print(b) c = np.full((2, 2), 7) print(c) d = np.eyes(2) print(d) e = np.random.random((2, 2)) print(e)