{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 找出数据中的异常值并做处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import sys\n",
    "import os\n",
    "import matplotlib.pyplot\n",
    "from pandas import DataFrame\n",
    "#sys.path.append(\"../tools/\")\n",
    "workpath = os.getcwd()\n",
    "os.chdir(\"../tools\")\n",
    "from feature_format import featureFormat, targetFeatureSplit\n",
    "os.chdir(workpath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "### read in data dictionary, convert to numpy array\n",
    "data_dict = pickle.load(open(\"../final_project/final_project_dataset.pkl\", \"rb\"),encoding = \"utf8\" )\n",
    "features = [\"salary\", \"bonus\"]\n",
    "\n",
    "data_dict_temp = data_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在去除异常值前的图像："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 8168 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = featureFormat(data_dict, features)\n",
    "for point in data:\n",
    "    salary = point[0]\n",
    "    bonus = point[1]\n",
    "    matplotlib.pyplot.scatter( salary, bonus )\n",
    "\n",
    "matplotlib.pyplot.xlabel(\"salary\")\n",
    "matplotlib.pyplot.ylabel(\"bonus\")\n",
    "matplotlib.pyplot.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "看到最大的一个异常值，要想办法去掉这个数据点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "### your code below\n",
    "\n",
    "def find_outlier(data_dict,obj):\n",
    "    #print(data_dict)\n",
    "    max_bonus = 0\n",
    "    max_name = None\n",
    "    for i in data_dict:\n",
    "        #下一句把数据中的 NaN 设置为 0 防止错误\n",
    "        if data_dict[i][obj] == \"NaN\":data_dict[i][obj] = 0 \n",
    "        if data_dict[i][obj] > max_bonus and data_dict[i][obj]!= 'NaN':\n",
    "            max_bonus = data_dict[i][obj]\n",
    "            max_name = i\n",
    "    return max_name\n",
    "#调用函数删除字典中的异常值\n",
    "data_dict.pop(find_outlier(data_dict_temp,\"bonus\"))\n",
    "\n",
    "data = featureFormat(data_dict_temp, features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17616 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "for point in data:\n",
    "    salary = point[0]\n",
    "    bonus = point[1]\n",
    "    matplotlib.pyplot.scatter( salary, bonus )\n",
    "\n",
    "matplotlib.pyplot.xlabel(\"salary\")\n",
    "matplotlib.pyplot.ylabel(\"bonus\")\n",
    "matplotlib.pyplot.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "遍历一遍字典找出拿奖金和工资超过 5e6 和 1e6的人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LAY KENNETH L has taken $7000000 bonus and $1072321 salary.\n",
      "SKILLING JEFFREY K has taken $5600000 bonus and $1111258 salary.\n"
     ]
    }
   ],
   "source": [
    "for k in data_dict_temp.keys():\n",
    "    if data_dict_temp[k][\"bonus\"] != \"NaN\" and data_dict_temp[k][\"salary\"] != \"NaN\":\n",
    "        if data_dict_temp[k][\"bonus\"] > 5e6 and data_dict_temp[k][\"salary\"] > 1e6:\n",
    "            print(k,\"has taken $%d bonus and $%d salary.\" % (data_dict_temp[k][\"bonus\"],data_dict_temp[k][\"salary\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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.7.3"
  },
  "toc-showmarkdowntxt": true,
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
