{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n========================================\nExample: skhubness in Pipelines\n========================================\n\nEstimators from scikit-hubness can - of course - be used in a scikit-learn ``Pipeline``.\nIn this example, we select the best hubness reduction method and several other\nhyperparameters in grid search w.r.t. to classification performance.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.datasets import make_classification\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import StratifiedKFold, train_test_split, GridSearchCV\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.preprocessing import StandardScaler\n\nfrom skhubness.neighbors import KNeighborsClassifier\n\n# Not so high-dimensional data\nX, y = make_classification(n_samples=1_000,\n                           n_features=50,\n                           n_informative=20,\n                           n_classes=2,\n                           random_state=3453)\n\nX, X_test, y, y_test = train_test_split(X, y,\n                                        test_size=100,\n                                        stratify=y,\n                                        shuffle=True,\n                                        random_state=124)\n\n# Pipeline of standardization, dimensionality reduction, and kNN classification\npipe = Pipeline([('scale', StandardScaler(with_mean=True, with_std=True)),\n                 ('pca', PCA(n_components=20, random_state=1213)),\n                 ('knn', KNeighborsClassifier(n_neighbors=10, algorithm='lsh', hubness='mp'))])\n\n# Exhaustive search for best algorithms and hyperparameters\nparam_grid = {'pca__n_components': [10, 20, 30],\n              'knn__n_neighbors': [5, 10, 20],\n              'knn__algorithm': ['auto', 'hnsw', 'lsh', 'falconn_lsh', 'nng', 'rptree'],\n              'knn__hubness': [None, 'mp', 'ls', 'dsl']}\ncv = StratifiedKFold(n_splits=5, shuffle=True, random_state=1354)\nsearch = GridSearchCV(pipe, param_grid, n_jobs=5, cv=cv, verbose=1)\nsearch.fit(X, y)\n\n# Performance on hold-out data\nacc = search.score(y_test, y_test)\nprint(acc)\n# 0.79\n\nprint(search.best_params_)\n# {'knn__algorithm': 'auto',\n#  'knn__hubness': 'dsl',\n#  'knn__n_neighbors': 20,\n#  'pca__n_components': 30}"
      ]
    }
  ],
  "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.7.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}