{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "冒險6.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yenlung/Python-AI-Book/blob/main/%E5%86%92%E9%9A%AA06_%E4%BA%BA%E5%B7%A5%E6%99%BA%E6%85%A7%E5%B0%B1%E6%98%AF%E5%95%8F%E5%80%8B%E5%A5%BD%E5%95%8F%E9%A1%8C%EF%BC%8C%E5%8C%96%E6%88%90%E5%87%BD%E6%95%B8%E7%9A%84%E5%9E%8B%E5%BC%8F%E5%AD%B8%E5%80%8B%E5%87%BD%E6%95%B8!.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "oO6NDJHbjZIt"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 純量(0階tensor)"
      ],
      "metadata": {
        "id": "UnQtco1lB5bQ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = np.array([[9], [4], [8], [7]])     #一維陣列建立"
      ],
      "metadata": {
        "id": "9zkvcHcLjwu4"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4feJbPBIklCK",
        "outputId": "2f4bf960-52b4-4296-f603-f66f3784a6b0"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[9],\n",
              "       [4],\n",
              "       [8],\n",
              "       [7]])"
            ]
          },
          "metadata": {},
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.shape"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "xb79QkMM_UAM",
        "outputId": "3a0273c1-9ae5-4695-abfb-db0a6e34653e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(4, 1)"
            ]
          },
          "metadata": {},
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 向量(1階tensor)"
      ],
      "metadata": {
        "id": "nNH-e9liYbFm"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = np.array([[9, 4, 8 , 7], [8, 7, 4, 5]])  #二維陣列建立"
      ],
      "metadata": {
        "id": "B3UlBHrjAyFB"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mswSVV8TklWL",
        "outputId": "ac97654a-922f-4519-e5f1-b1a0fcbed065"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[9, 4, 8, 7],\n",
              "       [8, 7, 4, 5]])"
            ]
          },
          "metadata": {},
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.shape"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "eh6yfoDg_X1Q",
        "outputId": "daffff55-7ea2-44ab-a1bd-25139eddb7bd"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(2, 4)"
            ]
          },
          "metadata": {},
          "execution_count": 28
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## 矩陣(2階tensor)"
      ],
      "metadata": {
        "id": "bJOkIJ7sYpLf"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# 矩陣\n",
        "a = x[0]\n",
        "b = x[1]"
      ],
      "metadata": {
        "id": "ZeF99aI0_nKZ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "a"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mhiHfJBNBCE_",
        "outputId": "8e197126-a9f2-4cc5-f566-0acc87d77006"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([9, 4, 8, 7])"
            ]
          },
          "metadata": {},
          "execution_count": 37
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a.reshape(2,2)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "afPKcYuTBGfL",
        "outputId": "850463a7-b1d1-483f-dedd-6a1d6ea1722e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[9, 4],\n",
              "       [8, 7]])"
            ]
          },
          "metadata": {},
          "execution_count": 38
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "a = a.reshape(2,2)\n",
        "b = b.reshape(2,2)"
      ],
      "metadata": {
        "id": "8_e0r1qBBLxE"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "x = np.array([a,b])"
      ],
      "metadata": {
        "id": "NKDl4obRBULH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "x"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0ALpCf4nBdAN",
        "outputId": "fed9cc9a-f72d-492d-f6a5-fb6c94b53715"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[[9, 4],\n",
              "        [8, 7]],\n",
              "\n",
              "       [[8, 7],\n",
              "        [4, 5]]])"
            ]
          },
          "metadata": {},
          "execution_count": 42
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "x.shape"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "flRPnXWUBe4v",
        "outputId": "cea65ee3-9f6a-44b4-846a-4dd56202a7a1"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(2, 2, 2)"
            ]
          },
          "metadata": {},
          "execution_count": 43
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## one hot encoding"
      ],
      "metadata": {
        "id": "z6glZirnBrvN"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#one hot encoding\n",
        "from tensorflow.keras.utils import to_categorical"
      ],
      "metadata": {
        "id": "LbMyOYikotn5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "x=[0,1,2,3,4,5]"
      ],
      "metadata": {
        "id": "PdM0ZaUBxUsf"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# 類別個數:6\n",
        "x_1 = to_categorical(x,6)\n",
        "x_1"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Lzm1CyOVx8oP",
        "outputId": "f104abd5-8537-4147-ecaa-bdc2c01fc7c0"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[1., 0., 0., 0., 0., 0.],\n",
              "       [0., 1., 0., 0., 0., 0.],\n",
              "       [0., 0., 1., 0., 0., 0.],\n",
              "       [0., 0., 0., 1., 0., 0.],\n",
              "       [0., 0., 0., 0., 1., 0.],\n",
              "       [0., 0., 0., 0., 0., 1.]], dtype=float32)"
            ]
          },
          "metadata": {},
          "execution_count": 3
        }
      ]
    }
  ]
}