for_investigation.ipynb 4.5 KB
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "from keras.models import Model\n",
    "from keras.layers import Input, Dense, Dropout, Activation, BatchNormalization\n",
    "from keras.optimizers import SGD, Adam\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "filename = 'test_set.csv'\n",
    "raw_data = open(filename, 'rt')\n",
    "test_data = np.loadtxt(raw_data, delimiter= '\\t')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "number_of_features = 1126\n",
    "test_set = test_data[:,0:1126]\n",
    "test_labels = test_data[:,1126] #last column consists of labels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Neural network configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "inputs = Input(shape=(number_of_features,))\n",
    "output_from_1st_layer = Dense(1000, activation='relu')(inputs)\n",
    "output_from_1st_layer = Dropout(0.5)(output_from_1st_layer)\n",
    "output_from_1st_layer = BatchNormalization()(output_from_1st_layer)\n",
    "output_from_2nd_layer = Dense(500, activation='relu')(output_from_1st_layer)\n",
    "output_from_2nd_layer = Dropout(0.5)(output_from_2nd_layer)\n",
    "output_from_2nd_layer = BatchNormalization()(output_from_2nd_layer)\n",
    "output = Dense(1, activation='sigmoid')(output_from_2nd_layer)\n",
    "\n",
    "model = Model(inputs, output)\n",
    "model.compile(optimizer='Adam',loss='binary_crossentropy',metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's load weights learnt earlier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.load_weights(\"weights_2017_05_10.h5\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Evaluation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, calculate predictions for test set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "predictions = model.predict(test_set)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can calculate basic metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy:0.7316259444607988\n",
      "Precision: 0.7378337531486147\n",
      "Recall: 0.7185752134236091\n",
      "F1: 0.7280771525154107\n"
     ]
    }
   ],
   "source": [
    "true_positives = 0.0\n",
    "    false_positives = 0.0\n",
    "    true_negatives = 0.0\n",
    "    false_negatives = 0.0\n",
    "\n",
    "    for i in range(len(test_set)):\n",
    "        if (predictions[i]<0.5 and test_labels[i]==0): true_negatives += 1     \n",
    "        if (predictions[i]<0.5 and test_labels[i]==1): false_negatives += 1\n",
    "        if (predictions[i]>=0.5 and test_labels[i]==1): true_positives += 1\n",
    "        if (predictions[i]>=0.5 and test_labels[i]==0): false_positives += 1  \n",
    "    \n",
    "    accuracy = (true_positives+true_negatives)/len(test_set)\n",
    "    precision = true_positives/(true_positives+false_positives)\n",
    "    recall = true_positives/(true_positives+false_negatives)\n",
    "    f1 = 2*(precision*recall)/(precision+recall)\n",
    "\n",
    "    print ('Accuracy:' + repr(accuracy))\n",
    "    print ('Precision: ' + repr(precision))\n",
    "    print ('Recall: ' + repr(recall))\n",
    "    print ('F1: ' + repr(f1))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}