{"cells":[{"metadata":{"trusted":false,"_uuid":"771a50a8892f7478b7d61287d3f8071616f6aef1"},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.callbacks import TensorBoard\nfrom time import time\nfrom sklearn.metrics import accuracy_score\nimport random, operator","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"4f94092442ff12b29b7df611a186dc8e6f4484b7"},"cell_type":"code","source":"#Read train and test csv\ntrain = pd.read_csv(\"../input/train/train.csv\") \ntest = pd.read_csv(\"../input/test/test.csv\") ","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"ad97f1d24af0abaea83f68401a0380fb541f4570"},"cell_type":"code","source":"x=(train[['Type', 'Age', 'Breed1', 'Breed2','Gender','Color1','Color2','Color3','MaturitySize','FurLength','Vaccinated','Dewormed','Sterilized','Health','Quantity','Fee']])\ny=(train[['AdoptionSpeed']])\ntestx=(test[['Type', 'Age', 'Breed1', 'Breed2','Gender','Color1','Color2','Color3','MaturitySize','FurLength','Vaccinated','Dewormed','Sterilized','Health','Quantity','Fee']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"7cde9cfca647dbd141118e62a258b18a21a08d32"},"cell_type":"code","source":"train_x,dev_x=x[200:],x[:200] \ntrain_y,dev_y=y[200:],y[:200]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"bf4268ed71476a56e952d62d1a76013fc039ebb4"},"cell_type":"code","source":"#Pandas to numpy\ntrain_x=train_x.values\ntrain_y=train_y.values\n\nflat_y = [item for sublist in dev_y.astype(float).values for item in sublist]\n#dev_x=dev_x.values\n#dev_y=dev_y.values\n#Transform targets [0,3,...,4] to [[1,0,0,0,0],[0,0,0,1,0],...,[0,0,0,0,1]]\ntrain_y = tf.keras.utils.to_categorical(train_y, 5)\ndev_y = tf.keras.utils.to_categorical(dev_y, 5)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"e1d42723846bd13d20d7287a35725374d4a5cca9"},"cell_type":"code","source":"#Create the model\nmodel = tf.keras.Sequential()\n\nmodel.add(tf.keras.layers.Dense(5, input_shape=(16,),activation='softmax', use_bias=False)) #Dense Layer with softmax activation so it can predict one of the 5 Labels\n\nmodel.compile(loss=tf.keras.losses.categorical_crossentropy,\n              optimizer=tf.keras.optimizers.Adadelta(),\n              metrics=['accuracy'])\nprint(model.summary())","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"4d97be8a151cc1a31df472fbe946aa2c3fcccf40"},"cell_type":"code","source":"#Train the model\ntensorboard = TensorBoard(log_dir=\"logs/{}\".format(time()))\nbestEpoch=tf.keras.callbacks.ModelCheckpoint(\"logs/checkpoint\", monitor='val_acc', verbose=0, save_best_only=True, save_weights_only=False, mode='auto', period=1)\n\nmodel.fit(train_x, train_y,\n          batch_size=1000,\n          epochs=2000,\n          verbose=1,\n          validation_data=(dev_x, dev_y),\n          callbacks=[tensorboard,bestEpoch])","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"bf5e00d560823b162a1f385f88221c025c14adb2"},"cell_type":"code","source":"model=tf.keras.models.load_model(\n    \"logs/checkpoint\",\n    custom_objects=None,\n    compile=True\n)\n\npred=model.predict(dev_x)\nprint(pred.argmax(axis=1)[:5])\nprint(\"Dev accuracy:\",accuracy_score(pred.argmax(axis=1),flat_y))","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"33df7cfdb06dbc22dd68760b90b3c55c0ae5591f"},"cell_type":"code","source":"class Weight:\n    def __init__(self, w):\n        self.w = w\n    \n    \n    def __repr__(self):\n        return \"(\" + str(self.w) + \")\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"4eff0cae0cd08bcd6ac4131ab27e7cba8140937a"},"cell_type":"code","source":"class Fitness:\n    def __init__(self, weights):\n        self.weights = weights   \n    \n    def routeFitness(self):\n        model.set_weights(toNumpyWeights(self.weights))\n        pred=model.predict(dev_x)\n        self.fitness=-(1/accuracy_score(pred.argmax(axis=1),flat_y))\n        return self.fitness","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"8c999900c9b403bb9ddebaae71c86645e31c0889"},"cell_type":"code","source":"def geneticAlgorithm(population, popSize, eliteSize, mutationRate, generations):\n    pop = initialPopulation(popSize, population)\n    \n    print(\"Initial distance: \" + str(1 / rankRoutes(pop)[0][1]))\n    \n    for i in range(0, generations):\n        print(\"Iteration \", i+1,\" of \",generations)\n        pop = nextGeneration(pop, eliteSize, mutationRate)\n    \n    print(\"Final distance: \" + str(1 / rankRoutes(pop)[0][1]))\n    bestRouteIndex = rankRoutes(pop)[0][0]\n    bestRoute = pop[bestRouteIndex]\n    return bestRoute","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"c0b87e4f43a92cc86c8daa991a592d736f5d7f97"},"cell_type":"code","source":"def initialPopulation(popSize, weights):\n    population = []\n    population.append(weights)\n    for i in range(0, popSize-1):\n        print(\"\\rCreating weights \", i+2,\" of \",popSize, end='', flush=True)\n        population.append(createWeights(weights))\n    print(\"\\n\")\n    return population","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"b5d78c81d21ad38466f5294d9935a516cc45c3a3"},"cell_type":"code","source":"def createWeights(weights):\n    newWeights = random.sample(weights, len(weights))\n    return newWeights","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"3538f6fcdc8d0f725b7f0923990b1ec8f5fa8946"},"cell_type":"code","source":"def rankRoutes(population):\n    fitnessResults = {}\n    for i in range(0,len(population)):\n        \n        fitnessResults[i] = Fitness(population[i]).routeFitness()\n\n    return sorted(fitnessResults.items(), key = operator.itemgetter(1), reverse = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"7a2aa87dcf9aea1ecf7e8d1ad3543070cf898fd8"},"cell_type":"code","source":"def nextGeneration(currentGen, eliteSize, mutationRate):\n    popRanked = rankRoutes(currentGen)\n    selectionResults = selection(popRanked, eliteSize)\n    matingpool = matingPool(currentGen, selectionResults)\n    children = breedPopulation(matingpool, eliteSize)\n    print(\"Generation score: \",1/Fitness(matingpool[0]).routeFitness())\n    nextGeneration = mutatePopulation(eliteSize, children, mutationRate)\n    return nextGeneration","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"1497f0bbe6bb6ada858d422b7bb66ab5c1e00336"},"cell_type":"code","source":"def selection(popRanked, eliteSize):\n    selectionResults = []\n    df = pd.DataFrame(np.array(popRanked), columns=[\"Index\",\"Fitness\"])\n    df['cum_sum'] = df.Fitness.cumsum()\n    df['cum_perc'] = 100*df.cum_sum/df.Fitness.sum()\n    \n    for i in range(0, eliteSize):\n        selectionResults.append(popRanked[i][0])\n    for i in range(0, len(popRanked) - eliteSize):\n        pick = 100*random.random()\n        for i in range(0, len(popRanked)):\n            if pick <= df.iat[i,3]:\n                selectionResults.append(popRanked[i][0])\n                break\n    return selectionResults","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"8bc7848cfd5835333d7ad8a0011c663fe39cf75c"},"cell_type":"code","source":"def matingPool(population, selectionResults):\n    matingpool = []\n    for i in range(0, len(selectionResults)):\n        index = selectionResults[i]\n        matingpool.append(population[index])\n    return matingpool","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"85e143f80f8cd7cb651b4fd4b4581d50fe8e353c"},"cell_type":"code","source":"def breed(parent1, parent2):\n    child = []\n    childP1 = []\n    childP2 = []\n    \n    geneA = int(random.random() * len(parent1))\n    geneB = int(random.random() * len(parent1))\n    \n    startGene = min(geneA, geneB)\n    endGene = max(geneA, geneB)\n\n\n    for i in range(startGene, endGene):\n        childP1.append(parent1[i])\n    s = set(childP1)   \n    childP2 = [item for item in parent2 if item not in s]\n\n    child = childP1 + childP2\n    return child","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"7bb8f5d968c67b2d26fd6e54ecad9c9d424177d3"},"cell_type":"code","source":"\ndef breedPopulation(matingpool, eliteSize):\n    children = []\n    length = len(matingpool) - eliteSize\n    pool = random.sample(matingpool, len(matingpool))\n\n    for i in range(0,eliteSize):\n        children.append(matingpool[i])\n    \n    for i in range(0, length-eliteSize):\n        child = breed(pool[i], pool[len(matingpool)-i-1])\n        children.append(child)\n    return children","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"70a6256c29c18efd91f7ea54fc8a94f3cd9fcbf8"},"cell_type":"code","source":"def mutate(individual, mutationRate):\n    for swapped in range(len(individual)):\n        if (random.random() < mutationRate): \n            swapWith = int(random.random() * len(individual))\n            individual[swapWith] = Weight(random.uniform(-1, 1))\n        elif(random.random() < mutationRate):\n            swapWith = int(random.random() * len(individual))\n            \n            city1 = individual[swapped]\n            city2 = individual[swapWith]\n            \n            individual[swapped] = Weight((city2.w+city1.w)/2)\n            individual[swapWith] = Weight((city2.w+city1.w)/2)\n        \n    return individual","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"0e7683d65d0cfbc122d25b813cf71ccababc04c3"},"cell_type":"code","source":"def mutatePopulation(eliteSize, population, mutationRate):\n    mutatedPop = []\n    for i in range(0,eliteSize):\n        mutatedPop.append(population[i].copy())\n\n    for ind in range(0, len(population)):\n        mutatedInd = mutate(population[ind], mutationRate)\n        mutatedPop.append(mutatedInd)\n    return mutatedPop","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"f1cb58d576c79833f9827e7d82464b5011ef6fcd"},"cell_type":"code","source":"def toNumpyWeights(w):\n    curr=0\n    wNumpy=[]\n    for i in range(wSize):\n        currW=[w[curr].w,w[curr+1].w,w[curr+2].w,w[curr+3].w,w[curr+4].w]\n        curr+=5\n        wNumpy.append(currW)\n    toReturn = np.array([wNumpy],dtype=np.float32)\n    #print(toReturn)\n    return toReturn","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"7363011f2f192e7ae39c6e7ef12ba77764480559"},"cell_type":"code","source":"\ndef wtoww():\n    w=model.get_weights()\n    #print(w)\n    w=list(w)[0]\n    ww=[]\n    wSize=len(w)\n    for i in w:\n        for ii in i:\n            ww.append(Weight(ii))\n    return ww","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"ba72c14c3917ff4654218a3a5429466cae2afa1d"},"cell_type":"code","source":"def evolve(ww):\n    w=geneticAlgorithm(population=ww, popSize=100, eliteSize=2, mutationRate=0.01, generations=20)\n    w = toNumpyWeights(w)\n    return w","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"0f0c5316d77404a3a107a8b4e89c9dbe85332f4a"},"cell_type":"code","source":"def score_model(w):  \n    model.set_weights(w)\n    pred=model.predict(dev_x)\n    print(pred.argmax(axis=1)[:5])\n    print(\"Dev accuracy:\",accuracy_score(pred.argmax(axis=1),flat_y))","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"5a927397c47984a6932b9dfec6610979d0fd0e21"},"cell_type":"code","source":"w=model.get_weights()\n#print(w)\nw=list(w)[0]\nww=[]\nwSize=len(w)\nfor i in w:\n    for ii in i:\n        ww.append(Weight(ii))\n\nfor i in range(10):\n    print(str(i+1)+\" of 1000\")\n    ww=wtoww()\n    w=evolve(ww)\n    score_model(w)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"a3eb00ee250ae08ecaa3f882b773de924b89418b"},"cell_type":"code","source":"pred=model.predict(dev_x)\nprint(pred.argmax(axis=1)[:5])\nprint(\"Dev accuracy:\",accuracy_score(pred.argmax(axis=1),flat_y))\nprediction=model.predict(testx)\nyy=(test[['PetID']])\n\n#Save results\nfinal=pd.DataFrame(np.array(prediction.argmax(axis=1)),columns=['AdoptionSpeed'])\nfinal['PetID']=yy\nfinal=final[['PetID','AdoptionSpeed']]\nfinal.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false,"_uuid":"93986715176b42ae82dc101f8aecf3500f190040"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}