diff --git "a/Week1_\353\263\265\354\212\265\352\263\274\354\240\234_\354\265\234\354\234\244\354\204\234.ipynb" "b/Week1_\353\263\265\354\212\265\352\263\274\354\240\234_\354\265\234\354\234\244\354\204\234.ipynb" new file mode 100644 index 0000000..e24ac4f --- /dev/null +++ "b/Week1_\353\263\265\354\212\265\352\263\274\354\240\234_\354\265\234\354\234\244\354\204\234.ipynb" @@ -0,0 +1,1948 @@ +{ + "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": 2, + "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", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "96d3d49a-d0b9-4f88-8226-11159388f847" + }, + "execution_count": 3, + "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": [ + "null_vector = np.zeros(10)\n", + "print(null_vector)" + ], + "metadata": { + "id": "eK4DTjPwS_zU", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "52fa562e-4cc1-49d4-c048-05d4f58a5626" + }, + "execution_count": 4, + "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": [ + "null_vector = np.zeros(10)\n", + "null_vector[4] = 1\n", + "print(null_vector)" + ], + "metadata": { + "id": "ofohgzsTTBs6", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "2d48bd0c-8fec-4708-d50e-d907e4ccedaf" + }, + "execution_count": 5, + "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": [ + "vector = np.arange(10, 50)\n", + "print(vector)" + ], + "metadata": { + "id": "oQ1Mo5W9TC0P", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "8a636784-4446-49fc-f40c-9da0219acc94" + }, + "execution_count": 6, + "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": [ + "vector = np.arange(10)\n", + "reversed_vector = vector[::-1]\n", + "print(reversed_vector)" + ], + "metadata": { + "id": "wrpNYd4jTDsx", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "fc5f5c0f-c183-41c9-d56b-e18cb54c11e6" + }, + "execution_count": 7, + "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": [ + "matrix = np.arange(9).reshape(3, 3)\n", + "print(matrix)" + ], + "metadata": { + "id": "4g8WetkATEnw", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6d67f9ed-8ebc-4cbd-8891-bcf19d2e0a8b" + }, + "execution_count": 8, + "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": [ + "indices = np.nonzero([1, 2, 0, 0, 4, 0])\n", + "print(indices)" + ], + "metadata": { + "id": "jw9iUP7sTL7D", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "bce0c6da-bee5-4e32-d4b2-77918128674c" + }, + "execution_count": 9, + "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": [ + "matrix = np.eye(3)\n", + "print(matrix)" + ], + "metadata": { + "id": "fFzDGJT5TNh7", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "c2d696d8-161b-4401-ec0a-a9ec0d0f4cbe" + }, + "execution_count": 10, + "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": [ + "array= np.random.random((3, 3, 3))\n", + "print(array)" + ], + "metadata": { + "id": "084SAfnwTPpt", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "c850f849-2289-4261-f8ab-bc94ef40126f" + }, + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[[0.53319894 0.04968071 0.08511909]\n", + " [0.81444641 0.38765835 0.71488352]\n", + " [0.40574384 0.43198236 0.84212173]]\n", + "\n", + " [[0.22280394 0.845116 0.70338582]\n", + " [0.47919253 0.61093608 0.66815181]\n", + " [0.63058673 0.4658989 0.55100563]]\n", + "\n", + " [[0.84279938 0.20940063 0.3795359 ]\n", + " [0.88365928 0.15088799 0.31228215]\n", + " [0.46671333 0.5990896 0.57677881]]]\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": [ + "random_array = np.random.random((10, 10))\n", + "min_value = np.min(random_array)\n", + "max_value = np.max(random_array)\n", + "print(min_value, max_value)" + ], + "metadata": { + "id": "W14PP5x6TRh6", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "623074e2-e3fb-4b72-dedc-5a38f9f17769" + }, + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "0.008650419083488958 0.9908229705068893\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": [ + "random_vector = np.random.random(30)\n", + "mean_value = np.mean(random_vector)\n", + "print(mean_value)" + ], + "metadata": { + "id": "FS6ggiNJTStp", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "053be87d-073f-4f0b-d0fd-30725e8c061a" + }, + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "0.5298213736960945\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": [ + "array_2d = np.ones((10, 10))\n", + "array_2d[1:-1, 1:-1] = 0\n", + "print(array_2d)" + ], + "metadata": { + "id": "pKEi08edTUMt", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "aea8c290-b4f6-4a5f-ce70-633df353874d" + }, + "execution_count": 14, + "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": [ + "array_2d = np.ones((5, 5))\n", + "print(\"# before\")\n", + "print(array_2d)" + ], + "metadata": { + "id": "A4M1huiqTWXy", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d514bdb4-0ad2-4d7a-9ce1-b9a5083bdd32" + }, + "execution_count": 15, + "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": [ + "padded_array = np.pad(array_2d, pad_width=1, mode='constant', constant_values=0)\n", + "print(\"\\n# after\")\n", + "print(padded_array)" + ], + "metadata": { + "id": "idWhL4zzTWRO", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "4bcb9ce9-e176-4594-afcd-65b114b771b4" + }, + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\n", + "# 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. 다음 표현식의 결과는 무엇입니까?\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", + "```\n", + "\n" + ], + "metadata": { + "id": "tggOHEUGM9PK" + } + }, + { + "cell_type": "code", + "source": [ + "print(0 * np.nan)\n", + "print(np.nan == np.nan)\n", + "print(np.inf > np.nan)\n", + "print(np.nan - np.nan)\n", + "print(np.nan in set([np.nan]))\n", + "print(0.3 == 3 * 0.1)\n", + "\n", + "# 이유: nan과의 산술 연산은 nan 반환하기 때문에->false나오는 이유" + ], + "metadata": { + "id": "RmjjLx8_TYNp" + }, + "execution_count": null, + "outputs": [] + }, + { + "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": [ + "random_matrix = np.random.random((5, 5))\n", + "print(\"Original random matrix:\\n\", random_matrix)\n", + "\n", + "mean = np.mean(random_matrix)\n", + "std = np.std(random_matrix)\n", + "normalized_matrix = (random_matrix - mean) / std\n", + "\n", + "print(\"\\nNormalized matrix:\\n\", normalized_matrix)" + ], + "metadata": { + "id": "0L0IE4qOTZW4", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d977ccac-8c36-4b18-86af-99c2fbcb3384" + }, + "execution_count": 20, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Original random matrix:\n", + " [[0.88253851 0.23959865 0.70859455 0.52154702 0.25538537]\n", + " [0.95503018 0.97652652 0.07558529 0.68507947 0.31920339]\n", + " [0.15666992 0.00288964 0.08110677 0.30799137 0.77123344]\n", + " [0.97705316 0.91856628 0.11745733 0.92424244 0.53996395]\n", + " [0.86870834 0.12289972 0.20828916 0.50662307 0.21779917]]\n", + "\n", + "Normalized matrix:\n", + " [[ 1.15743187 -0.7559906 0.6397656 0.08310242 -0.7090085 ]\n", + " [ 1.37317086 1.43714509 -1.2441029 0.56978349 -0.51908276]\n", + " [-1.00279082 -1.46044892 -1.22767069 -0.55245031 0.82618221]\n", + " [ 1.43871239 1.26465239 -1.11948953 1.28154493 0.13791216]\n", + " [ 1.11627257 -1.10329268 -0.84916927 0.03868796 -0.82086698]]\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": [ + "matrix_a = np.random.random((5, 3))\n", + "matrix_b = np.random.random((3, 2))\n", + "result = np.dot(matrix_a, matrix_b)\n", + "print(result)" + ], + "metadata": { + "id": "01PxOWW5Te7I", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "16042f73-c9d9-4120-c808-4a7406c9a229" + }, + "execution_count": 21, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[[0.50442391 0.88605108]\n", + " [0.559007 0.57291697]\n", + " [0.36599981 0.6434869 ]\n", + " [0.56252795 1.07201688]\n", + " [0.55287468 0.52761441]]\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": [ + "np.array(0) / np.array(0)\n", + "np.array(0) // np.array(0)\n", + "np.array([np.nan]).astype(int).astype(float)\n", + "\n", + "#이유: NumPy에서 0나누기0은 nan을 반환하며 경고 발생/ nan은 자기 자신을 포함해 어떤 값과도 같지 않으며, 정수형으로 변환시 큰 음수 값으로 변화 가능" + ], + "metadata": { + "id": "SLP--cFwTh10", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "f230d4d2-032e-459d-c851-559ad0cd6133" + }, + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "/tmp/ipykernel_1649/3376911992.py:1: RuntimeWarning: invalid value encountered in divide\n", + " np.array(0) / np.array(0)\n", + "/tmp/ipykernel_1649/3376911992.py:2: RuntimeWarning: divide by zero encountered in floor_divide\n", + " np.array(0) // np.array(0)\n", + "/tmp/ipykernel_1649/3376911992.py:3: RuntimeWarning: invalid value encountered in cast\n", + " np.array([np.nan]).astype(int).astype(float)\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([-9.22337204e+18])" + ] + }, + "metadata": {}, + "execution_count": 22 + } + ] + }, + { + "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": [ + "matrix = np.zeros((5, 5))\n", + "matrix += np.arange(5)\n", + "print(matrix)" + ], + "metadata": { + "id": "XHj8J0pLTjrf", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d44f9b9c-ffe8-40a5-8252-197db70682c3" + }, + "execution_count": 23, + "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": [ + "random_vector = np.random.random(10)\n", + "sorted_vector = np.sort(random_vector)\n", + "print(sorted_vector)" + ], + "metadata": { + "id": "a3lZxnx-Tngw", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "9844cee9-1787-468a-cc94-4f63a86ec8a2" + }, + "execution_count": 24, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[0.16749987 0.2194041 0.2936178 0.39814319 0.56392283 0.63932163\n", + " 0.66415326 0.80370528 0.87377817 0.91258182]\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\n" + ], + "metadata": { + "id": "ZiANPeHhz00W" + }, + "execution_count": 25, + "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": "dbc197e7-3e6f-42ef-dd21-5d88460c4691" + }, + "execution_count": 26, + "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": 27, + "metadata": { + "id": "7CbS_gtmzyCW" + }, + "outputs": [], + "source": [ + "# 문제 풀이 전 numpy 임포트\n", + "import numpy as np" + ] + }, + { + "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": [ + "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", + "dataframe = pd.DataFrame(data, index=labels)\n", + "print(dataframe)" + ], + "metadata": { + "id": "BWZpEuGtz7ky", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "0b311785-1c72-4b12-885f-35461faa1ec5" + }, + "execution_count": 28, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " 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\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": [ + "dataframe = pd.DataFrame(data, index=labels)\n", + "print(dataframe.describe())" + ], + "metadata": { + "id": "wzzc100Oz8c3", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "28bce9a6-c52b-4660-8467-db09a7f30498" + }, + "execution_count": 29, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " 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\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XJ59aPcrzyCX" + }, + "source": [ + "### 6. Return the first 3 rows of the DataFrame `df`." + ] + }, + { + "cell_type": "code", + "source": [ + "df = pd.DataFrame(data, index=labels)\n", + "print(df.head(3))" + ], + "metadata": { + "id": "vZro7sh9z_rY", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "1dfa8ee0-b065-447b-e2ad-97e4935a428c" + }, + "execution_count": 30, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " 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" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KBVp0_3ZzyCY" + }, + "source": [ + "### 7. Select just the 'animal' and 'age' columns from the DataFrame `df`." + ] + }, + { + "cell_type": "code", + "source": [ + "dff = pd.DataFrame(data, index=labels)\n", + "print(dff[['animal', 'age']])" + ], + "metadata": { + "id": "pC6sH-1F0AJo", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "0ebf578f-5eb5-4b52-fb9b-887675411a45" + }, + "execution_count": 31, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " 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\n" + ] + } + ] + }, + { + "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": [ + "rows = [3, 4, 8]\n", + "columns = ['animal', 'age']\n", + "print(dff.iloc[rows][columns])" + ], + "metadata": { + "id": "tkSynFGH0BLD", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "27a596bb-a5d2-4367-b444-b92e062b1172" + }, + "execution_count": 34, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " animal age\n", + "d dog NaN\n", + "e dog 5.0\n", + "i dog 7.0\n" + ] + } + ] + }, + { + "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": [ + "rows = dff[dff['visits'] > 3]\n", + "print(rows)" + ], + "metadata": { + "id": "d_G0WeMG0Dgd", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "a7d79895-6706-4257-adcf-2e3b3ba91a20" + }, + "execution_count": 37, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Empty DataFrame\n", + "Columns: [animal, age, visits, priority]\n", + "Index: []\n" + ] + } + ] + }, + { + "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": [ + "rows = dff[dff['age'].isnull()]\n", + "print(rows)" + ], + "metadata": { + "id": "KaV4Ypkj0Ea2", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d00974f9-2deb-46d2-c1b2-6245b1a9b8b8" + }, + "execution_count": 38, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " animal age visits priority\n", + "d dog NaN 3 yes\n", + "h cat NaN 1 yes\n" + ] + } + ] + }, + { + "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": [ + "row = dff[(dff['animal'] == 'cat') & (dff['age'] < 3)]\n", + "print(row)" + ], + "metadata": { + "id": "8sCDh9Ez0FND", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "93510799-aca3-4e66-ec28-09947f1c51c9" + }, + "execution_count": 39, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " animal age visits priority\n", + "a cat 2.5 1 yes\n", + "f cat 2.0 3 no\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "62Y0JsYazyCZ" + }, + "source": [ + "### 12. Select the rows the age is between 2 and 4 (inclusive). \n" + ] + }, + { + "cell_type": "code", + "source": [ + "rows = dff[(dff['age'] >= 2) & (dff['age'] <= 4)]\n", + "print(rows)" + ], + "metadata": { + "id": "svjvRtgZ0G76", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "1b0ee88f-b9b6-4270-fab1-e7b02fdf33d6" + }, + "execution_count": 40, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " 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\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4lIGMIkPzyCZ" + }, + "source": [ + "### 13. Change the age in row 'f' to 1.5." + ] + }, + { + "cell_type": "code", + "source": [ + "rows = dff.loc['f']\n", + "rows['age'] = 1.5\n", + "print(rows)" + ], + "metadata": { + "id": "h4U4A6Ai0Hvk", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "38c66ff5-ba68-40a0-9342-d865d9babed5" + }, + "execution_count": 41, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "animal cat\n", + "age 1.5\n", + "visits 3\n", + "priority no\n", + "Name: f, dtype: object\n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "/tmp/ipykernel_1649/2460173498.py:2: SettingWithCopyWarning: \n", + "A value is trying to be set on a copy of a slice from a DataFrame\n", + "\n", + "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", + " rows['age'] = 1.5\n" + ] + } + ] + }, + { + "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": [ + "sum_visits = dff['visits'].sum()\n", + "print(sum_visits)" + ], + "metadata": { + "id": "FXLAUqR40I6C", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "c1bb2ac1-bbdc-467e-eed2-7f1d9d94c243" + }, + "execution_count": 42, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "19\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PO0xpJ_OzyCa" + }, + "source": [ + "### 15. Calculate the mean age for each different animal in `df`." + ] + }, + { + "cell_type": "code", + "source": [ + "mean_age = dff.groupby('animal')['age'].mean()\n", + "print(mean_age)" + ], + "metadata": { + "id": "L63WRx_20Kta", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "8416630e-931c-40b3-db80-c0bc43ccd7f0" + }, + "execution_count": 43, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "animal\n", + "cat 2.5\n", + "dog 5.0\n", + "snake 2.5\n", + "Name: age, dtype: float64\n" + ] + } + ] + }, + { + "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": [ + "row_k = {'animal': 'dog', 'age': 3.0, 'visits': 2, 'priority': 'yes'}\n", + "dff.loc['k'] = row_k\n", + "print(dff)" + ], + "metadata": { + "id": "Per12Ekp0Mc0", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "26056e5a-a271-4069-9dd7-99cc7f5f5750" + }, + "execution_count": 44, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " 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\n", + "k dog 3.0 2 yes\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tDkk_tHszyCa" + }, + "source": [ + "### 17. Count the number of each type of animal in `df`." + ] + }, + { + "cell_type": "code", + "source": [ + "from numpy._core.defchararray import count\n", + "count_animal = dff['animal'].value_counts()\n", + "print(count_animal)" + ], + "metadata": { + "id": "v21izXST0NTR", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "ae59249b-7231-444e-bbef-aea4cfa8fa20" + }, + "execution_count": 45, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "animal\n", + "dog 5\n", + "cat 4\n", + "snake 2\n", + "Name: count, dtype: int64\n" + ] + } + ] + }, + { + "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": [ + "sort = dff.sort_values(by=['age', 'visits'], ascending=[False, True])\n", + "print(sort)" + ], + "metadata": { + "id": "2l6Pb7T10Qpi", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "2c8c8f18-4c75-44ec-e818-02e58dba798f" + }, + "execution_count": 46, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " 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", + "k dog 3.0 2 yes\n", + "b cat 3.0 3 yes\n", + "a cat 2.5 1 yes\n", + "f cat 2.0 3 no\n", + "c snake 0.5 2 no\n", + "h cat NaN 1 yes\n", + "d dog NaN 3 yes\n" + ] + } + ] + }, + { + "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": [ + "priority_map = {'yes': True, 'no': False}\n", + "dff['priority'] = dff['priority'].map(priority_map)\n", + "print(dff)" + ], + "metadata": { + "id": "lPLmBRUP0SnR", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "797f7eb8-dc04-44b4-a73a-16aa4f5cdc1c" + }, + "execution_count": 47, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " animal age visits priority\n", + "a cat 2.5 1 True\n", + "b cat 3.0 3 True\n", + "c snake 0.5 2 False\n", + "d dog NaN 3 True\n", + "e dog 5.0 2 False\n", + "f cat 2.0 3 False\n", + "g snake 4.5 1 False\n", + "h cat NaN 1 True\n", + "i dog 7.0 2 False\n", + "j dog 3.0 1 False\n", + "k dog 3.0 2 True\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ycaIJncEzyCb" + }, + "source": [ + "### 20. In the 'animal' column, change the 'snake' entries to 'python'. " + ] + }, + { + "cell_type": "code", + "source": [ + "change_animal = dff['animal'].replace('snake', 'python')\n", + "print(change_animal)" + ], + "metadata": { + "id": "MZelDUlE0Wag", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "ba7a3c0e-235d-4017-f39d-467c193f05ec" + }, + "execution_count": 48, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "a cat\n", + "b cat\n", + "c python\n", + "d dog\n", + "e dog\n", + "f cat\n", + "g python\n", + "h cat\n", + "i dog\n", + "j dog\n", + "k dog\n", + "Name: animal, dtype: object\n" + ] + } + ] + }, + { + "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": 50, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "f452f358", + "outputId": "270a5b29-d8de-4270-f8a9-9ac2429b40b7" + }, + "source": [ + "row_means = df.mean(axis=1)\n", + "normalized_df = df.sub(row_means, axis=0)\n", + "print(normalized_df)" + ], + "execution_count": 52, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " 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\n" + ] + } + ] + } + ] +} \ No newline at end of file diff --git "a/Week1_\354\230\210\354\212\265\352\263\274\354\240\234_\354\265\234\354\234\244\354\204\234.ipynb" "b/Week1_\354\230\210\354\212\265\352\263\274\354\240\234_\354\265\234\354\234\244\354\204\234.ipynb" new file mode 100644 index 0000000..11a57cb --- /dev/null +++ "b/Week1_\354\230\210\354\212\265\352\263\274\354\240\234_\354\265\234\354\234\244\354\204\234.ipynb" @@ -0,0 +1,4101 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "WBipMcgkunmR" + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "source": [ + "array1 = np.array([1,2,3])\n", + "print('array1 type:', type(array1))\n", + "print('array1 array 형태:',array1.shape)\n", + "\n", + "array2 = np.array([[1,2,3],\n", + " [2,3,4]])\n", + "print('array2 type:', type(array2))\n", + "print('array2 array 형태:', array2.shape)\n", + "\n", + "array3 = np.array([[1,2,3]])\n", + "print('array3 type:', type(array3))\n", + "print('array3 array 형태:', array3.shape)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 291 + }, + "id": "oXNbJNvsu0sy", + "outputId": "c39b5360-7f65-4c55-d80d-2daeaabbbebf" + }, + "execution_count": 2, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/645659951.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marray1\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;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\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[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'array1 type:'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray1\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 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'array1 array 형태:'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0marray1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m array2 = np.array([[1,2,3],\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print('array1: {:0}차원, array2: {:1}차원, array3: {:2}차원'.format(array1.ndim,\n", + " array2.ndim, array3.ndim))" + ], + "metadata": { + "id": "Q7S2UysKxYtI" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "list1=[1,2,3]\n", + "print(type(list1))\n", + "array1=np.array(list1)\n", + "print(type(array1))\n", + "print(array1, array1.dtype)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 291 + }, + "id": "XQSLNAJmyG96", + "outputId": "50305e2f-ad5c-43c3-ed40-82cd3f9edb2e" + }, + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\n" + ] + }, + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/249164064.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mlist1\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;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist1\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[0;32m----> 3\u001b[0;31m \u001b[0marray1\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[0mlist1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray1\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 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "list2=[1,2,'test']\n", + "array2=np.array(list2)\n", + "print(array2, array2.dtype)\n", + "\n", + "list3=[1,2,3.0]\n", + "array3=np.array(list3)\n", + "print(array3, array3.dtype)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 273 + }, + "id": "laMgTtK2yim9", + "outputId": "003ce865-706e-4f20-c980-83a0a541557e" + }, + "execution_count": 4, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/824939284.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mlist2\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'test'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0marray2\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[0mlist2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mlist3\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;36m3.0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "array_int=np.array([1,2,3])\n", + "array_float=array_int.astype('float64')\n", + "print(array_float, array_float.dtype)\n", + "\n", + "array_int1=array_float.astype('int32')\n", + "print(array_int1, array_int1.dtype)\n", + "\n", + "array_float1=np.array([1.1,2.1,3.1])\n", + "array_int2=array_float1.astype('int32')\n", + "print(array_int2, array_int2.dtype)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 327 + }, + "id": "eDinL7nVgIC-", + "outputId": "f4e55fb9-565a-4d99-ef27-65e2ef496c08" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/1740540798.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marray_int\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;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\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[0m\u001b[1;32m 2\u001b[0m \u001b[0marray_float\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marray_int\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'float64'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray_float\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray_float\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0marray_int1\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marray_float\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'int32'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "sequence_array=np.arange(10)\n", + "print(sequence_array)\n", + "print(sequence_array.dtype, sequence_array.shape)" + ], + "metadata": { + "id": "o4S6xVYIh-uv" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "zero_array=np.zeros((3,2), dtype='int32')\n", + "print(zero_array)\n", + "print(zero_array.dtype, zero_array.shape)\n", + "\n", + "one_array=np.ones((3,2))\n", + "print(one_array)\n", + "print(one_array.dtype, one_array.shape)" + ], + "metadata": { + "id": "ik7C-FgfiU5U" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array1=np.arange(10)\n", + "print('array1:\\n', array1)\n", + "\n", + "array2=array1.reshape(2,5)\n", + "print('array2:\\n', array2)\n", + "\n", + "array3=array1.reshape(5,2)\n", + "print('array3:\\n', array3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 273 + }, + "id": "Ej363euYieKL", + "outputId": "fe81c410-21fe-4a50-bf21-ef5fd07aede7" + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/3369047960.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marray1\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'array1:\\n'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0marray2\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marray1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'array2:\\n'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "array1=np.arange(10)\n", + "print(array1)\n", + "array2=array1.reshape(-1,5)\n", + "print('array2 shape:', array2.shape)\n", + "array3=array1.reshape(5,-1)\n", + "print('array3 shape:', array3.shape)" + ], + "metadata": { + "id": "MnL36eZ4iadm" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array1=np.arange(8)\n", + "array3d=array1.reshape((2,2,2))\n", + "print('array3d:\\n', array3d.tolist())\n", + "\n", + "array5=array3d.reshape(-1,1)\n", + "print('array5:\\n', array5.tolist())\n", + "print('array5 shape:', array5.shape)\n", + "\n", + "array6=array1.reshape(-1,1)\n", + "print('array6:\\n', array6.tolist())\n", + "print('array6 shape:', array6.shape)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 291 + }, + "id": "E4izFpgGi8gv", + "outputId": "9e3b3a2d-1ed4-46cb-9986-06f604c004dc" + }, + "execution_count": 7, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/3429315025.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marray1\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0marray3d\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marray1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\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;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'array3d:\\n'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray3d\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtolist\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0marray5\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marray3d\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "array1=np.arange(start=1, stop=10)\n", + "print('array1:', array1)\n", + "value=array1[2]\n", + "print('value:', value)\n", + "print(type(value))" + ], + "metadata": { + "id": "skpXK1Crjesk" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [], + "metadata": { + "id": "euU_EtTcgOLF" + } + }, + { + "cell_type": "code", + "source": [ + "print('맨 뒤의 값:', array1[-1], '맨 뒤에서 두 번째 값:', array1[-2])\n", + "array1[0] = 9\n", + "array1[8] = 0\n", + "print('array1:', array1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 273 + }, + "id": "INr8jjuil-H7", + "outputId": "81de29a2-f600-4f8d-df25-0e1eace20ed9" + }, + "execution_count": 8, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'array1' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/957792696.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'맨 뒤의 값:'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'맨 뒤에서 두 번째 값:'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray1\u001b[0m\u001b[0;34m[\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;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0marray1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m9\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0marray1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'array1:'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'array1' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "array1d=np.arange(start=1, stop=10)\n", + "array2d=array1d.reshape(3,3)\n", + "print(array2d)\n", + "\n", + "print('(row=0, col=0) index 가리키는 값:', array2d[0,0])\n", + "print('(row=0, col=1) index 가리키는 값:', array2d[0,1])\n", + "print('(row=1, col=0) index 가리키는 값:', array2d[1,0])\n", + "print('(row=2, col=2) index 가리키는 값:', array2d[2,2])" + ], + "metadata": { + "id": "B3_aLeWXmN_5" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array1=np.arange(start=1, stop=10)\n", + "array3=array1[0:3]\n", + "print(array3)\n", + "print(type(array3))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 255 + }, + "id": "YqrMmeIMmcvv", + "outputId": "91494ac4-b558-40cf-dc11-33d3aba7c4a4" + }, + "execution_count": 9, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/1795288367.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marray1\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstart\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstop\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0marray3\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marray1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray3\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marray3\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[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "array1=np.arange(start=1, stop=10)\n", + "array4=array1[:3]\n", + "print(array4)\n", + "\n", + "array5=array1[3:]\n", + "print(array5)\n", + "\n", + "array6=array1[:]\n", + "print(array6)" + ], + "metadata": { + "id": "cQycwPvFmqTZ" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array1d=np.arange(start=1, stop=10)\n", + "array2d=array1d.reshape(3,3)\n", + "print('array2d:\\n', array2d)\n", + "\n", + "print('array2d[0:2, 0:2] \\n', array2d[0:2, 0:2])\n", + "print('array2d[1:3, 0:3] \\n', array2d[1:3, 0:3])\n", + "print('array2d[1:3, :] \\n', array2d[1:3, :])\n", + "print('array2d[:, :] \\n', array2d[:, :])\n", + "print('array2d[:2, 1:] \\n', array2d[:2, 1:])\n", + "print('array2d[:2, 0] \\n', array2d[:2, 0])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 291 + }, + "id": "pJcONG5em0qH", + "outputId": "e7ec254c-8a8a-47e6-f364-8c47d0a57812" + }, + "execution_count": 10, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/1984874030.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marray1d\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstart\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstop\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0marray2d\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marray1d\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'array2d:\\n'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray2d\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[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'array2d[0:2, 0:2] \\n'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray2d\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\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;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "print(array2d[0])\n", + "print(array2d[1])\n", + "print('array2d[0] shape:', array2d[0].shape, 'array2d[1] shape:', array2d[1].shape)" + ], + "metadata": { + "id": "HrRnb_6UnVah" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "array1d = np.arange(start=1, stop=10)\n", + "array2d= array1d.reshape(3,3)\n", + "\n", + "array3=array2d[[0,1], 2]\n", + "print('array2d[[0,1],2]=>', array3.tolist())\n", + "\n", + "array4=array2d[[0,1], 0:2]\n", + "print('array2d[[0,1], 0:2]=>', array4.tolist())\n", + "\n", + "array5=array2d[[0,1]]\n", + "print('array2d[[0,1]]=>',array5.tolist())" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 309 + }, + "id": "LPuehcrRndaW", + "outputId": "eb43f8b0-e066-4615-fb2c-7981afe3c9b6" + }, + "execution_count": 11, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/3184845039.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marray1d\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstart\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstop\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0marray2d\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0marray1d\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0marray3\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marray2d\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\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[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'array2d[[0,1],2]=>'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray3\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtolist\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[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "array1d=np.arange(start=1, stop=10)\n", + "# []안에 array1d>5 Boolean indexing을 적용\n", + "array3=array1d[array1d>5]\n", + "print('array1d>5 불린 인덱싱 결과 값:', array3)\n", + "array1d>5" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 309 + }, + "id": "JJQOZzHlox1a", + "outputId": "e7a7a135-152b-48db-f373-40460c4fe729" + }, + "execution_count": 12, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/3392735504.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marray1d\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstart\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstop\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;31m# []안에 array1d>5 Boolean indexing을 적용\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0marray3\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marray1d\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0marray1d\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;36m5\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[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'array1d>5 불린 인덱싱 결과 값:'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0marray1d\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "boolean_indexes=np.array([False, False, False, False, False, True, True, True, True])\n", + "array3=array1d[boolean_indexes]\n", + "print('불린 인덱스로 필터링 결과:', array3)" + ], + "metadata": { + "id": "H1eqDXW4pTtS" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "indexes=np.array([5,6,7,8])\n", + "array4=array1d[indexes]\n", + "print('일반 인덱스로 필터링 결과:', array4)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 255 + }, + "id": "Sduh0ic5pb5X", + "outputId": "44a7a6bf-2b72-4fcc-bbc8-57c2d4768990" + }, + "execution_count": 13, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/2691075439.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mindexes\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;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m8\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[0m\u001b[1;32m 2\u001b[0m \u001b[0marray4\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0marray1d\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindexes\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'일반 인덱스로 필터링 결과:'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marray4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "org_array=np.array([3,1,9,5])\n", + "print('원본 행렬:', org_array)\n", + "sort_array1=np.sort(org_array)\n", + "print('np.sort() 호출 후 반환된 정렬 행렬:', sort_array1)\n", + "print('np.sort() 호출 후 원본 행렬:', org_array)\n", + "sort_array2=org_array.sort()\n", + "print('org_array.sort() 호출 후 반환된 행렬:', sort_array2)\n", + "print('org_array.sort() 호출 후 원본 행렬:', org_array)" + ], + "metadata": { + "id": "LRKFVbQPprHr" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "sort_array1_desc=np.sort(org_array)[::-1]\n", + "print('내림차순으로 정렬:', sort_array1_desc)\n", + "array2d=np.array([[8,12],\n", + " [7,1]])\n", + "\n", + "sort_array2d_axis0=np.sort(array2d, axis=0)\n", + "print('로우 방향으로 정렬:\\n', sort_array2d_axis0)\n", + "\n", + "sort_array2d_axis1=np.sort(array2d, axis=1)\n", + "print('칼럼 방향으로 정렬:\\n', sort_array2d_axis1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 309 + }, + "id": "e1Jn6tkLpwrP", + "outputId": "a1758918-2aa6-4ee5-d310-d4ca61a115a3" + }, + "execution_count": 14, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/1926666348.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msort_array1_desc\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0morg_array\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[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'내림차순으로 정렬:'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msort_array1_desc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m array2d=np.array([[8,12],\n\u001b[1;32m 4\u001b[0m [7,1]])\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "org_array=np.array([3,1,9,5])\n", + "sort_indices=np.argsort(org_array)\n", + "print(type(sort_indices))\n", + "print('행렬 정렬 시 원본 행렬의 인덱스:', sort_indices)" + ], + "metadata": { + "id": "Ay788vYqrr6A" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "org_array=np.array([3,1,9,5])\n", + "sort_indices_desc=np.argsort(org_array)[::-1]\n", + "print('행렬 내림차순 정렬 시 원본 행렬의 인덱스:', sort_indices_desc)" + ], + "metadata": { + "id": "ZPpNjlN7r-np" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "import numpy as np\n", + "import pandas as pd\n", + "\n", + "col_name1=['col1']\n", + "list1=[1,2,3]\n", + "array1=np.array(list1)\n", + "df_list1=pd.DataFrame(list1, columns=col_name1)\n", + "print('1차원 리스트로 만든 DataFrame:\\n', df_list1)\n", + "df_array1=pd.DataFrame(array1, columns=col_name1)\n", + "print('1차원 ndarray로 만든 DataFrame:\\n', df_array1)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "szWsbzs8uA_M", + "outputId": "c9403c2d-ecfd-446a-f0cc-a004342fe550" + }, + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "1차원 리스트로 만든 DataFrame:\n", + " col1\n", + "0 1\n", + "1 2\n", + "2 3\n", + "1차원 ndarray로 만든 DataFrame:\n", + " col1\n", + "0 1\n", + "1 2\n", + "2 3\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "import numpy as np\n", + "import pandas as pd\n", + "\n", + "#3개의 칼럼명이 필요함.\n", + "col_name2=['col1', 'col2', 'col3']\n", + "\n", + "list2=[[1,2,3],\n", + " [11,12,13]]\n", + "array2=np.array(list2)\n", + "df_list2=pd.DataFrame(list2, columns=col_name2)\n", + "print('2차원 리스트로 만든 DataFrame:\\n', df_list2)\n", + "df_array2=pd.DataFrame(array2, columns=col_name2)\n", + "print('2차원 ndarray로 만든 DataFrame:\\n', df_array2)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 309 + }, + "id": "sLbpjwmRvOlZ", + "outputId": "355052b5-b192-4756-cfe5-ee8e181164a3" + }, + "execution_count": 1, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'np' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_7573/85147693.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m list2=[[1,2,3],\n\u001b[1;32m 5\u001b[0m [11,12,13]]\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0marray2\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[0mlist2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mdf_list2\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcol_name2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'2차원 리스트로 만든 DataFrame:\\n'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdf_list2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "dict={'col1':[1,11 ], 'col2':[2,22], 'col3':[3,33]}\n", + "df_dict=pd.DataFrame(dict)\n", + "print('딕셔너리로 만든 DataFrame:\\n', df_dict)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8bLJgjr4v_dW", + "outputId": "2aa896c1-4a32-467d-e752-22338e8e55fc" + }, + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "딕셔너리로 만든 DataFrame:\n", + " col1 col2 col3\n", + "0 1 2 3\n", + "1 11 22 33\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "array3=df_dict.values\n", + "print('df_dict.values 타입:', type(array3), 'df_dict.values shape:', array3.shape)\n", + "print(array3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "RVgXan0NwEY4", + "outputId": "b71bd044-f3a0-4f60-8b45-55f535e0e0f8" + }, + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "df_dict.values 타입: df_dict.values shape: (2, 3)\n", + "[[ 1 2 3]\n", + " [11 22 33]]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "list3=df_dict.values.tolist()\n", + "print('df_dict.values.tolist() 타입:', type(list3))\n", + "print(list3)\n", + "\n", + "dict3=df_dict.to_dict('list')\n", + "print('\\n df_dict.to_dict() 타입:', type(dict3))\n", + "print(dict3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "3PFSfqs4wZLo", + "outputId": "e6223252-3c9d-4beb-d2bc-3b9f1e6f592d" + }, + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "df_dict.values.tolist() 타입: \n", + "[[1, 2, 3], [11, 22, 33]]\n", + "\n", + " df_dict.to_dict() 타입: \n", + "{'col1': [1, 11], 'col2': [2, 22], 'col3': [3, 33]}\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "pd.read_csv('titanic_train.csv')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 929 + }, + "id": "yqWH1g5dy45p", + "outputId": "c72af004-aff4-4828-8fc1-d9d501dedff2" + }, + "execution_count": 29, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " PassengerId Survived Pclass \\\n", + "0 1 0 3 \n", + "1 2 1 1 \n", + "2 3 1 3 \n", + "3 4 1 1 \n", + "4 5 0 3 \n", + ".. ... ... ... \n", + "886 887 0 2 \n", + "887 888 1 1 \n", + "888 889 0 3 \n", + "889 890 1 1 \n", + "890 891 0 3 \n", + "\n", + " Name Sex Age SibSp \\\n", + "0 Braund, Mr. Owen Harris male 22.0 1 \n", + "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n", + "2 Heikkinen, Miss. Laina female 26.0 0 \n", + "3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 \n", + "4 Allen, Mr. William Henry male 35.0 0 \n", + ".. ... ... ... ... \n", + "886 Montvila, Rev. Juozas male 27.0 0 \n", + "887 Graham, Miss. Margaret Edith female 19.0 0 \n", + "888 Johnston, Miss. Catherine Helen \"Carrie\" female NaN 1 \n", + "889 Behr, Mr. Karl Howell male 26.0 0 \n", + "890 Dooley, Mr. Patrick male 32.0 0 \n", + "\n", + " Parch Ticket Fare Cabin Embarked \n", + "0 0 A/5 21171 7.2500 NaN S \n", + "1 0 PC 17599 71.2833 C85 C \n", + "2 0 STON/O2. 3101282 7.9250 NaN S \n", + "3 0 113803 53.1000 C123 S \n", + "4 0 373450 8.0500 NaN S \n", + ".. ... ... ... ... ... \n", + "886 0 211536 13.0000 NaN S \n", + "887 0 112053 30.0000 B42 S \n", + "888 2 W./C. 6607 23.4500 NaN S \n", + "889 0 111369 30.0000 C148 C \n", + "890 0 370376 7.7500 NaN Q \n", + "\n", + "[891 rows x 12 columns]" + ], + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS
3411Futrelle, Mrs. Jacques Heath (Lily May Peel)female35.01011380353.1000C123S
4503Allen, Mr. William Henrymale35.0003734508.0500NaNS
.......................................
88688702Montvila, Rev. Juozasmale27.00021153613.0000NaNS
88788811Graham, Miss. Margaret Edithfemale19.00011205330.0000B42S
88888903Johnston, Miss. Catherine Helen \"Carrie\"femaleNaN12W./C. 660723.4500NaNS
88989011Behr, Mr. Karl Howellmale26.00011136930.0000C148C
89089103Dooley, Mr. Patrickmale32.0003703767.7500NaNQ
\n", + "

891 rows × 12 columns

\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\": \"pd\",\n \"rows\": 891,\n \"fields\": [\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 257,\n \"min\": 1,\n \"max\": 891,\n \"num_unique_values\": 891,\n \"samples\": [\n 710,\n 440,\n 841\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Survived\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 1,\n \"num_unique_values\": 2,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 3,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 891,\n \"samples\": [\n \"Moubarek, Master. Halim Gonios (\\\"William George\\\")\",\n \"Kvillner, Mr. Johan Henrik Johannesson\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Sex\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"female\",\n \"male\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.526497332334044,\n \"min\": 0.42,\n \"max\": 80.0,\n \"num_unique_values\": 88,\n \"samples\": [\n 0.75,\n 22.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"SibSp\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 0,\n \"max\": 8,\n \"num_unique_values\": 7,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Parch\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 6,\n \"num_unique_values\": 7,\n \"samples\": [\n 0,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Ticket\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 681,\n \"samples\": [\n \"11774\",\n \"248740\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Fare\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 49.693428597180905,\n \"min\": 0.0,\n \"max\": 512.3292,\n \"num_unique_values\": 248,\n \"samples\": [\n 11.2417,\n 51.8625\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Cabin\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 147,\n \"samples\": [\n \"D45\",\n \"B49\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Embarked\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"S\",\n \"C\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 29 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_df = pd.read_csv('titanic_train.csv')\n", + "titanic_df['Age_0']=0\n", + "titanic_df.head(3)\n", + "\n", + "titanic_df['Age_by_10']=titanic_df['Age']*10\n", + "titanic_df['Family_No']=titanic_df['SibSp']+titanic_df['Parch']+1\n", + "titanic_df.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 319 + }, + "id": "KPap6zs9wLGJ", + "outputId": "e2efb12c-5d05-483b-d7f1-6392c711f7d6" + }, + "execution_count": 31, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " PassengerId Survived Pclass \\\n", + "0 1 0 3 \n", + "1 2 1 1 \n", + "2 3 1 3 \n", + "\n", + " Name Sex Age SibSp \\\n", + "0 Braund, Mr. Owen Harris male 22.0 1 \n", + "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n", + "2 Heikkinen, Miss. Laina female 26.0 0 \n", + "\n", + " Parch Ticket Fare Cabin Embarked Age_0 Age_by_10 \\\n", + "0 0 A/5 21171 7.2500 NaN S 0 220.0 \n", + "1 0 PC 17599 71.2833 C85 C 0 380.0 \n", + "2 0 STON/O2. 3101282 7.9250 NaN S 0 260.0 \n", + "\n", + " Family_No \n", + "0 2 \n", + "1 2 \n", + "2 1 " + ], + "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", + "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarkedAge_0Age_by_10Family_No
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS0220.02
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C0380.02
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS0260.01
\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": "titanic_df", + "summary": "{\n \"name\": \"titanic_df\",\n \"rows\": 891,\n \"fields\": [\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 257,\n \"min\": 1,\n \"max\": 891,\n \"num_unique_values\": 891,\n \"samples\": [\n 710,\n 440,\n 841\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Survived\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 1,\n \"num_unique_values\": 2,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 3,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 891,\n \"samples\": [\n \"Moubarek, Master. Halim Gonios (\\\"William George\\\")\",\n \"Kvillner, Mr. Johan Henrik Johannesson\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Sex\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"female\",\n \"male\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.526497332334044,\n \"min\": 0.42,\n \"max\": 80.0,\n \"num_unique_values\": 88,\n \"samples\": [\n 0.75,\n 22.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"SibSp\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 0,\n \"max\": 8,\n \"num_unique_values\": 7,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Parch\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 6,\n \"num_unique_values\": 7,\n \"samples\": [\n 0,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Ticket\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 681,\n \"samples\": [\n \"11774\",\n \"248740\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Fare\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 49.693428597180905,\n \"min\": 0.0,\n \"max\": 512.3292,\n \"num_unique_values\": 248,\n \"samples\": [\n 11.2417,\n 51.8625\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Cabin\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 147,\n \"samples\": [\n \"D45\",\n \"B49\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Embarked\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"S\",\n \"C\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age_0\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 0,\n \"num_unique_values\": 1,\n \"samples\": [\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age_by_10\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 145.2649733233403,\n \"min\": 4.2,\n \"max\": 800.0,\n \"num_unique_values\": 88,\n \"samples\": [\n 7.5\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Family_No\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 1,\n \"max\": 11,\n \"num_unique_values\": 9,\n \"samples\": [\n 8\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 31 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_drop_df=titanic_df.drop('Age_0', axis=1)\n", + "titanic_drop_df.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 319 + }, + "id": "muv-dsUhweUM", + "outputId": "dbc14c57-dc71-4976-991e-92c3ac68e8b3" + }, + "execution_count": 32, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " PassengerId Survived Pclass \\\n", + "0 1 0 3 \n", + "1 2 1 1 \n", + "2 3 1 3 \n", + "\n", + " Name Sex Age SibSp \\\n", + "0 Braund, Mr. Owen Harris male 22.0 1 \n", + "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n", + "2 Heikkinen, Miss. Laina female 26.0 0 \n", + "\n", + " Parch Ticket Fare Cabin Embarked Age_by_10 Family_No \n", + "0 0 A/5 21171 7.2500 NaN S 220.0 2 \n", + "1 0 PC 17599 71.2833 C85 C 380.0 2 \n", + "2 0 STON/O2. 3101282 7.9250 NaN S 260.0 1 " + ], + "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", + "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarkedAge_by_10Family_No
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS220.02
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C380.02
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS260.01
\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": "titanic_drop_df", + "summary": "{\n \"name\": \"titanic_drop_df\",\n \"rows\": 891,\n \"fields\": [\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 257,\n \"min\": 1,\n \"max\": 891,\n \"num_unique_values\": 891,\n \"samples\": [\n 710,\n 440,\n 841\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Survived\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 1,\n \"num_unique_values\": 2,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 3,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 891,\n \"samples\": [\n \"Moubarek, Master. Halim Gonios (\\\"William George\\\")\",\n \"Kvillner, Mr. Johan Henrik Johannesson\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Sex\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"female\",\n \"male\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.526497332334044,\n \"min\": 0.42,\n \"max\": 80.0,\n \"num_unique_values\": 88,\n \"samples\": [\n 0.75,\n 22.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"SibSp\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 0,\n \"max\": 8,\n \"num_unique_values\": 7,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Parch\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 6,\n \"num_unique_values\": 7,\n \"samples\": [\n 0,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Ticket\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 681,\n \"samples\": [\n \"11774\",\n \"248740\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Fare\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 49.693428597180905,\n \"min\": 0.0,\n \"max\": 512.3292,\n \"num_unique_values\": 248,\n \"samples\": [\n 11.2417,\n 51.8625\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Cabin\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 147,\n \"samples\": [\n \"D45\",\n \"B49\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Embarked\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"S\",\n \"C\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age_by_10\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 145.2649733233403,\n \"min\": 4.2,\n \"max\": 800.0,\n \"num_unique_values\": 88,\n \"samples\": [\n 7.5,\n 220.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Family_No\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 1,\n \"max\": 11,\n \"num_unique_values\": 9,\n \"samples\": [\n 8,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 32 + } + ] + }, + { + "cell_type": "code", + "source": [ + "drop_result=titanic_df.drop(['Age_0', 'Age_by_10', 'Family_No'], axis=1, inplace=True)\n", + "print('inplace=True로 drop 후 반환된 값:', drop_result)\n", + "titanic_df.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 338 + }, + "id": "O7q-8oYgzZm_", + "outputId": "10c35c13-db54-4b2c-c3a6-576c85f83ab0" + }, + "execution_count": 33, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "inplace=True로 drop 후 반환된 값: None\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " PassengerId Survived Pclass \\\n", + "0 1 0 3 \n", + "1 2 1 1 \n", + "2 3 1 3 \n", + "\n", + " Name Sex Age SibSp \\\n", + "0 Braund, Mr. Owen Harris male 22.0 1 \n", + "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n", + "2 Heikkinen, Miss. Laina female 26.0 0 \n", + "\n", + " Parch Ticket Fare Cabin Embarked \n", + "0 0 A/5 21171 7.2500 NaN S \n", + "1 0 PC 17599 71.2833 C85 C \n", + "2 0 STON/O2. 3101282 7.9250 NaN S " + ], + "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", + "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS
\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": "titanic_df", + "summary": "{\n \"name\": \"titanic_df\",\n \"rows\": 891,\n \"fields\": [\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 257,\n \"min\": 1,\n \"max\": 891,\n \"num_unique_values\": 891,\n \"samples\": [\n 710,\n 440,\n 841\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Survived\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 1,\n \"num_unique_values\": 2,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 3,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 891,\n \"samples\": [\n \"Moubarek, Master. Halim Gonios (\\\"William George\\\")\",\n \"Kvillner, Mr. Johan Henrik Johannesson\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Sex\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"female\",\n \"male\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.526497332334044,\n \"min\": 0.42,\n \"max\": 80.0,\n \"num_unique_values\": 88,\n \"samples\": [\n 0.75,\n 22.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"SibSp\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 0,\n \"max\": 8,\n \"num_unique_values\": 7,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Parch\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 6,\n \"num_unique_values\": 7,\n \"samples\": [\n 0,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Ticket\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 681,\n \"samples\": [\n \"11774\",\n \"248740\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Fare\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 49.693428597180905,\n \"min\": 0.0,\n \"max\": 512.3292,\n \"num_unique_values\": 248,\n \"samples\": [\n 11.2417,\n 51.8625\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Cabin\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 147,\n \"samples\": [\n \"D45\",\n \"B49\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Embarked\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"S\",\n \"C\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 33 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_df=pd.read_csv('titanic_train.csv')\n", + "indices=titanic_df[titanic_df['Age']>60].index\n", + "print('열향식 조건식 인덱스 객체의 전체 데이터:', indices)\n", + "print(titanic_df['Name'].head(3))\n", + "\n", + "titanic_reset_df=titanic_df.reset_index(inplace=False)\n", + "titanic_reset_df.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 446 + }, + "id": "EgGjx2ZF0JlL", + "outputId": "eee679b4-0a80-4ff4-c8a1-a55c842bfb37" + }, + "execution_count": 37, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "열향식 조건식 인덱스 객체의 전체 데이터: Index([ 33, 54, 96, 116, 170, 252, 275, 280, 326, 438, 456, 483, 493, 545,\n", + " 555, 570, 625, 630, 672, 745, 829, 851],\n", + " dtype='int64')\n", + "0 Braund, Mr. Owen Harris\n", + "1 Cumings, Mrs. John Bradley (Florence Briggs Th...\n", + "2 Heikkinen, Miss. Laina\n", + "Name: Name, dtype: object\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " index PassengerId Survived Pclass \\\n", + "0 0 1 0 3 \n", + "1 1 2 1 1 \n", + "2 2 3 1 3 \n", + "\n", + " Name Sex Age SibSp \\\n", + "0 Braund, Mr. Owen Harris male 22.0 1 \n", + "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n", + "2 Heikkinen, Miss. Laina female 26.0 0 \n", + "\n", + " Parch Ticket Fare Cabin Embarked \n", + "0 0 A/5 21171 7.2500 NaN S \n", + "1 0 PC 17599 71.2833 C85 C \n", + "2 0 STON/O2. 3101282 7.9250 NaN S " + ], + "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", + "
indexPassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
00103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS
11211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C
22313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS
\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": "titanic_reset_df", + "summary": "{\n \"name\": \"titanic_reset_df\",\n \"rows\": 891,\n \"fields\": [\n {\n \"column\": \"index\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 257,\n \"min\": 0,\n \"max\": 890,\n \"num_unique_values\": 891,\n \"samples\": [\n 709,\n 439,\n 840\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 257,\n \"min\": 1,\n \"max\": 891,\n \"num_unique_values\": 891,\n \"samples\": [\n 710,\n 440,\n 841\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Survived\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 1,\n \"num_unique_values\": 2,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 3,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 891,\n \"samples\": [\n \"Moubarek, Master. Halim Gonios (\\\"William George\\\")\",\n \"Kvillner, Mr. Johan Henrik Johannesson\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Sex\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"female\",\n \"male\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.526497332334044,\n \"min\": 0.42,\n \"max\": 80.0,\n \"num_unique_values\": 88,\n \"samples\": [\n 0.75,\n 22.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"SibSp\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 0,\n \"max\": 8,\n \"num_unique_values\": 7,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Parch\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 6,\n \"num_unique_values\": 7,\n \"samples\": [\n 0,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Ticket\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 681,\n \"samples\": [\n \"11774\",\n \"248740\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Fare\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 49.693428597180905,\n \"min\": 0.0,\n \"max\": 512.3292,\n \"num_unique_values\": 248,\n \"samples\": [\n 11.2417,\n 51.8625\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Cabin\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 147,\n \"samples\": [\n \"D45\",\n \"B49\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Embarked\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"S\",\n \"C\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 37 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_sorted=titanic_df.sort_values(by=['Name'])\n", + "titanic_sorted.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 319 + }, + "id": "JbqmlkmF0qyo", + "outputId": "cb5d3726-418a-42d3-8dd6-aeda91e9c3c7" + }, + "execution_count": 38, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " PassengerId Survived Pclass Name Sex \\\n", + "845 846 0 3 Abbing, Mr. Anthony male \n", + "746 747 0 3 Abbott, Mr. Rossmore Edward male \n", + "279 280 1 3 Abbott, Mrs. Stanton (Rosa Hunt) female \n", + "\n", + " Age SibSp Parch Ticket Fare Cabin Embarked \n", + "845 42.0 0 0 C.A. 5547 7.55 NaN S \n", + "746 16.0 1 1 C.A. 2673 20.25 NaN S \n", + "279 35.0 1 1 C.A. 2673 20.25 NaN S " + ], + "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", + "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
84584603Abbing, Mr. Anthonymale42.000C.A. 55477.55NaNS
74674703Abbott, Mr. Rossmore Edwardmale16.011C.A. 267320.25NaNS
27928013Abbott, Mrs. Stanton (Rosa Hunt)female35.011C.A. 267320.25NaNS
\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": "titanic_sorted", + "summary": "{\n \"name\": \"titanic_sorted\",\n \"rows\": 891,\n \"fields\": [\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 257,\n \"min\": 1,\n \"max\": 891,\n \"num_unique_values\": 891,\n \"samples\": [\n 518,\n 622,\n 15\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Survived\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 1,\n \"num_unique_values\": 2,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 3,\n 2\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 891,\n \"samples\": [\n \"Ryan, Mr. Patrick\",\n \"Kimball, Mr. Edwin Nelson Jr\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Sex\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"female\",\n \"male\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.526497332334044,\n \"min\": 0.42,\n \"max\": 80.0,\n \"num_unique_values\": 88,\n \"samples\": [\n 36.5,\n 42.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"SibSp\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 0,\n \"max\": 8,\n \"num_unique_values\": 7,\n \"samples\": [\n 0,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Parch\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 6,\n \"num_unique_values\": 7,\n \"samples\": [\n 0,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Ticket\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 681,\n \"samples\": [\n \"SOTON/OQ 3101316\",\n \"2685\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Fare\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 49.693428597180926,\n \"min\": 0.0,\n \"max\": 512.3292,\n \"num_unique_values\": 248,\n \"samples\": [\n 7.8958,\n 9.35\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Cabin\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 147,\n \"samples\": [\n \"A6\",\n \"C23 C25 C27\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Embarked\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"S\",\n \"C\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 38 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_df.count()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 460 + }, + "id": "Fja2-mZR0KRL", + "outputId": "15093ce3-ab21-44c2-d466-a6c99974d408" + }, + "execution_count": 39, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "PassengerId 891\n", + "Survived 891\n", + "Pclass 891\n", + "Name 891\n", + "Sex 891\n", + "Age 714\n", + "SibSp 891\n", + "Parch 891\n", + "Ticket 891\n", + "Fare 891\n", + "Cabin 204\n", + "Embarked 889\n", + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
0
PassengerId891
Survived891
Pclass891
Name891
Sex891
Age714
SibSp891
Parch891
Ticket891
Fare891
Cabin204
Embarked889
\n", + "

" + ] + }, + "metadata": {}, + "execution_count": 39 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_df[['Age', 'Fare']].mean()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 147 + }, + "id": "gPFoygXS008C", + "outputId": "6f3ab176-f073-4af1-a44b-030af9043fc9" + }, + "execution_count": 40, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "Age 29.699118\n", + "Fare 32.204208\n", + "dtype: float64" + ], + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
0
Age29.699118
Fare32.204208
\n", + "

" + ] + }, + "metadata": {}, + "execution_count": 40 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_groupby=titanic_df.groupby(by='Pclass')\n", + "print(type(titanic_groupby))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "CGsR96_708cC", + "outputId": "937aa6a6-6bc2-4e87-d5d2-8223fdb07372" + }, + "execution_count": 41, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_groupby=titanic_df.groupby('Pclass').count()\n", + "titanic_groupby" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 195 + }, + "id": "Cr-7Jgur1AQT", + "outputId": "be8273f3-c764-44af-8dc5-24beac4b91b9" + }, + "execution_count": 42, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " PassengerId Survived Name Sex Age SibSp Parch Ticket Fare \\\n", + "Pclass \n", + "1 216 216 216 216 186 216 216 216 216 \n", + "2 184 184 184 184 173 184 184 184 184 \n", + "3 491 491 491 491 355 491 491 491 491 \n", + "\n", + " Cabin Embarked \n", + "Pclass \n", + "1 176 214 \n", + "2 16 184 \n", + "3 12 491 " + ], + "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", + "
PassengerIdSurvivedNameSexAgeSibSpParchTicketFareCabinEmbarked
Pclass
1216216216216186216216216216176214
218418418418417318418418418416184
349149149149135549149149149112491
\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": "titanic_groupby", + "summary": "{\n \"name\": \"titanic_groupby\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 1,\n 2,\n 3\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 168,\n \"min\": 184,\n \"max\": 491,\n \"num_unique_values\": 3,\n \"samples\": [\n 216,\n 184,\n 491\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Survived\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 168,\n \"min\": 184,\n \"max\": 491,\n \"num_unique_values\": 3,\n \"samples\": [\n 216,\n 184,\n 491\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 168,\n \"min\": 184,\n \"max\": 491,\n \"num_unique_values\": 3,\n \"samples\": [\n 216,\n 184,\n 491\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Sex\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 168,\n \"min\": 184,\n \"max\": 491,\n \"num_unique_values\": 3,\n \"samples\": [\n 216,\n 184,\n 491\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 101,\n \"min\": 173,\n \"max\": 355,\n \"num_unique_values\": 3,\n \"samples\": [\n 186,\n 173,\n 355\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"SibSp\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 168,\n \"min\": 184,\n \"max\": 491,\n \"num_unique_values\": 3,\n \"samples\": [\n 216,\n 184,\n 491\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Parch\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 168,\n \"min\": 184,\n \"max\": 491,\n \"num_unique_values\": 3,\n \"samples\": [\n 216,\n 184,\n 491\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Ticket\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 168,\n \"min\": 184,\n \"max\": 491,\n \"num_unique_values\": 3,\n \"samples\": [\n 216,\n 184,\n 491\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Fare\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 168,\n \"min\": 184,\n \"max\": 491,\n \"num_unique_values\": 3,\n \"samples\": [\n 216,\n 184,\n 491\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Cabin\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 93,\n \"min\": 12,\n \"max\": 176,\n \"num_unique_values\": 3,\n \"samples\": [\n 176,\n 16,\n 12\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Embarked\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 169,\n \"min\": 184,\n \"max\": 491,\n \"num_unique_values\": 3,\n \"samples\": [\n 214,\n 184,\n 491\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 42 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_groupby=titanic_df.groupby('Pclass')[['PassengerId', 'Survived']].count()\n", + "titanic_groupby" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 175 + }, + "id": "7keFaiWA1D9_", + "outputId": "89837525-cb18-4970-8f65-fd840287b81f" + }, + "execution_count": 43, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " PassengerId Survived\n", + "Pclass \n", + "1 216 216\n", + "2 184 184\n", + "3 491 491" + ], + "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", + "
PassengerIdSurvived
Pclass
1216216
2184184
3491491
\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": "titanic_groupby", + "summary": "{\n \"name\": \"titanic_groupby\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 1,\n 2,\n 3\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 168,\n \"min\": 184,\n \"max\": 491,\n \"num_unique_values\": 3,\n \"samples\": [\n 216,\n 184,\n 491\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Survived\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 168,\n \"min\": 184,\n \"max\": 491,\n \"num_unique_values\": 3,\n \"samples\": [\n 216,\n 184,\n 491\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 43 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_df.groupby('Pclass')['Age'].agg([max,min])" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 267 + }, + "id": "nRPd5nwK1HKY", + "outputId": "9b4b141f-f31c-49e2-9b8a-adf5a6bdab9f" + }, + "execution_count": 44, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "/tmp/ipykernel_7573/414103430.py:1: FutureWarning: The provided callable is currently using SeriesGroupBy.max. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"max\" instead.\n", + " titanic_df.groupby('Pclass')['Age'].agg([max,min])\n", + "/tmp/ipykernel_7573/414103430.py:1: FutureWarning: The provided callable is currently using SeriesGroupBy.min. In a future version of pandas, the provided callable will be used directly. To keep current behavior pass the string \"min\" instead.\n", + " titanic_df.groupby('Pclass')['Age'].agg([max,min])\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " max min\n", + "Pclass \n", + "1 80.0 0.92\n", + "2 70.0 0.67\n", + "3 74.0 0.42" + ], + "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", + "
maxmin
Pclass
180.00.92
270.00.67
374.00.42
\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\": \"titanic_df\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 1,\n 2,\n 3\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"max\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 5.033222956847166,\n \"min\": 70.0,\n \"max\": 80.0,\n \"num_unique_values\": 3,\n \"samples\": [\n 80.0,\n 70.0,\n 74.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"min\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0.25,\n \"min\": 0.42,\n \"max\": 0.92,\n \"num_unique_values\": 3,\n \"samples\": [\n 0.92,\n 0.67,\n 0.42\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 44 + } + ] + }, + { + "cell_type": "code", + "source": [ + "agg_format={'Age':'max', 'SibSp':'sum', 'Fare':'mean'}\n", + "titanic_df.groupby('Pclass').agg(agg_format)" + ], + "metadata": { + "id": "R2__toP61UMD" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "titanic_df.isna().head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 163 + }, + "id": "b3UuYorX1Xss", + "outputId": "58ec0a84-f9f3-4215-f5a1-1c4abc901308" + }, + "execution_count": 45, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket \\\n", + "0 False False False False False False False False False \n", + "1 False False False False False False False False False \n", + "2 False False False False False False False False False \n", + "\n", + " Fare Cabin Embarked \n", + "0 False True False \n", + "1 False False False \n", + "2 False True False " + ], + "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", + "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
0FalseFalseFalseFalseFalseFalseFalseFalseFalseFalseTrueFalse
1FalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalseFalse
2FalseFalseFalseFalseFalseFalseFalseFalseFalseFalseTrueFalse
\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\": \"titanic_df\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 1,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Survived\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 1,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 1,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 1,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Sex\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 1,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 1,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"SibSp\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 1,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Parch\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 1,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Ticket\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 1,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Fare\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 1,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Cabin\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 2,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Embarked\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 1,\n \"samples\": [\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 45 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_df.isna().sum()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 460 + }, + "id": "2ABAipCF1cD-", + "outputId": "28170cde-35f5-4349-ea35-41b1a5c21fbf" + }, + "execution_count": 46, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "PassengerId 0\n", + "Survived 0\n", + "Pclass 0\n", + "Name 0\n", + "Sex 0\n", + "Age 177\n", + "SibSp 0\n", + "Parch 0\n", + "Ticket 0\n", + "Fare 0\n", + "Cabin 687\n", + "Embarked 2\n", + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
0
PassengerId0
Survived0
Pclass0
Name0
Sex0
Age177
SibSp0
Parch0
Ticket0
Fare0
Cabin687
Embarked2
\n", + "

