{ "metadata": { "name": "", "signature": "sha256:fbbaf755166ee8e7ebdd63d35c9afb2492ac0e276f7a7556fca9f9d55cb6eacc" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 1" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# EXERCISE 1: Try to compute the BMI of each subject, as well as the average BMI across subjects\n", "# BMI = weight/(length/100)**2\n", "\n", "subj_length = [180.0,165.0,190.0,172.0,156.0]\n", "subj_weight = [75.0,60.0,83.0,85.0,62.0]\n", "subj_bmi = []" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 2" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# EXERCISE 2: Try to complete the program now!\n", "# Hint: np.mean() computes the mean of an ndarray\n", "# Note that unlike MATLAB, Python does not need the '.' before elementwise operators\n", "\n", "import numpy as np\n", "subj_length = np.array([180.0,165.0,190.0,172.0,156.0])\n", "subj_weight = np.array([75.0,60.0,83.0,85.0,62.0])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 3" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# EXERCISE 3: Create a 2x3 array containing the column-wise and the row-wise means of the original matrix\n", "# Do not use a for-loop, and also do not use the np.mean() function for now.\n", "\n", "arr = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype='float')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 4" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# EXERCISE 4: Create your own meshgrid3d function\n", "# Like np.meshgrid(), it should take two vectors and replicate them; one into columns, the other into rows\n", "# Unlike np.meshgrid(), it should return them as a single 3D array rather than 2D arrays\n", "# ...do not use the np.meshgrid() function" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 5" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# EXERCISE 5: Make a better version of Exercise 3 with what you've just learned\n", "arr = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype='float')\n", "\n", "# What we had:\n", "print np.array([(arr[:,0]+arr[:,1]+arr[:,2])/3,(arr[0,:]+arr[1,:]+arr[2,:])/3])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 6" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# EXERCISE 6: Create a Gabor patch of 100 by 100 pixels\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 7" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# EXERCISE 7: Vectorize the above program\n", "# You get these lines for free...\n", "\n", "import numpy as np\n", "throws = np.random.randint(1,7,(5000,2000))\n", "one = (throws==1)\n", "two = (throws==2)\n", "three = (throws==3)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 8" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# EXERCISE 8: Visualize the difference between the PIL conversion to grayscale, and a simple average of RGB\n", "# Display pixels where the average is LESS luminant in red, and where it is MORE luminant in shades green\n", "# The luminance of these colors should correspond to the size of the difference\n", "# Extra: Maximize the overall contrast in your image\n", "# Extra2: Save as three PNG files, of different sizes (large, medium, small)\n", "\n", "import numpy as np\n", "from PIL import Image\n", "im = Image.open('python.jpg')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 9" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# EXERCISE 9: Plot y=sin(x) and y=sin(x^2) in two separate subplots, one above the other\n", "# Let x range from 0 to 2*pi\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import matplotlib.patches" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise 10" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# EXERCISE 10: Add regression lines\n", "\n", "import numpy as np\n", "from PIL import Image\n", "import matplotlib.pyplot as plt\n", "import matplotlib.lines as lines\n", "\n", "# Open image, convert to an array\n", "im = Image.open('python.jpg')\n", "im = im.resize((400,300))\n", "arr = np.array(im, dtype='float')\n", "\n", "# Split the RGB layers and flatten them\n", "R,G,B = np.dsplit(arr,3)\n", "R = R.flatten()\n", "G = G.flatten()\n", "B = B.flatten()\n", "\n", "# Do the plotting\n", "plt.figure(figsize=(5,5))\n", "plt.plot(R, B, marker='x', linestyle='None', color=(0,0,0.6))\n", "plt.plot(R, G, marker='.', linestyle='None', color=(0,0.35,0))\n", "\n", "# Tweak the plot\n", "plt.axis([0,255,0,255])\n", "plt.xlabel('Red value')\n", "plt.ylabel('Green/Blue value')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution\n", "" ] } ], "metadata": {} } ] }