{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": "true"
   },
   "source": [
    "# Table of Contents\n",
    " <p><div class=\"lev1 toc-item\"><a href=\"#DataPreprocessing\" data-toc-modified-id=\"DataPreprocessing-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>DataPreprocessing</a></div><div class=\"lev2 toc-item\"><a href=\"#FunctionTransformer：创造转换器\" data-toc-modified-id=\"FunctionTransformer：创造转换器-11\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>FunctionTransformer：创造转换器</a></div><div class=\"lev2 toc-item\"><a href=\"#StandardScaler：归一化\" data-toc-modified-id=\"StandardScaler：归一化-12\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>StandardScaler：归一化</a></div><div class=\"lev2 toc-item\"><a href=\"#OneHotEncoder：编码\" data-toc-modified-id=\"OneHotEncoder：编码-13\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span>OneHotEncoder：编码</a></div><div class=\"lev2 toc-item\"><a href=\"#pipeline：管道函数\" data-toc-modified-id=\"pipeline：管道函数-14\"><span class=\"toc-item-num\">1.4&nbsp;&nbsp;</span>pipeline：管道函数</a></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DataPreprocessing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FunctionTransformer：创造转换器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "code_folding": [],
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn import pipeline\n",
    "from sklearn.preprocessing import StandardScaler,MinMaxScaler,Imputer,OneHotEncoder,FunctionTransformer\n",
    "\n",
    "def MinMax(X):\n",
    "   return (X -X.min(axis=0))/(X.max(axis=0)-X.min(axis=0))\n",
    "FunctionTransformer(MinMax).fit_transform(X)\n",
    "MinMaxScaler().fit_transform(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## StandardScaler：归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "code_folding": [],
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "StandardScaler().fit_transform(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## OneHotEncoder：编码\n",
    "- n_values：类别数\n",
    "- categorical_features：索引（np.array([1,2])）\n",
    "- sparse：是否稀疏True\n",
    "- handle_unknown：遇到新特征ignore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "OneHotEncoder(n_values='auto',categorical_features='all', sparse=True, handle_unknown='ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## pipeline：管道函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "step = pipeline.make_pipeline(StandardScaler(),OneHotEncoder())\n",
    "list(step.get_params().keys())"
   ]
  },
  {
   "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.6.0"
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "30px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": true,
   "toc_position": {
    "height": "665px",
    "left": "0px",
    "right": "auto",
    "top": "106px",
    "width": "111px"
   },
   "toc_section_display": "none",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
