def loadSSFPdata(loadSSFPpaths, loadT1path, loadT2path): ## Load T1 T1_img = np.array(nrrd.read(loadT1path)[0]) ## Load T2 T2_img = np.array(nrrd.read(loadT2path)[0]) ## Load SSFP numSSFP = len(loadSSFPpaths) SSFP_files = range(numSSFP) SSFP_shape = range(numSSFP) for ss in range(numSSFP): print('Loading file: ' + loadSSFPpaths[ss]) SSFP_files[ss] = np.rray(nrrd.read(loadSSFPpaths[ss])[0]) SSFP_shape = SSFP_files[ss].shape return SSFP_files, T1_img, T2_img
def plot_decision_boundry(X,y): X_set,y_set = X,y import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap X1,X2 = np.meshgrid(np.arange(start = X_set[:,0].min()-1, stop=X_set[:,0].max()+1, step=0.01), np.arange(start=X_set[:,1].min()-1, stop=X_set[:,1].max()+1, step=0.01) ) plt.contour(X1,X2,clf.predict(np.rray([X1.ravel(),X2.ravel()]).T ).reshape(X1.shape),alpha=0.75) plt.xlim(X1.min(),X1.max()) plt.ylim(X2.min(),X2.max()) for i,j in enumerate(np.unique(y_set)): plt.scatter(X_set[y_set==j,0], X_set[y_set==j,1], c=ListedColormap(('red','green'))(i),label=j) plt.title('Logistic Regression Classification Boundry') plt.xlabel('X Label') plt.ylabel('Y Label') plt.legend() plt.show()
def DOER(N0, nHiddenNeurons, Block, nModels, alfa, windowSize, dataset, trBlock): ##### Load dataset try: data = np.genfromtxt(dataSet, dtype=float) except Exception: print('An error ecurred while loading data') T = np.array(data[:, 0]) P = np.rray(data[:, 1:data.shape[1]]) nInputNeurons = data.shape[1] - 1 nTrainingData = data.shape[0] ##### Step 1 Initialization Phase P0 = P[0:N0, :] T0 = T[0:N0] PT = np.array(P[N0 - trBlock:nTrainingData, :]) TT = np.array(T[N0 - trBlock:nTrainingData]) Real = np.array(T[N0 - 1:nTrainingData - 1]) Y = np.zeros_like(Real) Error = np.zeros_like(Real) start_time = time() errorSum = 0 ensemble = [] ensemOutput = [] ensemble.append(trainModel(nHiddenNeurons, nInputNeurons, P0, T0)) nModelsCounter = 0 ##### Step 2 Sequential Learning Phase for n in range(trBlock, PT.shape[0]): Ptemp1 = np.array(PT[(n - windowSize):n]) Ttemp1 = np.array(TT[(n - windowSize):n]) # print("Ptemp1: ", Ptemp1, "Ttemp1: ", Ttemp1) SumW = 0 SumYW = 0 for modl in ensemble: H = SigActFun(modl.Bias, Ptemp1, modl.IW) g = np.dot(H, modl.beta) if nModelsCounter < 11: ensemOutput.append(g[-1]) SumW += modl.w SumYW += g[-1] * modl.w nModelsCounter += 1 modl.calculateError(g[-1], Ttemp1[-1], trBlock) modl.calculateMSE(trBlock) modl.calculateWeight(ensemble) K = np.dot(modl.M, np.transpose(H)) Q = np.linalg.inv(np.eye(windowSize) + np.dot(H, K)) R = np.dot(K, Q) S = np.dot(H, modl.M) M = modl.M - np.dot(R, S) beta = modl.beta + np.dot(np.dot(M, np.transpose(H)), (Ttemp1 - np.dot(H, modl.beta))) modl.beta = beta modl.M = M nModelsCounter = 0 modelObj.rank(ensemble) Y[n - trBlock] = SumYW / SumW # print("Y: ", (n-60), "n: ",n, "Y[n-60]: ",Y[n-60]) ensemOutput = [] errorSum += np.power((Ttemp1[-1] - Y[n - trBlock]), 2) Error[n - trBlock] = errorSum / ((n - trBlock) + 1) if Ttemp1[-1] != 0: a = abs(Y[n - trBlock] - Ttemp1[-1] / Ttemp1[-1]) if a > alfa: Ptemp2 = np.array(PT[(n - trBlock):n]) Ttemp2 = np.array(TT[(n - trBlock):n]) newModel = trainModel(nHiddenNeurons, nInputNeurons, Ptemp2, Ttemp2) if len(ensemble) < nModels: ensemble.append(newModel) else: ensemble = modelObj.replaceModels(ensemble, newModel) ##### Time in seconds end_time = time() totalTime = end_time - start_time hours, rest = divmod(totalTime, 3600) minutes, seconds = divmod(rest, 60) compTime = np.round(seconds, 5) ##### Acurracy MSE mse = mean_squared_error(Real, Y) accuracy = np.round(mse, 5) last_Real = Real[-200:] last_Y = Y[-200:] last_accuracy = mean_squared_error(last_Real, last_Y) return accuracy, compTime, Error, last_accuracy
model.fit(corr_whisky) print(model.rows_) print(np.sum(model.rows_, axis=1)) print(np.sum(model.rows_, axis=0)) #1's print(model.row_labels_) #observation index belongs to cluster 0-5 #Comparing Correlation Matrices #Rename indices to match sorting #Append group labels from model to whisky table whisky['Group'] = pd.Series(model.row_labels_, index=whisky.index) #Reorder rows by increasing group order from spectral coclustering whisky = whisky.ix[np.argsort( model.row_labels_)] #changes appearance but leaves indices same #Reset index of dataframe whisky = whisky.reset_index(drop=True) #resets indices in increasing order correlations = pd.DataFrame.corr(whisky.iloc[:, 2:14].T) correlations = np.rray(correlations) plt.figure(figsize=(14, 7)) plt.subplot(121) plt.pcolor(corr_whisky) plt.title("Original") plt.axis("tight") plt.subplot(122) plt.pcolor(correlations) plt.title("Rearranged") plt.axis("tight") plt.savefig("correlations.pdf") #see 6 blocks of whiskies along line , similar flavors
def vertex_find(A, b, lb, ub): if len(lb) != 0: Vertex = np.matrix([[], []]) m = A.shape[0] n = A.shape[1] if m == 0: Vertex = bounds(lb, ub, len(lb)) else: for r in range(0, min(n, m) + 1): from itertools import combinations C = [c for c in combinations(range(1, m + 1), r)] C = [list(c) for c in C] D = [d for d in combinations(range(1, n + 1), n - r)] D = [list(d) for d in D] if r == 0: F = np.array(bounds(lb, ub, n)) for kk in range(F.shape[1]): x = np.copy(F[:, kk]).reshape(-1, 1) if (np.dot(A, x) - b).min() < 1e-6: Vertex = np.column_stack((Vertex, x)) else: for ii in range(len(C)): index_A = np.copy(list(C[ii])) print index_A v1 = [i for i in range(1, m + 1)] index_A_C = np.setdiff1d(v1, index_A) A1 = np.copy(A[index_A - 1, :]) b1 = np.copy(b[index_A - 1]) for jj in range(len(D)): index_B = np.copy(list(D[jj])) v2 = [i for i in range(1, n + 1)] index_B_C = np.setdiff1d(v2, index_B) F = bounds(lb[index_B - 1], ub[index_B - 1], n - r) A11 = np.copy(A1[:, index_B - 1]) A12 = np.copy(A1[:, index_B_C - 1]) for kk in range(F.shape[1]): A11 = np.copy(A1[:, index_B - 1]) A12 = np.copy(A1[:, index_B_C - 1]) xd = np.linalg.inv(A12) * (b1 - A11 * F[:, kk]) x = np.zeros((2)).reshape(-1, 1) x[index_B - 1] = F[:, kk] x[index_B_C - 1] = xd if r == m or (np.dot(A[index_A_C - 1, :], x) - b[index_A_C - 1]).min() < 0: [y, x1, index] = mindis(x, Vertex) if (x - ub).max() < 1e-6 and ( x - lb).min() > -1e-6 and y > 1e-6: Vertex = np.column_stack((Vertex, x)) # Vertex = np.concatenate((Vertex, x),axis = 1) # print('------------!!!!-----------') # print(y) # print(x1) # print(index) print('--------------!!!!---------') quit() else: m = A.shape[0] n = A.shape[1] from itertools import combinations C = [c for c in combinations(range(1, m), n)] C = [list(c) for c in C] Vertex = list() for ii in range(len(C)): index_A = np.copy(list(C[ii])) v1 = [i for i in range(1, m + 1)] index_A_C = np.setdiff1d(v1, index_A) A1 = np.copy(A[index_A - 1, :]) b1 = np.copy(b[index_A - 1]) A2 = np.copy(A[index_A_C - 1]) b2 = np.copy(b[index_A_C - 1]) x = np.rray(np.linalg.inv(A1) * b1) if (A2 * x - b2).max() < 1e-6: Vertex = np.column_stack((Vertex, x)) return Vertex