" + ] + }, + "metadata": {}, + "execution_count": 46 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_df['Cabin']=titanic_df['Cabin'].fillna('C000')\n", + "titanic_df.head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 319 + }, + "id": "bhZyOpTy1fsX", + "outputId": "ad8a109b-7147-4991-c0ba-bb887ae5bb8b" + }, + "execution_count": 47, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " PassengerId Survived Pclass \\\n", + "0 1 0 3 \n", + "1 2 1 1 \n", + "2 3 1 3 \n", + "\n", + " Name Sex Age SibSp \\\n", + "0 Braund, Mr. Owen Harris male 22.0 1 \n", + "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n", + "2 Heikkinen, Miss. Laina female 26.0 0 \n", + "\n", + " Parch Ticket Fare Cabin Embarked \n", + "0 0 A/5 21171 7.2500 C000 S \n", + "1 0 PC 17599 71.2833 C85 C \n", + "2 0 STON/O2. 3101282 7.9250 C000 S " + ], + "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", + "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500C000S
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250C000S
\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": "titanic_df", + "summary": "{\n \"name\": \"titanic_df\",\n \"rows\": 891,\n \"fields\": [\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 257,\n \"min\": 1,\n \"max\": 891,\n \"num_unique_values\": 891,\n \"samples\": [\n 710,\n 440,\n 841\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Survived\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 1,\n \"num_unique_values\": 2,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Pclass\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 3,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 891,\n \"samples\": [\n \"Moubarek, Master. Halim Gonios (\\\"William George\\\")\",\n \"Kvillner, Mr. Johan Henrik Johannesson\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Sex\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"female\",\n \"male\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.526497332334044,\n \"min\": 0.42,\n \"max\": 80.0,\n \"num_unique_values\": 88,\n \"samples\": [\n 0.75,\n 22.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"SibSp\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1,\n \"min\": 0,\n \"max\": 8,\n \"num_unique_values\": 7,\n \"samples\": [\n 1,\n 0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Parch\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 0,\n \"max\": 6,\n \"num_unique_values\": 7,\n \"samples\": [\n 0,\n 1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Ticket\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 681,\n \"samples\": [\n \"11774\",\n \"248740\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Fare\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 49.693428597180905,\n \"min\": 0.0,\n \"max\": 512.3292,\n \"num_unique_values\": 248,\n \"samples\": [\n 11.2417,\n 51.8625\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Cabin\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 148,\n \"samples\": [\n \"B101\",\n \"A19\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Embarked\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"S\",\n \"C\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 47 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_df['Age']=titanic_df['Age'].fillna(titanic_df['Age'].mean())\n", + "titanic_df['Embarked']=titanic_df['Embarked'].fillna('S')\n", + "titanic_df.isna().sum()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 460 + }, + "id": "XctYM-6i1rfB", + "outputId": "aff9b12d-0edf-40cb-abdb-14a7a6949cd1" + }, + "execution_count": 48, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "PassengerId 0\n", + "Survived 0\n", + "Pclass 0\n", + "Name 0\n", + "Sex 0\n", + "Age 0\n", + "SibSp 0\n", + "Parch 0\n", + "Ticket 0\n", + "Fare 0\n", + "Cabin 0\n", + "Embarked 0\n", + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
0
PassengerId0
Survived0
Pclass0
Name0
Sex0
Age0
SibSp0
Parch0
Ticket0
Fare0
Cabin0
Embarked0
\n", + "

" + ] + }, + "metadata": {}, + "execution_count": 48 + } + ] + }, + { + "cell_type": "code", + "source": [ + "def get_square(a):\n", + " return a**2\n", + "\n", + "print('3의 제곱은:', get_square(3))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dtwMqtXv3jpz", + "outputId": "8933217c-485e-44e1-ff6f-5964438ad550" + }, + "execution_count": 50, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "3의 제곱은: 9\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "lambda_square=lambda x : x**2\n", + "print('3의 제곱은:', lambda_square(3))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "LvG9x3gK3icd", + "outputId": "5a6eb431-2318-48d6-ac5e-37edcfe48efe" + }, + "execution_count": 51, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "3의 제곱은: 9\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "a=[1,2,3]\n", + "squares=map(lambda x: x**2, a)\n", + "list(squares)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Kh5h_RVX4CkV", + "outputId": "80e444a7-96ae-41fa-c2ad-57ecf0ea4795" + }, + "execution_count": 52, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1, 4, 9]" + ] + }, + "metadata": {}, + "execution_count": 52 + } + ] + }, + { + "cell_type": "code", + "source": [ + "titanic_df['Name_len']=titanic_df['Name'].apply(lambda x:len(x))\n", + "titanic_df[['Name', 'Name_len']].head(3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 195 + }, + "id": "Nl0bwfei4JZd", + "outputId": "1e740907-9c2d-4966-c3fc-684e0977cb06" + }, + "execution_count": 54, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " Name Name_len\n", + "0 Braund, Mr. Owen Harris 23\n", + "1 Cumings, Mrs. John Bradley (Florence Briggs Th... 51\n", + "2 Heikkinen, Miss. Laina 22" + ], + "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", + "
NameName_len
0Braund, Mr. Owen Harris23
1Cumings, Mrs. John Bradley (Florence Briggs Th...51
2Heikkinen, Miss. Laina22
\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\": \"titanic_df[['Name', 'Name_len']]\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Braund, Mr. Owen Harris\",\n \"Cumings, Mrs. John Bradley (Florence Briggs Thayer)\",\n \"Heikkinen, Miss. Laina\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name_len\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 16,\n \"min\": 22,\n \"max\": 51,\n \"num_unique_values\": 3,\n \"samples\": [\n 23,\n 51,\n 22\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 54 + } + ] + } + ] +} \ No newline at end of file diff --git "a/Week1_\354\230\210\354\212\265\352\263\274\354\240\234_\354\265\234\354\234\244\354\204\234.pdf" "b/Week1_\354\230\210\354\212\265\352\263\274\354\240\234_\354\265\234\354\234\244\354\204\234.pdf" new file mode 100644 index 0000000..c49a4b1 Binary files /dev/null and "b/Week1_\354\230\210\354\212\265\352\263\274\354\240\234_\354\265\234\354\234\244\354\204\234.pdf" differ