{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "a = [1,2,3]\n", "print a\n", "#list" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3]\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "'yellow' + '_' + 'orange'" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "'yellow_orange'" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "b = 2/5\n", "b" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "0" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "float(2)/float(5)\n" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "0.4" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "import datetime\n", "\n", "tweet = {\n", " 'date': datetime.datetime(2014, 4, 1, 23, 14, 20),\n", " 'user': 899110,\n", " 'text': '<3 OMG BEST FRIENDS ARE THE BEST #bestiesforlife #oclove',\n", " 'location' : {\n", " 'lat': 27.0,\n", " 'lng': 114.9,\n", " 'hex': 'Efabe3'\n", " }\n", "}\n", "#dictionary\n", "#datetime" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "print tweet['text']" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "<3 OMG BEST FRIENDS ARE THE BEST #bestiesforlife #oclove\n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "some_values = [100,107.7,92]\n", "print sum(some_values)/len(some_values)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "99.9\n" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "def mean(arg):\n", " return sum(arg) / len(arg)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "mean(some_values)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "99.89999999999999" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "\n", "a = np.array([[1, 2, 3], [4, 5, 6]])\n", "b = np.array([[1, 2, 3], [4, 5, 6]])\n", "c = np.array([[1, 2], [3, 4], [5,6]])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "a*b" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "array([[ 1, 4, 9],\n", " [16, 25, 36]])" ] } ], "prompt_number": 25 }, { "cell_type": "code", "collapsed": false, "input": [ "help(np.dot)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Help on built-in function dot in module numpy.core._dotblas:\n", "\n", "dot(...)\n", " dot(a, b, out=None)\n", " \n", " Dot product of two arrays.\n", " \n", " For 2-D arrays it is equivalent to matrix multiplication, and for 1-D\n", " arrays to inner product of vectors (without complex conjugation). For\n", " N dimensions it is a sum product over the last axis of `a` and\n", " the second-to-last of `b`::\n", " \n", " dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])\n", " \n", " Parameters\n", " ----------\n", " a : array_like\n", " First argument.\n", " b : array_like\n", " Second argument.\n", " out : ndarray, optional\n", " Output argument. This must have the exact kind that would be returned\n", " if it was not used. In particular, it must have the right type, must be\n", " C-contiguous, and its dtype must be the dtype that would be returned\n", " for `dot(a,b)`. This is a performance feature. Therefore, if these\n", " conditions are not met, an exception is raised, instead of attempting\n", " to be flexible.\n", " \n", " Returns\n", " -------\n", " output : ndarray\n", " Returns the dot product of `a` and `b`. If `a` and `b` are both\n", " scalars or both 1-D arrays then a scalar is returned; otherwise\n", " an array is returned.\n", " If `out` is given, then it is returned.\n", " \n", " Raises\n", " ------\n", " ValueError\n", " If the last dimension of `a` is not the same size as\n", " the second-to-last dimension of `b`.\n", " \n", " See Also\n", " --------\n", " vdot : Complex-conjugating dot product.\n", " tensordot : Sum products over arbitrary axes.\n", " einsum : Einstein summation convention.\n", " \n", " Examples\n", " --------\n", " >>> np.dot(3, 4)\n", " 12\n", " \n", " Neither argument is complex-conjugated:\n", " \n", " >>> np.dot([2j, 3j], [2j, 3j])\n", " (-13+0j)\n", " \n", " For 2-D arrays it's the matrix product:\n", " \n", " >>> a = [[1, 0], [0, 1]]\n", " >>> b = [[4, 1], [2, 2]]\n", " >>> np.dot(a, b)\n", " array([[4, 1],\n", " [2, 2]])\n", " \n", " >>> a = np.arange(3*4*5*6).reshape((3,4,5,6))\n", " >>> b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))\n", " >>> np.dot(a, b)[2,3,2,1,2,2]\n", " 499128\n", " >>> sum(a[2,3,2,:] * b[1,2,:,2])\n", " 499128\n", "\n" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "a.dot(b)\n", "#The number of columns in a must match the number of rows in b for matrix multiplication" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "objects are not aligned", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: objects are not aligned" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "np.dot(a,b)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "objects are not aligned", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: objects are not aligned" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "a.dot(c)\n", "#The number of columns in a matches the number of rows in c for matrix multiplication\n", "# = [[1*1 + 2*3 + 3*5 = 22, 1*2 + 2*4 + 3*6 = 28], \n", "# [4*1, 5*3, 6*5 = 49, 4*2 + 5*4 + 6*6 = 64]]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "array([[22, 28],\n", " [49, 64]])" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "np.dot(a,c)\n", "#yes it's the same because a is the first matrix using either notation" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 36, "text": [ "array([[22, 28],\n", " [49, 64]])" ] } ], "prompt_number": 36 }, { "cell_type": "code", "collapsed": false, "input": [ "np.dot(c,a)\n", "#not the same because the result will have the same number of rows as the 1st matrix\n", "#and the same number of columns as the 2nd matrix:\n", "#above we are multiplying a 2x3 with 3x2 = 2x2\n", "#and here is a 3x2 with 2x3 = 3x3\n", "# = [[1*1 + 2*4 = 9, 1*2 + 2*5 = 12, 1*3 + 2*6 + 15],\n", "# [3*1 + 4*4 = 19, 3*2 + 4*5 + 26, 3*3 + 4*6 + 33],\n", "# [5*1 + 6*4 = 29, 5*2 + 6*5 = 40, 5*3 + 6*6 = 51]]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 37, "text": [ "array([[ 9, 12, 15],\n", " [19, 26, 33],\n", " [29, 40, 51]])" ] } ], "prompt_number": 37 } ], "metadata": {} } ] }