{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0, 1, 2], [3, 4, 5]]\n", "\n" ] } ], "source": [ "l_2d = [[0, 1, 2], [3, 4, 5]]\n", "\n", "print(l_2d)\n", "print(type(l_2d))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [3 4 5]]\n", "\n" ] } ], "source": [ "arr = np.array([[0, 1, 2], [3, 4, 5]])\n", "\n", "print(arr)\n", "print(type(arr))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5]\n" ] } ], "source": [ "arr = np.arange(6)\n", "\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [3 4 5]]\n" ] } ], "source": [ "arr = np.arange(6).reshape((2, 3))\n", "\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [3 4 5]]\n", "\n" ] } ], "source": [ "mat = np.matrix([[0, 1, 2], [3, 4, 5]])\n", "\n", "print(mat)\n", "print(type(mat))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [3 4 5]]\n", "\n" ] } ], "source": [ "mat = np.matrix(arr)\n", "\n", "print(mat)\n", "print(type(mat))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]]\n", "\n", "(1, 3)\n" ] } ], "source": [ "mat_1d = np.matrix([0, 1, 2])\n", "\n", "print(mat_1d)\n", "print(type(mat_1d))\n", "print(mat_1d.shape)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# mat_3d = np.matrix([[[0, 1, 2]]])\n", "# ValueError: matrix must be 2-dimensional" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(l_2d[0][1])" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0, 100, 2], [3, 4, 5]]\n" ] } ], "source": [ "l_2d[0][1] = 100\n", "\n", "print(l_2d)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(arr[0][1])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(arr[0, 1])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 100 2]\n", " [ 3 4 5]]\n" ] } ], "source": [ "arr[0, 1] = 100\n", "\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0, 1, 2], [3, 4, 5]]\n" ] } ], "source": [ "l_2d = [[0, 1, 2], [3, 4, 5]]\n", "\n", "print(l_2d)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0, 3], [1, 4], [2, 5]]\n" ] } ], "source": [ "l_2d_t = [list(x) for x in list(zip(*l_2d))]\n", "print(l_2d_t)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [3 4 5]]\n" ] } ], "source": [ "arr = np.arange(6).reshape((2, 3))\n", "\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 3]\n", " [1 4]\n", " [2 5]]\n" ] } ], "source": [ "arr_t = arr.T\n", "\n", "print(arr_t)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "l_2d_1 = [[0, 1, 2], [3, 4, 5]]\n", "l_2d_2 = [[0, 2, 4], [6, 8, 10]]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0, 1, 2], [3, 4, 5], [0, 2, 4], [6, 8, 10]]\n" ] } ], "source": [ "l_2d_sum = l_2d_1 + l_2d_2\n", "\n", "print(l_2d_sum)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# l_2d_sub = l_2d_1 - l_2d_2\n", "# TypeError: unsupported operand type(s) for -: 'list' and 'list'" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [3 4 5]]\n" ] } ], "source": [ "arr1 = np.arange(6).reshape((2, 3))\n", "\n", "print(arr1)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 2 4]\n", " [ 6 8 10]]\n" ] } ], "source": [ "arr2 = np.arange(0, 12, 2).reshape((2, 3))\n", "\n", "print(arr2)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 3 6]\n", " [ 9 12 15]]\n" ] } ], "source": [ "arr_sum = arr1 + arr2\n", "\n", "print(arr_sum)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 -1 -2]\n", " [-3 -4 -5]]\n" ] } ], "source": [ "arr_sub = arr1 - arr2\n", "\n", "print(arr_sub)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "mat1 = np.matrix(arr1)\n", "mat2 = np.matrix(arr2)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 3 6]\n", " [ 9 12 15]]\n" ] } ], "source": [ "mat_sum = mat1 + mat2\n", "\n", "print(mat_sum)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 -1 -2]\n", " [-3 -4 -5]]\n" ] } ], "source": [ "mat_sub = mat1 - mat2\n", "\n", "print(mat_sub)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0, 1, 2], [3, 4, 5], [0, 1, 2], [3, 4, 5]]\n" ] } ], "source": [ "l_2d_mul = l_2d_1 * 2\n", "\n", "print(l_2d_mul)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# l_2d_mul = l_2d_1 * l_2d_2\n", "# TypeError: can't multiply sequence by non-int of type 'list'" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 2 4]\n", " [ 6 8 10]]\n" ] } ], "source": [ "arr_mul = arr1 * 2\n", "\n", "print(arr_mul)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 2 4]\n", " [ 6 8 10]]\n" ] } ], "source": [ "mat_mul = mat1 * 2\n", "\n", "print(mat_mul)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 2 8]\n", " [18 32 50]]\n" ] } ], "source": [ "arr_mul = np.multiply(arr1, arr2)\n", "\n", "print(arr_mul)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 2 8]\n", " [18 32 50]]\n" ] } ], "source": [ "mat_mul = np.multiply(mat1, mat2)\n", "\n", "print(mat_mul)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0 2 8]\n", " [18 32 50]]\n" ] } ], "source": [ "arr_mul = arr1 * arr2\n", "\n", "print(arr_mul)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1]\n", " [2 3]]\n" ] } ], "source": [ "arr1 = np.arange(4).reshape((2, 2))\n", "\n", "print(arr1)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0 1 2]\n", " [3 4 5]]\n" ] } ], "source": [ "arr2 = np.arange(6).reshape((2, 3))\n", "\n", "print(arr2)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 4 5]\n", " [ 9 14 19]]\n" ] } ], "source": [ "arr_mul_matrix = np.dot(arr1, arr2)\n", "\n", "print(arr_mul_matrix)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 4 5]\n", " [ 9 14 19]]\n" ] } ], "source": [ "arr_mul_matrix = arr1.dot(arr2)\n", "\n", "print(arr_mul_matrix)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 4 5]\n", " [ 9 14 19]]\n" ] } ], "source": [ "arr_mul_matrix = np.matmul(arr1, arr2)\n", "\n", "print(arr_mul_matrix)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 4 5]\n", " [ 9 14 19]]\n" ] } ], "source": [ "arr_mul_matrix = arr1 @ arr2\n", "\n", "print(arr_mul_matrix)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": true }, "outputs": [], "source": [ "mat1 = np.matrix(arr1)\n", "mat2 = np.matrix(arr2)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 4 5]\n", " [ 9 14 19]]\n" ] } ], "source": [ "mat_mul_matrix = np.dot(mat1, mat2)\n", "\n", "print(mat_mul_matrix)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 4 5]\n", " [ 9 14 19]]\n" ] } ], "source": [ "mat_mul_matrix = mat1.dot(mat2)\n", "\n", "print(mat_mul_matrix)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 4 5]\n", " [ 9 14 19]]\n" ] } ], "source": [ "mat_mul_matrix = np.matmul(mat1, mat2)\n", "\n", "print(mat_mul_matrix)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 4 5]\n", " [ 9 14 19]]\n" ] } ], "source": [ "mat_mul_matrix = mat1 @ mat2\n", "\n", "print(mat_mul_matrix)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 4 5]\n", " [ 9 14 19]]\n" ] } ], "source": [ "mat_mul_matrix = mat1 * mat2\n", "\n", "print(mat_mul_matrix)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2]\n", " [3 4]]\n" ] } ], "source": [ "arr = np.arange(1, 5).reshape(2, 2)\n", "\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 4]\n", " [ 9 16]]\n" ] } ], "source": [ "arr_p = arr**2\n", "\n", "print(arr_p)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2]\n", " [3 4]]\n" ] } ], "source": [ "mat = np.matrix(arr)\n", "\n", "print(mat)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 7 10]\n", " [15 22]]\n" ] } ], "source": [ "mat_p = mat**2\n", "\n", "print(mat_p)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ True True]\n", " [ True True]]\n" ] } ], "source": [ "print(mat**2 == mat * mat)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ True True]\n", " [ True True]]\n" ] } ], "source": [ "print(mat**3 == mat * mat * mat)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# arr_n = arr**-1\n", "# ValueError: Integers to negative integer powers are not allowed." ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1. 0.5 ]\n", " [0.33333333 0.25 ]]\n" ] } ], "source": [ "arr_f = np.array(arr, dtype=float)\n", "\n", "arr_f_n = arr_f**-1\n", "\n", "print(arr_f_n)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-2. 1. ]\n", " [ 1.5 -0.5]]\n" ] } ], "source": [ "mat_inv = mat**-1\n", "\n", "print(mat_inv)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 5.5 -2.5 ]\n", " [-3.75 1.75]]\n" ] } ], "source": [ "mat_inv2 = mat**-2\n", "\n", "print(mat_inv2)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ True True]\n", " [ True True]]\n" ] } ], "source": [ "print(mat**-2 == mat**-1 * mat**-1)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ True True]\n", " [ True True]]\n" ] } ], "source": [ "print(mat**-3 == mat**-1 * mat**-1 * mat**-1)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }