import numpy as np from keras.layers.core import Dense, Dropout, Activation, Flatten from keras.layers import Convolution1D, MaxPooling1D from keras.utils import np_utils from keras.models import Sequential from sklearn.metrics import mean_squared_error # Preprocess the data noemalize the data training_data = np.genfromtxt('employment_data.csv', delimiter=',') train_data1 = [row[0] for row in training_data] td = train_data1 / max(train_data1) X = np.array(td) #td is of shape(924,) temp = np.array([td[0:12]]) i = 1 while i < (len(td)-11): t_data = np.array([td[i:i+12]]) temp = np.vstack((temp, t_data)) i = i+1 print temp.shape x_train = temp[:900] x_test = temp[901:] # Divide the data in test dataset and train dataset train_data2 = np.array([row[1] for row in training_data]) train_data3 = train_data2[0:924] y_train = train_data3[:900] y_test = train_data3[901:913] print "This is x_train shape : " print x_train.shape print "This is y_train shape : " print y_train.shape print "This is x_test shape : " print x_test.shape print "This is y_test shape : " print y_test.shape # Reshape the data according to neural network x_train = x_train.reshape((900,12,1)) x_test = x_test.reshape((12,12,1)) # one hot encode outputs categoricaly y_train = np_utils.to_categorical(y_train) y_test = np_utils.to_categorical(y_test) num_classes = y_test.shape[1] #build the convolutional neural network print 'Building model...' model = Sequential() model.add(Convolution1D(input_dim = 1, nb_filter= 18, filter_length= 3, subsample_length = 1, border_mode='valid', activation='relu', input_length=12)) print model.input_shape model.add(MaxPooling1D(pool_length=2)) print model.output_shape model.add(Convolution1D(input_dim = 1, nb_filter= 6, filter_length= 2, subsample_length = 1, border_mode='valid', activation='relu', input_length=12)) print model.output_shape model.add(Dropout(0.1)) print model.output_shape model.add(Flatten()) print model.output_shape model.add(Dense(13, activation='softmax')) print model.output_shape model.compile(loss='mse', optimizer='adam', metrics=['accuracy']) model.fit(x_train, y_train, nb_epoch=300, batch_size = 12, verbose=2, validation_split=0.1) score = model.evaluate(x_test, y_test, batch_size=12) print score predicted = model.predict_classes(x_test) print predicted