diff --git "a/Week1_\341\204\207\341\205\251\341\206\250\341\204\211\341\205\263\341\206\270\341\204\200\341\205\252\341\204\214\341\205\246_\352\271\200\352\260\200\354\230\201.ipynb" "b/Week1_\341\204\207\341\205\251\341\206\250\341\204\211\341\205\263\341\206\270\341\204\200\341\205\252\341\204\214\341\205\246_\352\271\200\352\260\200\354\230\201.ipynb" new file mode 100644 index 0000000..0f67597 --- /dev/null +++ "b/Week1_\341\204\207\341\205\251\341\206\250\341\204\211\341\205\263\341\206\270\341\204\200\341\205\252\341\204\214\341\205\246_\352\271\200\352\260\200\354\230\201.ipynb" @@ -0,0 +1,4585 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "# **1주차 복습과제**\n", + "- 1주차 복습과제는 **넘파이/판다스 연습문제**입니다.\n", + "- 코드 작성하시고, 출력 결과까지 나오도록 실행 부탁드립니다.\n", + " - 제출 시 파일명 본인 이름으로 변경해 주세요. ex) Week1_복습과제_OOO\n", + "- 교재에서 다루지 않은 메소드도 다수 포함되어 있지만 구글링이나 챗지피티 등을 활용해서라도 풀어주세요! 한 번씩 사용해 보면 좋을 것 같아 어려워도 문제에 포함했습니다 🤗" + ], + "metadata": { + "id": "jkGNNJY3S65Z" + } + }, + { + "cell_type": "markdown", + "source": [ + "## **넘파이**" + ], + "metadata": { + "id": "WedDHAHPJPIA" + } + }, + { + "cell_type": "markdown", + "source": [ + "### 1. Import the numpy package under the name `np`." + ], + "metadata": { + "id": "7Ue21e0fKFRI" + } + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "id": "60ACXMoSGe0H" + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 2. Print the numpy version and the configuration.\n", + "\n", + "(hint: `np.__version__`, `np.show_config`)" + ], + "metadata": { + "id": "FnHQOUj-KNiT" + } + }, + { + "cell_type": "code", + "source": [ + "print(np.__version__)\n", + "np.show_config()" + ], + "metadata": { + "id": "FgXzYXoRS-V7", + "collapsed": true, + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "dd4f0adb-510a-42c5-9196-c8abcccb99c6" + }, + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "2.1.3\n", + "Build Dependencies:\n", + " blas:\n", + " detection method: pkgconfig\n", + " found: true\n", + " include directory: /opt/_internal/cpython-3.13.0/lib/python3.13/site-packages/scipy_openblas64/include\n", + " lib directory: /opt/_internal/cpython-3.13.0/lib/python3.13/site-packages/scipy_openblas64/lib\n", + " name: scipy-openblas\n", + " openblas configuration: OpenBLAS 0.3.27 USE64BITINT DYNAMIC_ARCH NO_AFFINITY\n", + " Haswell MAX_THREADS=64\n", + " pc file directory: /project/.openblas\n", + " version: 0.3.27\n", + " lapack:\n", + " detection method: pkgconfig\n", + " found: true\n", + " include directory: /opt/_internal/cpython-3.13.0/lib/python3.13/site-packages/scipy_openblas64/include\n", + " lib directory: /opt/_internal/cpython-3.13.0/lib/python3.13/site-packages/scipy_openblas64/lib\n", + " name: scipy-openblas\n", + " openblas configuration: OpenBLAS 0.3.27 USE64BITINT DYNAMIC_ARCH NO_AFFINITY\n", + " Haswell MAX_THREADS=64\n", + " pc file directory: /project/.openblas\n", + " version: 0.3.27\n", + "Compilers:\n", + " c:\n", + " commands: cc\n", + " linker: ld.bfd\n", + " name: gcc\n", + " version: 10.2.1\n", + " c++:\n", + " commands: c++\n", + " linker: ld.bfd\n", + " name: gcc\n", + " version: 10.2.1\n", + " cython:\n", + " commands: cython\n", + " linker: cython\n", + " name: cython\n", + " version: 3.0.11\n", + "Machine Information:\n", + " build:\n", + " cpu: x86_64\n", + " endian: little\n", + " family: x86_64\n", + " system: linux\n", + " host:\n", + " cpu: x86_64\n", + " endian: little\n", + " family: x86_64\n", + " system: linux\n", + "Python Information:\n", + " path: /tmp/build-env-v_9b5grh/bin/python\n", + " version: '3.13'\n", + "SIMD Extensions:\n", + " baseline:\n", + " - SSE\n", + " - SSE2\n", + " - SSE3\n", + " found:\n", + " - SSSE3\n", + " - SSE41\n", + " - POPCNT\n", + " - SSE42\n", + " - AVX\n", + " - F16C\n", + " - FMA3\n", + " - AVX2\n", + " not found:\n", + " - AVX512F\n", + " - AVX512CD\n", + " - AVX512_KNL\n", + " - AVX512_KNM\n", + " - AVX512_SKX\n", + " - AVX512_CLX\n", + " - AVX512_CNL\n", + " - AVX512_ICL\n", + "\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 3. Create a null vector of size 10.\n", + "\n", + "#### ✅ 출력 예시\n", + "\n", + "\n", + "```\n", + "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n", + "```\n", + "\n" + ], + "metadata": { + "id": "0lB5hLlTKb1Z" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.zeros(10)\n", + "print(a)" + ], + "metadata": { + "id": "eK4DTjPwS_zU", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "5bd10f54-4450-484c-8667-bbcc60c57ef9" + }, + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 4. Create a null vector of size 10 but the fifth value which is 1.\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n", + "```" + ], + "metadata": { + "id": "7lSSfwh6Kj5v" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.zeros(10)\n", + "a[4] = 1\n", + "print(a)" + ], + "metadata": { + "id": "ofohgzsTTBs6", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "30e62097-70b1-4db6-b24d-5a87e530c799" + }, + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 5. Create a vector with values ranging from 10 to 49.\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33\n", + " 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]\n", + "```" + ], + "metadata": { + "id": "-NeqvmpLKkdY" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.arange(10, 50)\n", + "print(a)" + ], + "metadata": { + "id": "oQ1Mo5W9TC0P", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "c0b88910-a141-43df-ae9d-73d904e10ad0" + }, + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33\n", + " 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 6. Reverse a vector (first element becomes last).\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "[9 8 7 6 5 4 3 2 1 0]\n", + "```" + ], + "metadata": { + "id": "B1dFQzRNLN23" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.arange(10)\n", + "a = a[::-1]\n", + "print(a)" + ], + "metadata": { + "id": "wrpNYd4jTDsx", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "7c7a108f-f717-4f67-a30b-350d44fe4948" + }, + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[9 8 7 6 5 4 3 2 1 0]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 7. Create a 3x3 matrix with values ranging from 0 to 8.\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "[[0 1 2]\n", + " [3 4 5]\n", + " [6 7 8]]\n", + "```" + ], + "metadata": { + "id": "doUDUe_NLOhe" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.arange(9).reshape(3, 3)\n", + "print(a)" + ], + "metadata": { + "id": "4g8WetkATEnw", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "90f967d5-d486-417f-acda-6bbd98d55f5f" + }, + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[0 1 2]\n", + " [3 4 5]\n", + " [6 7 8]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 8. Find indices of non-zero elements from [1,2,0,0,4,0].\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "(array([0, 1, 4]),)\n", + "```" + ], + "metadata": { + "id": "s-MtSq2RLzJx" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.array([1, 2, 0, 0, 4, 0])\n", + "print(np.nonzero(a))" + ], + "metadata": { + "id": "jw9iUP7sTL7D", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "7b24015d-cee6-4b60-b8fb-a14f5dabdf95" + }, + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "(array([0, 1, 4]),)\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 9. Create a 3x3 identity matrix.\n", + "\n", + "(hint: `np.eye`)\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "[[1. 0. 0.]\n", + " [0. 1. 0.]\n", + " [0. 0. 1.]]\n", + "```" + ], + "metadata": { + "id": "ihRWHFUxL6ak" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.eye(3)\n", + "print(a)" + ], + "metadata": { + "id": "fFzDGJT5TNh7", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d1515ab1-a627-423c-aa6c-9e7f0adaacec" + }, + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[1. 0. 0.]\n", + " [0. 1. 0.]\n", + " [0. 0. 1.]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 10. Create a 3x3x3 array with random values.\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "[[[0.30742852 0.26458726 0.85474383]\n", + " [0.80234531 0.76268902 0.91286677]\n", + " [0.8804109 0.51011378 0.82103555]]\n", + "\n", + " [[0.403753 0.83120234 0.15202655]\n", + " [0.75782685 0.0732058 0.48148689]\n", + " [0.2089428 0.44968622 0.88134184]]\n", + "\n", + " [[0.52251191 0.10738374 0.86279648]\n", + " [0.26203911 0.42157754 0.68856833]\n", + " [0.58133626 0.31059127 0.71354172]]]\n", + "```" + ], + "metadata": { + "id": "FvkGsY8eLPCG" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.random.random((3, 3, 3))\n", + "print(a)" + ], + "metadata": { + "id": "084SAfnwTPpt", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "f240246d-31d4-4e01-f515-58e100ab068a" + }, + "execution_count": 20, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[[0.03593104 0.2353642 0.5445187 ]\n", + " [0.25584542 0.91390591 0.00562641]\n", + " [0.9930815 0.66159525 0.22517526]]\n", + "\n", + " [[0.60797016 0.01061487 0.311095 ]\n", + " [0.71896739 0.70443172 0.57885033]\n", + " [0.08264436 0.00170929 0.0944805 ]]\n", + "\n", + " [[0.81116459 0.8830158 0.87981388]\n", + " [0.81963238 0.63595265 0.39136283]\n", + " [0.05696045 0.00421298 0.45956177]]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 11. Create a 10x10 array with random values and find the minimum and maximum values.\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "0.018360924693465508 0.9990506368595156\n", + "```" + ], + "metadata": { + "id": "4-VynNt5MVYY" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.random.random((10, 10))\n", + "print(a.min(), a.max())" + ], + "metadata": { + "id": "W14PP5x6TRh6", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "832fb69b-06b6-4dc7-fd80-86faf4f2909b" + }, + "execution_count": 21, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "0.0028190672459244004 0.9971531844150502\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 12. Create a random vector of size 30 and find the mean value.\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "0.46249036320403636\n", + "```" + ], + "metadata": { + "id": "cQKXmfJBMVQ_" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.random.random(30)\n", + "print(a.mean())" + ], + "metadata": { + "id": "FS6ggiNJTStp", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "77810321-74c1-42af-b617-f9a3be6cf6e9" + }, + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "0.4403328346470911\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 13. Create a 2d array with 1 on the border and 0 inside. (size: 10x10)\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n", + "```" + ], + "metadata": { + "id": "QZ4h-AddMVJb" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.ones((10, 10))\n", + "a[1:-1, 1:-1] = 0\n", + "print(a)" + ], + "metadata": { + "id": "pKEi08edTUMt", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d574e92d-6d8e-46b1-a930-7eda829ef789" + }, + "execution_count": 23, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n", + " [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 14. How to add a border (filled with 0's) around an existing array?\n", + "\n", + "(hint: 인덱싱 사용)\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "# before\n", + "[[1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]]\n", + "\n", + " # after\n", + " [[0. 0. 0. 0. 0.]\n", + " [0. 1. 1. 1. 0.]\n", + " [0. 1. 1. 1. 0.]\n", + " [0. 1. 1. 1. 0.]\n", + " [0. 0. 0. 0. 0.]]\n", + "\n", + "```" + ], + "metadata": { + "id": "BIwX-BiSMUz_" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.ones((5, 5))\n", + "print(\"# before\")\n", + "print(a)" + ], + "metadata": { + "id": "A4M1huiqTWXy", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "75f8ee17-f5c0-4e67-cc4f-c26d1db19a0b" + }, + "execution_count": 24, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "# before\n", + "[[1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]\n", + " [1. 1. 1. 1. 1.]]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "b = np.zeros((7, 7))\n", + "b[1:-1, 1:-1] = a\n", + "print(\"# after\")\n", + "print(b)" + ], + "metadata": { + "id": "idWhL4zzTWRO", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "43563928-7ce9-4d07-c193-ffcbe3fc4853" + }, + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "# after\n", + "[[0. 0. 0. 0. 0. 0. 0.]\n", + " [0. 1. 1. 1. 1. 1. 0.]\n", + " [0. 1. 1. 1. 1. 1. 0.]\n", + " [0. 1. 1. 1. 1. 1. 0.]\n", + " [0. 1. 1. 1. 1. 1. 0.]\n", + " [0. 1. 1. 1. 1. 1. 0.]\n", + " [0. 0. 0. 0. 0. 0. 0.]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 15. What is the result of the following expression?\n", + "\n", + "(실행 후, 결과에 대한 주석 작성해주세요. ex. 출력 결과에 대한 이유)\n", + "\n", + "```python\n", + "0 * np.nan\n", + "np.nan == np.nan\n", + "np.inf > np.nan\n", + "np.nan - np.nan\n", + "np.nan in set([np.nan])\n", + "0.3 == 3 * 0.1\n", + "```" + ], + "metadata": { + "id": "tggOHEUGM9PK" + } + }, + { + "cell_type": "code", + "source": [ + "print(0 * np.nan) # nan: NaN은 계산에 포함되면 결과도 NaN\n", + "print(np.nan == np.nan) # False: NaN은 자기 자신과도 같지 않음\n", + "print(np.inf > np.nan) # False: NaN과 비교하면 False\n", + "print(np.nan != np.nan) # True: NaN은 자기 자신과 같지 않음\n", + "print(np.nan in set([np.nan])) # True: 같은 NaN 객체가 set에 존재함\n", + "print(0.3 == 3 * 0.1) # False: 부동소수점 계산의 정밀도 차이" + ], + "metadata": { + "id": "RmjjLx8_TYNp", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "12ae6da3-a386-47de-ef55-b19d48972427" + }, + "execution_count": 26, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "nan\n", + "False\n", + "False\n", + "True\n", + "True\n", + "False\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 16. Normalize a 5x5 random matrix.\n", + "\n", + "(hint: (x - mean) / std)\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "[[-1.4765971 0.9582795 -1.29910242 1.0250981 -0.79801651]\n", + " [ 0.57945383 -1.28559078 0.02733238 0.99675052 1.2702088 ]\n", + " [-0.08513084 -1.4686812 -0.78813593 -0.34559303 -1.15052328]\n", + " [ 0.98183813 -1.41255344 0.37190169 1.05697419 -0.02668639]\n", + " [-1.14466851 0.98911205 1.14499256 0.69454882 1.18478886]]\n", + "```" + ], + "metadata": { + "id": "28iOVtXXM8gA" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.random.random((5, 5))\n", + "a = (a - a.mean()) / a.std()\n", + "print(a)" + ], + "metadata": { + "id": "0L0IE4qOTZW4", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "432831f3-d397-4444-bc39-a4f04a5fb4af" + }, + "execution_count": 27, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[-1.99588271 -0.3464484 -0.46320366 1.21216171 1.72254863]\n", + " [ 0.9805843 1.38366017 -0.52953676 -1.96591643 0.73929381]\n", + " [ 0.47637047 0.5673763 -1.20418587 0.39239036 0.24613627]\n", + " [-0.65110178 0.39675138 -1.31227703 -0.5374522 1.20724389]\n", + " [ 0.33243034 -0.07954592 0.89048983 -0.2640553 -1.19783139]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 17. Multiply a 5x3 matrix by a 3x2 matrix. (real matrix product)\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "[[3. 3.]\n", + " [3. 3.]\n", + " [3. 3.]\n", + " [3. 3.]\n", + " [3. 3.]]\n", + "```" + ], + "metadata": { + "id": "asOmp4b5M8dx" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.ones((5, 3)) * 1.1\n", + "b = np.ones((3, 2))\n", + "print(np.dot(a, b))" + ], + "metadata": { + "id": "01PxOWW5Te7I", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "9139f0a5-bc99-4b42-bd29-24fc57cc6d83" + }, + "execution_count": 28, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[3.3 3.3]\n", + " [3.3 3.3]\n", + " [3.3 3.3]\n", + " [3.3 3.3]\n", + " [3.3 3.3]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 18. What are the result of the following expressions?\n", + "\n", + "(실행 후, 결과에 대한 주석 작성해주세요. ex. 출력 결과에 대한 이유)\n", + "\n", + "```python\n", + "np.array(0) / np.array(0)\n", + "np.array(0) // np.array(0)\n", + "np.array([np.nan]).astype(int).astype(float)\n", + "```" + ], + "metadata": { + "id": "H9zVpsYuM8aR" + } + }, + { + "cell_type": "code", + "source": [ + "print(np.array(0) / np.array(0)) # 0을 0으로 나누므로 nan\n", + "print(np.array(0) // np.array(0)) # 0을 0으로 나누는 연산이라 경고가 발생할 수 있음\n", + "print(np.array([np.nan]).astype(int).astype(float)) # nan을 정수형으로 변환하면 정수의 최솟값으로 변환될 수 있음" + ], + "metadata": { + "id": "SLP--cFwTh10", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "10c4e9d8-b125-4431-daca-894b6fa26a6f" + }, + "execution_count": 29, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "nan\n", + "0\n", + "[-9.22337204e+18]\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "/tmp/ipykernel_1243/1835376348.py:1: RuntimeWarning: invalid value encountered in divide\n", + " print(np.array(0) / np.array(0)) # 0을 0으로 나누므로 nan\n", + "/tmp/ipykernel_1243/1835376348.py:2: RuntimeWarning: divide by zero encountered in floor_divide\n", + " print(np.array(0) // np.array(0)) # 0을 0으로 나누는 연산이라 경고가 발생할 수 있음\n", + "/tmp/ipykernel_1243/1835376348.py:3: RuntimeWarning: invalid value encountered in cast\n", + " print(np.array([np.nan]).astype(int).astype(float)) # nan을 정수형으로 변환하면 정수의 최솟값으로 변환될 수 있음\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 19. Create a 5x5 matrix with row values ranging from 0 to 4.\n", + "\n", + "#### ✅ 출력 예시\n", + "\n", + "```\n", + "[[0. 1. 2. 3. 4.]\n", + " [0. 1. 2. 3. 4.]\n", + " [0. 1. 2. 3. 4.]\n", + " [0. 1. 2. 3. 4.]\n", + " [0. 1. 2. 3. 4.]]\n", + "```" + ], + "metadata": { + "id": "fbi_T8LIO3yc" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.tile(np.arange(5), (5, 1))\n", + "print(a)" + ], + "metadata": { + "id": "XHj8J0pLTjrf", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6bc141bf-bc64-43f7-bab4-bd03b657be87" + }, + "execution_count": 30, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[0 1 2 3 4]\n", + " [0 1 2 3 4]\n", + " [0 1 2 3 4]\n", + " [0 1 2 3 4]\n", + " [0 1 2 3 4]]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 20. Create a random vector of size 10 and sort it.\n", + "\n", + "#### ✅출력 예시\n", + "\n", + "\n", + "```\n", + "[0.11443045 0.16715169 0.32515597 0.38374227 0.48327044 0.78728921\n", + " 0.80523192 0.85915686 0.96930939 0.99586525]\n", + "\n", + "```" + ], + "metadata": { + "id": "cNRIzEzEO_Ft" + } + }, + { + "cell_type": "code", + "source": [ + "a = np.random.random(10)\n", + "a.sort()\n", + "print(a)" + ], + "metadata": { + "id": "a3lZxnx-Tngw", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "09e71a85-605f-4383-d641-408424e102d1" + }, + "execution_count": 31, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[0.16110832 0.16738196 0.18049933 0.31957566 0.39553183 0.44334241\n", + " 0.59172898 0.62805866 0.89075051 0.9702591 ]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "## **판다스**" + ], + "metadata": { + "id": "fkX-tY1oKDq5" + } + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yl7zlBgszyCS" + }, + "source": [ + "\n", + "### 1. Import pandas under the alias `pd`." + ] + }, + { + "cell_type": "code", + "source": [ + "import pandas as pd" + ], + "metadata": { + "id": "ZiANPeHhz00W" + }, + "execution_count": 32, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2Hk3SuquzyCU" + }, + "source": [ + "### 2. Print the version of pandas that has been imported." + ] + }, + { + "cell_type": "code", + "source": [ + "print(pd.__version__)" + ], + "metadata": { + "id": "i2NtsBbjz1x2", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "7c41e384-67aa-450b-e4c7-cf5e0a498cd4" + }, + "execution_count": 33, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "2.2.3\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oOTRFQtFzyCV" + }, + "source": [ + "## 3~20번 문제는 아래 데이터프레임으로 진행됩니다.\n", + "\n", + "Consider the following Python dictionary `data` and Python list `labels`:\n", + "\n", + "``` python\n", + "data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],\n", + " 'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],\n", + " 'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],\n", + " 'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}\n", + "\n", + "labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']\n", + "```\n" + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 3. Create a DataFrame `df` from this dictionary `data` which has the index `labels`.\n" + ], + "metadata": { + "id": "m_8US0jJzyCW" + } + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "id": "7CbS_gtmzyCW", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 363 + }, + "outputId": "bb220722-aa5c-4ccb-8e9b-449d9ecd8980" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " animal age visits priority\n", + "a cat 2.5 1 yes\n", + "b cat 3.0 3 yes\n", + "c snake 0.5 2 no\n", + "d dog NaN 3 yes\n", + "e dog 5.0 2 no\n", + "f cat 2.0 3 no\n", + "g snake 4.5 1 no\n", + "h cat NaN 1 yes\n", + "i dog 7.0 2 no\n", + "j dog 3.0 1 no" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalagevisitspriority
acat2.51yes
bcat3.03yes
csnake0.52no
ddogNaN3yes
edog5.02no
fcat2.03no
gsnake4.51no
hcatNaN1yes
idog7.02no
jdog3.01no
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + " \n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "df", + "summary": "{\n \"name\": \"df\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"animal\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"cat\",\n \"snake\",\n \"dog\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.0077973005261263,\n \"min\": 0.5,\n \"max\": 7.0,\n \"num_unique_values\": 7,\n \"samples\": [\n 2.5,\n 3.0,\n 4.5\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"visits\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 1,\n 3,\n 2\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"priority\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"no\",\n \"yes\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 34 + } + ], + "source": [ + "import numpy as np\n", + "\n", + "data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],\n", + " 'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],\n", + " 'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],\n", + " 'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}\n", + "\n", + "labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']\n", + "\n", + "df = pd.DataFrame(data, index=labels)\n", + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "f2shsFGyzyCW" + }, + "source": [ + "### 4. Display a summary of the basic information about this DataFrame and its data. \n", + "(hint: there is a single method that can be called on the DataFrame)" + ] + }, + { + "cell_type": "code", + "source": [ + "df.info()" + ], + "metadata": { + "id": "BWZpEuGtz7ky", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "9303114f-34b0-4749-ba4e-fc7151a29a8f" + }, + "execution_count": 35, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\n", + "Index: 10 entries, a to j\n", + "Data columns (total 4 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 animal 10 non-null object \n", + " 1 age 8 non-null float64\n", + " 2 visits 10 non-null int64 \n", + " 3 priority 10 non-null object \n", + "dtypes: float64(1), int64(1), object(2)\n", + "memory usage: 700.0+ bytes\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### 5. Display a summary of the basic statistics about data of this DataFrame. \n", + "(hint: there is a single method that can be called on the DataFrame)" + ], + "metadata": { + "id": "AfbkaEOyzyCX" + } + }, + { + "cell_type": "code", + "source": [ + "df.describe()" + ], + "metadata": { + "id": "wzzc100Oz8c3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 300 + }, + "outputId": "c61da426-c748-4209-e213-8cd9808103f7" + }, + "execution_count": 36, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " age visits\n", + "count 8.000000 10.000000\n", + "mean 3.437500 1.900000\n", + "std 2.007797 0.875595\n", + "min 0.500000 1.000000\n", + "25% 2.375000 1.000000\n", + "50% 3.000000 2.000000\n", + "75% 4.625000 2.750000\n", + "max 7.000000 3.000000" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
agevisits
count8.00000010.000000
mean3.4375001.900000
std2.0077970.875595
min0.5000001.000000
25%2.3750001.000000
50%3.0000002.000000
75%4.6250002.750000
max7.0000003.000000
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"df\",\n \"rows\": 8,\n \"fields\": [\n {\n \"column\": \"age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.5474771307751376,\n \"min\": 0.5,\n \"max\": 8.0,\n \"num_unique_values\": 8,\n \"samples\": [\n 3.4375,\n 3.0,\n 8.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"visits\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3.0122099789789663,\n \"min\": 0.8755950357709131,\n \"max\": 10.0,\n \"num_unique_values\": 7,\n \"samples\": [\n 10.0,\n 1.9,\n 2.75\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 36 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XJ59aPcrzyCX" + }, + "source": [ + "### 6. Return the first 3 rows of the DataFrame `df`." + ] + }, + { + "cell_type": "code", + "source": [ + "df.head(3)" + ], + "metadata": { + "id": "vZro7sh9z_rY", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 143 + }, + "outputId": "6b1cbcb5-b659-4e0f-9b3b-61a5d7419b7d" + }, + "execution_count": 37, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " animal age visits priority\n", + "a cat 2.5 1 yes\n", + "b cat 3.0 3 yes\n", + "c snake 0.5 2 no" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalagevisitspriority
acat2.51yes
bcat3.03yes
csnake0.52no
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "df", + "summary": "{\n \"name\": \"df\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"animal\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"cat\",\n \"snake\",\n \"dog\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.0077973005261263,\n \"min\": 0.5,\n \"max\": 7.0,\n \"num_unique_values\": 7,\n \"samples\": [\n 2.5,\n 3.0,\n 4.5\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"visits\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 1,\n 3,\n 2\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"priority\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"no\",\n \"yes\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 37 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KBVp0_3ZzyCY" + }, + "source": [ + "### 7. Select just the 'animal' and 'age' columns from the DataFrame `df`." + ] + }, + { + "cell_type": "code", + "source": [ + "df[['animal', 'age']]" + ], + "metadata": { + "id": "pC6sH-1F0AJo", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 363 + }, + "outputId": "fc5b0714-1330-4bf7-fab4-143dd6d81222" + }, + "execution_count": 38, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " animal age\n", + "a cat 2.5\n", + "b cat 3.0\n", + "c snake 0.5\n", + "d dog NaN\n", + "e dog 5.0\n", + "f cat 2.0\n", + "g snake 4.5\n", + "h cat NaN\n", + "i dog 7.0\n", + "j dog 3.0" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalage
acat2.5
bcat3.0
csnake0.5
ddogNaN
edog5.0
fcat2.0
gsnake4.5
hcatNaN
idog7.0
jdog3.0
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"df[['animal', 'age']]\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"animal\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"cat\",\n \"snake\",\n \"dog\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.0077973005261263,\n \"min\": 0.5,\n \"max\": 7.0,\n \"num_unique_values\": 7,\n \"samples\": [\n 2.5,\n 3.0,\n 4.5\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 38 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a1giftRYzyCY" + }, + "source": [ + "### 8. Select the data in rows `[3, 4, 8]` **and** in columns `['animal', 'age']`." + ] + }, + { + "cell_type": "code", + "source": [ + "df.iloc[[3, 4, 8]][['animal', 'age']]" + ], + "metadata": { + "id": "tkSynFGH0BLD", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 143 + }, + "outputId": "9e021f9b-30dd-48dc-d184-2fc06c4550c7" + }, + "execution_count": 39, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " animal age\n", + "d dog NaN\n", + "e dog 5.0\n", + "i dog 7.0" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalage
ddogNaN
edog5.0
idog7.0
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"df\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"animal\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"dog\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1.4142135623730951,\n \"min\": 5.0,\n \"max\": 7.0,\n \"num_unique_values\": 2,\n \"samples\": [\n 7.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 39 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "67sYTLBqzyCY" + }, + "source": [ + "### 9. Select only the rows where the number of visits is greater than 3." + ] + }, + { + "cell_type": "code", + "source": [ + "df[df['visits'] > 3]" + ], + "metadata": { + "id": "d_G0WeMG0Dgd", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + }, + "outputId": "4bccaf6c-6854-42d1-b575-7ab440073a99" + }, + "execution_count": 40, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "Empty DataFrame\n", + "Columns: [animal, age, visits, priority]\n", + "Index: []" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalagevisitspriority
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "repr_error": "Out of range float values are not JSON compliant: nan" + } + }, + "metadata": {}, + "execution_count": 40 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FXX4YRWgzyCY" + }, + "source": [ + "### 10. Select the rows where the age is missing, i.e. it is `NaN`." + ] + }, + { + "cell_type": "code", + "source": [ + "df[df['age'].isnull()]" + ], + "metadata": { + "id": "KaV4Ypkj0Ea2", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 112 + }, + "outputId": "01b989bc-65e0-4971-f75a-fa96e86d2038" + }, + "execution_count": 41, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " animal age visits priority\n", + "d dog NaN 3 yes\n", + "h cat NaN 1 yes" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalagevisitspriority
ddogNaN3yes
hcatNaN1yes
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"df[df['age']\",\n \"rows\": 2,\n \"fields\": [\n {\n \"column\": \"animal\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"cat\",\n \"dog\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": null,\n \"min\": null,\n \"max\": null,\n \"num_unique_values\": 0,\n \"samples\": [],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"visits\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 2,\n \"samples\": [],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"priority\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 1,\n \"samples\": [],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 41 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kMH5n-UBzyCZ" + }, + "source": [ + "### 11. Select the rows where the animal is a cat *and* the age is less than 3." + ] + }, + { + "cell_type": "code", + "source": [ + "df[(df['animal'] == 'cat') & (df['age'] < 3)]" + ], + "metadata": { + "id": "8sCDh9Ez0FND", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 112 + }, + "outputId": "aa616225-d14a-45ae-f2a4-6682ab5ae8ba" + }, + "execution_count": 42, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " animal age visits priority\n", + "a cat 2.5 1 yes\n", + "f cat 2.0 3 no" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalagevisitspriority
acat2.51yes
fcat2.03no
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"df[(df['animal'] == 'cat') & (df['age'] < 3)]\",\n \"rows\": 2,\n \"fields\": [\n {\n \"column\": \"animal\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"cat\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.3535533905932738,\n \"min\": 2.0,\n \"max\": 2.5,\n \"num_unique_values\": 2,\n \"samples\": [\n 2.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"visits\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 2,\n \"samples\": [\n 3\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"priority\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"no\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 42 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "62Y0JsYazyCZ" + }, + "source": [ + "### 12. Select the rows the age is between 2 and 4 (inclusive). \n" + ] + }, + { + "cell_type": "code", + "source": [ + "df[df['age'].between(2, 4)]" + ], + "metadata": { + "id": "svjvRtgZ0G76", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 175 + }, + "outputId": "343b09f1-89ea-4ed8-bfab-e6e923d968ce" + }, + "execution_count": 43, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " animal age visits priority\n", + "a cat 2.5 1 yes\n", + "b cat 3.0 3 yes\n", + "f cat 2.0 3 no\n", + "j dog 3.0 1 no" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalagevisitspriority
acat2.51yes
bcat3.03yes
fcat2.03no
jdog3.01no
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"df[df['age']\",\n \"rows\": 4,\n \"fields\": [\n {\n \"column\": \"animal\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"dog\",\n \"cat\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.47871355387816905,\n \"min\": 2.0,\n \"max\": 3.0,\n \"num_unique_values\": 3,\n \"samples\": [\n 2.5,\n 3.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"visits\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 2,\n \"samples\": [\n 3,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"priority\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"no\",\n \"yes\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 43 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4lIGMIkPzyCZ" + }, + "source": [ + "### 13. Change the age in row 'f' to 1.5." + ] + }, + { + "cell_type": "code", + "source": [ + "df.loc['f', 'age'] = 1.5\n", + "df" + ], + "metadata": { + "id": "h4U4A6Ai0Hvk", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 363 + }, + "outputId": "9609be1c-f56e-49dc-fbf4-3c74c53a0507" + }, + "execution_count": 60, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " animal age visits priority\n", + "a cat 2.5 1 NaN\n", + "b cat 3.0 3 NaN\n", + "c snake 0.5 2 NaN\n", + "d dog NaN 3 NaN\n", + "e dog 5.0 2 NaN\n", + "f cat 1.5 3 NaN\n", + "g snake 4.5 1 NaN\n", + "h cat NaN 1 NaN\n", + "i dog 7.0 2 NaN\n", + "j dog 3.0 1 NaN" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalagevisitspriority
acat2.51NaN
bcat3.03NaN
csnake0.52NaN
ddogNaN3NaN
edog5.02NaN
fcat1.53NaN
gsnake4.51NaN
hcatNaN1NaN
idog7.02NaN
jdog3.01NaN
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + " \n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "df", + "repr_error": "Out of range float values are not JSON compliant: nan" + } + }, + "metadata": {}, + "execution_count": 60 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FZzPr9ObzyCZ" + }, + "source": [ + "### 14. Calculate the sum of all visits in `df` (i.e. the total number of visits)." + ] + }, + { + "cell_type": "code", + "source": [ + "df['visits'].sum()" + ], + "metadata": { + "id": "FXLAUqR40I6C", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "ab6b99b3-80e8-49ab-f63b-e98a2ca2936b" + }, + "execution_count": 48, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "np.int64(19)" + ] + }, + "metadata": {}, + "execution_count": 48 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PO0xpJ_OzyCa" + }, + "source": [ + "### 15. Calculate the mean age for each different animal in `df`." + ] + }, + { + "cell_type": "code", + "source": [ + "df.groupby('animal')['age'].mean()" + ], + "metadata": { + "id": "L63WRx_20Kta", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 210 + }, + "outputId": "38b653e8-5150-484d-c351-b1a1eed9ff00" + }, + "execution_count": 49, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "animal\n", + "cat 2.333333\n", + "dog 5.000000\n", + "snake 2.500000\n", + "Name: age, dtype: float64" + ], + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
age
animal
cat2.333333
dog5.000000
snake2.500000
\n", + "

" + ] + }, + "metadata": {}, + "execution_count": 49 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YfVdzQXIzyCa" + }, + "source": [ + "### 16. Append a new row 'k' to `df` with your choice of values for each column. Then delete that row to return the original DataFrame." + ] + }, + { + "cell_type": "code", + "source": [ + "df.loc['k'] = ['dog', 5.5, 2, 'no']\n", + "print(\"추가된 후 df의 마지막 부분:\\n\", df.tail(3))\n", + "\n", + "df = df.drop('k')\n", + "print(\"\\n삭제된 후 df의 마지막 부분:\\n\", df.tail(3))" + ], + "metadata": { + "id": "Per12Ekp0Mc0", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6580a011-b0fe-401c-eee3-0743e8c7d33d" + }, + "execution_count": 59, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "추가된 후 df의 마지막 부분:\n", + " animal age visits priority\n", + "i dog 7.0 2 NaN\n", + "j dog 3.0 1 NaN\n", + "k dog 5.5 2 no\n", + "\n", + "삭제된 후 df의 마지막 부분:\n", + " animal age visits priority\n", + "h cat NaN 1 NaN\n", + "i dog 7.0 2 NaN\n", + "j dog 3.0 1 NaN\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tDkk_tHszyCa" + }, + "source": [ + "### 17. Count the number of each type of animal in `df`." + ] + }, + { + "cell_type": "code", + "source": [ + "df['animal'].value_counts()" + ], + "metadata": { + "id": "v21izXST0NTR", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 210 + }, + "outputId": "9bb68117-05e1-4553-f00d-a718e7e626dd" + }, + "execution_count": 51, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "animal\n", + "cat 4\n", + "dog 4\n", + "snake 2\n", + "Name: count, dtype: int64" + ], + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
count
animal
cat4
dog4
snake2
\n", + "

" + ] + }, + "metadata": {}, + "execution_count": 51 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cwz4s5RYzyCa" + }, + "source": [ + "### 18. Sort `df` first by the values in the 'age' in *decending* order, then by the value in the 'visits' column in *ascending* order (so row `i` should be first, and row `d` should be last).\n", + "#### ✅출력 예시" + ] + }, + { + "cell_type": "markdown", + "source": [ + " index |animal\t| age\t|visits|\tpriority\n", + "---|---|---|---|---\n", + "i|\tdog|\t7.0|\t2|\tno\n", + "e|\tdog|\t5.0|\t2|\tno\n", + "g|\tsnake|\t4.5|\t1|\tno\n", + "j|\tdog|\t3.0|\t1|\tno\n", + "b|\tcat|\t3.0|\t3|\tyes\n", + "a|\tcat|\t2.5|\t1|\tyes\n", + "f|\tcat|\t1.5|\t3|\tno\n", + "c|\tsnake|\t0.5|\t2|\tno\n", + "h|\tcat|\tNaN|\t1|\tyes\n", + "d|\tdog|\tNaN|\t3|\tyes" + ], + "metadata": { + "id": "YILpLKeqzyCa" + } + }, + { + "cell_type": "code", + "source": [ + "df.sort_values(by=['age', 'visits'], ascending=[False, True])" + ], + "metadata": { + "id": "2l6Pb7T10Qpi", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 363 + }, + "outputId": "f58f44fc-c54f-4fb3-9b71-f91a232fd490" + }, + "execution_count": 52, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " animal age visits priority\n", + "i dog 7.0 2 no\n", + "e dog 5.0 2 no\n", + "g snake 4.5 1 no\n", + "j dog 3.0 1 no\n", + "b cat 3.0 3 yes\n", + "a cat 2.5 1 yes\n", + "f cat 1.5 3 no\n", + "c snake 0.5 2 no\n", + "h cat NaN 1 yes\n", + "d dog NaN 3 yes" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalagevisitspriority
idog7.02no
edog5.02no
gsnake4.51no
jdog3.01no
bcat3.03yes
acat2.51yes
fcat1.53no
csnake0.52no
hcatNaN1yes
ddogNaN3yes
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "summary": "{\n \"name\": \"df\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"animal\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"dog\",\n \"snake\",\n \"cat\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.065879266282796,\n \"min\": 0.5,\n \"max\": 7.0,\n \"num_unique_values\": 7,\n \"samples\": [\n 7.0,\n 5.0,\n 1.5\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"visits\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 2,\n 1,\n 3\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"priority\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"yes\",\n \"no\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 52 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6hfE99qHzyCb" + }, + "source": [ + "### 19. The 'priority' column contains the values 'yes' and 'no'. Replace this column with a column of boolean values: 'yes' should be `True` and 'no' should be `False`. \n", + "(hint: `map`)" + ] + }, + { + "cell_type": "code", + "source": [ + "df['priority'] = df['priority'].map({'yes': True, 'no': False})\n", + "df" + ], + "metadata": { + "id": "lPLmBRUP0SnR", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 363 + }, + "outputId": "22278948-5df9-48f5-822e-3b798915ef7a" + }, + "execution_count": 58, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " animal age visits priority\n", + "a cat 2.5 1 NaN\n", + "b cat 3.0 3 NaN\n", + "c snake 0.5 2 NaN\n", + "d dog NaN 3 NaN\n", + "e dog 5.0 2 NaN\n", + "f cat 1.5 3 NaN\n", + "g snake 4.5 1 NaN\n", + "h cat NaN 1 NaN\n", + "i dog 7.0 2 NaN\n", + "j dog 3.0 1 NaN" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalagevisitspriority
acat2.51NaN
bcat3.03NaN
csnake0.52NaN
ddogNaN3NaN
edog5.02NaN
fcat1.53NaN
gsnake4.51NaN
hcatNaN1NaN
idog7.02NaN
jdog3.01NaN
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + " \n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "df", + "repr_error": "Out of range float values are not JSON compliant: nan" + } + }, + "metadata": {}, + "execution_count": 58 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ycaIJncEzyCb" + }, + "source": [ + "### 20. In the 'animal' column, change the 'snake' entries to 'python'. " + ] + }, + { + "cell_type": "code", + "source": [ + "df['animal'] = df['animal'].replace('snake', 'python')\n", + "df" + ], + "metadata": { + "id": "MZelDUlE0Wag", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 363 + }, + "outputId": "df356c40-4230-4549-8438-a5e549e71094" + }, + "execution_count": 61, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " animal age visits priority\n", + "a cat 2.5 1 NaN\n", + "b cat 3.0 3 NaN\n", + "c python 0.5 2 NaN\n", + "d dog NaN 3 NaN\n", + "e dog 5.0 2 NaN\n", + "f cat 1.5 3 NaN\n", + "g python 4.5 1 NaN\n", + "h cat NaN 1 NaN\n", + "i dog 7.0 2 NaN\n", + "j dog 3.0 1 NaN" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
animalagevisitspriority
acat2.51NaN
bcat3.03NaN
cpython0.52NaN
ddogNaN3NaN
edog5.02NaN
fcat1.53NaN
gpython4.51NaN
hcatNaN1NaN
idog7.02NaN
jdog3.01NaN
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + " \n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "df", + "repr_error": "Out of range float values are not JSON compliant: nan" + } + }, + "metadata": {}, + "execution_count": 61 + } + ] + }, + { + "cell_type": "code", + "source": [ + "# 확인용 df 출력 셀\n", + "df" + ], + "metadata": { + "id": "uYFF5Jew0Xz7" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5YmjKvYizyCb" + }, + "source": [ + "### 21. Given a DataFrame of random numeric values:\n", + "```python\n", + "df = pd.DataFrame(np.random.random(size=(5, 3))) # this is a 5x3 DataFrame of float values\n", + "```\n", + "\n", + "how do you subtract the row mean from each element in the row?" + ] + }, + { + "cell_type": "code", + "source": [ + "# 랜덤 시드 고정\n", + "np.random.seed(2025)\n", + "\n", + "df = pd.DataFrame(np.random.random(size=(5, 3)))\n", + "\n", + "\n" + ], + "metadata": { + "id": "EeAkDWv40e9J" + }, + "execution_count": 65, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# 각 행의 원소에서 각 행의 평균 빼기\n", + "result = df.sub(df.mean(axis=1), axis=0)\n", + "\n", + "# 계산된 결과 출력\n", + "result" + ], + "metadata": { + "id": "Z24qNGpgKEVi", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 206 + }, + "outputId": "3deb31be-7835-4117-f6ba-f662e0321959" + }, + "execution_count": 66, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " 0 1 2\n", + "0 -0.516494 0.235870 0.280624\n", + "1 0.081768 0.024435 -0.106204\n", + "2 -0.047373 -0.212124 0.259497\n", + "3 0.115235 -0.230544 0.115309\n", + "4 -0.229731 0.498009 -0.268278" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
012
0-0.5164940.2358700.280624
10.0817680.024435-0.106204
2-0.047373-0.2121240.259497
30.115235-0.2305440.115309
4-0.2297310.498009-0.268278
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + " \n", + " \n", + "
\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "result", + "summary": "{\n \"name\": \"result\",\n \"rows\": 5,\n \"fields\": [\n {\n \"column\": 0,\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.2600799877766293,\n \"min\": -0.5164936717536524,\n \"max\": 0.1152351926457702,\n \"num_unique_values\": 5,\n \"samples\": [\n 0.08176811556001623,\n -0.22973101435186788,\n -0.04737339527141471\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": 1,\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.30921884463997473,\n \"min\": -0.230543997014095,\n \"max\": 0.4980088847671926,\n \"num_unique_values\": 5,\n \"samples\": [\n 0.02443549762640368,\n 0.4980088847671926,\n -0.21212404323618994\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": 2,\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.23814416048884277,\n \"min\": -0.26827787041532475,\n \"max\": 0.2806238044548883,\n \"num_unique_values\": 5,\n \"samples\": [\n -0.10620361318641997,\n -0.26827787041532475,\n 0.25949743850760476\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 66 + } + ] + } + ] +} \ No newline at end of file