# Third-party libraries import numpy as np import random import network2 as nn def vectorized_result(j, nClasses): e = np.zeros((nClasses, 1)) e[j] = 1.0 return e def load_3fgl_data(): """Return a tuple containing ``(training_data, validation_data, test_data)``. ``training_data`` is a list containing 2-tuples ``(x, y)``. ``x`` is a numpy.ndarray containing the observables. ``y`` is a 2-dimensional numpy.ndarray representing the unit vector corresponding to the correct type of object for ``x``. ``validation_data`` and ``test_data`` are lists containing 2-tuples ``(x, y)``. In each case, ``x`` is a numpy.ndarry containing the observables, and ``y`` is the corresponding classification, i.e., the class lable integers. """ D3 = np.loadtxt("../data/3fgl_full.dat", dtype='string') labelIndex = D3.shape[1]-1 # 29 tp = D3[:, labelIndex] D3[np.where( (tp == 'bll') | (tp == 'BLL') | (tp == 'bcu') | (tp == 'BCU') | (tp == 'fsrq') | (tp == 'FSRQ')), labelIndex] = 0 D3[np.where((tp == 'psr') | (tp == 'PSR')), labelIndex] = 1 D_all = D3[np.where((D3[:, labelIndex] == '0') | (D3[:, labelIndex] == '1'))][:,1:] X=D_all[:,:-1].astype('float') #observables Y=D_all[:,-1:].astype('int') #labels nObservables = X[0].shape[0] # convert to format suitable for network X = [np.reshape(x,(nObservables, 1)) for x in X] data=zip(X, Y) l=len(data) training_data = [(x, vectorized_result(y, 2)) for x, y in data[0:l/2]] validation_data = data[l/2 : 3*l/4] test_data = data[3 * l / 4:] return (training_data, validation_data, test_data) def main(): np.random.seed(0) # avoid random fluctuations while adjustimg meta parameters training_data, validation_data, test_data = load_3fgl_data() PSRtrain=[(x,y) for x,y in training_data if y[0]==0] BLAZARtrain=[(x, y) for x, y in training_data if y[0] == 1] PSReval=[(x,y) for x,y in validation_data if y==1] BLAZAReval=[(x, y) for x, y in validation_data if y == 0] PSRtest=[(x,y) for x,y in test_data if y==1] BLAZARtest=[(x, y) for x, y in test_data if y == 0] random.shuffle(training_data) nObservables = len(training_data[0][0]) nClasses = len(training_data[0][1]) net = nn.Network([nObservables, 5, nClasses]) net.SGD(training_data,epochs=20,mini_batch_size=10,eta=0.01,lmbda=0.0,evaluation_data=validation_data, monitor_evaluation_accuracy=True,monitor_evaluation_cost=False,monitor_training_accuracy=True,monitor_training_cost=True) print 'blazar eval accuracy: ' + str(float(net.accuracy(BLAZAReval)) / len(BLAZAReval)) print 'PSR eval accuracy: ' + str(float(net.accuracy(PSReval)) / len(PSReval)) print 'blazar test accuracy: ' + str(float(net.accuracy(BLAZARtest)) / len(BLAZARtest)) print 'PSR test accuracy: ' + str(float(net.accuracy(PSRtest)) / len(PSRtest)) main()