{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "training score: 0.522125\n",
      "testing score: 0.511\n"
     ]
    }
   ],
   "source": [
    "# Binary Classification\n",
    "from __future__ import print_function\n",
    "\n",
    "from vowpalwabbit.sklearn_vw import VWClassifier\n",
    "\n",
    "import numpy as np\n",
    "from sklearn import datasets\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# get some data\n",
    "X, y = datasets.make_hastie_10_2(n_samples=10000, random_state=1)\n",
    "X = X.astype(np.float32)\n",
    "\n",
    "# split train and test set\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=256)\n",
    "\n",
    "# build vowpal wabbit model\n",
    "model = VWClassifier()\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "# evaluate\n",
    "print('training score: {}'.format(model.score(X_train, y_train)))\n",
    "print('testing score: {}'.format(model.score(X_test, y_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter search took 53.03 seconds for 20 candidate parameter settings.\n",
      "Model with rank: 1\n",
      "Mean validation score: 0.552 (std: 0.006)\n",
      "Parameters: {'l': 0.01, 'l2': 0.006918202991034834, 'power_t': 0.359507900573786}\n",
      "\n",
      "Model with rank: 2\n",
      "Mean validation score: 0.541 (std: 0.012)\n",
      "Parameters: {'l': 0.1, 'l2': 0.006767667154456677, 'power_t': 0.6706378696181594}\n",
      "\n",
      "Model with rank: 3\n",
      "Mean validation score: 0.541 (std: 0.009)\n",
      "Parameters: {'l': 0.01, 'l2': 0.005318483217500717, 'power_t': 0.4146619399905236}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Parameter Grid Search\n",
    "# http://scikit-learn.org/stable/auto_examples/model_selection/randomized_search.html#example-model-selection-randomized-search-py\n",
    "\n",
    "from operator import itemgetter\n",
    "from time import time\n",
    "from sklearn.grid_search import RandomizedSearchCV\n",
    "from scipy.stats.distributions import uniform\n",
    "\n",
    "# Utility function to report best scores\n",
    "def report(grid_scores, n_top=3):\n",
    "    top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]\n",
    "    for i, score in enumerate(top_scores):\n",
    "        print(\"Model with rank: {0}\".format(i + 1))\n",
    "        print(\"Mean validation score: {0:.3f} (std: {1:.3f})\".format(\n",
    "              score.mean_validation_score,\n",
    "              np.std(score.cv_validation_scores)))\n",
    "        print(\"Parameters: {0}\".format(score.parameters))\n",
    "        print(\"\")\n",
    "        \n",
    "# use a full grid over all parameters\n",
    "np.random.seed(0)\n",
    "n_iter = 20\n",
    "params = {\"l2\": uniform(0.0001, 0.01),\n",
    "          \"l\": [0.01, 0.1, 1.0],\n",
    "          \"power_t\": uniform()}\n",
    "\n",
    "# run search\n",
    "search = RandomizedSearchCV(VWClassifier(), param_distributions=params, n_iter=n_iter)\n",
    "start = time()\n",
    "search.fit(X, y)\n",
    "\n",
    "print(\"Parameter search took %.2f seconds for %d candidate parameter settings.\"\n",
    "      % (time() - start, len(search.grid_scores_)))\n",
    "report(search.grid_scores_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "training score: 0.55475\n",
      "testing score: 0.541\n"
     ]
    }
   ],
   "source": [
    "# evaluate\n",
    "model = VWClassifier(loss_function='logistic', l=0.01, l2=0.1)\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "print('training score: {}'.format(model.score(X_train, y_train)))\n",
    "print('testing score: {}'.format(model.score(X_test, y_test)))\n",
    "\n",
    "# cleanup\n",
    "del model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "intercept: 146.3594207763672\n",
      "predictions: [ 194.89273071   71.24694061  164.77400208  158.98957825  130.94725037\n",
      "  104.06398773   71.54694366  123.39061737  144.63090515  206.62693787]\n",
      "training R2 score: 0.5049730154443529\n"
     ]
    }
   ],
   "source": [
    "# Linear Regression\n",
    "\n",
    "from vowpalwabbit.sklearn_vw import VWRegressor\n",
    "from sklearn import datasets\n",
    "\n",
    "# Load the diabetes dataset\n",
    "diabetes = datasets.load_diabetes()\n",
    "X = diabetes.data\n",
    "y = diabetes.target\n",
    "\n",
    "model = VWRegressor(l=100)\n",
    "model.fit(X, y)\n",
    "\n",
    "print('intercept: {}'.format(model.get_intercept()))\n",
    "print('predictions: {}'.format(model.predict(X[:10])))\n",
    "print('training R2 score: {}'.format(model.score(X, y)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "intercept: 146.3594207763672\n",
      "predictions: [ 194.89273071   71.24694061  164.77400208  158.98957825  130.94725037\n",
      "  104.06398773   71.54694366  123.39061737  144.63090515  206.62693787]\n",
      "training R2 score: 0.5049730154443529\n"
     ]
    }
   ],
   "source": [
    "# Save the model and reload it\n",
    "model.save('test.model')\n",
    "del model\n",
    "model = VWRegressor()\n",
    "model.load('test.model')\n",
    "print('intercept: {}'.format(model.get_intercept()))\n",
    "print('predictions: {}'.format(model.predict(X[:10])))\n",
    "print('training R2 score: {}'.format(model.score(X, y)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
