diff --git a/.ipynb_checkpoints/Homework-0-checkpoint.ipynb b/.ipynb_checkpoints/Homework-0-checkpoint.ipynb new file mode 100644 index 0000000..11973fb --- /dev/null +++ b/.ipynb_checkpoints/Homework-0-checkpoint.ipynb @@ -0,0 +1,286 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Homework 0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The goal of this homework is to practice the skills learned in Lecture 2. These skills will be essential for the rest of this course." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Before starting to work on this homework, **make a new folder** named ```hw0-submission``` in your private repo and download ```Homework-0.ipynb``` to that folder." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's make sure that you placed everything in the correct folder" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You did everything correctly. Congrats!\n" + ] + } + ], + "source": [ + "import os\n", + "error = False\n", + "\n", + "if os.path.basename(os.path.realpath('.')) != \"hw0-submission\":\n", + " print \"The name of the current directory should be 'hw0-submission'\"\n", + " error = True\n", + "\n", + "if not error:\n", + " print \"You did everything correctly. Congrats!\"\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now, assuming that everything is in their correct place, you should ``git add`` all the new directories/files and make your initial ```git commit```." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## This is a Markdown cell" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Insert and edit your own markdown cell *below*. Write your favorite quote" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello world\n" + ] + } + ], + "source": [ + "# This is a code cell. Try executing it.\n", + "print \"hello world\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Write a function that takes as argument a string and returns its 3-letter suffix. If the string is shorter than 3 letters, return the whole string." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def suffix_3(string):\n", + " \"\"\"\n", + " Returns the 3-letter suffix.\n", + " \n", + " Parameters:\n", + " string: str\n", + " \n", + " Returns:\n", + " suffix: str\n", + " If `string` is more than 3-letters long, `suffix` is its 3-letter suffix. Otherwise, `suffix` is the same as `string`.\n", + " \"\"\"\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "print suffix_3(\"Hi!\")\n", + "print suffix_3(\"Superb\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "When you finish with the above function, don't forget to ```add``` and ```commit``` to your local git repository." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's print the versions of some packages that we will be using in the future. If any of these packages are not present, ```conda install``` them." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import numpy\n", + "print 'numpy:', numpy.__version__\n", + "\n", + "import scipy\n", + "print 'scipy:', scipy.__version__\n", + "\n", + "import matplotlib\n", + "print 'matplotlib:', matplotlib.__version__\n", + "\n", + "import sklearn\n", + "print 'scikit-learn:', sklearn.__version__\n", + "\n", + "import pandas\n", + "print 'pandas:', pandas.__version__" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Write a function that takes as argument a 2D list representing a matrix **M** and returns a boolean indicating whether **M** is an orthogonal matrix." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy\n", + "\n", + "def is_matrix_orthogonal(M):\n", + " \"\"\"\n", + " Returns whether M is an orthogonal matrix.\n", + " \n", + " Parameters:\n", + " M: 2D list\n", + " \n", + " Returns:\n", + " is_orthogonal: boolean\n", + " \"\"\"\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "print is_matrix_orthogonal([[1, 0], [0, -1]])\n", + "print is_matrix_orthogonal([[-1.2396, -4.3801, -1.7737], [-1.3121, -2.9193, -4.5496], [-2.1143, 0.0363, 0.2022]])\n", + "print is_matrix_orthogonal([[-0.8185, 0.5740, -0.0249], [-0.2510, -0.3183, 0.9142], [-0.5168, -0.7544, -0.4046]])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "When you finish with the above function, don't forget to ```add``` and ```commit``` to your local git repository." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Submitting" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now, you can push your changes to Github." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# Code for setting the style of the notebook\n", + "from IPython.core.display import HTML\n", + "def css_styling():\n", + " styles = open(\"../../theme/custom.css\", \"r\").read()\n", + " return HTML(styles)\n", + "css_styling()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.11" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/Homework-0.ipynb b/Homework-0.ipynb index 6db6c6b..8d9f73d 100644 --- a/Homework-0.ipynb +++ b/Homework-0.ipynb @@ -30,11 +30,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You did everything correctly. Congrats!\n" + ] + } + ], "source": [ "import os\n", "error = False\n", @@ -44,7 +52,8 @@ " error = True\n", "\n", "if not error:\n", - " print \"You did everything correctly. Congrats!\"" + " print \"You did everything correctly. Congrats!\"\n", + " " ] }, { @@ -74,14 +83,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello world\n" + ] + } + ], "source": [ "# This is a code cell. Try executing it.\n", - "print \"hi\"" + "print \"hello world\"" ] }, { @@ -93,7 +110,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -110,16 +127,27 @@ " suffix: str\n", " If `string` is more than 3-letters long, `suffix` is its 3-letter suffix. Otherwise, `suffix` is the same as `string`.\n", " \"\"\"\n", + " return string[0:3]\n", + "\n", " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hi!\n", + "Sup\n" + ] + } + ], "source": [ "print suffix_3(\"Hi!\")\n", "print suffix_3(\"Superb\")" @@ -141,11 +169,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "numpy: 1.10.1\n", + "scipy: 0.16.0\n", + "matplotlib: 1.5.0\n", + "scikit-learn: 0.17\n", + "pandas: 0.17.1\n" + ] + } + ], "source": [ "import numpy\n", "print 'numpy:', numpy.__version__\n", @@ -172,13 +212,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], "source": [ - "import numpy\n", + "import numpy as np\n", "\n", "def is_matrix_orthogonal(M):\n", " \"\"\"\n", @@ -190,17 +230,51 @@ " Returns:\n", " is_orthogonal: boolean\n", " \"\"\"\n", + " \n", + " x = np.matrix(M)\n", + " \n", + " if x.shape == (2,2):\n", + " y= x.getT()\n", + " z = [[1, 0],[0, 1]]\n", + " s= np.matrix(z)\n", + " l = x*y\n", + " if l.all() == s.all():\n", + " return True\n", + " else:\n", + " return False\n", + " elif x.shape == (3,3):\n", + " \n", + " y= x.getT()\n", + " z = [[1, 0, 0],[0, 1, 0], [0, 0, 1]]\n", + " s= np.matrix(z)\n", + " l = x*y\n", + " if l.all() == s.all():\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + " \n", " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "collapsed": false, "scrolled": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "False\n" + ] + } + ], "source": [ "print is_matrix_orthogonal([[1, 0], [0, -1]])\n", "print is_matrix_orthogonal([[-1.2396, -4.3801, -1.7737], [-1.3121, -2.9193, -4.5496], [-2.1143, 0.0363, 0.2022]])\n",