# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

import numpy as np
import random
from math import floor
from keras.models import Sequential
from keras.layers.core import Dense, Activation
import csv
import copy
from math import sqrt

with open ('train.csv') as source:
    spam = csv.reader(source)
    trainset = list(spam)
tags =lst2=copy.deepcopy(trainset[0]) 
del trainset[0]


def getData(splitper):
    random.shuffle(trainset)
    splitper = int(floor(splitper * len(trainset)) + 1)
    #get The Informations
    traininglst = trainset[splitper:]
    testinglst = trainset[:splitper]
    train_X=[]
    train_Y=[]
    test_X=[]
    test_Y=[]
    for element in traininglst:
        train_X.append(element[0:16])
        train_Y.append(element[16])
    for element in testinglst:
        test_X.append(element[0:16])
        test_Y.append(element[16])
    
    
    return (np.array(train_X),np.array(train_Y),np.array(test_X),np.array(test_Y))


(train_X,train_Y,test_X,test_Y)= getData(0.2)

print 'trainx:{},trainy:{},testx:{},testy{}'.format(train_X.shape,train_Y.shape,test_X.shape,test_Y.shape)
batchsize=1

print 'Building model'
model = Sequential()
model.add(Dense(10, input_dim=16))
model.add(Activation('tanh'))
model.add(Dense(1, input_shape=(16,)))
model.compile(loss='mse', optimizer='rmsprop')
print 'start fitting'
model.fit(train_X, train_Y, nb_epoch=30, batch_size=batchsize,validation_split=0.05, show_accuracy=True)
score = model.evaluate(test_X, test_Y, batch_size=batchsize,show_accuracy=True)

print sqrt(score[0])

with open ('test.csv') as source:
    spam = csv.reader(source)
    testset = list(spam)
testtags =lst2=copy.deepcopy(trainset[0]) 
del testset[0]

topredict_X=[]
for i in testset:
    topredict_X.append( i[1:])

topredict_X= np.array(topredict_X)

prediction_Y= model.predict(topredict_X,batch_size=16,verbose=0)

with open('submit 004.csv','w') as output:
    output.write('ID,target\n')
    for i in range(len(prediction_Y)):
        output.write('{},{}'.format(i+1,prediction_Y[i][0])+'\n')
    



StarcoderdataJupyterScriptsDedupFiltered