def __init__(self,string1,string2): self.string1=str(string1) self.string2=str(string2) a=len(self.string1)+1 b=len(self.string2)+1 self.matrix=np.zeros([a,b],dtype=int) for i in np.arange(len(self.string2)+1): self.matrix[0,i]=i for j in np.arange(len(self.string1)+1): self.matrix[j,0]=j
def edit_distance(self): for i in np.arange(1,len(self.string1)+1): for j in np.arange(1,len(self.string2)+1): if self.string1[i-1]==self.string2[j-1]: self.matrix[i,j]=self.matrix[i-1,j-1] else: deletion = self.matrix[i-1,j] + 1 insertion = self.matrix[i,j-1] + 1 substitution =self.matrix[i-1,j-1] +1 self.matrix[i,j]=min(deletion,insertion,substitution) print(self.matrix)
def curve(c, offset): if c <= 0: return None if c <= 1: (a, b) = dom(c) (a1, b1) = (a + offset, b + offset) cur = [] for x in np.arange(a1, b1, .1): cur.append((x, lsy(x, c))) for x in np.arange(b1, a1, -.1): cur.append((x, -lsy(x, c))) return cur else: return [(x, lsy(x, c) * offset) for x in np.linspace(-3, 3, 15)]
def fetch_pids_ttol(pnts: MatrixVector, psys: PanelSystem, ztol: float=0.01, ttol: float=0.1): shp = pnts.shape pnts = pnts.reshape((-1, 1)) numpnt = pnts.shape[0] numpnl = len(psys.pnls) pidm = zeros((1, numpnl), dtype=int) wintm = zeros((numpnt, numpnl), dtype=bool) abszm = zeros((numpnt, numpnl), dtype=float) for pnl in psys.pnls.values(): pidm[0, pnl.ind] = pnl.pid wintm[:, pnl.ind], abszm[:, pnl.ind] = pnl.within_and_absz_ttol(pnts[:, 0], ttol=ttol) abszm[wintm is False] = float('inf') minm = argmin(abszm, axis=1) minm = array(minm).flatten() pidm = array(pidm).flatten() pids = pidm[minm] pids = matrix([pids], dtype=int).transpose() indp = arange(numpnt) minz = array(abszm[indp, minm]).flatten() minz = matrix([minz], dtype=float).transpose() chkz = minz < ztol pids[logical_not(chkz)] = 0 pids = pids.reshape(shp) chkz = chkz.reshape(shp) return pids, chkz
def initialization(self): self.Weights = [] self.Weights.append(np.random.randn(self.inputs, self.hiddenNodes[0])) for i in arange(self.hiddenLayers - 1) + 1: self.Weights.append( np.random.randn(self.hiddenNodes[i - 1], self.hiddenNodes[i])) self.Weights.append(np.random.randn(self.hiddenNodes[-1], self.outputs))
def sim(self,Xi): H = [] N = [] import ipdb; ipdb.set_trace() N.append(np.dot(Xi,self.Weights[0])) H.append(tanh(N[0])) for i in arange(self.hiddenLayers-1)+1: N.append(np.dot(H[i-1],self.Weights[i])) H.append(tanh(N[i])) N.append(np.dot(H[-1],self.Weights[-1])) Out = np.dot(H[-1],self.Weights[-1]) return Out,H,N
def sim(self, Xi): H = [] N = [] import ipdb ipdb.set_trace() N.append(np.dot(Xi, self.Weights[0])) H.append(tanh(N[0])) for i in arange(self.hiddenLayers - 1) + 1: N.append(np.dot(H[i - 1], self.Weights[i])) H.append(tanh(N[i])) N.append(np.dot(H[-1], self.Weights[-1])) Out = np.dot(H[-1], self.Weights[-1]) return Out, H, N
def train(self, data, training): 'En esta funcion se realiza 10-Fold CV para entrenar la red con una expansion de entre 20-75%.' 'El algoritmo de entrenamiento es Descenso por Gradiente Estocastico o Extreme Learning Machine.' # 10-Fold Cross Validation folds = 10; iters = 10; kf = KFold(data.shape[0], n_folds=folds) hiddenNodes = arange(2*data.shape[1])+1 Error_HNodes = [] Nets_HNodes = [] for j in hiddenNodes: self.setHiddenNodes([j]) Mean_error_iter = [] Mean_nets_iter = [] for train_index, val_index in kf: X, Xval = data[train_index], data[val_index] Error_iter = [] Nets_iter = [] for i in np.arange(iters): self.initialization() # Inicializaciones comunes if training == 'elm': Out,H,N = self.sim(X) H = H[-1] pseudoinverse = pinv(H) beta = np.dot(pseudoinverse,X) self.Weights[-1] = beta # Validation Out_val,H_val,N_val = self.sim(Xval) # Se guarda el error y la red MSE = [mean_squared_error(Xval,Out_val)] Networks = [self.Weights] Error_iter.append(np.min(MSE)) Nets_iter.append(Networks[np.argmin(MSE)]) Mean_error_iter.append(np.mean(Error_iter)) Mean_nets_iter.append(Nets_iter[np.argmin(Error_iter)]) Error_HNodes.append(np.mean(Mean_error_iter)) Nets_HNodes.append(Mean_nets_iter[np.argmin(Mean_error_iter)]) self.Weights = Nets_HNodes[np.argmin(Error_HNodes)] Final_Error = np.min(Error_HNodes) selected_Nodes = hiddenNodes[np.argmin(Error_HNodes)] self.setHiddenNodes([selected_Nodes]) return Final_Error
CZ_LOBS = config.getLobsConfig()["lobs"]["CZ"] # CZ_LOBS = ["SMS", "GSM", "MMS","SMG","ATS","OTA"] data = [] lobsNameList = [] for lob in CZ_LOBS: data.append(lobData(lob)) lobsNameList.append(lob) R = corrcoef(data) print(R) plt.pcolor(R) plt.colorbar() plt.yticks(arange(0.5, len(CZ_LOBS) + 0.5), CZ_LOBS) plt.xticks(arange(0.5, len(CZ_LOBS) + 0.5), CZ_LOBS) #plt.show() def dendo(): from matplotlib import pyplot as plt from scipy.cluster.hierarchy import dendrogram, linkage import numpy as np np.random.seed(4711) # for repeatability of this tutorial a = np.random.multivariate_normal([10, 0], [[3, 1], [1, 4]], size=[100, ]) b = np.random.multivariate_normal([0, 20], [[3, 1], [1, 4]], size=[50, ]) X = R Z = linkage(X, 'ward') plt.figure(figsize=(25, 10))
return pt return showPair(pt) glops = "" def drawPoint(pt, options=None): if options is None: options = glops return "\\node[{}] at {} {{}}; ".format(options, showPt(pt)) def drawPath(pt, options=None): if options is None: options = glops return "\\draw[{}] {};".format(options, "--".join(map(showPt, pt))) glops = "red,->" for x in [.5, 1.5, 2.5]: for y in np.arange(-3, 4): print( drawPath([(x, y + x * .1 - .1), (3.2, y + x * .1 - .1)], "red,->, thick")) #print(drawPoint((x,y+x*.1-.1), "atom")) print( drawPath([(-3.2, y - x * .1 + .1), (-x, y - x * .1 + .1)], "blue,->, thick")) #print(drawPoint((-x,y-x*.1+.1), "atom"))
def initialization(self): self.Weights = [] self.Weights.append(np.random.randn(self.inputs,self.hiddenNodes[0])) for i in arange(self.hiddenLayers-1)+1: self.Weights.append(np.random.randn(self.hiddenNodes[i-1],self.hiddenNodes[i])) self.Weights.append(np.random.randn(self.hiddenNodes[-1],self.outputs))
return pt return showPair(pt) glops = "" def drawNode(points, options=None): if options is None: options = glops return "\\draw[{}] {}; ".format(options, "--".join(map(showPt, points))) def drawPoint(pt, options=None): if options is None: options = glops return "\\node[{}] at {} {{}}; ".format(options, showPt(pt)) glops = "atom" def p(r, theta): return (r * math.cos(theta), r * math.sin(theta)) for r in np.arange(.5, 6, .5): for theta in np.arange(0, 2 * math.pi, .5 * math.pi / (r**3)): (x, y) = p(r, theta) if -3 < x < 3 and -3 < y < 3: print(drawPoint((x, y)))