{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/python_with_Birds.gif)\n" ] }, { "cell_type": "markdown", "metadata": { "cell_style": "center" }, "source": [ "# Numpy เบื้องต้น - 2 D\n", "\n", "**20** minutes\n", "\n", " **วัตถุประสงค์**\n", "\n", "\n", " หลังจากทำทำแล็บ นศ.จะสามารถ \n", "\n", "\n", "* สร้างและดำเนินการทางคณิตศาสตร์กับข้อมูลชนิด `numpy` อาร์เรย์ 2 มิติได้\n", "\n", "Ref:\n", "- https://docs.scipy.org/doc/numpy/user/quickstart.html\n", "- https://docs.scipy.org/doc/numpy/user/basics.html\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ความสำคัญของข้อมูลอาร์เรย์ สถิติ หลายมิติ RBG ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## การสร้างอาร์เรย์ 2 มิติ (2D Numpy Array)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Import the libraries\n", "\n", "import numpy as np \n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "สร้างลิสต์ (List) a ขึ้นมา ซึ่งเป็น nested list 3 ลิสต์**ที่มีขนาดเท่ากัน**\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[11, 12, 13], [21, 22, 23], [31, 32, 33]]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a list\n", "\n", "#a = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]\n", "a = [[11, 12, 13], \n", " [21, 22, 23], \n", " [31, 32, 33]]\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถสร้าง Numpy Array จากลิสต์ได้ ดังต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[11, 12, 13],\n", " [21, 22, 23],\n", " [31, 32, 33]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Convert list to Numpy Array\n", "# Every element is the same type\n", "\n", "A = np.array(a)\n", "A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถเรียกแอตทริบิวต์ (attribute) ndim (number of dimensions) เพื่อดูจำนวนแกน (axes) หรือ จำนวนมิติ (dimensions) ของอาเรย์ได้\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show the numpy array dimensions\n", "\n", "A.ndim" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "แอตทริบิวต์ shape ส่งค่ากลับเป็นข้อมูลชนิด tuple ที่สอดคล้องกับขนาด (size) หรือจำนวนสมาชิกในแต่ละมิติ (รูปร่างของอาเรย์) " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show the numpy array shape\n", "\n", "A.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "จำนวนสมาชิกทั้งหมดที่มีอยู่ในอาร์เรย์สามารถดูได้จากแอตทริบิวต์ size.\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show the numpy array size\n", "\n", "A.size" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## การเข้าถึงสมาชิกในอาร์เรย์\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เช่นเดียวกันกับอาร์เรย์ 1 มิติ เราสามารถเข้าถึงข้อมูลแต่ละตัวภายในอาร์เรย์ได้โดยใช้เครื่องหมายวงเล็บเหลี่ยม (ก้ามปู) **[ ]** (square brackets)\n", "\n", "ความสัมพันธ์ระหว่างเลขดัชนีในวงเล็บเหลี่ยมกับสมาชิกแต่ละตัวแสดงได้ในรูปต่อไปนี้ (อาร์เรย์ 3x3)\n", "\n", "**อาร์เรย์ 2 มิติ คือ Array of Array**\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/NumTwoEg.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถเข้าถึงสมาชิกที่อยู่ในแถวที่ 2 (2nd-row) และคอลัมน์ที่ 3 (3rd column) ดังแสดงในรูป\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/NumTwoFT.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "โดยใช้เครื่องหมายวงเล็บเหลี่ยม **[ ]** และดัชนีที่สอดคล้องกับตำแน่งที่เราต้องการ" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "23" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Access the element on the second row and third column\n", "\n", "A[1, 2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "หรือจะเขียนรูปแบบแบบนี้ก็ได้ ให้ผลลัพธ์เหมือนกัน **(มีสองรูปแบบ!)**" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "23" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Access the element on the second row and third column\n", "\n", "A[1][2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ลองเข้าถึงสมาชิกที่กำหนดในรูปภาพต่อไปนี้" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/NumTwoFF.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "แถวที่ 0 คอลัมน์ที่ 0 ดังนั้น" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Access the element on the first row and first column\n", "\n", "A[0][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถตัดเฉพาะส่วน (slicing) ได้เช่นเดียวกัยอาร์เรย์ 1 มิติ\n", "\n", "รูปด้านล่าง: เราจะตัดเอาเฉพาะสองคอลัมน์แรกที่อยู่ในแถวแรกเท่านั้น\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/NumTwoFSF.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ตัดได้โดยใช้คำสั่งต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([11, 12])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Access the element on the first row and first and second columns\n", "\n", "A[0][0:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เช่นเดียวกัน เราสามารถตัดเอาเฉพาะสองแถวแรกที่อยู่ในคอลัมน์ที่ 3 เท่านั้น" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([13, 23])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Access the element on the first and second rows and third column\n", "\n", "A[0:2, 2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ดังแสดงในรูป\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/2D_numpy.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "สมาชิกที่อยู่ในแถวที่ 0, แถวที่ 1, แถวที่ 2 ตามลำดับ" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([11, 12, 13])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[0]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([21, 22, 23])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[1]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([31, 32, 33])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "หรือแบบนี้ก็ได้" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([31, 32, 33])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[2, :]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "สมาชิกที่อยู่ในคอลัมน์ที่ 0, คอลัมน์ที่ 1, คอลัมน์ที่ 2 ตามลำดับ" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([11, 12, 13])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:][0]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([21, 22, 23])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:][1]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([31, 32, 33])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A[:][2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " Note:\n", "ใน Numpy axis 0 คือแกนของแถว (row) ส่วน axis 1 คือแกนของคอลัมน์ (column)\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## การดำเนินการสำหรับ Numpy Array 2 มิติขั้นพื้นฐาน" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เรายังสามารถบวกอาร์เรย์ได้อีกด้วย การบวกนี้เหมือนกับการบวกเมทริกซ์ กล่าวคือเป็นการนำสมาชิกแต่ละตัวมาบวกกันเป็นคู่ๆ\n", "\n", "รูปต่อไปนี้แสดงการบวกเมทริกซ์ X และ Y\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/NumTwoAdd.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "สร้างอาร์เรย์ X และ Y ขึ้นมา\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 0],\n", " [0, 1]])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a numpy array X\n", "\n", "X = np.array([[1, 0], [0, 1]]) \n", "X" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 1],\n", " [1, 2]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a numpy array Y\n", "\n", "Y = np.array([[2, 1], [1, 2]]) \n", "Y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ทำการบวกอาร์เรย์ X และ Y ด้วยคำสั่งต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[3, 1],\n", " [1, 3]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add X and Y\n", "\n", "Z = X + Y\n", "Z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ทำการลบอาร์เรย์ X และ Y ด้วยคำสั่งต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-1, -1],\n", " [-1, -1]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# X - Y\n", "\n", "Z = X - Y\n", "Z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "อาร์เรย์จะบวกลบกันได้ ก็ต่อเมื่อมีรูปร่าง (shape) เหมือนกัน!" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 0 1]\n", " [0 1 1]]\n", "[[2 1]\n", " [1 2]\n", " [1 1]]\n" ] } ], "source": [ "X = np.array([[1, 0, 1], [0, 1, 1]]) \n", "Y = np.array([[2, 1], [1, 2], [1, 1]]) \n", "print(X)\n", "print(Y)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "# ValueError: operands could not be broadcast together with shapes (2,3) (3,2) \n", "#Z = X+Y\n", "#print(Z)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "การคูณ Numpy array ด้วยสเกลาร์นั้นเหมือนกับการคูณเมทริกซ์ด้วยสเกลาร์ ถ้าเราคูณเมทริกซ์ Y ด้วยสเกล 2 เราก็คูณทุกองค์ประกอบในเมทริกซ์ด้วย 2 ดังที่แสดงในรูป" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/NumTwoDb.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "คูณอาร์เรย์ Y ด้วย 2 " ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 1],\n", " [1, 2]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a numpy array Y\n", "\n", "Y = np.array([[2, 1], [1, 2]]) \n", "Y" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[4, 2],\n", " [2, 4]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Multiply Y with 2\n", "\n", "Z = 2 * Y\n", "Z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "การคูณอาร์เรย์สองอาร์เรย์เป็น การคูณฮาดามาร์ด (Hadamard product/Element-wise multiplication) *\n", "\n", "การคูณ Hadamard เป็นการคูณแต่ละสมาชิกที่อยู่ในตำแหน่งเดียวกันเป็นคู่ๆ ผลลัพธ์ของการคูณจะมีขนาดเดียวกับ X หรือ Y ดังแสดงในรูปต่อไปนี้\n", "\n", "* ผลคูณฮาดามาร์ดใช้ในการวิเคราะห์เชิงสถิติ โดยเฉพาะอย่างยิ่งใช้ในแบบจำลองเชิงเส้นทั่วไป (general linear models) และแบบจำลองการวิเคราะห์ตัวประกอบในรูปทั่วไป (generalized factor analysis model)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/NumTwoMul.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถคูณแต่ละสมาชิกของอาร์เรย์ X และ Y ได้โดยการเขียนโค้ดต่อไปนี้\n" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 0],\n", " [0, 1]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a numpy array X\n", "\n", "X = np.array([[1, 0], [0, 1]]) \n", "X" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 1],\n", " [1, 2]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a numpy array \n", "\n", "Y = np.array([[2, 1], [1, 2]]) \n", "Y" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2, 0],\n", " [0, 2]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Multiply X with Y\n", "\n", "Z = X * Y\n", "Z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "นอกจากการคูณแล้ว การหาร การยกกำลัง และหารเอาเศษ ก็เป็นการคำนวณเป็นคู่ๆ ทีละตัว ดังตัวอย่างต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.5 0. ]\n", " [0. 0.5]] \n", "\n", "[[1 0]\n", " [0 1]] \n", "\n", "[[1 0]\n", " [0 1]] \n", "\n" ] } ], "source": [ "print(X/Y, '\\n')\n", "print(X**Y, '\\n')\n", "print(X%Y, '\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**คูณแบบเมทริกซ์**\n", "\n", "นอกจากนี้ เรายังสามารถคูณ Numpy arrays A และ B แบบเมทริกซ์ ได้อีกด้วย" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ก่อนอื่น กำหนดเมทริกซ์ A (2x3) และ B (3x2)\n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 1],\n", " [1, 0, 1]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a matrix A\n", "\n", "A = np.array([[0, 1, 1], [1, 0, 1]])\n", "A" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 1],\n", " [ 1, 1],\n", " [-1, 1]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a matrix B\n", "\n", "B = np.array([[1, 1], [1, 1], [-1, 1]])\n", "B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ใช้ฟังก์ชั่น dot คูณอาร์เรย์ทั้งสอง" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 2],\n", " [0, 2]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Calculate the dot product\n", "\n", "Z = np.dot(A,B)\n", "Z" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0. , 0.90929743],\n", " [0. , 0.90929743]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Calculate the sine of Z\n", "\n", "np.sin(Z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "\n", "เราสามารถเรียกแอตทริบิวต์ T (Transpose) เพื่อคำนวณเมทริกซ์สลับเปลี่ยน (Transpose) **สลับแถวและคอลัมน์**\n", "\n", "*Note:* เมทริกซ์สลับเปลี่ยน (transpose of a matrix) คือเมทริกซ์ที่ได้จากการสลับสมาชิกจากแถวเป็นคอลัมน์ และจากคอลัมน์เป็นแถวของเมทริกซ์ต้นแบบ เมตริกซ์ทรานสโพสของ A เขียนแทนด้วย AT" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [2, 2],\n", " [3, 3]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a matrix C\n", "\n", "C = np.array([[1,1],[2,2],[3,3]])\n", "C" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [1, 2, 3]])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the transposed of C\n", "\n", "C.T" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [2, 2],\n", " [3, 3]])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C.T.T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Exercise]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1 จงเขียนโค้ดเปลี่ยนลิสต์ a ต่อไปนี้ให้เป็น Numpy Array\n" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "# Write your code below and press Shift+Enter to execute\n", "\n", "a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Click here for the solution\n", "\n", "```python\n", "A = np.array(a)\n", "A\n", "\n", "```\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2 จงคำนวณหาจำนวนสมาชิกหรือขนาด (size) ของอาร์เรย์\n" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "# Write your code below and press Shift+Enter to execute\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Click here for the solution\n", "\n", "```python\n", "A.size\n", "\n", "```\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3 จงเข้าถึงสมาชิกที่อยู่ในแถวแรกคอลัมน์แรก และ แถวแรกคอลัมน์ที่สอง\n", "\n" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "# Write your code below and press Shift+Enter to execute\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Click here for the solution\n", "\n", "```python\n", "A[0][0:2]\n", "\n", "```\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4 จงหาผลคูณแบบเมทริกซ์ของ numpy arrays A และ B" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "# Write your code below and press Shift+Enter to execute\n", "\n", "B = np.array([[0, 1], [1, 0], [1, 1], [-1, 0]])" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "
Click here for the solution\n", "\n", "```python\n", "X = np.dot(A,B)\n", "X\n", "\n", "```\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Author\n", "\n", "S.C.\n", "\n", "### Change Log\n", "\n", " \n", "| Date | Version | Change Description |\n", "|---|---|---|\n", "| 08-08-2021 | 0.1 | First edition |\n", "| | | |\n", "| | | |\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel(3.8.13))", "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.8.13" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }