{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Session 2: 初步理解机器学习 (Get some intuitions about Machine Learning by creating a linear regression model)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What is linear regression? (什么是线性回归)\n",
    "\n",
    "1. 数学概率\n",
    "2. 一种算法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How we supposed to learn it? (我们应该怎样去学习它呢)\n",
    "\n",
    "1. Learn by example\n",
    "2. Learn by experiment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### So the first package you should use is `scikit-learn`\n",
    "\n",
    "> Scikit-learn is a free software machine learning library for the Python programming language.\n",
    "\n",
    "    1. It is easy for use\n",
    "    2. It has a good community"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import your first ML package (导入你的第一个机器学习包)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import linear_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = linear_model.LinearRegression()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit([ [0, 0], [1, 1], [2, 2] ], \n",
    "          [   0,       1,      2   ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3., 55.])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict([ [3, 3], [55, 55] ])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### One more example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([28.98113208, 37.09433962])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import linear_model\n",
    "\n",
    "model = linear_model.LinearRegression()\n",
    "model.fit([[1], [2], [3], [4], [7]],\n",
    "          [ 2,   4,   9,   16,  49])\n",
    "model.predict([[5], [6]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### One more example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8.])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import linear_model\n",
    "\n",
    "model = linear_model.LinearRegression()\n",
    "model.fit([[1,2,3], [2,3,4], [3,10,1], [8,9,4], [7,4,3]],\n",
    "          [   6,       9,       14,       21,      14])\n",
    "model.predict([[5, 1, 2]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Convert sklearn model to keras\n",
    "\n",
    "1. find problem\n",
    "2. serach that problem\n",
    "3. find solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.models import Sequential\n",
    "\n",
    "model = Sequential()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.layers import Dense\n",
    "\n",
    "model.add(Dense(units=2, activation='relu', input_dim=2))\n",
    "model.add(Dense(units=1))\n",
    "\n",
    "model.compile(loss='mean_squared_error',\n",
    "              optimizer='sgd',\n",
    "              metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "5/5 [==============================] - 1s 145ms/step - loss: 28.5372 - acc: 0.0000e+00\n",
      "Epoch 2/10\n",
      "5/5 [==============================] - 0s 2ms/step - loss: 16.0311 - acc: 0.0000e+00\n",
      "Epoch 3/10\n",
      "5/5 [==============================] - 0s 3ms/step - loss: 13.3427 - acc: 0.0000e+00\n",
      "Epoch 4/10\n",
      "5/5 [==============================] - 0s 2ms/step - loss: 11.2660 - acc: 0.0000e+00\n",
      "Epoch 5/10\n",
      "5/5 [==============================] - 0s 3ms/step - loss: 9.5507 - acc: 0.2000 \n",
      "Epoch 6/10\n",
      "5/5 [==============================] - 0s 4ms/step - loss: 8.0622 - acc: 0.2000\n",
      "Epoch 7/10\n",
      "5/5 [==============================] - 0s 2ms/step - loss: 6.8147 - acc: 0.2000\n",
      "Epoch 8/10\n",
      "5/5 [==============================] - 0s 2ms/step - loss: 5.8422 - acc: 0.4000\n",
      "Epoch 9/10\n",
      "5/5 [==============================] - 0s 3ms/step - loss: 5.1508 - acc: 0.4000\n",
      "Epoch 10/10\n",
      "5/5 [==============================] - 0s 4ms/step - loss: 4.6979 - acc: 0.4000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7fb5696d4390>"
      ]
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy\n",
    "\n",
    "x = [[1,1], [2,2], [1,2], [4,3], [1,4]]\n",
    "y = [ [2] ,  [4]  , [3], [7], [5]]\n",
    "\n",
    "x = numpy.array(x)\n",
    "y = numpy.array(y)\n",
    "\n",
    "model.fit(x, y, epochs=10, batch_size=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2.5749485]], dtype=float32)"
      ]
     },
     "execution_count": 223,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inputs = numpy.array([[2,1]])\n",
    "model.predict(inputs)"
   ]
  }
 ],
 "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
