{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Pythonæ°æ®åæ\n", "\n", "### 第äºç« Numpyåºç¡\n", "- æ°æ®ç±»å\n", "- æ°ç»ç±»å\n", "- ç±»å转æ¢\n", "- å建æ°ç»\n", "- æ°ç»ç´¢å¼\n", "- æ°ç»åç\n", "- æ¹å维度" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def pythonsum(n):\n", " a = range(n)\n", " b = range(n)\n", " c = []\n", " \n", " for i in range(len(a)):\n", " a[i] = i ** 2\n", " b[i] = i ** 3\n", " c.append(a[i] + b[i])\n", " \n", " return c" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def numpysum(n):\n", " a = numpy.arange(n) ** 2\n", " b = numpy.arange(n) ** 3\n", " c = a + b\n", " return c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2.1 Numpyæ°ç»å¯¹è±¡\n", "\n", "ndarrayå¤ç»´æ°ç»å¯¹è±¡ï¼å®é çæ°æ® + æè¿°æ°æ®çå æ°æ®\n" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dtype('int64')" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 使ç¨arange()彿°å建æ°ç»ï¼å¹¶è·åå ¶æ°æ®ç±»åï¼\n", "import numpy\n", "\n", "a = np.arange(5)\n", "a.dtype # dtype('int64')" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5,)" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# å建ä¸ä¸ªåéï¼ä¸ç»´Numpyæ°ç»ï¼ï¼å¹¶ç¡®å®å ¶åéç维度ï¼\n", "a\n", "np.array([0, 1, 2, 3, 4])\n", "# æ°ç»çshape屿§è¿åä¸ä¸ªå ç»(tuple)\n", "a.shape # (5,)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2.2 å®è·µï¼å建å¤ç»´æ°ç»" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1],\n", " [0, 1]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# å建ä¸ä¸ªå¤ç»´æ°ç»ï¼å¹¶æ¾ç¤ºå ¶ç»´åº¦ï¼\n", "# np.arange()彿°åå»ºçæ°ç»ä½ä¸ºå表å ç´ ï¼np.array()彿°å建2 * 2çæ°ç»\n", "m = np.array([np.arange(2), np.arange(2)])\n", "m" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 2)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ " m.shape" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2],\n", " [0, 1, 2],\n", " [0, 1, 2]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# å建ä¸ä¸ª3*3çå¤ç»´æ°ç»\n", "b = np.array([np.arange(3), np.arange(3), np.arange(3)])\n", "b" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### ä¸ãéåæ°ç»å ç´ " ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2],\n", " [3, 4]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# éåæ°ç»ä¸çæä¸ªç¹å®å ç´ \n", "a = np.array([[1, 2], [3, 4]])\n", "a" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0, 0]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0, 1]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1, 0]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1, 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##### äºãNumpyæ°æ®ç±»å" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.0" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.float64(42)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'int8' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[33], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m int8(\u001b[38;5;241m42.0\u001b[39m)\n", "\u001b[0;31mNameError\u001b[0m: name 'int8' is not defined" ] } ], "source": [ "np.int8(42.0)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(42)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(42.0)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(True)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(False)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(7, dtype = np.uint16)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1+0j)" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# å°æµ®ç¹æ°è½¬æ¢ä¸ºå¤æ°ï¼åä¹ä¸è¡ï¼è§¦åTypeErrorï¼\n", "complex(1.0)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "float() argument must be a string or a real number, not 'complex'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[48], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mfloat\u001b[39m(\u001b[38;5;241m1\u001b[39m\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m0\u001b[39mj)\n", "\u001b[0;31mTypeError\u001b[0m: float() argument must be a string or a real number, not 'complex'" ] } ], "source": [ "float(1+0j)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### ä¸ãæ°æ®ç±»å对象\n", "æ°æ®ç±»å对象ç»åºå个æ°ç»å ç´ å¨å åä¸å ç¨çåèæ°ï¼dtypeçitemsize屿§ï¼\n", "\n", "numpy.dtype" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.dtype.itemsize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### åãå符ç¼ç \n", "\n", "\n", "\n", "注ï¼å¨Numpyä¸ä¼å 使ç¨dtypeå¯¹è±¡è¡¨ç¤ºæ°æ®ç±»å" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0., 1., 2., 3., 4., 5., 6.], dtype=float32)" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# å建ä¸ä¸ªåç²¾åº¦æµ®ç¹æ°æ°ç»ï¼\n", "np.arange(7, dtype = 'f')" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j, 5.+0.j, 6.+0.j])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# å建ä¸ä¸ªå¤æ°æ°ç»ï¼\n", "np.arange(7, dtype = 'D')" ] } ], "metadata": { "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 }