Example #1
0
def testold():
	perceptron_h = myp.Perceptron(100)
	perceptron_e = myp.Perceptron(100)
	perceptron_u = myp.Perceptron(100)
	
	trainingProteins = []
	for aastr,targetstr in mydata.trainingData():
		trainingProteins.append(Protein(aastr,targetstr))
	
	testingProteins = []
	for aastr,targetstr in mydata.testData():
		testingProteins.append(Protein(aastr,targetstr))
	
	inputs_targets = []
	for p in trainingProteins:
		inputs_targets.extend(zip(p.inputss,p.targetss))
	
	def train():
		random.shuffle(inputs_targets)
		for inputs,targets in inputs_targets:
			perceptron_h.train(inputs,targets['H'])
			perceptron_e.train(inputs,targets['E'])
			perceptron_u.train(inputs,targets['U'])
	
	def test():
		for protein in testingProteins:
			print (protein.acids)
			print (protein.types)
			sys.stdout.write('\033[91m')
			i = 0
			for inputs,targets in zip(protein.inputss,protein.targetss):
				i = i + 1
				if i < 3:
					continue
				highestOutput = perceptron_h.test(inputs);
				bestPrediction = 'H';
				e = perceptron_e.test(inputs)
				if e > highestOutput:
					highestOutput = e;
					bestPrediction = 'E'
				u = perceptron_u.test(inputs)
				if u > highestOutput:
					highestOutput = u;
					bestPrediction = '_'
				sys.stdout.write(bestPrediction)
			print('\033[0m')
	for i in range(5):
		print i
		train()
	test()
	return 0
Example #2
0
def testpybrain():
	X = list()
	yH = list()
	yE = list()
	yU = list()
	
	perceptron = buildNetwork(100, 5, 3, bias=True)
	
	trainingProteins = []
	for aastr,targetstr in mydata.trainingData():
		trainingProteins.append(Protein(aastr,targetstr))
	
	testingProteins = []
	for aastr,targetstr in mydata.testData():
		testingProteins.append(Protein(aastr,targetstr))
	
	inputs_targets = []
	for p in trainingProteins:
		inputs_targets.extend(zip(p.inputss,p.targetss))
		
	
	ds = SupervisedDataSet(100,3)
	for inputs,targets in inputs_targets:
		ds.addSample(inputs,(targets['H'],targets['E'], targets['U']))
		
	trainer = BackpropTrainer(perceptron, ds)
	
	def test():
		alltargets = StringIO()
		allpredictions = StringIO()
		for protein in testingProteins:
			alltargets.write(protein.types)
			i = 0;
			for inputs in protein.inputss:
				i = i + 1
				if i < 3:
					continue
				highestOutput = 0
				bestPrediction = 'H'
				for prediction,value in zip(perceptron.activate(inputs),('H','E','_')):
					if prediction > highestOutput:
						highestOutput = prediction;
						bestPrediction = value
				allpredictions.write(bestPrediction)
		ch = cc()
		ce = cc()
		cu = cc()
		ch,ce,cu = calculatecoef(alltargets.getvalue(),allpredictions.getvalue())
		
		yH.append(ch.performance())
		yE.append(ce.performance())
		yU.append(cu.performance())
		
		print(ch.performance(),ce.performance(),cu.performance())
		
	for i in range(1,100):
		print i
		X.append(i)
		trainer.train()
		test()
	plt.plot(X,yH,c='red', label = "Alpha Helix")
	plt.plot(X,yE,c='blue', label = "Beta Sheet")
	plt.plot(X,yU,c='green', label = "Coil")
	
	plt.xlabel("Iteration")
	plt.ylabel("Correlation Coefficient")
	plt.legend()
	plt.show()
	
	print(perceptron_h.weights)
	print(perceptron_e.weights)
	print(perceptron_u.weights)
	
	return 0
Example #3
0
def testnew():
	X = list()
	yH = list()
	yE = list()
	yU = list()
	
	perceptron_h = myp.Perceptron(100)
	perceptron_e = myp.Perceptron(100)
	perceptron_u = myp.Perceptron(100)
	
	trainingProteins = []
	for aastr,targetstr in mydata.trainingData():
		trainingProteins.append(Protein(aastr,targetstr))
	
	testingProteins = []
	for aastr,targetstr in mydata.testData():
		testingProteins.append(Protein(aastr,targetstr))
	
	inputs_targets = []
	for p in trainingProteins:
		inputs_targets.extend(zip(p.inputss,p.targetss))
	
	def train():
		random.shuffle(inputs_targets)
		for inputs,targets in inputs_targets:
			perceptron_h.train(inputs,targets['H'])
			perceptron_e.train(inputs,targets['E'])
			perceptron_u.train(inputs,targets['U'])
	
	def test():
		alltargets = StringIO()
		allpredictions = StringIO()
		for protein in testingProteins:
			alltargets.write(protein.types)
			i = 0;
			for inputs,targets in zip(protein.inputss,protein.targetss):
				i = i + 1
				if i < 3:
					continue
				highestOutput = perceptron_h.test(inputs);
				bestPrediction = 'H';
				e = perceptron_e.test(inputs)
				if e > highestOutput:
					highestOutput = e;
					bestPrediction = 'E'
				u = perceptron_u.test(inputs)
				if u > highestOutput:
					highestOutput = u;
					bestPrediction = '_'
				allpredictions.write(bestPrediction)
		ch,ce,cu = calculatecoef(alltargets.getvalue(),allpredictions.getvalue())
		yH.append(ch.performance())
		yE.append(ce.performance())
		yU.append(cu.performance())
		print('')
		
	for i in range(1,100):
		print i
		X.append(i)
		train()
		test()
	plt.plot(X,yH,c='red', label = "Alpha Helix")
	plt.plot(X,yE,c='blue', label = "Beta Sheet")
	plt.plot(X,yU,c='green', label = "Coil")
	
	plt.xlabel("Iteration")
	plt.ylabel("Correlation Coefficient")
	plt.legend()
	plt.show()
	
	print(perceptron_h.weights)
	print(perceptron_e.weights)
	print(perceptron_u.weights)
	
	return 0