{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "B_D-kg_gMPVd" }, "source": [ "# Lecture 1.3: Introduction to Jupyter and Python\n", "\n", "Welcome to Jupyter (IPython Notebooks)! In this lecture you will get familiar with the Jupyter computing environment, and also practice writing some small Python programs." ] }, { "cell_type": "markdown", "metadata": { "id": "fDMp7WkeMPVj" }, "source": [ "# What's Jupyter Notebook?\n", "The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Jupyter Notebook is maintained by the people at [Project Jupyter](https://jupyter.org/).\n", "\n", "Jupyter Notebook is a spin-off project from the IPython project, which used to have an IPython Notebook project itself. The name, Jupyter, comes from the core supported programming languages that it supports: Julia, Python, and R. Jupyter ships with the IPython kernel, which allows you to write your programs in Python, but there are currently over 100 other kernels that we can also use." ] }, { "cell_type": "markdown", "metadata": { "id": "R8NOu9U3MPVk" }, "source": [ "## 1. Installation \n", "The Jupyter Notebook is not included with Python, so you need to install Python. We recommend [Anaconda](https://www.anaconda.com/), which is a free and open-source distribution of the Python and R programming languages for scientific computing (data science, machine learning applications, large-scale data processing, predictive analytics, etc), that aims to simplify package management and deployment.\n", "\n", "The Anaconda provides large selection of packages (Jupyter, Numpy, Panda, Conda, etc) and commercial support. It is an environment manager, which provides the facility to create different python environments, each with their own settings. In addition, Anaconda has its own installer tool called conda that you could use for installing a third-party package.\n", "\n", "Recommanded version: [Anaconda3-4.2.0](https://repo.continuum.io/archive/) \n", "\n", "Enter the terminal and run the command: jupyter notebook\n", "![ipynb](jupyter.png)\n", "## 2. Basics\n", "Notebooks are organized by cells. Each cell can hold either Markdown formatted text or Python code. \n", "- To create a new cell, use the \"Insert\" Menu.\n", "- To cut, copy, delete, or move the cell, use the \"Edit\" Menu\n", "- To choose the cell type using the dropdown menu in the toolbar, or in the \"Cell\" -> \"Cell Type\" menu. \n", "- To \"run\" the cell, use the \"Cell/Run\" menu item, or push the \"play\" button on the toolbar." ] }, { "cell_type": "markdown", "metadata": { "id": "VzbtfbkHMPVl" }, "source": [ "Here are some useful keyboard shortcuts:\n", "- [arrow keys] - move up or down cells\n", "- b - create new cell below current one\n", "- a - create new cell above current one\n", "- dd - delete current cell\n", "- m - change cell to Markdown (default is code)\n", "- [enter] - edit cell\n", "- [ctrl+enter] - render/run the current cell\n", "- [shift+enter] - render/run the current cell, and move to the next cell." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "tc3SRcApMPVm" }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (1868572516.py, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m Cell \u001b[1;32mIn[3], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m **Tips**:\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "**Tips**: \n", "- In the lecture and assignment ipynb files, it is okay to make additional code cells in the file. This will allow you to work incrementally, and avoid re-running time-consuming code blocks.\n", "- This cell is holding Markdown formatted text. Here is a [Markdown cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet).\n", "- use the `--notebook-dir=mydir` option to start the notebook in a particular directory.\n", "- Windows: create a shortcut to run `jupyter-notebook.exe --notebook-dir=%userprofile%`." ] }, { "cell_type": "markdown", "metadata": { "id": "9aq3Ex3pMPVm" }, "source": [ "# What is Python?\n", "- General-purpose high-level programming language\n", "- Design philosophy emphasizes programmer productivity and code readability\n", " - \"executable pseudo-code\"\n", "- Supports multiple programming paradigms\n", " - object-oriented, imperative, functional\n", "- Dynamic typing and automatic memory management" ] }, { "cell_type": "markdown", "metadata": { "id": "X2uo75LAMPVn", "tags": [] }, "source": [ "## 1. Introduction\n", "- Object-oriented: everything is an object\n", "- Clean: usually one way to do something, not a dozen\n", "- Easy-to-learn: learn in 1-2 days\n", "- Easy-to-read\n", "- Powerful: full-fledged programming language\n", "- Useful: Rich expansion package" ] }, { "cell_type": "markdown", "metadata": { "id": "JTp196l-MPVo" }, "source": [ "## 2. Python Basics\n", "- Formatting\n", " - case-sensitive\n", " - statements end in **newline** (not semicolon)\n", " - use semicolon for multiple statements in one line.\n", " - **indentation** for code blocks (after a colon)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 107 }, "executionInfo": { "elapsed": 632, "status": "ok", "timestamp": 1661690096107, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "GfL4uNmAMPVp", "outputId": "a495c986-73fc-4b69-b214-3559ae8e3ea4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n", "Hello\n", "World\n", "Who are you?\n" ] }, { "data": { "text/plain": [ "'D:\\\\研一港城莞\\\\20240913_Yoga\\\\Lecture1.3(without answer)\\\\lecture 1'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(\"Hello\")\n", "print(\"Hello\"); print(\"World\")\n", "name = \"Bob\"\n", "if name == \"George\":\n", " print(\"Hi George\")\n", "else:\n", " print(\"Who are you?\")\n", "\n", "import os \n", "os.getcwd()" ] }, { "cell_type": "markdown", "metadata": { "id": "RgKj3rYLMPVs" }, "source": [ "### 2.1 Identifiers and Variables" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690096758, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "G6DUtj31MPVs", "outputId": "23ed28fb-1d10-4947-ebe5-55800e5bb88a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\n" ] } ], "source": [ "print(\"Hello World!\")" ] }, { "cell_type": "markdown", "metadata": { "id": "UR2utbVkMPVt" }, "source": [ "The notebook remembers the current state of the environment. Thus, variables and functions that are executed will persist. Here is a variable. Run the cell to initialize the variable." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690097472, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "wnXNe7jLMPVt", "outputId": "d26aa57e-90a1-458e-f8ab-4b0774cd5511", "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n", "1000.0\n", "John\n" ] } ], "source": [ "x = 100 # int variable\n", "miles = 1000.0 # float variable\n", "name = \"John\" # string\n", "print(x)\n", "print(miles)\n", "print(name)" ] }, { "cell_type": "markdown", "metadata": { "id": "-4dLHcwsMPVu" }, "source": [ "Now try running the below cell several times. You will see the value of `x` increase each time you run it." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690098286, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "FV7dwk1GMPVu", "outputId": "bf866ae9-5776-41b0-90e3-f1098462e021", "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "101\n" ] } ], "source": [ "x += 1\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": { "id": "2d3UMKyFMPVu" }, "source": [ "To reset the environment, you need to restart the kernel by either: 1) using the \"Kernel/Restart\" menu; or 2) the \"Refresh\" button on the toolbar. \n", "\n", "If you restart the kernel and try to run the above cell, you will get an error because `x` is not defined yet. \n", "\n", "The outputs can also be cleared using \"Cell/All output/Clear\" menu item." ] }, { "cell_type": "markdown", "metadata": { "id": "Kp5xdbPWMPVv" }, "source": [ "### 2.2 List" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690098894, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "s5fl3i2GMPVv", "outputId": "52a0a43e-d5e2-44cd-bcd9-2ea1b79681ea" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['runoob', 786, 2.23, 'john', 70.2]\n", "runoob\n", "[786, 2.23]\n", "[2.23, 'john', 70.2]\n", "[123, 'john', 123, 'john']\n", "['runoob', 786, 2.23, 'john', 70.2, 123, 'john']\n" ] } ], "source": [ " \n", "list_tmp = [ 'runoob', 786 , 2.23, 'john', 70.2 ]\n", "tinylist = [123, 'john']\n", " \n", "print(list_tmp) # output whole list\n", "print(list_tmp[0]) # print first element of list\n", "print(list_tmp[1:3]) # print second to fourth element\n", "print(list_tmp[2:]) # print third to end element\n", "print(tinylist * 2) # print twice \n", "print(list_tmp + tinylist) # print combination of list and tinylist" ] }, { "cell_type": "markdown", "metadata": { "id": "kHqEYkSWMPVv" }, "source": [ "Creating lists of numbers" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690098894, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "miHfDNm7MPVw", "outputId": "61950f72-b5a2-48f6-8805-44fd234b45ab" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4]\n", "[0, 1, 2, 3, 4, 'blah']\n" ] } ], "source": [ "a = list(range(0,5))\n", "print(a)\n", "a.append('blah') # add item to end\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690099605, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "dzyiOjI6MPVw", "outputId": "b6849030-d67f-4c29-924f-bf4dae16d2a5" }, "outputs": [ { "data": { "text/plain": [ "'blah'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.pop() # remove last item and return it" ] }, { "cell_type": "markdown", "metadata": { "id": "agqvsgnOMPVw" }, "source": [ "- insert and delete" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690100266, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "8sI9HC2sMPVx", "outputId": "6fde96c2-7c85-43dc-bc73-25c545f28c19" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[42, 0, 1, 2, 3, 4]\n" ] } ], "source": [ "a.insert(0,42) # insert 42 at index 0\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690100266, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "QzdqNwhEMPVx", "outputId": "88f5f1e8-b009-4147-fba0-f9a92b7fcfe1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[42, 0, 2, 3, 4]\n" ] } ], "source": [ "del a[2] # delete item 2\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": { "id": "eSQVLnrjMPVy" }, "source": [ "### 2.3 Tuples\n", "- Similar to a list\n", " - but immutable (read-only)\n", " - cannot change the contents (like a string constant)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690101582, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "5c1aTyrVMPVy", "outputId": "f152c99e-6b19-4147-9439-179cfe2353ad" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 'three')\n" ] } ], "source": [ "# make some tuples\n", "x = (1,2,'three')\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690103606, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "byG3mCMIMPVy", "outputId": "1a6458d7-d3f2-4ab6-ff20-8cb5b848f14f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4, 5, 6)\n" ] } ], "source": [ "y = 4,5,6 # parentheses not needed!\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 1, "status": "ok", "timestamp": 1661690104296, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "hVab_zmKMPVz", "outputId": "125fce54-6624-4e02-c968-603fea65fee5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1,)\n" ] }, { "data": { "text/plain": [ "tuple" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = (1,) # tuple with 1 element (the trailing comma is required)\n", "print(z)\n", "type(z)" ] }, { "cell_type": "markdown", "metadata": { "id": "3_0SieGkMPVz" }, "source": [ "### 2.4 String methods\n", "- Useful methods" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690105023, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "Yr5AtgzuMPVz", "outputId": "1da44267-cfa1-4afb-fffa-3d7842c1cc52" }, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"112211\".count(\"11\") # 2\n", "\"this.com\".endswith(\".com\") # True\n", "\"wxyz\".startswith(\"wx\") # True\n", "\"abc\".find(\"c\") # finds first: 2\n", "\",\".join(['a', 'b', 'c']) # join list: 'a,b,c'\n", "\"aba\".replace(\"a\", \"d\") # replace all: \"dbd\"\n", "\"a,b,c\".split(',') # make list: ['a', 'b', 'c']\n", "\" abc \".strip() # \"abc\", also rstrip(), lstrip()" ] }, { "cell_type": "markdown", "metadata": { "id": "BrtIrpELMPVz" }, "source": [ "- String formatting: automatically fill in type" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690106406, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "xjIQfJnjMPVz", "outputId": "a8569bd6-d51c-4e6a-b341-e567efd480df" }, "outputs": [ { "data": { "text/plain": [ "'string and 123 and 1.6789'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"{} and {} and {}\".format('string', 123, 1.6789)" ] }, { "cell_type": "markdown", "metadata": { "id": "XsIydp3mMPV0" }, "source": [ "- String formatting: specify type (similar to C)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690107119, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "FTK50im3MPV0", "outputId": "0c3453af-f80a-4400-c087-5df7b1985a4e" }, "outputs": [ { "data": { "text/plain": [ "'0 and 3.000000 and 1.23'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"{:d} and {:f} and {:0.2f}\".format(False, 3, 1.234)" ] }, { "cell_type": "markdown", "metadata": { "id": "I73fJv8QMPV0" }, "source": [ "### 2.5 Dictionaries\n", "- Stores key-value pairs (associative array or hash table)\n", " - key can be a string, number, or tuple" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690108416, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "9QBb1klSMPV0", "outputId": "1f9b8219-ce9b-4a30-d5c8-616f2f516d0a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': 'john', 42: 'sales', ('hello', 'world'): 6734}\n" ] } ], "source": [ "mydict = {'name': 'john', 42: 'sales', ('hello', 'world'): 6734}\n", "print(mydict)" ] }, { "cell_type": "markdown", "metadata": { "id": "P_G7JV3mMPV1" }, "source": [ "- Access" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690108417, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "EJWobM2bMPV1", "outputId": "8f4c152c-5de7-4a09-a3cc-53cd9e4d5bd0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "john\n" ] } ], "source": [ "print(mydict['name']) # get value for key 'name'" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690109164, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "IQzPthslMPV1", "outputId": "e8a24a09-1a22-4dd7-dd14-b7f1fd6e4507" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': 'jon', 42: 'sales', ('hello', 'world'): 6734, 2: 5}\n" ] } ], "source": [ "mydict['name'] = 'jon' # change value for key 'name'\n", "mydict[2] = 5 # insert a new key-value pair 不是在第3个位置前面插入,而是在末尾插入2:5\n", "print(mydict)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690109165, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "mglKfvjAMPV2", "outputId": "63b40e7a-ecdd-4f47-eff5-4c8969eb519f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': 'jon', 42: 'sales', ('hello', 'world'): 6734}\n" ] } ], "source": [ "del mydict[2] # delete entry for key 2 很重要,mydict[2]不是list里第三个位置\n", "print(mydict)" ] }, { "cell_type": "markdown", "metadata": { "id": "dDnY5-usMPV2" }, "source": [ "- Other operations:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 7, "status": "ok", "timestamp": 1661690109924, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "aSlHdxYMMPV2", "outputId": "9dd28616-7725-450c-b352-d8701f235086" }, "outputs": [ { "data": { "text/plain": [ "dict_keys(['name', 42, ('hello', 'world')])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mydict.keys() # iterator of all keys (no random access)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690109924, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "4zvfY3DtMPV2", "outputId": "838cc251-3a52-4c3e-ea24-8845fc5ce49a" }, "outputs": [ { "data": { "text/plain": [ "['name', 42, ('hello', 'world')]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(mydict.keys()) # convert to a list for random access" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690109924, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "2gyU9DrwMPV2", "outputId": "2bcada43-f291-4fa1-c956-1fb6fa7b4b41" }, "outputs": [ { "data": { "text/plain": [ "dict_values(['jon', 'sales', 6734])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mydict.values() # iterator of all values" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690109925, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "_HzFBi4yMPV3", "outputId": "007aec63-2b0d-4fcc-bbf1-6e94ce5f7d44" }, "outputs": [ { "data": { "text/plain": [ "dict_items([('name', 'jon'), (42, 'sales'), (('hello', 'world'), 6734)])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mydict.items() # iterator of tuples (key, value)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 6, "status": "ok", "timestamp": 1661690110689, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "RMt8gqg0MPV3", "outputId": "133cea6c-ab3d-4f77-b866-8674507b7fe6" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'name' in mydict # check the presence of a key " ] }, { "cell_type": "markdown", "metadata": { "id": "uUVJSNv3MPV3" }, "source": [ "### 2.6 Sets\n", "- A set is a collection of unique items" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690110689, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "68LazJDBMPV3", "outputId": "d4678f62-fc8c-4647-8e67-1d29781b4ebb" }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 4, 5}" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=[1, 2, 2, 2, 4, 5, 5]\n", "sA = set(a)\n", "sA" ] }, { "cell_type": "markdown", "metadata": { "id": "NXNlLGC4MPV4" }, "source": [ "Set operations" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 6, "status": "ok", "timestamp": 1661690111480, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "94lm_4ZFMPV4", "outputId": "6f77b5da-ebd0-47a7-8900-edefc1d6b6a1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2}\n" ] } ], "source": [ "sB = {4, 5, 6, 7}\n", "print(sA - sB) # set difference" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 6, "status": "ok", "timestamp": 1661690111481, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "Yrpq8f-CMPV4", "outputId": "9a852fff-ceaf-4d56-9ccf-2511ae8a8f9e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2, 4, 5, 6, 7}\n" ] } ], "source": [ "print (sA | sB) # set union" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690111481, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "QYFuSvhoMPV5", "outputId": "e3056d4e-7a9f-47f5-cef0-2fafa47023d4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{4, 5}\n" ] } ], "source": [ "print (sA & sB) # set intersect" ] }, { "cell_type": "markdown", "metadata": { "id": "MFHyYeE-MPV5" }, "source": [ "### 2.7 List Comprehension\n", "- Build a new list with a \"for\" loop" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690112243, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "Ddqz6G59MPV6", "outputId": "e41f843d-a92a-4ef7-ee75-3891bec2c6b6" }, "outputs": [ { "data": { "text/plain": [ "[4, 8, 8, 8, 16, 20, 20]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myList = [1, 2, 2, 2, 4, 5, 5]\n", "myList4 = [4*item for item in myList] # multiply each item by 4\n", "myList4" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690112243, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "XQpcW5SaMPV6", "outputId": "cbe7605d-b296-4d96-fc7f-6a74cf92eea3" }, "outputs": [ { "data": { "text/plain": [ "[4, 8, 8, 8, 16, 20, 20]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# equivalent code\n", "myList4=[]\n", "for item in myList:\n", " myList4.append(4*item)\n", "myList4" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690112243, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "ZZjQadOoMPV6", "outputId": "2d64a6f8-9228-4601-c0b9-36d5059704b8" }, "outputs": [ { "data": { "text/plain": [ "[64, 80, 80]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# can also use conditional to select items\n", "[4*item*4 for item in myList if item>2]" ] }, { "cell_type": "markdown", "metadata": { "id": "CVlQkhAkMPV7" }, "source": [ "To import package" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690112991, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "j0uGXC66MPV7" }, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690112991, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "GHiUR5CQMPV8", "outputId": "b4bb9b39-d675-4cb4-9813-280b835391b0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.3862943611198906\n" ] } ], "source": [ "print(math.log(4)) #他这个默认的log底数是e" ] }, { "cell_type": "markdown", "metadata": { "id": "4hhx9rfxMPV8" }, "source": [ "### 2.8 Operators" ] }, { "cell_type": "markdown", "metadata": { "id": "_CsvRldoMPV8" }, "source": [ "operator| descriptor \n", "- | -\n", "\\+ | add \n", "\\- | subtracte\n", "\\* | multiplicate\n", "\\/ | divide\n", "\\% | mode\n", "\\** | power \t" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690115106, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "OCUTnpQYMPV9", "outputId": "769284a5-d876-450e-81b9-050d349bc090" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "c = 31\n", "c = 11\n", "c = 210\n", "c = 2.1\n", "c = 1\n", "c = 8\n" ] } ], "source": [ "a = 21\n", "b = 10\n", "c = 0\n", "\n", "c = a + b\n", "print(\"c = \", c)\n", "\n", "c = a - b\n", "print(\"c = \", c)\n", "\n", "c = a * b\n", "print(\"c = \", c)\n", "\n", "c = a / b\n", "print(\"c = \", c) #这和C语言不一样,C的话恐怕就是2了\n", "\n", "c = a % b\n", "print(\"c = \", c)\n", "\n", "a = 2\n", "b = 3\n", "c = a**b\n", "print(\"c = \", c)" ] }, { "cell_type": "markdown", "metadata": { "id": "cyOAB95sMPV9" }, "source": [ "### 2.9 Loop: while\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "id": "47bjoJ8cMPV9", "outputId": "1d0e8a33-dadc-4f70-b18e-53d58dd1eea3" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3\n", "5\n", "7\n", "9\n" ] } ], "source": [ "a = 1\n", "while a < 10:\n", " print(a)\n", " a += 2" ] }, { "cell_type": "markdown", "metadata": { "id": "di7C19xrMPV9" }, "source": [ "### 2.10 Conditional statement\n", "" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 757, "status": "ok", "timestamp": 1661690229887, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "ASIeJOQTMPV9", "outputId": "caf7f72d-b618-4363-eb8e-bf98c861e081" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "luren\n" ] } ], "source": [ "flag = False\n", "name = 'luren'\n", "if name == 'python': # Determine whether the variable is python\n", " flag = True # Set the flag to true when the condition is true\n", " print('welcome boss')\n", "else:\n", " print(name) # Output variable name when condition is not valid" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690230655, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "g9oSQTW0MPV-", "outputId": "ace0d5c0-606c-4f86-ea62-58a88f1cf29f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n", "undefine\n", "undefine\n" ] } ], "source": [ "num = 9\n", "if num >= 0 and num <= 10: # Determine whether the value is between 0 and 10\n", " print('hello')\n", "# hello\n", "\n", "num = 10\n", "if num < 0 or num > 10: # Determine whether the value is less than 0 or greater than 10\n", " print('hello')\n", "else:\n", " print('undefine')\n", "\n", "num = 8\n", "# Determine whether the value is between 0 and 5 or between 10 and 15\n", "if (num >= 0 and num <= 5) or (num >= 10 and num <= 15): #这里可以观察到:if 本来后面是不用()的,这和C不太一样。\n", " print('hello')\n", "else:\n", " print('undefine')" ] }, { "cell_type": "markdown", "metadata": { "id": "iAiG_eDXMPV-", "tags": [] }, "source": [ "### 2.11 Function" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690231287, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "UMFuYeAbMPV-" }, "outputs": [], "source": [ "def functionname( parameters ):\n", " function_suite\n", " return [expression]\n" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690231287, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "xvEtlcsJMPV-", "outputId": "8cdcb706-6057-4876-d4f1-e485f68be690" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Use user-defined functions\n" ] } ], "source": [ "def printme( str ):\n", " print(str);\n", " return;\n", "\n", "printme(\"Use user-defined functions\");" ] }, { "cell_type": "markdown", "metadata": { "id": "hfRbgeSTMPV_" }, "source": [ "### 2.12 File Input/Ouput\n", "- Write a file" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "executionInfo": { "elapsed": 1179, "status": "ok", "timestamp": 1661690233186, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "Pch3Ax_vMPV_" }, "outputs": [], "source": [ "f = open(\"myfile.txt\", \"w\")\n", "f.write(\"blah\\n\")\n", "f.writelines(['line1\\n', 'line2\\n', 'line3\\n'])\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": { "id": "YLPiwHXMMPV_" }, "source": [ "- Read a whole file" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690233186, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "FdhoxBK3MPV_", "outputId": "17ce463c-3173-43ef-f577-3b8479994b95" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "blah\n", "line1\n", "line2\n", "line3\n", "\n" ] } ], "source": [ "f = open(\"myfile.txt\", \"r\")\n", "contents = f.read() # read the whole file as a string\n", "f.close()\n", "print(contents)" ] }, { "cell_type": "markdown", "metadata": { "id": "LSffK2_oMPWA" }, "source": [ "- Read line or remaining lines" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690233958, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "enL37ay5MPWA", "outputId": "3c7c3184-f7d3-403f-d024-048b9f07cf13" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "blah\n", "\n" ] } ], "source": [ "f = open(\"myfile.txt\", 'r')\n", "print(f.readline()) # read a single line." ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690233958, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "dOYGa4X5MPWA", "outputId": "b32bd596-026c-4277-cac8-6783bab45840" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['line1\\n', 'line2\\n', 'line3\\n']\n" ] } ], "source": [ "print(f.readlines()) # read remaining lines in a list. 这里没有把第一行再打出来了,细节\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": { "id": "VWDvfZuXMPWA" }, "source": [ "- Read line by line with a loop" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690234745, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "je2MqzPIMPWA", "outputId": "54d1d881-4280-49f9-dbb0-61ae17bff037" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "blah\n", "\n", "line1\n", "\n", "line2\n", "\n", "line3\n", "\n" ] } ], "source": [ "f = open(\"myfile.txt\", 'r')\n", "for line in f:\n", " print(line) # still contains newline char" ] }, { "cell_type": "markdown", "metadata": { "id": "XEDfextsMPWB" }, "source": [ "### 2.13 Saving Objects with Pickle\n", "- Turns almost **any** Python **object** into a string representation for saving into a file." ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "executionInfo": { "elapsed": 843, "status": "ok", "timestamp": 1661690235586, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "wS6cT960MPWB" }, "outputs": [], "source": [ "import pickle # load the pickle library\n", "mylist = [0,1,2,3,4,5] # an object\n", "file = open('alist.pickle', 'wb') # open file to save object (write bytes)\n", "pickle.dump(mylist, file) # save the object using pickle\n", "file.close() # close the file" ] }, { "cell_type": "markdown", "metadata": { "id": "E3jJPGPzMPWB" }, "source": [ "- Load object from file" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690235587, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "LeQGLk9wMPWB", "outputId": "04715291-8948-4410-c51c-2d247eeea48a" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4, 5]\n" ] } ], "source": [ "file = open('alist.pickle', 'rb') # (read bytes)\n", "mylist2 = pickle.load(file) # load pickled object from file\n", "file.close()\n", "print(mylist2)" ] }, { "cell_type": "markdown", "metadata": { "id": "RhCMTDAhMPWB" }, "source": [ "- cPickle is a faster version (1,000 times faster!)" ] }, { "cell_type": "markdown", "metadata": { "id": "BQgUjZJyMPWC" }, "source": [ "### 2.14 Pandas\n", "- pandas is a Python library for data wrangling and analysis.\n", "- `Dataframe` is a table of entries (like an Excel spreadsheet).\n", " - each column does not need to be the same type\n", " - operations to modify and operate on the table" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690235587, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "T5m7YF_rMPWC" }, "outputs": [], "source": [ "# setup pandas and display\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 175 }, "executionInfo": { "elapsed": 686, "status": "ok", "timestamp": 1661690237035, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "4IcMG6vpMPWC", "outputId": "5fed8037-0c49-40ee-aebf-6f2f3c1f352c" }, "outputs": [ { "data": { "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", "
NameLocationAge
0JohnNew York24
1AnnaParis13
2PeterBerlin53
3LindaLondon33
\n", "
" ], "text/plain": [ " Name Location Age\n", "0 John New York 24\n", "1 Anna Paris 13\n", "2 Peter Berlin 53\n", "3 Linda London 33" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# read CSV file\n", "df = pd.read_csv('mycsv.csv')\n", "\n", "# print the dataframe\n", "df" ] }, { "cell_type": "markdown", "metadata": { "id": "Fz_ug6Z4MPWC" }, "source": [ "- select a column" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 6, "status": "ok", "timestamp": 1661690237035, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "u9t8eSuvMPWC", "outputId": "2ad1a87a-4f1c-4e6e-9910-ea3da6403356" }, "outputs": [ { "data": { "text/plain": [ "0 John\n", "1 Anna\n", "2 Peter\n", "3 Linda\n", "Name: Name, dtype: object" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['Name']" ] }, { "cell_type": "markdown", "metadata": { "id": "7siXi2EdMPWD" }, "source": [ "- query the table" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 112 }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690237036, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "VIDBZqZyMPWD", "outputId": "0c3d09c8-6a99-4e49-f29e-51936870fdb0" }, "outputs": [ { "data": { "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", "
NameLocationAge
2PeterBerlin53
3LindaLondon33
\n", "
" ], "text/plain": [ " Name Location Age\n", "2 Peter Berlin 53\n", "3 Linda London 33" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# select Age greater than 30\n", "df[df.Age > 30] #这里并不是df['Age']而是直接df[df.Age > 30]的那些行" ] }, { "cell_type": "markdown", "metadata": { "id": "JmKEFWfdMPWD" }, "source": [ "- compute statistics " ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690237721, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "Nri1GLNrMPWD", "outputId": "d1e46ecb-85b6-49f6-8f15-7df07e358ed9" }, "outputs": [ { "ename": "TypeError", "evalue": "Could not convert ['JohnAnnaPeterLinda' 'New YorkParisBerlinLondon'] to numeric", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\nanops.py:1680\u001b[0m, in \u001b[0;36m_ensure_numeric\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 1679\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 1680\u001b[0m x \u001b[38;5;241m=\u001b[39m x\u001b[38;5;241m.\u001b[39mastype(np\u001b[38;5;241m.\u001b[39mcomplex128)\n\u001b[0;32m 1681\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mTypeError\u001b[39;00m, \u001b[38;5;167;01mValueError\u001b[39;00m):\n", "\u001b[1;31mValueError\u001b[0m: complex() arg is a malformed string", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\nanops.py:1683\u001b[0m, in \u001b[0;36m_ensure_numeric\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 1682\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 1683\u001b[0m x \u001b[38;5;241m=\u001b[39m x\u001b[38;5;241m.\u001b[39mastype(np\u001b[38;5;241m.\u001b[39mfloat64)\n\u001b[0;32m 1684\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[0;32m 1685\u001b[0m \u001b[38;5;66;03m# GH#29941 we get here with object arrays containing strs\u001b[39;00m\n", "\u001b[1;31mValueError\u001b[0m: could not convert string to float: 'JohnAnnaPeterLinda'", "\nThe above exception was the direct cause of the following exception:\n", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[54], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m df\u001b[38;5;241m.\u001b[39mmean()\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\generic.py:11556\u001b[0m, in \u001b[0;36mNDFrame._add_numeric_operations..mean\u001b[1;34m(self, axis, skipna, numeric_only, **kwargs)\u001b[0m\n\u001b[0;32m 11539\u001b[0m \u001b[38;5;129m@doc\u001b[39m(\n\u001b[0;32m 11540\u001b[0m _num_doc,\n\u001b[0;32m 11541\u001b[0m desc\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mReturn the mean of the values over the requested axis.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 11554\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs,\n\u001b[0;32m 11555\u001b[0m ):\n\u001b[1;32m> 11556\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m NDFrame\u001b[38;5;241m.\u001b[39mmean(\u001b[38;5;28mself\u001b[39m, axis, skipna, numeric_only, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\generic.py:11201\u001b[0m, in \u001b[0;36mNDFrame.mean\u001b[1;34m(self, axis, skipna, numeric_only, **kwargs)\u001b[0m\n\u001b[0;32m 11194\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmean\u001b[39m(\n\u001b[0;32m 11195\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m 11196\u001b[0m axis: Axis \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 11199\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs,\n\u001b[0;32m 11200\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Series \u001b[38;5;241m|\u001b[39m \u001b[38;5;28mfloat\u001b[39m:\n\u001b[1;32m> 11201\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_stat_function(\n\u001b[0;32m 11202\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmean\u001b[39m\u001b[38;5;124m\"\u001b[39m, nanops\u001b[38;5;241m.\u001b[39mnanmean, axis, skipna, numeric_only, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[0;32m 11203\u001b[0m )\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\generic.py:11158\u001b[0m, in \u001b[0;36mNDFrame._stat_function\u001b[1;34m(self, name, func, axis, skipna, numeric_only, **kwargs)\u001b[0m\n\u001b[0;32m 11154\u001b[0m nv\u001b[38;5;241m.\u001b[39mvalidate_stat_func((), kwargs, fname\u001b[38;5;241m=\u001b[39mname)\n\u001b[0;32m 11156\u001b[0m validate_bool_kwarg(skipna, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mskipna\u001b[39m\u001b[38;5;124m\"\u001b[39m, none_allowed\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m> 11158\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_reduce(\n\u001b[0;32m 11159\u001b[0m func, name\u001b[38;5;241m=\u001b[39mname, axis\u001b[38;5;241m=\u001b[39maxis, skipna\u001b[38;5;241m=\u001b[39mskipna, numeric_only\u001b[38;5;241m=\u001b[39mnumeric_only\n\u001b[0;32m 11160\u001b[0m )\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\frame.py:10519\u001b[0m, in \u001b[0;36mDataFrame._reduce\u001b[1;34m(self, op, name, axis, skipna, numeric_only, filter_type, **kwds)\u001b[0m\n\u001b[0;32m 10515\u001b[0m df \u001b[38;5;241m=\u001b[39m df\u001b[38;5;241m.\u001b[39mT\n\u001b[0;32m 10517\u001b[0m \u001b[38;5;66;03m# After possibly _get_data and transposing, we are now in the\u001b[39;00m\n\u001b[0;32m 10518\u001b[0m \u001b[38;5;66;03m# simple case where we can use BlockManager.reduce\u001b[39;00m\n\u001b[1;32m> 10519\u001b[0m res \u001b[38;5;241m=\u001b[39m df\u001b[38;5;241m.\u001b[39m_mgr\u001b[38;5;241m.\u001b[39mreduce(blk_func)\n\u001b[0;32m 10520\u001b[0m out \u001b[38;5;241m=\u001b[39m df\u001b[38;5;241m.\u001b[39m_constructor(res)\u001b[38;5;241m.\u001b[39miloc[\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m 10521\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m out_dtype \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\internals\\managers.py:1534\u001b[0m, in \u001b[0;36mBlockManager.reduce\u001b[1;34m(self, func)\u001b[0m\n\u001b[0;32m 1532\u001b[0m res_blocks: \u001b[38;5;28mlist\u001b[39m[Block] \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m 1533\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m blk \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mblocks:\n\u001b[1;32m-> 1534\u001b[0m nbs \u001b[38;5;241m=\u001b[39m blk\u001b[38;5;241m.\u001b[39mreduce(func)\n\u001b[0;32m 1535\u001b[0m res_blocks\u001b[38;5;241m.\u001b[39mextend(nbs)\n\u001b[0;32m 1537\u001b[0m index \u001b[38;5;241m=\u001b[39m Index([\u001b[38;5;28;01mNone\u001b[39;00m]) \u001b[38;5;66;03m# placeholder\u001b[39;00m\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\internals\\blocks.py:339\u001b[0m, in \u001b[0;36mBlock.reduce\u001b[1;34m(self, func)\u001b[0m\n\u001b[0;32m 333\u001b[0m \u001b[38;5;129m@final\u001b[39m\n\u001b[0;32m 334\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mreduce\u001b[39m(\u001b[38;5;28mself\u001b[39m, func) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28mlist\u001b[39m[Block]:\n\u001b[0;32m 335\u001b[0m \u001b[38;5;66;03m# We will apply the function and reshape the result into a single-row\u001b[39;00m\n\u001b[0;32m 336\u001b[0m \u001b[38;5;66;03m# Block with the same mgr_locs; squeezing will be done at a higher level\u001b[39;00m\n\u001b[0;32m 337\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m2\u001b[39m\n\u001b[1;32m--> 339\u001b[0m result \u001b[38;5;241m=\u001b[39m func(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvalues)\n\u001b[0;32m 341\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvalues\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m 342\u001b[0m \u001b[38;5;66;03m# TODO(EA2D): special case not needed with 2D EAs\u001b[39;00m\n\u001b[0;32m 343\u001b[0m res_values \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([[result]])\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\frame.py:10482\u001b[0m, in \u001b[0;36mDataFrame._reduce..blk_func\u001b[1;34m(values, axis)\u001b[0m\n\u001b[0;32m 10480\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m values\u001b[38;5;241m.\u001b[39m_reduce(name, skipna\u001b[38;5;241m=\u001b[39mskipna, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds)\n\u001b[0;32m 10481\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m> 10482\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m op(values, axis\u001b[38;5;241m=\u001b[39maxis, skipna\u001b[38;5;241m=\u001b[39mskipna, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds)\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\nanops.py:96\u001b[0m, in \u001b[0;36mdisallow.__call__.._f\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 94\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m 95\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m np\u001b[38;5;241m.\u001b[39merrstate(invalid\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mignore\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m---> 96\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m f(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m 97\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m 98\u001b[0m \u001b[38;5;66;03m# we want to transform an object array\u001b[39;00m\n\u001b[0;32m 99\u001b[0m \u001b[38;5;66;03m# ValueError message to the more typical TypeError\u001b[39;00m\n\u001b[0;32m 100\u001b[0m \u001b[38;5;66;03m# e.g. this is normally a disallowed function on\u001b[39;00m\n\u001b[0;32m 101\u001b[0m \u001b[38;5;66;03m# object arrays that contain strings\u001b[39;00m\n\u001b[0;32m 102\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_object_dtype(args[\u001b[38;5;241m0\u001b[39m]):\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\nanops.py:158\u001b[0m, in \u001b[0;36mbottleneck_switch.__call__..f\u001b[1;34m(values, axis, skipna, **kwds)\u001b[0m\n\u001b[0;32m 156\u001b[0m result \u001b[38;5;241m=\u001b[39m alt(values, axis\u001b[38;5;241m=\u001b[39maxis, skipna\u001b[38;5;241m=\u001b[39mskipna, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds)\n\u001b[0;32m 157\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m--> 158\u001b[0m result \u001b[38;5;241m=\u001b[39m alt(values, axis\u001b[38;5;241m=\u001b[39maxis, skipna\u001b[38;5;241m=\u001b[39mskipna, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds)\n\u001b[0;32m 160\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\nanops.py:421\u001b[0m, in \u001b[0;36m_datetimelike_compat..new_func\u001b[1;34m(values, axis, skipna, mask, **kwargs)\u001b[0m\n\u001b[0;32m 418\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m datetimelike \u001b[38;5;129;01mand\u001b[39;00m mask \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 419\u001b[0m mask \u001b[38;5;241m=\u001b[39m isna(values)\n\u001b[1;32m--> 421\u001b[0m result \u001b[38;5;241m=\u001b[39m func(values, axis\u001b[38;5;241m=\u001b[39maxis, skipna\u001b[38;5;241m=\u001b[39mskipna, mask\u001b[38;5;241m=\u001b[39mmask, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m 423\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m datetimelike:\n\u001b[0;32m 424\u001b[0m result \u001b[38;5;241m=\u001b[39m _wrap_results(result, orig_values\u001b[38;5;241m.\u001b[39mdtype, fill_value\u001b[38;5;241m=\u001b[39miNaT)\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\nanops.py:727\u001b[0m, in \u001b[0;36mnanmean\u001b[1;34m(values, axis, skipna, mask)\u001b[0m\n\u001b[0;32m 724\u001b[0m dtype_count \u001b[38;5;241m=\u001b[39m dtype\n\u001b[0;32m 726\u001b[0m count \u001b[38;5;241m=\u001b[39m _get_counts(values\u001b[38;5;241m.\u001b[39mshape, mask, axis, dtype\u001b[38;5;241m=\u001b[39mdtype_count)\n\u001b[1;32m--> 727\u001b[0m the_sum \u001b[38;5;241m=\u001b[39m _ensure_numeric(values\u001b[38;5;241m.\u001b[39msum(axis, dtype\u001b[38;5;241m=\u001b[39mdtype_sum))\n\u001b[0;32m 729\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m axis \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mgetattr\u001b[39m(the_sum, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mndim\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m):\n\u001b[0;32m 730\u001b[0m count \u001b[38;5;241m=\u001b[39m cast(np\u001b[38;5;241m.\u001b[39mndarray, count)\n", "File \u001b[1;32mC:\\ProgramData\\anaconda3\\Lib\\site-packages\\pandas\\core\\nanops.py:1686\u001b[0m, in \u001b[0;36m_ensure_numeric\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 1683\u001b[0m x \u001b[38;5;241m=\u001b[39m x\u001b[38;5;241m.\u001b[39mastype(np\u001b[38;5;241m.\u001b[39mfloat64)\n\u001b[0;32m 1684\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[0;32m 1685\u001b[0m \u001b[38;5;66;03m# GH#29941 we get here with object arrays containing strs\u001b[39;00m\n\u001b[1;32m-> 1686\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCould not convert \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mx\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m to numeric\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[0;32m 1687\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 1688\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m np\u001b[38;5;241m.\u001b[39many(np\u001b[38;5;241m.\u001b[39mimag(x)):\n", "\u001b[1;31mTypeError\u001b[0m: Could not convert ['JohnAnnaPeterLinda' 'New YorkParisBerlinLondon'] to numeric" ] } ], "source": [ "df.mean()" ] }, { "cell_type": "markdown", "metadata": { "id": "EMq0kEyaMPWD" }, "source": [ "### 2.15 NumPy\n", "- Library for multidimensional arrays and 2D matrices\n", "- `ndarray` class for multidimensional arrays\n", " - elements are all the same type\n", " - aliased to `array`" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690237722, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "jE0q6IqtMPWE", "outputId": "67d9ba48-e48f-426e-cc1b-128b0aca6d7f" }, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from numpy import * # import all classes from numpy\n", "a = arange(15)\n", "a" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690238375, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "1ujPL2KwMPWE", "outputId": "90892199-d4e9-426b-817a-bd08f55777b6" }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4],\n", " [ 5, 6, 7, 8, 9],\n", " [10, 11, 12, 13, 14]])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a.reshape(3,5)\n", "b" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690238376, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "C8z-Ha5vMPWE", "outputId": "158f4f5d-2612-48e0-cfa3-b8d9feec5264" }, "outputs": [ { "data": { "text/plain": [ "(3, 5)" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.shape # get the shape (num rows x num columns)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 6, "status": "ok", "timestamp": 1661690239077, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "nnc2Uf33MPWE", "outputId": "ea60129b-5b12-4bc8-ac24-db6d677347c9" }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.ndim # get number of dimensions" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 6, "status": "ok", "timestamp": 1661690239078, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "vnBux_YeMPWF", "outputId": "9b20d5dd-477b-44cf-9356-2c806656db88" }, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.size # get number of elements" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690239078, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "iYKKGE3EMPWF", "outputId": "63515ccf-6a6e-4000-8e3c-27de55e1d385" }, "outputs": [ { "data": { "text/plain": [ "dtype('int32')" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.dtype # get the element type" ] }, { "cell_type": "markdown", "metadata": { "id": "NxmZTwqpMPWF" }, "source": [ "### 2.16 Array Creation" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 2, "status": "ok", "timestamp": 1661690239826, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "BK3nJ9CNMPWF", "outputId": "f5ed75eb-46c5-4706-b6fd-5f26d8622422" }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array([1, 2, 3, 4]) # use a list to initialize\n", "a" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 12, "status": "ok", "timestamp": 1661690240707, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "s6MnJnGCMPWG", "outputId": "5a30ae48-3cac-4e3a-b0c3-7df95ee307fc" }, "outputs": [ { "data": { "text/plain": [ "array([[1.1, 2. , 3. ],\n", " [4. , 5. , 6. ]])" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = array([[1.1,2,3], [4,5,6]]) # or list of lists\n", "b" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 11, "status": "ok", "timestamp": 1661690240707, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "jgzbgaLeMPWG", "outputId": "14d831d4-9f77-46d8-823b-c9b71c2b625f" }, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0.],\n", " [0., 0., 0., 0.],\n", " [0., 0., 0., 0.]])" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zeros( (3,4) ) # 3x4 array of zeros" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 10, "status": "ok", "timestamp": 1661690240708, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "MZiSsAWKMPWG", "outputId": "cd774e0d-7eda-4111-df1b-2871fd852330" }, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1., 1.],\n", " [1., 1., 1., 1.]])" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ones( (2,4) ) # 2x4 array of ones" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 8, "status": "ok", "timestamp": 1661690240708, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "p4_TyWYsMPWG", "outputId": "35f6bdab-7b59-49ac-9131-1669e836f1a5" }, "outputs": [ { "data": { "text/plain": [ "array([[37926976, 32765, 37926976],\n", " [ 32765, 37934352, 32765]])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "empty( (2,3),int ) # create an array, but do not prepopulate it.\n", " # contents are random" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 6, "status": "ok", "timestamp": 1661690240708, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "gANTxmNiMPWG", "outputId": "059231a1-0045-4e0e-bffb-5c47891e7493" }, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arange(0,5,0.5) # from 0 to 5 (exclusive), increment by 0.5 这俩头回见" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 7, "status": "ok", "timestamp": 1661690241482, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "dkaijiaCMPWH", "outputId": "b166d5e0-ac74-4b84-fb3a-76cb3d2b69aa" }, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444,\n", " 0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ])" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "linspace(0,1,10) # 10 evenly-spaced numbers between 0 to 1 (inclusive)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 6, "status": "ok", "timestamp": 1661690241483, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "4KUPK9yqMPWH", "outputId": "c6003491-ac65-4adc-d3ba-778d0c712293" }, "outputs": [ { "data": { "text/plain": [ "array([1.00000000e-03, 3.16227766e-03, 1.00000000e-02, 3.16227766e-02,\n", " 1.00000000e-01, 3.16227766e-01, 1.00000000e+00, 3.16227766e+00,\n", " 1.00000000e+01, 3.16227766e+01, 1.00000000e+02, 3.16227766e+02,\n", " 1.00000000e+03])" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "logspace(-3,3,13) # 13 numbers evendly spaced in log-space between 1e-3 and 1e3" ] }, { "cell_type": "markdown", "metadata": { "id": "ChSoOZa9MPWH" }, "source": [ "### 2.17 Array Indexing" ] }, { "cell_type": "markdown", "metadata": { "id": "azhzr0k7MPWH" }, "source": [ "- One-dimensional arrays are indexed, sliced, and iterated similar to Python lists." ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 9, "status": "ok", "timestamp": 1661690242314, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "hyBWnEfwMPWH", "outputId": "9db5b9a1-0692-4d00-d0c5-f12e3058d270" }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array([1,2,3,4,5])\n", "a[2]" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 7, "status": "ok", "timestamp": 1661690242314, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "FBIMhF1_MPWI", "outputId": "27e6a5b5-c8f1-4456-a46b-ba6e1ae3eb42" }, "outputs": [ { "data": { "text/plain": [ "array([3, 4, 5])" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:5] # index 2 through 4" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690242314, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "mntcPreaMPWI", "outputId": "75c965ba-cf14-482d-a9ab-d35705fbaaf1" }, "outputs": [ { "data": { "text/plain": [ "array([1, 3, 5])" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0:5:2] # index 0 through 4, by 2 头回见" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 9, "status": "ok", "timestamp": 1661690243055, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "_tmztX1FMPWI", "outputId": "bec88bd5-c818-4fd3-8031-3e34ccfcc06f", "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n" ] } ], "source": [ "# iterating with loop\n", "for i in a:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": { "id": "dPyLOKVEMPWI" }, "source": [ "- For multi-dimensional arrays, each axis had an index.\n", " - indices are given using tuples (separated by commas)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 7, "status": "ok", "timestamp": 1661690243055, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "EahRnSyAMPWJ", "outputId": "7e096d3c-e212-4e64-9b48-266f4d670806" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]\n", " [7 8 9]]\n" ] } ], "source": [ "a = array([[1, 2, 3], [4, 5, 6], [7,8,9]])\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690243055, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "v9k-kKuyMPWJ", "outputId": "3a9ccc85-d07d-42e7-9e38-77d97b9a1d7f" }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0,1] # row 0, column 1" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690243056, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "zceoqulQMPWJ", "outputId": "1d1b2f7c-c5fc-45c1-8037-1f1b5d3ce041" }, "outputs": [ { "data": { "text/plain": [ "array([2, 5, 8])" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:,1] # all elements in column 1" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 7, "status": "ok", "timestamp": 1661690243829, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "DQfKD2yXMPWJ", "outputId": "a15be33e-b1bb-487c-9c7f-0a62a864fb8b" }, "outputs": [ { "data": { "text/plain": [ "array([[2, 3],\n", " [5, 6]])" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0:2, 1:3] # sub array: rows 0-1, and columns 1-2" ] }, { "cell_type": "markdown", "metadata": { "id": "ry5TDhUJMPWJ" }, "source": [ "- indexing with a boolean mask" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 6, "status": "ok", "timestamp": 1661690243829, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "F3iOkAWdMPWK", "outputId": "fcea194b-2146-4418-e1e8-df2ca3367470" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "m = [ True False False True]\n" ] }, { "data": { "text/plain": [ "array([3, 4])" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array([3, 1, 2, 4])\n", "m = array([True, False, False, True])\n", "print(\"m =\", m)\n", "a[m] # select with a mask" ] }, { "cell_type": "markdown", "metadata": { "id": "QZnKoCECMPWK" }, "source": [ "# 2.18 Array Operations\n", "- operators are applied **elementwise**" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 9, "status": "ok", "timestamp": 1661690244828, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "BYFFAdG6MPWK", "outputId": "6a9e8228-2c82-4e69-dae1-7df72f0325b9" }, "outputs": [ { "data": { "text/plain": [ "array([20, 29, 38, 47])" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array( [20,30,40,50] )\n", "b = arange( 4 ) # [0 1 2 3]\n", "a - b # element-wise subtraction" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 8, "status": "ok", "timestamp": 1661690244828, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "zplhlLPUMPWK", "outputId": "a5325436-f316-46db-fa0a-fec2230fce5e" }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 4, 9])" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b**2 # element-wise exponentiation" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 7, "status": "ok", "timestamp": 1661690244829, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "ev8hQ0ZOMPWK", "outputId": "8125c24d-a284-432a-8792-666c408fa214" }, "outputs": [ { "data": { "text/plain": [ "array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854])" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10*sin(a) # element-wise sin and product" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690244829, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "X_dSP8wIMPWL", "outputId": "400737d5-2a22-4b36-a2c9-e9e5acccb287" }, "outputs": [ { "data": { "text/plain": [ "array([ True, True, False, False])" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a < 35 # element-wise comparison" ] }, { "cell_type": "markdown", "metadata": { "id": "6bSsJijBMPWL" }, "source": [ "- product operator (`*`) is **elementwise**" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690245721, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "xK70AtLtMPWL", "outputId": "a5c9e59d-3f70-4191-8818-dcbc1ab024d7" }, "outputs": [ { "data": { "text/plain": [ "array([[2, 0],\n", " [0, 4]])" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = array( [[1,1],\n", " [0,1]] )\n", "B = array( [[2,0],\n", " [3,4]] )\n", "A*B # elementwise product" ] }, { "cell_type": "markdown", "metadata": { "id": "WxP77NFzMPWL" }, "source": [ "- use `dot` function for matrix product" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690245722, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "_iToM_OZMPWL", "outputId": "96f36f16-b1fa-43e0-bdfa-e41317aa7743" }, "outputs": [ { "data": { "text/plain": [ "array([[5, 4],\n", " [3, 4]])" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dot(A,B) # matrix product" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "id": "HPTOzuVlMPWM" }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (1601413746.py, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m Cell \u001b[1;32mIn[84], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m - compound assignment: `*=`, `+=`, `-=`\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "- compound assignment: `*=`, `+=`, `-=`\n", "- unary operators" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 7, "status": "ok", "timestamp": 1661690246486, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "2ewWdebVMPWM", "outputId": "bdceeec2-1be1-4698-e7c4-5280da14c635" }, "outputs": [ { "data": { "text/plain": [ "21" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array( [[1,2,3], [4, 5, 6]])\n", "a.sum()" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690246486, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "4ptHsg-sMPWN", "outputId": "5b83c5c6-0f00-424c-a68e-3e2cf19f75bb" }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.min()" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690246487, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "HwINqwzAMPWN", "outputId": "5750ffa4-99ae-44ca-b1df-43f020bf6fce" }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.max()" ] }, { "cell_type": "markdown", "metadata": { "id": "YhPjMhVnMPWN" }, "source": [ "- unary operators on each axis of array" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690247200, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "JIPD9YCkMPWO", "outputId": "d703addd-a7a4-4302-ba41-6b3bc86a90e8" }, "outputs": [ { "data": { "text/plain": [ "array([5, 7, 9])" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array( [[1,2,3], [4, 5, 6]])\n", "a.sum(axis=0) # sum over rows" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690247200, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "i5cb5CQGMPWO", "outputId": "d81c0045-cfd2-41b6-dd43-3012d3c11781" }, "outputs": [ { "data": { "text/plain": [ "array([ 6, 15])" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.sum(axis=1) # sum over column" ] }, { "cell_type": "markdown", "metadata": { "id": "A3XBj-MiMPWP" }, "source": [ "- Numpy provides functions for other operations (called universal functions)\n", " - `argmax`, `argmin`, `min`, `max`\n", " - `average`, `cov`, `std`, `mean`, `median`, \n", " - `ceil`, `floor`\n", " - `cumsum`, `cumprod`, `diff`, `sum`, `prod`\n", " - `inv`, `dot`, `trace`, `transpose`" ] }, { "cell_type": "markdown", "metadata": { "id": "5zr7avOaMPWP" }, "source": [ "### 2.19 Array Shape Manipulation\n", "- The shape of an array can be changed" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690247905, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "2aNrdMJhMPWP", "outputId": "304e03c7-65a3-4e96-d3b7-dc4279800ae6" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]\n", " [4 5 6]]\n" ] }, { "data": { "text/plain": [ "(2, 3)" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array([[1,2,3], [4, 5, 6]])\n", "print(a)\n", "a.shape" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690248535, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "kJTX53OdMPWQ", "outputId": "97a71b5a-0919-4786-c760-09f81f1dcddb" }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6])" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.ravel() # return flattened array (last index iterated first)." ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690248535, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "n3o4lQTQMPWQ", "outputId": "a600c7fe-56cb-4088-beb2-4f32ea864ded" }, "outputs": [ { "data": { "text/plain": [ "array([[1, 4],\n", " [2, 5],\n", " [3, 6]])" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.transpose() # return transposed array" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690249319, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "w8aAijBJMPWQ", "outputId": "7acb28bf-01c4-49d9-d081-89a6d6d58a5b" }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4],\n", " [5, 6]])" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.reshape(3,2) # return reshaped array" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690249319, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "5cQJreWbMPWQ", "outputId": "b8e92084-22f9-4347-9af8-095fbb49b0bb" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2]\n", " [3 4]\n", " [5 6]]\n" ] } ], "source": [ "a.resize(3,2) # change the shape directly\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": { "id": "_dL0LtDPMPWR" }, "source": [ "### 2.20 Concatenating arrays" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "id": "F0o5Edk2MPWR" }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6])" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array([1, 2, 3])\n", "b = array([4, 5, 6])\n", "concatenate((a,b))" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "id": "7mVaVpoXMPWR" }, "outputs": [ { "data": { "text/plain": [ "array([[1, 4],\n", " [2, 5],\n", " [3, 6]])" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c_[a,b] # concatenate as column vectors" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "id": "FUf7MFmMMPWR" }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6])" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r_[a,b] # concatenate as row vectors" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690256032, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "16PJ30KEMPWR" }, "outputs": [], "source": [ "# Stacking arrays" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690256032, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "dTXatTnVMPWS", "outputId": "3a6bb188-188c-4756-9200-b2b28da9615b" }, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [1, 1],\n", " [2, 2],\n", " [2, 2]])" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array([[1, 1],\n", " [1, 1]])\n", "b = array([[2, 2],\n", " [2, 2]])\n", "vstack( (a,b) ) # stack vertically" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690256033, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "PorlPAwpMPWS", "outputId": "9e7ad8c0-293b-4a54-a681-bbc31458db07" }, "outputs": [ { "data": { "text/plain": [ "array([[1, 1, 2, 2],\n", " [1, 1, 2, 2]])" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hstack( (a,b) ) # stack horizontally" ] }, { "cell_type": "markdown", "metadata": { "id": "4ZDYTPhRMPWS" }, "source": [ "### 2.21 Copies and Views\n", "- When operating on arrays, data is sometimes copied and sometimes not.\n", "- _No copy is made for simple assignment._\n", " - **Be careful!**" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 12, "status": "ok", "timestamp": 1661690257407, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "MqTnZCnzMPWT", "outputId": "288ad6b8-9ead-4312-9488-33697078c88c" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = array([1,2,3,4])\n", "b = a # simple assignment (no copy made!)\n", "b is a # yes, b references the same object" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 12, "status": "ok", "timestamp": 1661690257408, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "zXR_mIXJMPWT", "outputId": "1bcb6e83-f2e7-4b59-ab06-d7424152f439" }, "outputs": [ { "data": { "text/plain": [ "array([ 1, -2, 3, 4])" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[1] = -2 # changing b also changes a\n", "a" ] }, { "cell_type": "markdown", "metadata": { "id": "4Mzs4O4cMPWT" }, "source": [ "- View or shallow copy\n", " - different array objects can share the same data (called a view)\n", " - happens when slicing" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 10, "status": "ok", "timestamp": 1661690257408, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "h15GP7NbMPWU", "outputId": "823794bc-294f-44d8-d709-dc7c3b05f11f" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = a.view() # create a view of a\n", "c is a # not the same object" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 9, "status": "ok", "timestamp": 1661690257408, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "HrMTZW9eMPWU", "outputId": "8ecbfd6d-46c6-483d-b6fd-ac8a568761bf" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.base is a # but the data is owned by a" ] }, { "cell_type": "code", "execution_count": 101, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 8, "status": "ok", "timestamp": 1661690257408, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "kSpU0zbSMPWU", "outputId": "d91f9403-6533-4285-8c64-5d474058c333" }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, -2],\n", " [ 3, 4]])" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.shape = 2,2 # change shape of c\n", "c" ] }, { "cell_type": "code", "execution_count": 102, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 7, "status": "ok", "timestamp": 1661690257408, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "VJLmyy1wMPWV", "outputId": "0b0d54db-7877-41f9-8d02-5142de6b597c" }, "outputs": [ { "data": { "text/plain": [ "array([ 1, -2, 3, 4])" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a # but the shape of a is the same" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 6, "status": "ok", "timestamp": 1661690257409, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "j5ALZpnkMPWV", "outputId": "35a021dc-bd6b-4d02-ae71-418abcb05fd0" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = a.copy() # create a complete copy of a (new data is created)\n", "d is a # not the same object" ] }, { "cell_type": "code", "execution_count": 104, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5, "status": "ok", "timestamp": 1661690257409, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "qIRkj_O0MPWV", "outputId": "de48b67a-1656-4516-dd9e-3537046eb494" }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.base is a # not sharing the same data" ] }, { "cell_type": "markdown", "metadata": { "id": "YV25wezAMPWV" }, "source": [ "### 2.22 Visualizing Data\n", "- Use matplotlib package to make plots and graphs\n", "- Works with Jupyter to show plots within the notebook" ] }, { "cell_type": "code", "execution_count": 108, "metadata": { "executionInfo": { "elapsed": 4, "status": "ok", "timestamp": 1661690257409, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "3cBzj-bgMPWV" }, "outputs": [], "source": [ "# setup matplotlib\n", "%matplotlib inline\n", "# setup output image format (Chrome works best)\n", "import IPython.core.display \n", "#IPython.core.display.set_matplotlib_formats(\"svg\") # file format\n", "#import matplotlib_inline\n", "#matplotlib_inline.backend_inline.set_matplotlib_formats(\"svg\")\n", "%config InlineBackend.figure_format = 'svg'\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": { "id": "iDsRBJ8OMPWW" }, "source": [ "- Each cell will start a new figure automatically.\n", "- Plots are made piece by piece." ] }, { "cell_type": "code", "execution_count": 109, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 390 }, "executionInfo": { "elapsed": 3, "status": "ok", "timestamp": 1661690259325, "user": { "displayName": "Hanwei", "userId": "13726710393687068958" }, "user_tz": -480 }, "id": "6DzXTSoGMPWW", "outputId": "da1b0e3a-42c1-42ab-d0a6-e4733c8a5751" }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", " \n", " 2024-09-13T13:05:07.725350\n", " image/svg+xml\n", " \n", " \n", " Matplotlib v3.7.2, https://matplotlib.org/\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x = linspace(0,2*pi,16)\n", "y = sin(x)\n", "plt.plot(x, y, 'bo-')\n", "plt.grid(True)\n", "plt.ylabel('y label')\n", "plt.xlabel('x label')\n", "plt.title('my title')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "tx57BTz8MPWW" }, "source": [ "- plot string specifies three things (e.g., `'bo-'`)\n", " - colors:\n", " - **b**lue, **r**ed, **g**reen, **m**agenta, **c**yan, **y**ellow, blac**k**, **w**hite\n", " - markers: \n", " - ”.”\tpoint\n", " - “o”\tcircle\n", " - “v”\ttriangle down\n", " - “^”\ttriangle up\n", " - “<”\ttriangle left\n", " - “>”\ttriangle right\n", " - “8”\toctagon\n", " - “s”\tsquare\n", " - “p”\tpentagon\n", " - “*”\tstar\n", " - “h”\thexagon1\n", " - “+”\tplus\n", " - “x”\tx\n", " - “d”\tthin_diamond\n", " - line styles:\n", " - '-' solid line\n", " - '--' dashed line\n", " - '-.' dash-dotted line\n", " - ':' dotted lione" ] }, { "cell_type": "markdown", "metadata": { "id": "pTZGxi2KMPWW" }, "source": [ "### 2.23 Python Tutorials\n", "- Python - https://docs.python.org/3/tutorial/\n", "- numpy - https://docs.scipy.org/doc/numpy-dev/user/quickstart.html\n", "- scikit-learn - http://scikit-learn.org/stable/tutorial/\n", "- matplotlib - http://matplotlib.org/users/pyplot_tutorial.html\n", "- pandas - https://pandas.pydata.org/pandas-docs/stable/tutorials.html\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "_6I8pDzVMPWW" }, "source": [ "## 3. Python Program\n", "In the rest of the lecture, you will write a small program to get familiar with Python.\n", "\n", "The goal of the program is to count the number of factors (_not including 1 and the number itself_) for each number between 2 and 100. For example, the number of factors of 2 is 0, and the number of factors for 4 is 1.\n", "\n", "Here are two variables to get you started, `xs` stores the numbers from 2 to 100, and `fs` will store the factors for each number in `xs`." ] }, { "cell_type": "code", "execution_count": 121, "metadata": { "id": "awxKGMYWMPWX" }, "outputs": [], "source": [ "xs = range(2,101) # the number\n", "fs = [] # store number of factors in this list" ] }, { "cell_type": "markdown", "metadata": { "id": "tjYgZz7CMPWX" }, "source": [ "Write the program in the cell below." ] }, { "cell_type": "code", "execution_count": 122, "metadata": { "id": "siRR1tZPMPWX" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 0, 1, 0, 2, 0, 2, 1, 2, 0, 4, 0, 2, 2, 3, 0, 4, 0, 4, 2, 2, 0, 6, 1, 2, 2, 4, 0, 6, 0, 4, 2, 2, 2, 7, 0, 2, 2, 6, 0, 6, 0, 4, 4, 2, 0, 8, 1, 4, 2, 4, 0, 6, 2, 6, 2, 2, 0, 10, 0, 2, 4, 5, 2, 6, 0, 4, 2, 6, 0, 10, 0, 2, 4, 4, 2, 6, 0, 8, 3, 2, 0, 10, 2, 2, 2, 6, 0, 10, 2, 4, 2, 2, 2, 10, 0, 4, 4, 7]\n", "(99,)\n" ] } ], "source": [ "# INSERT YOUR CODE HERE\n", "for i in range(0,99):\n", " fn = 0\n", " for j in range(2,xs[i]+1):\n", " if xs[i]%j == 0:\n", " fn += 1\n", " fs.append(fn-1)\n", "print(fs)\n", "print(array(fs).shape)" ] }, { "cell_type": "markdown", "metadata": { "id": "mzXaqi38MPWX" }, "source": [ "Now we will make some plots using `xs` and `fs`. First, we need to initialize the matplotlib library." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "IPL1IYYZMPWX" }, "outputs": [], "source": [ "# setup matplotlib display\n", "%matplotlib inline\n", "import IPython.core.display # setup output image format\n", "IPython.core.display.set_matplotlib_formats(\"svg\") \n", "import matplotlib.pyplot as plt # import matplotlib" ] }, { "cell_type": "markdown", "metadata": { "id": "NPRxwosnMPWY" }, "source": [ "Write code to plot the number of factors (y-axis) vs the number (x-axis). Don't forget to label your axes!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "5tH9EXWoMPWY" }, "outputs": [], "source": [ "# INSERT YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": { "id": "M0cjqFheMPWY" }, "source": [ "Next we will plot a histogram of the number of factors." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "binedges = [i-0.5 for i in range(0,12)] # first get the edges of bins\n", "plt.hist(fs, binedges, # histogram plot (data, bins)\n", " facecolor='g', alpha=0.75, align='mid') # appearance options\n", "plt.xlabel('Number of factors')\n", "plt.ylabel('Count')\n", "plt.title('Histogram of number of factors')\n", "plt.axis([-0.5, 10.5, 0, 35]) # set the axis (xmin, xmax, ymin, ymax)\n", "plt.grid(True)" ] }, { "cell_type": "markdown", "metadata": { "id": "Q-JUbJoYMPWY" }, "source": [ "## 4. Pickle\n", "Next you will practice using pickle to load data from the file \"data1.pickle\". The pickle is storing an ndarray object (numpy multi-dimensional array). Load the pickle file and save its contents into a variable called `mydata`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "z2Qkn_M_MPWY" }, "outputs": [], "source": [ "# INSERT YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": { "id": "eKoIIASjMPWZ" }, "source": [ "`mydata` is a 2-d array. Each row of the array is a \"sample\". Let's look at the shape of the array." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mydata.shape" ] }, { "cell_type": "markdown", "metadata": { "id": "9szJjICDMPWZ" }, "source": [ "Inspecting the shape of the array, we see that there are 120 samples (rows). Each sample is a 2-dimensional vector (the number of columns)." ] }, { "cell_type": "markdown", "metadata": { "id": "SpU2GIWqMPWZ" }, "source": [ "Finally, let's visualise the data with a plot. Treat the 1st dimension of the samples as the x-variable, and the 2nd dimension as the y-variable. In other words, plot the 1st column of the data vs. the 2nd column of the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "laCNhkbfMPWZ" }, "outputs": [], "source": [ "# INSERT YOUR CODE HERE" ] }, { "cell_type": "markdown", "metadata": { "id": "JvvwTty0MPWZ" }, "source": [ "## 5. Getting Help\n", "\n", "IPython has built-in help. Here are some useful commands:\n", "- `?` - introduction to IPython\n", "- `object?` - info/help about an object, class, or module\n", "- `object??` - even more details\n", "- `%quickref` - quick reference for IPython magic functions (%)\n", "- `%history` - show the history of the session\n", "- tab-completion - hit tab to use auto-completion" ] } ], "metadata": { "anaconda-cloud": {}, "colab": { "collapsed_sections": [ "pTZGxi2KMPWW" ], "name": "lecture1.3.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }