{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Chapter 09. 선형대수와 신경망 기초"
      ],
      "metadata": {
        "id": "btQCSjWHu9ZP"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Section 01. 벡터\n",
        "##1. 스칼라와 벡터\n"
      ],
      "metadata": {
        "id": "mcjCjxCCnA7D"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "suIGIpSLnAO-"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "a=[1, 2]"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "a1=np.array([1, 2])\n",
        "a1"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GujUWRwwnOqI",
        "outputId": "809b7f7d-dc31-4808-dfdc-15fbff15be9b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([1, 2])"
            ]
          },
          "metadata": {},
          "execution_count": 2
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 벡터의 합과 차"
      ],
      "metadata": {
        "id": "zOhjMsNVnS_k"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# 벡터의 합\n",
        "a=np.array([1, 2])\n",
        "b=np.array([3, 4])\n",
        "c=a+b\n",
        "c"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_4mHDm2LnUrB",
        "outputId": "1b63d9bb-d08b-4458-d3c2-3389cd2c24ac"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([4, 6])"
            ]
          },
          "metadata": {},
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# 벡터의 차\n",
        "a=np.array([1, 2])\n",
        "b=np.array([3, 4])\n",
        "c=a-b\n",
        "c"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2n58jfIMnWfP",
        "outputId": "9220c0bc-72b6-4e29-ad96-1be5fdade59f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([-2, -2])"
            ]
          },
          "metadata": {},
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 리스트 합과 벡터 합"
      ],
      "metadata": {
        "id": "UJCmaYH_nZKN"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# 리스트의 합\n",
        "a=[1, 2]\n",
        "b=[3, 4]\n",
        "a+b"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "iot5_5QcnbLH",
        "outputId": "4527fa6b-a3e4-4ac4-d5c6-0f785a755367"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[1, 2, 3, 4]"
            ]
          },
          "metadata": {},
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# 벡터의 합\n",
        "a1=np.array([1, 2])\n",
        "b1=np.array([3, 4])\n",
        "a1+b1"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "oZ8j9izsncvt",
        "outputId": "58cde5f7-cdba-4d93-c2f2-ead958612120"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([4, 6])"
            ]
          },
          "metadata": {},
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##2. 벡터의 연산\n",
        "### 벡터의 합"
      ],
      "metadata": {
        "id": "qh_4srYkngNr"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "####❶ 리스트 원소끼리 각각 더하기"
      ],
      "metadata": {
        "id": "0rxLnzkDnp93"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "Hong1=[80, 90]\n",
        "Jung1=[70, 80]\n",
        "Score=[]\n",
        "for i in range(len(Hong1)): # len(Hong1)=2\n",
        " Score.append(Hong1[i]+Jung1[i])\n",
        "print(Score)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_RQtrQs6nmOk",
        "outputId": "1a42154d-c69f-4473-b2fb-29cd28787bba"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[150, 170]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "####❷ 넘파이를 이용해 더하기"
      ],
      "metadata": {
        "id": "QupdSoQznv4B"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "npHong1=np.array(Hong1)\n",
        "npJung1=np.array(Jung1)\n",
        "npHong1+npJung1"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "N4_8BsgLnx1p",
        "outputId": "4b4edbcb-3fbf-4141-d43c-9900d2229d0c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([150, 170])"
            ]
          },
          "metadata": {},
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "###벡터의 차"
      ],
      "metadata": {
        "id": "B10xtknYn0HO"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def vector_sub(*args):# 각 성분끼리 더하기\n",
        "  return [vi - wi for vi, wi in zip(*args)]\n",
        "sub=vector_sub(Hong1, Jung1)\n",
        "sub"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TacuPvzFn2Ac",
        "outputId": "c51b9120-b2ed-4945-f138-ac4e99e311e9"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[10, 10]"
            ]
          },
          "metadata": {},
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "npHong1=np.array(Hong1)\n",
        "npJung1=np.array(Jung1)\n",
        "npHong1-npJung1"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TKZAwCWYn4no",
        "outputId": "5669247b-3e44-4c6f-c06b-057693d61195"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([10, 10])"
            ]
          },
          "metadata": {},
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##3. 벡터의 선형조합\n",
        "### 스칼라와 벡터의 곱"
      ],
      "metadata": {
        "id": "2JLAUH-en7l5"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "####❶ 넘파이 배열 사용"
      ],
      "metadata": {
        "id": "Wa4gD8nMoAmj"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x1=np.array([1, 2])\n",
        "x2=np.array([3, 4])\n",
        "x3=0.5*x1 + 0.5*x2\n",
        "x3"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MBM2TC3hoCk5",
        "outputId": "b4e18dcb-d04e-4b73-c94e-bfd55e4c3661"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([2., 3.])"
            ]
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "####❷ map 함수 사용"
      ],
      "metadata": {
        "id": "nZSQ-sy-oEhG"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "A=[1, 2, 3, 4, 5]\n",
        "scalar_multiply=list(map(lambda x: x*2, A))\n",
        "print(scalar_multiply)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TfDv9hw6oGbt",
        "outputId": "a8901bc8-4bcb-4abe-82e1-757523a9d39f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[2, 4, 6, 8, 10]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "###선형조합의 예\n",
        "####❶ sum 함수, zip 함수 이용"
      ],
      "metadata": {
        "id": "mbE5lStBoI7f"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "Hong1=[80, 90]\n",
        "Jung1=[70, 80]\n",
        "a=0.5\n",
        "avg=[a*sum(i) for i in zip(Hong1, Jung1)]\n",
        "avg"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ttE9jC2GoLQR",
        "outputId": "31aa81ee-fbac-42f7-bc98-09ef9cd93255"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[75.0, 85.0]"
            ]
          },
          "metadata": {},
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "####❷ 넘파이를 이용한 스칼라와 벡터의 곱"
      ],
      "metadata": {
        "id": "pLbOku7LoQxv"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "npHong1=np.array(Hong1)\n",
        "npJung1=np.array(Jung1)\n",
        "np_S= npHong1+npJung1\n",
        "a=0.5\n",
        "a*np_S"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OQMhQCRHoTCN",
        "outputId": "c3cf9b03-80c0-44d5-c2bd-74299a5d002e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([75., 85.])"
            ]
          },
          "metadata": {},
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##4. 벡터의 내적\n",
        "###함수를 만들어 벡터의 내적 구하기\n"
      ],
      "metadata": {
        "id": "LyGLJZbmoVMI"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def dot(v, w):\n",
        " return sum(v_i * w_i for v_i, w_i in zip(v, w))\n",
        "A=[4, 3]\n",
        "B=[6, 0]\n",
        "dot(A, B)\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5t0GYjiiob2j",
        "outputId": "d362eb59-3039-4acc-a1a2-be029d2c7916"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "24"
            ]
          },
          "metadata": {},
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x1=[1, 2, 3]\n",
        "y1=[4, 5, 6]\n",
        "dot(x1, y1)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "t8GmClgVod-x",
        "outputId": "a4f70d4d-6ecc-414a-9411-63ea8e64f801"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "32"
            ]
          },
          "metadata": {},
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "###넘파이로 벡터의 내적 구하기"
      ],
      "metadata": {
        "id": "Hu6FC0KWofuP"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "npx1=np.array([1, 2, 3])\n",
        "npy1=np.array([4, 5, 6])\n",
        "sum(npx1*npy1)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "G3nnHRrEohwi",
        "outputId": "db50c181-877b-462b-8b34-60b1d8453bca"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "32"
            ]
          },
          "metadata": {},
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a=np.array([1, 2, 3, 4, 5])\n",
        "b=np.array([2, 4, 6, 8, 10])\n",
        "print(a*b)\n",
        "print(sum(a*b))\n",
        "print(np.sum(a*b))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LYUBYY0oojaD",
        "outputId": "b9901386-629d-49df-8682-fd9fa88e7cb8"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[ 2  8 18 32 50]\n",
            "110\n",
            "110\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "###넘파이 브로드캐스팅으로 벡터의 내적 구하기"
      ],
      "metadata": {
        "id": "U4F3lmq0omE5"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "np_Hong1=np.array([80, 90, 10, 9])\n",
        "np_Jung1=np.array([70, 80, 9, 8])\n",
        "v=np_Hong1+np_Jung1\n",
        "w=[0.5, 0.5, 0.5, 0.5]\n",
        "[i * j for i, j in zip(v, w)]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "si9fLECgooD0",
        "outputId": "4165f34a-499f-4e91-add3-98130d6305b0"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[75.0, 85.0, 9.5, 8.5]"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### ❶ 넘파이 브로드캐스팅 실습"
      ],
      "metadata": {
        "id": "LPtqmpwfoqM1"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "avg=[75.0, 85.0, 9.5, 8.5]\n",
        "avg1=np.array(avg)\n",
        "W1=np.array([0.4, 0.4, 1, 1])\n",
        "print(avg1*W1)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "cx3bALYDosZn",
        "outputId": "93bcfefc-5949-4d64-ef0b-a999f28a3615"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[30.  34.   9.5  8.5]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "####❷ 벡터의 내적 실습"
      ],
      "metadata": {
        "id": "ey-AIhCPovPH"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "Final=avg1*W1\n",
        "print(Final.sum())"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "P4rT6WB9ounf",
        "outputId": "fb27fea6-7a1c-4aa8-9256-69b08095e7c2"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "82.0\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##5. 벡터의 길이\n",
        "###넘파이 함수로 벡터의 길이 구하기"
      ],
      "metadata": {
        "id": "3QeHkcHso1LT"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "a1=np.array([1, 2])\n",
        "np.linalg.norm(a1)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "akdZAUDfo5R9",
        "outputId": "436c10af-8c80-4162-d845-edec2805f0f7"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2.23606797749979"
            ]
          },
          "metadata": {},
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "def dot(v, w):\n",
        " return sum(v_i * w_i for v_i, w_i in zip(v, w))\n",
        "def sum_of_squares(v):\n",
        " return dot(v, v)\n",
        "v=[1, 2]\n",
        "sum_of_squares(v)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "_Ip8aS_cpGn_",
        "outputId": "807c58a8-24ca-44ba-d995-7f3998e25b4b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "5"
            ]
          },
          "metadata": {},
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "def sum_of_squares1(v, w):\n",
        " return dot(v, w)\n",
        "v=[1, 2]\n",
        "w=[3, 4]\n",
        "sum_of_squares1(v, w)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mBFtvImyp95j",
        "outputId": "b77199c6-49a7-41c9-b872-b0bd55a56550"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "11"
            ]
          },
          "metadata": {},
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import math\n",
        "def magnitude(v):\n",
        "  return math.sqrt(sum_of_squares(v))\n",
        "v=[1, 2]\n",
        "magnitude(v)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "N2hzdvVFqPtH",
        "outputId": "75e1d0d1-5996-4fd0-c3ec-64af2d054ed9"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2.23606797749979"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "###벡터 간의 거리(유클리드 거리) 구하기"
      ],
      "metadata": {
        "id": "glURAucAqUi8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def vector_subtract(v, w):\n",
        "  return [v_i - w_i for v_i, w_i in zip(v, w)]\n",
        "def squared_distance(v, w):\n",
        "  return sum_of_squares(vector_subtract(v, w))\n",
        "v=[1, 2]\n",
        "w=[3, 4]\n",
        "squared_distance(v, w)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0xyIFKVCqS7V",
        "outputId": "9d6f5eaf-286a-49d8-cf06-01659c4966f4"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "8"
            ]
          },
          "metadata": {},
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "def distance(v, w):\n",
        " return math.sqrt(squared_distance(v, w))\n",
        "def distance1(v, w):\n",
        " return magnitude(vector_subtract(v, w))\n",
        "v=[1, 2]\n",
        "w=[3, 4]\n",
        "print(distance(v, w))\n",
        "print(distance1(v, w))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "N9wURvteqvSn",
        "outputId": "38b1db46-c8af-4ff5-d4f9-dc9c8dafbe7c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "2.8284271247461903\n",
            "2.8284271247461903\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##6. 벡터를 이용한 평균 제곱 오차와 평균 제곱근 오차\n",
        "###평균 제곱 오차"
      ],
      "metadata": {
        "id": "6TuWI5a_q08l"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "y=[1, 1, 0, 1, 0, 1, 0, 1, 0, 0] # 실제값\n",
        "t=[0, 0, 1, 0, 0, 1, 0, 1, 0, 0] # 예측값\n",
        "import numpy as np\n",
        "def mean_squared_error(y, t):\n",
        " return 0.5 * np.sum((y-t)**2)\n",
        "mean_squared_error(np.array(y), np.array(t))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "csKM2yrbq5KB",
        "outputId": "399bf246-6fe0-4436-ff03-a11783bd6e43"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2.0"
            ]
          },
          "metadata": {},
          "execution_count": 28
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "###평균 제곱근 오차"
      ],
      "metadata": {
        "id": "oScZ_L2lq9ju"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "c=np.array([1, 2, 3, 4, 5])\n",
        "d=np.array([2, 4, 6, 8, 10])\n",
        "print((sum((c-d)**2)/len(c))**(1/2))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "56U4tHKZrAS9",
        "outputId": "151ae10b-3ae7-4ab0-ce3d-d3b423be9213"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "3.3166247903554\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#Section 02. 행렬\n",
        "## 1. 행렬의 표기"
      ],
      "metadata": {
        "id": "3LoU0a7FrIxx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "Hong=[[80, 90, 20, 18], [70, 80, 18, 16]] # 홍길동 성적을 리스트로 표시\n",
        "Lee=[(90, 80, 18, 20), (100, 95, 20, 18)] # 이몽룡 성적을 튜플로 표시\n",
        "Kim={'m': (100, 95), 'f':(95, 100), 'a':(20, 20), 'r':(19, 18)} # 김철수 성적을 딕셔너리로 표시\n",
        "Kim"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FECSc7HhrOMf",
        "outputId": "78ff5c9e-4c60-4ce2-e926-fef9585c4070"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'m': (100, 95), 'f': (95, 100), 'a': (20, 20), 'r': (19, 18)}"
            ]
          },
          "metadata": {},
          "execution_count": 30
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "A=[[1, 2, 3],\n",
        " [4, 5, 6]]\n",
        "B=[[1, 2],\n",
        " [3, 4],\n",
        " [5, 6]]\n",
        "A1=np.array(A)\n",
        "A1.shape\n",
        "np.shape(A)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "eGjEo4cIrQq5",
        "outputId": "402be176-8a15-46fc-ee2d-48978970567f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(2, 3)"
            ]
          },
          "metadata": {},
          "execution_count": 31
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##2. 행렬의 연산\n",
        "###행렬의 덧셈, 뺄셈"
      ],
      "metadata": {
        "id": "UwnksXEirTMR"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x=np.array([10, 11, 12, 13, 14])\n",
        "y=np.array([0, 1, 2, 3, 4])\n",
        "x+y\n",
        "x-y"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "eVT2V3_-rXF0",
        "outputId": "cb488c5f-c0ed-4e30-ee73-876c24980ad9"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([10, 10, 10, 10, 10])"
            ]
          },
          "metadata": {},
          "execution_count": 32
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "###행렬의 곱셈"
      ],
      "metadata": {
        "id": "gAL4d69ArZ-K"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x=np.array([[1], [2], [3]])\n",
        "y=np.array([[4], [5], [6]])\n",
        "x*y"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "iWuebXbZrb6s",
        "outputId": "bf42d28d-a267-45ae-bd34-1dd5608c8696"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[ 4],\n",
              "       [10],\n",
              "       [18]])"
            ]
          },
          "metadata": {},
          "execution_count": 33
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.shape, y.shape # (3, 1), (3, 1)\n",
        "x.T # array([[1, 2, 3]])\n",
        "x.T.shape # (1, 3)\n",
        "x.T*y"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "UUx7Q06grjKI",
        "outputId": "78715276-3054-4628-ae4b-2488eb04c1e0"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[ 4,  8, 12],\n",
              "       [ 5, 10, 15],\n",
              "       [ 6, 12, 18]])"
            ]
          },
          "metadata": {},
          "execution_count": 34
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x_T=np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])\n",
        "y=np.array([[4, 4, 4], [5, 5, 5], [6, 6, 6]])\n",
        "x_T*y"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1JN5z45jrloe",
        "outputId": "086553ea-ce95-4f11-fd90-b83a93467c45"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[ 4,  8, 12],\n",
              "       [ 5, 10, 15],\n",
              "       [ 6, 12, 18]])"
            ]
          },
          "metadata": {},
          "execution_count": 35
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x=np.array([[1], [2], [3]]) # 3*1\n",
        "y=np.array([[4], [5], [6]]) # 3*1\n",
        "print(x.T@y)\n",
        "print(x@y.T)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3culme6-rnl5",
        "outputId": "1ef69337-aaaa-4b53-bdd9-b690e35a54d7"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[32]]\n",
            "[[ 4  5  6]\n",
            " [ 8 10 12]\n",
            " [12 15 18]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(np.dot(x.T, y))\n",
        "print(np.dot(x, y.T))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EIHrRkCdrpjn",
        "outputId": "db523435-5e90-45c6-e0e6-58aa3995d882"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[32]]\n",
            "[[ 4  5  6]\n",
            " [ 8 10 12]\n",
            " [12 15 18]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "A=np.array([[1, 2, 3], [4, 5, 6]])\n",
        "A.shape # (2, 3)\n",
        "B=np.array([[1, 2], [3, 4], [5, 6]])\n",
        "B.shape # (3, 2)\n",
        "A*B"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 211
        },
        "id": "jxOrCketsH2M",
        "outputId": "f0476f55-aca2-4a9f-b017-cfc239c055e7"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "ValueError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-39-d0a5ceb110c8>\u001b[0m in \u001b[0;36m<cell line: 5>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mB\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mB\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m \u001b[0;31m# (3, 2)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mA\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mB\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3) (3,2) "
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "A=np.array([[1, 2, 3], [4, 5, 6]])\n",
        "A.shape # (2, 3)\n",
        "B=np.array([[1, 2], [3, 4], [5, 6]])\n",
        "B.shape # (3, 2)\n",
        "print(A.T*B)\n",
        "print(A*B.T)\n",
        "print(A@B)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Kc5j26xDsKmu",
        "outputId": "1fb6a855-4542-49c2-88c0-d4c93b79427b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[ 1  8]\n",
            " [ 6 20]\n",
            " [15 36]]\n",
            "[[ 1  6 15]\n",
            " [ 8 20 36]]\n",
            "[[22 28]\n",
            " [49 64]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#Section 03. 신경망 기초\n",
        "##1. 시그모이드 함수"
      ],
      "metadata": {
        "id": "ymDVXNcisOVt"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "import matplotlib.pylab as plt\n",
        "def sigmoid(x):\n",
        " return 1 / (1 + np.exp(-x))\n",
        "X=np.arange(-5.0, 5.0, 0.1)\n",
        "Y=sigmoid(X)\n",
        "plt.plot(X, Y)\n",
        "plt.ylim(-0.1, 1.1)\n",
        "plt.show()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 430
        },
        "id": "51KKt9tqsbhz",
        "outputId": "85a62f2e-e24d-4cb8-d236-1e971a4a231a"
      },
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 640x480 with 1 Axes>"
            ],
            "image/png": "[encoded data, 19652 characters]\n"
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##2. 순전파 신경망 구현"
      ],
      "metadata": {
        "id": "W7gtKDAfs0ct"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 1)\t리스트를 활용한 순전파 신경망 계산"
      ],
      "metadata": {
        "id": "GVhzC5MGFzu-"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "input=[0.2, 0.9] # 2개의 입력층\n",
        "weight1=[[0.05,0.01], [-0.01,0.03], [0.02,-0.01]] #가중치 값\n",
        "bias1=[-0.3,0.2,0.05]\n",
        "weight2=[0.01,0.05,0.015] # 은닉층에서 출력층으로 가는 가중치\n",
        "bias2=[-0.015] # 출력층으로 가는 바이어스"
      ],
      "metadata": {
        "id": "rxdY8m12s4Vo"
      },
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 입력층에서 은닉층으로 신호 전달"
      ],
      "metadata": {
        "id": "QKx4nA64s89J"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# 입력층의 첫 번째 노드에서 은닉층의 첫 번째 노드로 신호 전달\n",
        "def dot(v, w):\n",
        "  return sum(i * j for i, j in zip(v, w))\n",
        "\n",
        "dot(input,weight1[0])+bias1[0] # 0.2*0.05 +0.9*0.01 + (-0.3)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1VUpo6UJtCQi",
        "outputId": "f880712f-d923-41f7-8441-05479d1d2b39"
      },
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "-0.28099999999999997"
            ]
          },
          "metadata": {},
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# 입력층에서 은닉층 노드로 보내는 신호값\n",
        "A3=dot(input,weight1[0])+bias1[0]\n",
        "A4=dot(input,weight1[1])+bias1[1]\n",
        "A5=dot(input,weight1[2])+bias1[2]\n",
        "A3, A4, A5"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bOE7sqPOtQ_-",
        "outputId": "07a1bc61-5d62-47b0-ae36-6558a66be8eb"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(-0.28099999999999997, 0.225, 0.045)"
            ]
          },
          "metadata": {},
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# 시그모이드 함수를 이용해 A3, A4, A5 노드에서 출력하는 값\n",
        "import numpy as np\n",
        "def sigmoid(x):\n",
        " return 1 / (1 + np.exp(-x))\n",
        "\n",
        "outputA3=sigmoid(A3)\n",
        "outputA4=sigmoid(A4)\n",
        "outputA5=sigmoid(A5)\n",
        "outputA3, outputA4, outputA5"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "isaEFnTZtTTw",
        "outputId": "2dc245b0-f7a6-448b-cec6-1a5785138918"
      },
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(0.4302086298074603, 0.5560138905446199, 0.5112481019468548)"
            ]
          },
          "metadata": {},
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 은닉층에서 출력층으로 신호 전달"
      ],
      "metadata": {
        "id": "sOvJPcnftlzl"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "Y1=[outputA3, outputA4, outputA5]\n",
        "A6=dot(Y1, weight2)+bias2\n",
        "outputA6=sigmoid(A6)\n",
        "outputA6"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "obgFmF-xtoCS",
        "outputId": "41f26b77-1367-4137-907a-c6372284a661"
      },
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0.50619256])"
            ]
          },
          "metadata": {},
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 2) 넘파이를 활용한 순전파 신경망 계산"
      ],
      "metadata": {
        "id": "RtuJe6KGtqEt"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# 입력층에서 첫 번째 은닉층으로 신호 전달 방법\n",
        "import numpy as np\n",
        "input=np.array([0.2, 0.9])\t\t\t\t                           # 1*2 행렬\n",
        "weight1=np.array([[0.05,0.01], [-0.01,0.03], [0.02,-0.01]]) \t# 3*2 행렬\n",
        "bias1=np.array([-0.3,0.2,0.05])\n",
        "A1=np.dot(input, weight1.T) + bias1\t\t                       \t# weight1.T는 weight의 전치 행렬\n",
        "Y1=sigmoid(A1)\n",
        "Y1\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "NN_67e92tsai",
        "outputId": "ec330b20-9936-4cff-b239-f976773380a3"
      },
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0.43020863, 0.55601389, 0.5112481 ])"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "weight2=np.array([0.01, 0.05, 0.015])\n",
        "bias2=np.array([-0.015])\n",
        "A2=np.dot(Y1, weight2.T)+bias2\n",
        "Y2=sigmoid(A2)\n",
        "Y2\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RvfjdPp1G5zl",
        "outputId": "420d6ee0-c9de-492c-d2e6-dce385de241a"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0.50619256])"
            ]
          },
          "metadata": {},
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "def identity_function(x):\n",
        " return x\n",
        "Y=identity_function(A2) # 혹은 Y=A2\n",
        "Y"
      ],
      "metadata": {
        "id": "cA_wT5nLt0G6",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "da0ad1e4-bdf5-4d48-f521-44e1b187530a"
      },
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0.0247715])"
            ]
          },
          "metadata": {},
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 순전파 신경망 구현 정리"
      ],
      "metadata": {
        "id": "yPfloxbHuETq"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def init_network():\n",
        "  network={}\n",
        "  network['W1']=np.array([[0.05,0.01], [-0.01,0.03], [0.02,-0.01]])\n",
        "  network['b1']=np.array([-0.3,0.2,0.05])\n",
        "  network['W2']=np.array([0.01, 0.05, 0.015])\n",
        "  network['b2']=np.array([-0.015])\n",
        "  return network\n",
        "\n",
        "def forward(network, x):\n",
        "  W1, W2=network['W1'], network['W2']\n",
        "  b1, b2=network['b1'], network['b2']\n",
        "  a1=np.dot(x, W1.T) + b1\n",
        "  y1=sigmoid(a1)\n",
        "  a2=np.dot(y1, W2.T) + b2\n",
        "  y2=sigmoid(a2)                # 시그모이드 함수 사용\n",
        "  # z2=identity_function(a2)    # 항등함수 사용\n",
        "  return y2\n",
        "\n",
        "network=init_network()\n",
        "input=np.array([0.2, 0.9])\n",
        "Y=forward(network, input)\n",
        "Y"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JdrjO2zOuG0A",
        "outputId": "567a37e5-4bce-4bef-f034-894ed39dff9b"
      },
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0.50619256])"
            ]
          },
          "metadata": {},
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##3. 역전파 신경망 구현"
      ],
      "metadata": {
        "id": "b-ervSFuuXVm"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "t,y=1, Y\n",
        "Error=t-y\n",
        "def MSE(t, y):\n",
        "   return 0.5 * np.sum((t-y)**2)\n",
        "Error, MSE(t,y)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bs1f2b5RuaWg",
        "outputId": "49bc3772-70fb-4303-bea3-5aacdd5b8d8d"
      },
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(array([0.49380744]), 0.1219228944272693)"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# 출력층에서 은닉층으로의 역전파 시 가중치 업데이트\n",
        "\n",
        "w2_old=np.array([0.01,0.05,0.015])    # 이전의 은닉층에서 출력층 노드간 가중치\n",
        "b2_old=np.array([-0.015])             # 이전의 은닉층에서 출력층 바이어스 가중치\n",
        "#업데이트 되는 가중치\n",
        "d2=-(t-y)*(1-y)*y           #δ2=–(1–0.5061)·(1–0.5061)·0.5061=–0.1234\n",
        "lr=0.01\t      #학습률\n",
        "\n",
        "# bias 업데이트\n",
        "b2_new=b2_old-lr*d2*1             # -0.015-0.1*(-0.12343)*1\n",
        "\n",
        "# weight 업데이트\n",
        "w36=w2_old[0]-lr*d2*Y1[0]         # 0.01-0.1*(-0.12343)*0.4302\n",
        "w46=w2_old[1]-lr*d2*Y1[1]         # 0.05-0.1*(-0.12343)*0.5560\n",
        "w56=w2_old[2]-lr*d2*Y1[2]         # 0.015-0.1*(-0.12343)*0.5112\n",
        "print(b2_new, w36, w46, w56)\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Pzi5EAwtM_yO",
        "outputId": "377192bc-e25b-41da-d971-c6bed7f98d1f"
      },
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[-0.01376567] [0.01053102] [0.0506863] [0.01563105]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# 아래 부분은 참고용\n",
        "from IPython.display import Image\n",
        "import numbers\n",
        "import pydotplus\n",
        "\n",
        "def layoutGraph(dot_data, pdfFile=None):\n",
        "    graph = pydotplus.graph_from_dot_data(dot_data)\n",
        "    return Image(graph.create_png())\n",
        "\n",
        "def unbiasedNode(id, label=None, output=None, style=None):\n",
        "    label = label or f'A{id}'\n",
        "    common = style or 'shape=record, style=rounded, color=gray26'\n",
        "    if output is None:\n",
        "        return f'{id} [ label=\"{label}\", fontsize=11, {common} ];'\n",
        "    return f'{id} [ label=\"{{ {label} | {output:.6f} }}\", fontsize=11, {common} ];'\n",
        "\n",
        "def biasedNode(id, bias, label=None, output=None):\n",
        "    label = label or f'A{id}'\n",
        "    if isinstance(bias, numbers.Number):\n",
        "        bias = f'{bias:.6f}'\n",
        "    if isinstance(output, numbers.Number):\n",
        "        output = f'{output:.6f}'\n",
        "    common = 'shape=record, style=rounded, fontsize=11, color=gray26'\n",
        "    if output is None:\n",
        "        return f'{id} [ label=\"{label}&#92;n{bias}\", {common} ];'\n",
        "    return f'{id} [ label=\"{{ {label}&#92;n{bias} | {output} }}\", {common} ];'\n",
        "\n",
        "def edge(n1, n2, weight=None):\n",
        "    if weight is None:\n",
        "        return f'{n1} -> {n2} [ label=\"w{n1}{n2}\" ];'\n",
        "    return f'{n1} -> {n2} [ label=\"{weight:.6f}\" ];'\n",
        "\n",
        "commonSettings = \"\"\"\n",
        "edge [ fontsize=11, color=gray55 ];\n",
        "rankdir=LR;\n",
        "# size=\"10,10\"\n",
        "graph [ranksep=\"0.8\"];\n",
        "\"\"\"\n",
        "# node [shape=record, style=rounded, fontsize=11, color=gray26];\n",
        "\n",
        "s = f\"\"\"\n",
        "digraph ethane {{\n",
        "{ commonSettings }\n",
        "{ unbiasedNode('Fat', label='Fat', style='color=white') }\n",
        "{ unbiasedNode('Salt', label='Salt', style='color=white') }\n",
        "\n",
        "subgraph cluster_input {{ label=\"Input Layer\"; color=\"grey75\";\n",
        "    { unbiasedNode(1) }\n",
        "    { unbiasedNode(2) }\n",
        "}}\n",
        "\n",
        "subgraph cluster_hidden {{ label=\"Hidden Layer\"; color=\"grey75\";\n",
        "    { biasedNode(3, '&theta;3')}\n",
        "    { biasedNode(4, '&theta;4')}\n",
        "    { biasedNode(5, '&theta;5')}\n",
        "}}\n",
        "subgraph cluster_output {{ label=\"Output Layer\"; color=\"grey75\";\n",
        "    { biasedNode(6, '&theta;6')}\n",
        "}}\n",
        "{ unbiasedNode('like', label='like', style='color=white') }\n",
        "Fat -> 1;\n",
        "Salt -> 2;\n",
        "\n",
        "{ edge(1, 3) }\n",
        "{ edge(2, 3) }\n",
        "{ edge(1, 4) }\n",
        "{ edge(2, 4) }\n",
        "{ edge(1, 5) }\n",
        "{ edge(2, 5) }\n",
        "{ edge(3, 6) }\n",
        "{ edge(4, 6) }\n",
        "{ edge(5, 6) }\n",
        "6 -> like\n",
        " }}\n",
        " \"\"\"\n",
        "layoutGraph(s, pdfFile='network-layout.pdf')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 298
        },
        "id": "sG_j0Af0wJdF",
        "outputId": "012fa2af-9a4d-4d4a-e007-d69cfd4d773f"
      },
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "image/png": "[encoded data, 44792 characters]\n",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {},
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Random network\n",
        "s = f\"\"\"\n",
        "digraph ethane {{\n",
        "{ commonSettings }\n",
        "{ unbiasedNode('Salt', label='Salt', style='color=white') }\n",
        "{ unbiasedNode('Fat', label='Fat', style='color=white') }\n",
        "{ unbiasedNode(2, output=0.9) }\n",
        "{ unbiasedNode(1, output=0.2) }\n",
        "{ biasedNode(4, 0.02, output=0.55601389)}\n",
        "{ biasedNode(3, -0.3, output=0.43020863)}\n",
        "{ biasedNode(5, 0.05, output=0.5112481 )}\n",
        "{ biasedNode(6, -0.01376567, output=0.505668252)}\n",
        "{ unbiasedNode('like', label='like', style='color=white') }\n",
        "2 -> Salt;\n",
        "1 -> Fat;\n",
        "{ edge(3,1 ) }\n",
        "{ edge(3,2 ) }\n",
        "{ edge(4,1 ) }\n",
        "{ edge(4,2) }\n",
        "{ edge(5,1) }\n",
        "{ edge(5,2 ) }\n",
        "{ edge(6,3, 0.01053102) }\n",
        "{ edge(6,4, 0.0506863) }\n",
        "{ edge(6,5, 0.01563105) }\n",
        " like -> 6\n",
        " }}\n",
        " \"\"\"\n",
        "layoutGraph(s, pdfFile='network-random.pdf')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 253
        },
        "id": "GrcHVFyW0-SP",
        "outputId": "f6d696bc-706d-4aa0-d510-c5ad9cdff38d"
      },
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "image/png": "[encoded data, 46904 characters]\n",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {},
          "execution_count": 28
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Random network\n",
        "s = f\"\"\"\n",
        "digraph ethane {{\n",
        "{ commonSettings }\n",
        "{ unbiasedNode('Salt', label='Salt', style='color=white') }\n",
        "{ unbiasedNode('Fat', label='Fat', style='color=white') }\n",
        "{ unbiasedNode(2, output=0.9) }\n",
        "{ unbiasedNode(1, output=0.2) }\n",
        "{ biasedNode(4, 0.02, output=0.55601389)}\n",
        "{ biasedNode(3, -0.3, output=0.43020863)}\n",
        "{ biasedNode(5, 0.05, output=0.5112481 )}\n",
        "{ biasedNode(6, -0.015, output=0.505668252)}\n",
        "{ unbiasedNode('like', label='like', style='color=white') }\n",
        "Salt -> 2;\n",
        "Fat -> 1;\n",
        "{ edge(1, 3, 0.05) }\n",
        "{ edge(2, 3, 0.01) }\n",
        "{ edge(1, 4, -0.01) }\n",
        "{ edge(2, 4, 0.03) }\n",
        "{ edge(1, 5, 0.2) }\n",
        "{ edge(2, 5, -0.01) }\n",
        "{ edge(3, 6, 0.01) }\n",
        "{ edge(4, 6, 0.05) }\n",
        "{ edge(5, 6, 0.015) }\n",
        "6 -> like\n",
        " }}\n",
        " \"\"\"\n",
        "layoutGraph(s, pdfFile='network-random.pdf')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 251
        },
        "id": "vXxqThWZwmgE",
        "outputId": "86500302-228b-4098-e0fd-b63dbfe36c0b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "image/png": "[encoded data, 46020 characters]\n",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {},
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "dZ1_dA1=sigmoid(A1)*(1-sigmoid(A1))\n",
        "d1=dZ1_dA1*weight2*d2\n",
        "d1"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uwFj35zzYYEO",
        "outputId": "9bd5f64b-5dcd-4cf4-ba1b-3d7ef7879c47"
      },
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([-0.00030257, -0.00152355, -0.00046264])"
            ]
          },
          "metadata": {},
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#이전의 가중치\n",
        "w1_old=np.array([[0.05,0.01], [-0.01,0.03], [0.02,-0.01]])\n",
        "b1_old=np.array([-0.3, 0.2, 0.05])\n",
        "#업데이트되는 가중치\n",
        "lr=0.01 # 학습률\n",
        "input=np.array([0.2, 0.9])\n",
        "b13_new=b1_old[0]-lr*d1[0] # 바이어스\n",
        "b14_new=b1_old[1]-lr*d1[1]\n",
        "b15_new=b1_old[2]-lr*d1[2]\n",
        "w13=w1_old[0][0]-lr*d1[0]*input[0] # 노드간 가중치\n",
        "w14=w1_old[1][0]-lr*d1[1]*input[0]\n",
        "w15=w1_old[2][0]-lr*d1[2]*input[0]\n",
        "w23=w1_old[0][1]-lr*d1[0]*input[1]\n",
        "w24=w1_old[1][1]-lr*d1[1]*input[1]\n",
        "w25=w1_old[2][1]-lr*d1[2]*input[1]\n",
        "print(b13_new,b14_new,b15_new)\n",
        "print(w13,w14,w15,w23,w24,w25)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JSJWW8YsbWcF",
        "outputId": "cc20f3c6-4c69-40e2-aa7c-173f7a54a10b"
      },
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "-0.2999969742990488 0.2000152354766293 0.05000462639213791\n",
            "0.050000605140190245 -0.009996952904674141 0.020000925278427582 0.010002723130856081 0.030013711928966364 -0.009995836247075882\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Random network\n",
        "s = f\"\"\"\n",
        "digraph ethane {{\n",
        "{ commonSettings }\n",
        "{ unbiasedNode('Salt', label='Salt', style='color=white') }\n",
        "{ unbiasedNode('Fat', label='Fat', style='color=white') }\n",
        "{ unbiasedNode(2, output=0.9) }\n",
        "{ unbiasedNode(1, output=0.2) }\n",
        "{ biasedNode(4, 0.2000152354766293, output=0.55601389)}\n",
        "{ biasedNode(3, -0.2999969742990488, output=0.43020863)}\n",
        "{ biasedNode(5, 0.05000462639213791, output=0.5112481 )}\n",
        "{ biasedNode(6, -0.01376567, output=0.505668252)}\n",
        "{ unbiasedNode('like', label='like', style='color=white') }\n",
        "2 -> Salt;\n",
        "1 -> Fat;\n",
        "{ edge(3,1, 0.0500006) }\n",
        "{ edge(3,2, 0.0100027) }\n",
        "{ edge(4,1, -0.009996) }\n",
        "{ edge(4,2, 0.0300137) }\n",
        "{ edge(5,1, 0.0200009) }\n",
        "{ edge(5,2, -0.009995 ) }\n",
        "{ edge(6,3, 0.01053102) }\n",
        "{ edge(6,4, 0.0506863) }\n",
        "{ edge(6,5, 0.01563105) }\n",
        " like -> 6\n",
        " }}\n",
        " \"\"\"\n",
        "layoutGraph(s, pdfFile='network-random.pdf')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 259
        },
        "id": "R03Wk_ocknsv",
        "outputId": "ef23ec5e-0573-4846-f693-dc18fc5310e3"
      },
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "image/png": "[encoded data, 49584 characters]\n",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {},
          "execution_count": 31
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "vlHR-OMojsMo"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "##4. 사이킷런을 이용한 신경망 구현"
      ],
      "metadata": {
        "id": "4Qci7O7yum1w"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import pandas as pd\n",
        "url='https://raw.githubusercontent.com/sehakflower/data/main/TinyData.csv'\n",
        "data= pd.read_csv(url,sep=',') # 또는 data=pd.read_csv('TinyData.csv')\n",
        "data"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 237
        },
        "id": "5aMzll37urPQ",
        "outputId": "3a13f878-52dd-4f3e-aee6-1d1287ee008d"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "   Obs.  Fat  Salt Acceptance\n",
              "0     1  0.2   0.9       like\n",
              "1     2  0.1   0.1    dislike\n",
              "2     3  0.2   0.4    dislike\n",
              "3     4  0.2   0.5    dislike\n",
              "4     5  0.4   0.5       like\n",
              "5     6  0.3   0.8       like"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-dc67f8fa-45c6-4bb7-be22-da1f1a368958\" class=\"colab-df-container\">\n",
              "    <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Obs.</th>\n",
              "      <th>Fat</th>\n",
              "      <th>Salt</th>\n",
              "      <th>Acceptance</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>1</td>\n",
              "      <td>0.2</td>\n",
              "      <td>0.9</td>\n",
              "      <td>like</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>2</td>\n",
              "      <td>0.1</td>\n",
              "      <td>0.1</td>\n",
              "      <td>dislike</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>3</td>\n",
              "      <td>0.2</td>\n",
              "      <td>0.4</td>\n",
              "      <td>dislike</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>4</td>\n",
              "      <td>0.2</td>\n",
              "      <td>0.5</td>\n",
              "      <td>dislike</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>5</td>\n",
              "      <td>0.4</td>\n",
              "      <td>0.5</td>\n",
              "      <td>like</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>6</td>\n",
              "      <td>0.3</td>\n",
              "      <td>0.8</td>\n",
              "      <td>like</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "    <div class=\"colab-df-buttons\">\n",
              "\n",
              "  <div class=\"colab-df-container\">\n",
              "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-dc67f8fa-45c6-4bb7-be22-da1f1a368958')\"\n",
              "            title=\"Convert this dataframe to an interactive table.\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
              "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
              "  </svg>\n",
              "    </button>\n",
              "\n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    .colab-df-buttons div {\n",
              "      margin-bottom: 4px;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "    <script>\n",
              "      const buttonEl =\n",
              "        document.querySelector('#df-dc67f8fa-45c6-4bb7-be22-da1f1a368958 button.colab-df-convert');\n",
              "      buttonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "      async function convertToInteractive(key) {\n",
              "        const element = document.querySelector('#df-dc67f8fa-45c6-4bb7-be22-da1f1a368958');\n",
              "        const dataTable =\n",
              "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                    [key], {});\n",
              "        if (!dataTable) return;\n",
              "\n",
              "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "          + ' to learn more about interactive tables.';\n",
              "        element.innerHTML = '';\n",
              "        dataTable['output_type'] = 'display_data';\n",
              "        await google.colab.output.renderOutput(dataTable, element);\n",
              "        const docLink = document.createElement('div');\n",
              "        docLink.innerHTML = docLinkHtml;\n",
              "        element.appendChild(docLink);\n",
              "      }\n",
              "    </script>\n",
              "  </div>\n",
              "\n",
              "\n",
              "<div id=\"df-aae4aedf-fe03-4b7e-bf4c-7cceaa055aa6\">\n",
              "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-aae4aedf-fe03-4b7e-bf4c-7cceaa055aa6')\"\n",
              "            title=\"Suggest charts.\"\n",
              "            style=\"display:none;\">\n",
              "\n",
              "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "     width=\"24px\">\n",
              "    <g>\n",
              "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
              "    </g>\n",
              "</svg>\n",
              "  </button>\n",
              "\n",
              "<style>\n",
              "  .colab-df-quickchart {\n",
              "    background-color: #E8F0FE;\n",
              "    border: none;\n",
              "    border-radius: 50%;\n",
              "    cursor: pointer;\n",
              "    display: none;\n",
              "    fill: #1967D2;\n",
              "    height: 32px;\n",
              "    padding: 0 0 0 0;\n",
              "    width: 32px;\n",
              "  }\n",
              "\n",
              "  .colab-df-quickchart:hover {\n",
              "    background-color: #E2EBFA;\n",
              "    box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "    fill: #174EA6;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart {\n",
              "    background-color: #3B4455;\n",
              "    fill: #D2E3FC;\n",
              "  }\n",
              "\n",
              "  [theme=dark] .colab-df-quickchart:hover {\n",
              "    background-color: #434B5C;\n",
              "    box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "    filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "    fill: #FFFFFF;\n",
              "  }\n",
              "</style>\n",
              "\n",
              "  <script>\n",
              "    async function quickchart(key) {\n",
              "      const charts = await google.colab.kernel.invokeFunction(\n",
              "          'suggestCharts', [key], {});\n",
              "    }\n",
              "    (() => {\n",
              "      let quickchartButtonEl =\n",
              "        document.querySelector('#df-aae4aedf-fe03-4b7e-bf4c-7cceaa055aa6 button');\n",
              "      quickchartButtonEl.style.display =\n",
              "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "    })();\n",
              "  </script>\n",
              "</div>\n",
              "    </div>\n",
              "  </div>\n"
            ]
          },
          "metadata": {},
          "execution_count": 50
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "predictors=['Fat', 'Salt']\n",
        "outcome='Acceptance'\n",
        "X=data[predictors]\n",
        "y=data[outcome]\n",
        "classes=sorted(y.unique())\n",
        "from sklearn.neural_network import MLPClassifier\n",
        "clf=MLPClassifier(hidden_layer_sizes=(3,), activation='logistic', solver='lbfgs', random_state=1)\n",
        "clf.fit(X, y)\n",
        "clf.predict(X)\n",
        "print('바이어스 값')\n",
        "print(clf.intercepts_)\n",
        "print('가중치 값')\n",
        "print(clf.coefs_)\n",
        "print(pd.concat([data, pd.DataFrame(clf.predict_proba(X), columns=classes)], axis=1))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Go7dx9rf_I3R",
        "outputId": "70c6eccb-5c45-4ee7-ac36-1337e817cce3"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "바이어스 값\n",
            "[array([0.13368042, 4.07247549, 7.00768105]), array([14.3074867])]\n",
            "가중치 값\n",
            "[array([[ -1.3065648 ,  -4.20427797, -13.29587331],\n",
            "       [ -0.04399729,  -4.91606921,  -6.03356987]]), array([[ -0.27348314],\n",
            "       [ -9.01211576],\n",
            "       [-17.63504684]])]\n",
            "   Obs.  Fat  Salt Acceptance   dislike      like\n",
            "0     1  0.2   0.9       like  0.000490  0.999510\n",
            "1     2  0.1   0.1    dislike  0.999994  0.000006\n",
            "2     3  0.2   0.4    dislike  0.999741  0.000259\n",
            "3     4  0.2   0.5    dislike  0.997368  0.002632\n",
            "4     5  0.4   0.5       like  0.002133  0.997867\n",
            "5     6  0.3   0.8       like  0.000075  0.999925\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "predictors=['Fat', 'Salt']\n",
        "outcome='Acceptance'\n",
        "X=data[predictors]\n",
        "y=data[outcome]\n",
        "classes=sorted(y.unique())\n",
        "from sklearn.neural_network import MLPClassifier\n",
        "clf=MLPClassifier(hidden_layer_sizes=(3,), activation='logistic', solver='lbfgs', random_state=1)\n",
        "clf.fit(X, y)\n",
        "clf.predict(X)\n",
        "print('바이어스 값')\n",
        "print(clf.intercepts_)\n",
        "print('가중치 값')\n",
        "print(clf.coefs_)\n",
        "print(pd.concat([data, pd.DataFrame(clf.predict_proba(X), columns=classes)], axis=1))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mqCEqlvFutx4",
        "outputId": "f41bcf07-66fd-475f-d682-547fb1a1459b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "바이어스 값\n",
            "[array([0.13368042, 4.07247549, 7.00768105]), array([14.3074867])]\n",
            "가중치 값\n",
            "[array([[ -1.3065648 ,  -4.20427797, -13.29587331],\n",
            "       [ -0.04399729,  -4.91606921,  -6.03356987]]), array([[ -0.27348314],\n",
            "       [ -9.01211576],\n",
            "       [-17.63504684]])]\n",
            "   Obs.  Fat  Salt Acceptance   dislike      like\n",
            "0     1  0.2   0.9       like  0.000490  0.999510\n",
            "1     2  0.1   0.1    dislike  0.999994  0.000006\n",
            "2     3  0.2   0.4    dislike  0.999741  0.000259\n",
            "3     4  0.2   0.5    dislike  0.997368  0.002632\n",
            "4     5  0.4   0.5       like  0.002133  0.997867\n",
            "5     6  0.3   0.8       like  0.000075  0.999925\n"
          ]
        }
      ]
    }
  ]
}