def testBroad(): data = Data() geneExp = data.get_gene_exp_matrix() labels = data.get_labels() geneNames = data.get_gene_names() print(labels) print(geneExp[0]) print(geneExp[5]) print(geneExp[17]) print(len(geneExp)) print(len(geneExp[0]))
def ecocfn(code_size=52./38, C=50, linSVC_L1=False, kernel ='linear', selection='chi2',numFeatures=330):#try different code sizes """l1Reg is used only if linSVC=True""" data = Data() gene_exp = data.get_gene_exp_matrix() labels = data.get_labels() if linSVC_L1: clf = svm.LinearSVC(C=C,penalty='l1',dual=False) else: clf = svm.SVC(C=C,kernel=kernel) ecocAlgo = OutputCodeClassifier(clf, code_size=code_size, random_state=0) print(leaveOneOutCrossValid(gene_exp,labels,ecocAlgo,selection=selection,numFeatures=numFeatures))
def svmfn(featureSelectionMethod = 'none',numFeaturesA = '330'): data = Data() gene_exp = data.get_gene_exp_matrix() labels = data.get_labels() names = data.get_gene_names() #USES a 1 vs 1 scheme - how does this work? clf = svm.SVC(C=125.,kernel='linear') #kernel can be poly, rbf, linear, sigmoid clfwrapper = OneVsRestClassifier(clf); #accuracy = leaveOneOutCrossValid(gene_exp,labels,clfwrapper,names=names,selection=featureSelectionMethod,numFeatures=numFeaturesA) #print 'accuracy is' #print accuracy trainingError =trainingSetPerformance(gene_exp,labels,clfwrapper,names=names,selection=featureSelectionMethod,numFeatures=numFeaturesA) #print 'accuracy is' #print accuracy print 'trainingError is' print trainingError
def run(self): d = Data() self.data = d.getData() m = map(self.data) self.edges = m.getEdges() c = car(self.data[0]) f = fuzzy() Y = c.getPosition() while(Y[1] < 43): self.mapping(self.edges) self.draw_car(c.getPosition()) c.sensor(self.edges) F, L, R = c.getDistance() self.front_value = tk.Label(windows, text= str(F)).grid(row=2,column=1, sticky=tk.W+tk.E) self.left_value = tk.Label(windows, text= str(L)).grid(row=3,column=1, sticky=tk.W+tk.E) self.right_value = tk.Label(windows, text= str(R)).grid(row=4,column=1, sticky=tk.W+tk.E) c.setWheel(f.system(F, L, R)) c.update_car_direction() c.update_car_pos() self.result_figure.clear()
def svmfn(featureSelectionMethod = 'none',numFeatures = '330'): data = Data() #data = RaviNormal() gene_exp = data.get_gene_exp_matrix() labels = data.get_labels() names = data.get_gene_names() #clf = svm.LinearSVC(C=125.,penalty="l1",dual=False,class_weight='auto') clf = svm.LinearSVC(C=125,penalty="l1",dual=False) clfwrapper = OneVsRestClassifier(clf); #accuracy = leaveOneOutCrossValid(gene_exp,labels,clfwrapper,names=names,selection=featureSelectionMethod,numFeatures=numFeatures) trainingError =trainingSetPerformance(gene_exp,labels,clfwrapper,names=names,selection=featureSelectionMethod,numFeatures=numFeatures) print 'accuracy is' #print accuracy print 'trainingError is' print trainingError estimators = clfwrapper.estimators_ j = 0 totalGeneListLength = 0 for estimator in estimators: print 'estimator for class' print data.getCellName(j) i = range(0,len(estimator.coef_[0])) b = sorted(zip(estimator.coef_[0], i), reverse=True)[:80] #TODO CHANGE indices = data.indices_of_celltype(j) #print 'indices of this class:' #print indices arraysum = [0.0]*11927 arraysum = numpy.array(arraysum) for i in indices: arraysum = numpy.add(arraysum,gene_exp[i]) arrayavg = numpy.divide(arraysum,len(indices)) k = 0 geneList = [] while k<80 and b[k][0] > 0: #TODO CHANGE avg_expr = arrayavg[b[k][1]] geneStr = str(b[k][0])+',' +names[b[k][1]] + ':' + str(avg_expr) geneList = geneList + [geneStr] k = k+1 j = j+1 print geneList print len(geneList) totalGeneListLength += len(geneList) print 'avg gene signature size:' print totalGeneListLength/35
pat1 = [ [[0, 0, 0], [0]], [[0, 0, 1], [1]], [[0, 1, 0], [1]], [[0, 1, 1], [0]], [[1, 0, 0], [1]], [[1, 0, 1], [0]], [[1, 1, 0], [0]], [[1, 1, 1], [1]], ] ActSig = sigmoid(4) # ActSig.view() d1 = Data() d1.loadList(pat1) d2 = Data() d2.loadList(pat2, 4) n2 = ANN([3, 3, 4], ActSig) # n2.displaySynpWt() arch1 = [3, 4, 1] n1 = ANN(arch1, ActSig) # n1.displaySynpWt() ########################################################## ########## Training With Gradient DescentPortion ######### cost = partial(n1.bpCost, data=d1, regLambda=0.003)
self.X[i] = self.X[i] + self.V[i] Fit.append(self.fit) print('self.fit: ', self.fit) print('self.gbest:', self.gbest) return Fit def get_parameter(self): self.set_parameter(self.gbest) return self.theta, self.weight, self.centers, self.beta # In[2]: if __name__ == "__main__": #----------------------程序執行----------------------- D = Data() x, y = D.getTrainData4d() x = D.normalize(x) y = D.normalize(y) print('lenY', len(y)) print('y:', y) rbf = RBF(3, 10, 1) x_dim, w, centers, beta = rbf.get_parameter() print('w:', w) print('centers:', centers) print('beta:', beta) my_pso = pso(5, 10, x_dim, x, y, w, centers, beta) my_pso.init_Population() fitness = my_pso.iterator() print('fitness: ', fitness) theta, w, centers, beta = my_pso.get_parameter()
def run(self): data4D = open("data/RBFN_params.txt", 'w+') d = Data() #new object of selected data self.data = d.getData() if (self.file_name == 'data/train4dAll.txt'): x, y = d.getTrainData4d() elif (self.file_name == 'data/train6dAll.txt'): x, y = d.getTrainData6d() else: x, y = d.getTrainData4d() #normalize training data x = d.normalize_input(x) y = d.normalize_Y(y) #get input parameters cross_rate = float(self.e4.get()) mutation_rate = float(self.e3.get()) pop_size = int(self.e2.get()) iterations = int(self.e1.get()) if (self.file_name != 'data/RBFN_params.txt'): #training weight --start-- rbf = RBF(3, 50, 1) #new object of RBFN x_dim, w, centers, beta = rbf.get_parameter() ga = genetic(len(x), cross_rate, mutation_rate, pop_size, x_dim, x, y, w, centers, beta) #new object of ga temp = 100 for i in range(0, iterations): fitness = ga.F() best_idx = np.argmin(fitness) if (fitness[best_idx] < temp): temp = fitness[best_idx] best_DNA = ga.get_best_DNA(best_idx) ga.update(best_DNA) #training weight --end-- ga.w, ga.centers, ga.beta = ga.get_data() rbf.set_parameter(ga.w, ga.centers, ga.beta) rbf.train(x, y) z1 = rbf.predict(x, rbf.get_weight()) y = d.inverse_normalize_Y(y) z = d.inverse_normalize_Y(z1) dim, output_W, output_centers, output_beta = rbf.get_parameter() for i in range(0, len(output_W)): #save trained parameters print(str(output_W[i][0]), str(output_centers[i][0]), str(output_centers[i][1]), str(output_centers[i][2]), str(output_beta[i][0]), file=data4D) data4D.close() else: load_w, load_centers, load_beta = d.load_parameters() rbf = RBF(3, len(load_centers), 1) rbf.set_parameter(load_w, load_centers, load_beta) m = map(self.data) #new object of map self.edges = m.getEdges() c = car(self.data[0]) #new object of car Y = c.getPosition() #get the position of car self.mapping(self.edges) while (Y[1] < 43): self.draw_car(c.getPosition()) #draw car on the window c.sensor(self.edges) #calculate the distane of front, left, right F, R, L = c.getDistance() input_x = [] if (self.file_name == 'data/train4dAll.txt'): input_x.append([F, R, L]) elif (self.file_name == 'data/train6dAll.txt'): pos = c.getPosition() input_x.append([pos[0], pos[1], F, R, L]) else: input_x.append([F, R, L]) input_x = np.array(input_x) input_x = d.normalize_input(input_x) wheel = rbf.predict(input_x, rbf.get_weight()) #predict the degree of wheel wheel = d.inverse_normalize_Y(wheel) if wheel < -40: wheel = -40 elif wheel > 40: wheel = 40 c.setWheel(wheel) c.update_car_direction() c.update_car_pos()
from optim import * import matplotlib.pyplot as plt from Activation import sigmoid import numpy as np from loadData import Data from functools import partial from linReg import * # data closer to y = x0 + x1 + 2* x2 data1 = [[[1, 1], [4.2]], [[1, 2], [6.0]], [[2, 1], [5.8]], [[2, 2], [7.2]], [[2, 3], [8.5]], [[3, 3], [9.2]], [[1, 3], [8.1]], [[3, 1], [6.1]], [[4, 1], [7.1]], [[1, 4], [10.3]], [[4, 2], [9.5]], [[2, 4], [10.8]]] d1 = Data() d1.loadList(data1) d1.addBiasRow() print d1.X theta_init = np.matrix(np.zeros((d1.n, 1))) cost = partial(linRegCost,data = d1,theta = theta_init, regLambda = 0.001) J, theta = gradDesc(cost, theta_init, 500, 0.1) print 'model is :', list(np.transpose(theta).flat) # test the trained model with data testLinReg(theta, d1) # test the model (1, 1, 2) testLinReg(np.matrix([[1], [1], [2]]), d1)
def run(self): d = Data() self.data = d.getData() if(self.file_name == 'data/train4dAll.txt'): x, y = d.getTrainData4d() elif(self.file_name == 'data/train6dAll.txt'): x, y = d.getTrainData6d() else: x, y = d.getTrainData4d() print('---------') print('data:',self.data) x = d.normalize_input(x) y = d.normalize_Y(y) print('x:', x) print('y:', y) learn_rate1 = float(self.e4.get()) learn_rate2 = float(self.e3.get()) pop_size = int(self.e2.get()) iterations = int(self.e1.get()) if(self.file_name != 'data/RBFN_params.txt'): data4D = open("data/RBFN_params.txt",'w+') #training weight --start-- rbf = RBF(3, 50, 1) x_dim, w, centers, beta = rbf.get_parameter() tStart = time.time() my_pso = pso(pop_size, iterations, learn_rate1, learn_rate2,x_dim, x, y, w, centers, beta) my_pso.initialize() print('pso!!!!!!!!!!!!!') fitness = my_pso.training() tEnd = time.time() print('It cost', (tEnd - tStart), ' sec.') print('fitness: ', fitness) theta, w, centers, beta = my_pso.get_parameter() rbf.set_parameter(theta, w, centers, beta) rbf.train(x,y) z1 = rbf.predict(x, rbf.get_weight()) print('predict_z', z1) y = d.inverse_normalize_Y(y) z = d.inverse_normalize_Y(z1) print('inverse_z', z) dim, output_W, output_centers, output_beta = rbf.get_parameter() print('output_W:',output_W) for i in range(0, len(output_W)): print(str(output_W[i][0]), str(output_centers[i][0]),str(output_centers[i][1]),str(output_centers[i][2]), str(output_beta[i][0]),file=data4D) data4D.close() else: load_w, load_centers, load_beta = d.load_parameters() rbf = RBF(3, len(load_centers), 1) rbf.set_parameter(0.5, load_w, load_centers, load_beta) print('rbf_W:', rbf.get_weight()) m = map(self.data) self.edges = m.getEdges() c = car(self.data[0]) Y = c.getPosition() self.mapping(self.edges) data4 = open("/Users/chengshu-yu/Documents/train4D.txt",'w+') data6 = open("/Users/chengshu-yu/Documents/train6D.txt",'w+') while(Y[1] < 43): self.draw_car(c.getPosition()) print('car direction:', c.getDirection(), ', wheel degree: ', c.getWheel()) c.sensor(self.edges) F, R, L = c.getDistance() input_x = [] if(self.file_name == 'data/train4dAll.txt'): input_x.append([F, R, L]) elif(self.file_name == 'data/train6dAll.txt'): pos = c.getPosition() input_x.append([pos[0], pos[1], F, R, L]) else: input_x.append([F, R, L]) input_x = np.array(input_x) input_x = d.normalize_input(input_x) print('input_x:', input_x) wheel = rbf.predict(input_x, rbf.get_weight()) print('predict_wheel:', wheel) wheel = d.inverse_normalize_Y(wheel) print('inverse wheel:', wheel) if wheel < -40: wheel = -40 elif wheel > 40: wheel = 40 print('-----------front right left:', input_x, '-----------') print('-----------predict wheel:', wheel, '-----------') c.setWheel(wheel) c.update_car_direction() c.update_car_pos() print(str(F),' ',str(R),' ',str(L),' ',str(c.getDirection()),file=data4) print(str(c.getPosX()),'',str(c.getPosY()),' ',str(F),' ',str(R),' ',str(L),' ',str(c.getDirection()),file=data6) data4.close() data6.close()
from loadData import Data from functools import partial from logReg import * # and function of 3 variables pat1 = [[[0, 0, 0], [0]], [[0, 0, 1], [1]], [[0, 1, 0], [1]], [[0, 1, 1], [1]], [[1, 0, 0], [2]], [[1, 0, 1], [3]], [[1, 1, 0], [3]], [[1, 1, 1], [3]], ] d1 = Data() d1.loadList(pat1, numClasses = 4) #print d1.y act = sigmoid().h # our activation function is simgmoid model, J = trainOneVsAllGD(d1, act,epochs = 5000, lr = 0.25) #print d1.y plt.plot(np.transpose(J)) plt.show() print predictMultiple(model, d1.X, act) # d1.addBiasRow() # theta_init = np.matrix(np.zeros((d1.n, 1)))
def get_parameter(self): return self.input_dim, self.W, self.centers, self.beta def set_parameter(self, theta, w, centers, beta): self.theta = theta self.W = w self.centers = centers self.beta = beta def get_weight(self): return self.W if __name__ == "__main__": data4D = open("/Users/chengshu-yu/Documents/train/train4D.txt", 'w+') D = Data() n = 100 #x = np.linspace(-1, 1, n).reshape(n, 1) #y = np.sin(3 * (x+0.5)**3 - 1) #training x, y = D.getTrainData() x = D.normalize(x) y = D.normalize(y) #print('x:', x) #print('lenY', len(y)) #print('y:', y) rbf = RBF(3, 200, 1) rbf.train(x, y) z = rbf.predict(x) y = D.inverse_normalize(y) z = D.inverse_normalize(z)