{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Introduction to data manipulation with scientific Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this section we'll go through the basics of the scientific Python stack for data manipulation: using numpy and matplotlib.\n", "\n", "You can skip this section if you already know the scipy stack." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "______\n", "\n", "**To learn the scientific Python ecosystem**: http://scipy-lectures.org" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Start pylab inline mode, so figures will appear in the notebook\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Numpy Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Manipulating `numpy` arrays is an important part of doing machine learning\n", "(or, really, any type of scientific computation) in Python. This will likely\n", "be review for most: we'll quickly go through some of the most important features." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "\n", "# Generating a random array\n", "X = np.random.random((3, 5)) # a 3 x 5 array\n", "\n", "print(X)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Accessing elements\n", "\n", "# get a single element\n", "print(X[0, 0])\n", "\n", "# get a row\n", "print(X[1])\n", "\n", "# get a column\n", "print(X[:, 1])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Transposing an array\n", "print(X.T)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Turning a row vector into a column vector\n", "y = np.linspace(0, 12, 5)\n", "print(y)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# make into a column vector\n", "print(y[:, np.newaxis])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is much, much more to know, but these few operations are fundamental to what we'll\n", "do during this tutorial." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Scipy Sparse Matrices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We won't make very much use of these in this tutorial, but sparse matrices are very nice\n", "in some situations. For example, in some machine learning tasks, especially those associated\n", "with textual analysis, the data may be mostly zeros. Storing all these zeros is very\n", "inefficient. We can create and manipulate sparse matrices as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from scipy import sparse\n", "\n", "# Create a random array with a lot of zeros\n", "X = np.random.random((10, 5))\n", "print(X)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# set the majority of elements to zero\n", "X[X < 0.7] = 0\n", "print(X)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# turn X into a csr (Compressed-Sparse-Row) matrix\n", "X_csr = sparse.csr_matrix(X)\n", "print(X_csr)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# convert the sparse matrix to a dense array\n", "print(X_csr.toarray())" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Matplotlib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another important part of machine learning is visualization of data. The most common\n", "tool for this in Python is `matplotlib`. It is an extremely flexible package, but\n", "we will go over some basics here.\n", "\n", "First, something special to IPython notebook. We can turn on the \"IPython inline\" mode,\n", "which will make plots show up inline in the notebook." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Here we import the plotting functions\n", "import matplotlib.pyplot as plt" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# plotting a line\n", "x = np.linspace(0, 10, 100)\n", "plt.plot(x, np.sin(x))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# scatter-plot points\n", "x = np.random.normal(size=500)\n", "y = np.random.normal(size=500)\n", "plt.scatter(x, y)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# showing images\n", "x = np.linspace(1, 12, 100)\n", "y = x[:, np.newaxis]\n", "\n", "im = y * np.sin(x) * np.cos(y)\n", "print(im.shape)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# imshow - note that origin is at the top-left by default!\n", "plt.imshow(im)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Contour plot - note that origin here is at the bottom-left by default!\n", "plt.contour(im)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are many, many more plot types available. One useful way to explore these is by\n", "looking at the matplotlib gallery: http://matplotlib.org/gallery.html\n", "\n", "You can test these examples out easily in the notebook: simply copy the ``Source Code``\n", "link on each page, and put it in a notebook using the ``%load`` magic.\n", "For example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load http://matplotlib.org/mpl_examples/pylab_examples/ellipse_collection.py" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "_______\n", "\n", "\n" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Additional plots: 3D -- skipped if lack of time" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# 3D plotting\n", "from mpl_toolkits.mplot3d import Axes3D\n", "ax = plt.axes(projection='3d')\n", "xgrid, ygrid = np.meshgrid(x, y.ravel())\n", "ax.plot_surface(xgrid, ygrid, im, cmap=plt.cm.jet, cstride=2, rstride=2, linewidth=0)" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }