From d13a77b833264f8ca050b3ff107fa16520a418ba Mon Sep 17 00:00:00 2001 From: Marcel Stampfer Date: Mon, 2 Nov 2015 17:52:37 +0000 Subject: [PATCH] Delete Coursera Stanford ML Python wiki.html --- Coursera Stanford ML Python wiki.html | 5549 ------------------------- 1 file changed, 5549 deletions(-) delete mode 100644 Coursera Stanford ML Python wiki.html diff --git a/Coursera Stanford ML Python wiki.html b/Coursera Stanford ML Python wiki.html deleted file mode 100644 index 6565455..0000000 --- a/Coursera Stanford ML Python wiki.html +++ /dev/null @@ -1,5549 +0,0 @@ - - - -Coursera Stanford ML Python wiki - - - - - - - - - - - - - - - - - - - - -
-
- -
-
-
-
-
-

Python tutorial

This tutorial loosely follows the topics covered in the Octave tutorial in week 2 of the course

-
-
-
-
-
-
-
-
-

The modules needed to run this tutorial are imported below

-
-
-
-
-
-
In [3]:
-
-
-
import numpy as np
-import scipy.io
-import scipy.misc
-import matplotlib.pyplot as plt
-
- -
-
-
- -
-
-
-
-
-
-

Elementary arithmetic operations

Python is capable of working like a calculator with some caveats.
-
-
-
-
-
-
In [75]:
-
-
-
5+6
-
- -
-
-
- -
-
- - -
Out[75]:
- - -
-
11
-
- -
- -
-
- -
-
-
-
In [76]:
-
-
-
3-2
-
- -
-
-
- -
-
- - -
Out[76]:
- - -
-
1
-
- -
- -
-
- -
-
-
-
In [77]:
-
-
-
5*8
-
- -
-
-
- -
-
- - -
Out[77]:
- - -
-
40
-
- -
- -
-
- -
-
-
-
-
-
-
Beware: integer division rounds the result down! You can implicitly convert to a float by adding a '.'
-
-
-
-
-
-
In [78]:
-
-
-
1/2
-
- -
-
-
- -
-
- - -
Out[78]:
- - -
-
0
-
- -
- -
-
- -
-
-
-
In [2]:
-
-
-
1./2 
-
- -
-
-
- -
-
- - -
Out[2]:
- - -
-
0.5
-
- -
- -
-
- -
-
-
-
-
-
-
Exponents use the '**' operator
-
-
-
-
-
-
In [80]:
-
-
-
2**6
-
- -
-
-
- -
-
- - -
Out[80]:
- - -
-
64
-
- -
- -
-
- -
-
-
-
-
-
-

Logical operations

Every object has a boolean value returned from bool(). The following elements are false:
    -
  • None
  • -
  • False
  • -
  • 0
  • -
  • Empty collections: “”, (), [], {}
  • -
- -
-
-
-
-
-
In [5]:
-
-
-
1 and 0 # AND
-
- -
-
-
- -
-
- - -
Out[5]:
- - -
-
0
-
- -
- -
-
- -
-
-
-
In [83]:
-
-
-
1 or 0 # OR
-
- -
-
-
- -
-
- - -
Out[83]:
- - -
-
1
-
- -
- -
-
- -
-
-
-
In [84]:
-
-
-
1 != 0 # XOR
-
- -
-
-
- -
-
- - -
Out[84]:
- - -
-
True
-
- -
- -
-
- -
-
-
-
In [9]:
-
-
-
bool([]) and True # False
-
- -
-
-
- -
-
- - -
Out[9]:
- - -
-
False
-
- -
- -
-
- -
-
-
-
In [12]:
-
-
-
a='foo'
-b='bar'
-bool(a) != bool(b)
-
- -
-
-
- -
-
- - -
Out[12]:
- - -
-
False
-
- -
- -
-
- -
-
-
-
In [13]:
-
-
-
b=None
-bool(a) != bool(b)
-
- -
-
-
- -
-
- - -
Out[13]:
- - -
-
True
-
- -
- -
-
- -
-
-
-
-
-
-

Python variables and types

Displaying variables

Variables are displayed on the console by typing the variable name
-
-
-
-
-
-
In [87]:
-
-
-
b=3
-b
-
- -
-
-
- -
-
- - -
Out[87]:
- - -
-
3
-
- -
- -
-
- -
-
-
-
In [16]:
-
-
-
from math import pi
-b=pi
-b
-
- -
-
-
- -
-
- - -
Out[16]:
- - -
-
3.141592653589793
-
- -
- -
-
- -
-
-
-
-
-
-

floating point numbers are formatted in two ways:

-

The 'old' way (pre-python 2.7):

- -
-
-
-
-
-
In [29]:
-
-
-
print '%1.4f'%b
-
- -
-
-
- -
-
- - -
-
-
3.1416
-
-
-
- -
-
- -
-
-
-
-
-
-
The 'new' way (python 2.7+):
-
-
-
-
-
-
In [31]:
-
-
-
print '{:1.5}'.format(b)
-
- -
-
-
- -
-
- - -
-
-
3.1416
-
-
-
- -
-
- -
-
-
-
-
-
-

Numpy basics

Vectors and matrices

-
-
-
-
-
-
In [40]:
-
-
-
a=np.array([[1,2],[3,4],[5,6]]) # 3x2 numpy matrix
-a
-
- -
-
-
- -
-
- - -
Out[40]:
- - -
-
array([[1, 2],
-       [3, 4],
-       [5, 6]])
-
- -
- -
-
- -
-
-
-
In [38]:
-
-
-
v=[1,2,3]   # ordinary python list
-v
-
- -
-
-
- -
-
- - -
Out[38]:
- - -
-
[1, 2, 3]
-
- -
- -
-
- -
-
-
-
In [39]:
-
-
-
v=np.array([1,2,3]) # numpy array
-v
-
- -
-
-
- -
-
- - -
Out[39]:
- - -
-
array([1, 2, 3])
-
- -
- -
-
- -
-
-
-
-
-
-
Use np.arange(start, stop, increment) to generate a sequence of floats in a numpy array
-
-
-
-
-
-
In [94]:
-
-
-
v=np.arange(1,2,0.1)
-v
-
- -
-
-
- -
-
- - -
Out[94]:
- - -
-
array([ 1. ,  1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9])
-
- -
- -
-
- -
-
-
-
-
-
-
Use tolist() to convert a numpy array to a python list
-
-
-
-
-
-
In [95]:
-
-
-
v.tolist()
-
- -
-
-
- -
-
- - -
Out[95]:
- - -
-
[1.0,
- 1.1,
- 1.2000000000000002,
- 1.3000000000000003,
- 1.4000000000000004,
- 1.5000000000000004,
- 1.6000000000000005,
- 1.7000000000000006,
- 1.8000000000000007,
- 1.9000000000000008]
-
- -
- -
-
- -
-
-
-
-
-
-
The range() built-in function generates integer sequences in a list
-
-
-
-
-
-
In [96]:
-
-
-
v=range(1,6)
-v
-
- -
-
-
- -
-
- - -
Out[96]:
- - -
-
[1, 2, 3, 4, 5]
-
- -
- -
-
- -
-
-
-
-
-
-
numpy's linspace function generates a non-integer sequence with a specific number of elements
-
-
-
-
-
-
In [43]:
-
-
-
v=np.linspace(1,2,11)
-v
-
- -
-
-
- -
-
- - -
Out[43]:
- - -
-
array([ 1. ,  1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ])
-
- -
- -
-
- -
-
-
-
-
-
-

Comprehensions

list comprehensions

List comprehensions allow you to create iterative code without using a loop
-
-
-
-
-
-
In [99]:
-
-
-
v=[1,2,3]
-[e**2 for e in v]
-
- -
-
-
- -
-
- - -
Out[99]:
- - -
-
[1, 4, 9]
-
- -
- -
-
- -
-
-
-
In [100]:
-
-
-
[e**2 for e in v if e%2 !=0]
-
- -
-
-
- -
-
- - -
Out[100]:
- - -
-
[1, 9]
-
- -
- -
-
- -
-
-
-
In [101]:
-
-
-
[e**2 if e%2 != 0 else -1 for e in v]
-
- -
-
-
- -
-
- - -
Out[101]:
- - -
-
[1, -1, 9]
-
- -
- -
-
- -
-
-
-
-
-
-

dictionary comprehensions

Dictionary comprehensions allow to generate dictionaries without a loop
-
-
-
-
-
-
In [102]:
-
-
-
d = {'a':1, 'b':2, 'c':3}   
-{v: k for k, v in d.items()}   # swap keys and values
-{1: 'a', 2: 'b', 3: 'c'}
-
- -
-
-
- -
-
- - -
Out[102]:
- - -
-
{1: 'a', 2: 'b', 3: 'c'}
-
- -
- -
-
- -
-
-
-
-
-
-

set comprehension

Set comrehensions generate sets in a similar way
-
-
-
-
-
-
In [103]:
-
-
-
{x**2 for x in [1, 1, 2]}
-set([1, 4])
-
- -
-
-
- -
-
- - -
Out[103]:
- - -
-
{1, 4}
-
- -
- -
-
- -
-
-
-
-
-
-

Special matrix functions

-
-
-
-
-
-
In [104]:
-
-
-
ones=np.ones((3,2))
-ones
-
- -
-
-
- -
-
- - -
Out[104]:
- - -
-
array([[ 1.,  1.],
-       [ 1.,  1.],
-       [ 1.,  1.]])
-
- -
- -
-
- -
-
-
-
In [105]:
-
-
-
3*ones
-
- -
-
-
- -
-
- - -
Out[105]:
- - -
-
array([[ 3.,  3.],
-       [ 3.,  3.],
-       [ 3.,  3.]])
-
- -
- -
-
- -
-
-
-
In [106]:
-
-
-
np.zeros((3,2))
-
- -
-
-
- -
-
- - -
Out[106]:
- - -
-
array([[ 0.,  0.],
-       [ 0.,  0.],
-       [ 0.,  0.]])
-
- -
- -
-
- -
-
-
-
-
-
-
Generate an array of uniform random numbers
-
-
-
-
-
-
In [107]:
-
-
-
np.random.rand(3,2)
-
- -
-
-
- -
-
- - -
Out[107]:
- - -
-
array([[ 0.62274829,  0.09702351],
-       [ 0.63787983,  0.7836852 ],
-       [ 0.2725037 ,  0.05886131]])
-
- -
- -
-
- -
-
-
-
-
-
-
Generate an array of normal random numbers
-
-
-
-
-
-
In [108]:
-
-
-
np.random.randn(3,2)
-
- -
-
-
- -
-
- - -
Out[108]:
- - -
-
array([[ 0.91457171, -2.09292019],
-       [-0.66121188,  1.47599627],
-       [ 0.5300655 , -0.7618794 ]])
-
- -
- -
-
- -
-
-
-
In [44]:
-
-
-
id=np.eye(3)
-id
-
- -
-
-
- -
-
- - -
Out[44]:
- - -
-
array([[ 1.,  0.,  0.],
-       [ 0.,  1.,  0.],
-       [ 0.,  0.,  1.]])
-
- -
- -
-
- -
-
-
-
In [45]:
-
-
-
3*id
-
- -
-
-
- -
-
- - -
Out[45]:
- - -
-
array([[ 3.,  0.,  0.],
-       [ 0.,  3.,  0.],
-       [ 0.,  0.,  3.]])
-
- -
- -
-
- -
-
-
-
-
-
-

Moving data around

shape and size of a matrix

-
-
-
-
-
-
In [50]:
-
-
-
a=np.random.rand(3,2)
-a
-
- -
-
-
- -
-
- - -
Out[50]:
- - -
-
array([[ 0.71577494,  0.51011023],
-       [ 0.60835921,  0.34374784],
-       [ 0.92554381,  0.85963944]])
-
- -
- -
-
- -
-
-
-
In [51]:
-
-
-
a.shape
-
- -
-
-
- -
-
- - -
Out[51]:
- - -
-
(3, 2)
-
- -
- -
-
- -
-
-
-
In [111]:
-
-
-
a.size
-
- -
-
-
- -
-
- - -
Out[111]:
- - -
-
6
-
- -
- -
-
- -
-
-
-
-
-
-

Loading files in python

-
-
-
-
-
-
-
-
-
Reading the contents of a simple text file
-
-
-
-
-
-
In [1]:
-
-
-
file=open('ex6/emailSample1.txt', 'r')
-file_contents=file.read()
-file_contents
-
- -
-
-
- -
-
- - -
Out[1]:
- - -
-
"> Anyone knows how much it costs to host a web portal ?\n>\nWell, it depends on how many visitors you're expecting.\nThis can be anywhere from less than 10 bucks a month to a couple of $100. \nYou should checkout http://www.rackspace.com/ or perhaps Amazon EC2 \nif youre running something big..\n\nTo unsubscribe yourself from this mailing list, send an email to:\ngroupname-unsubscribe@egroups.com\n\n"
-
- -
- -
-
- -
-
-
-
-
-
-
Loading image files
-
-
-
-
-
-
In [4]:
-
-
-
%pylab inline    # this line works in ipython only
-data = scipy.misc.imread('ex7/bird_small.png')
-plt.imshow(data)
-
- -
-
-
- -
-
- - -
-
-
Populating the interactive namespace from numpy and matplotlib
-
-
-
- -
Out[4]:
- - -
-
<matplotlib.image.AxesImage at 0x104fa6690>
-
- -
- -
- - -
- -
- -
- -
-
- -
-
-
-
-
-
-
Loading the contents of a csv file
-
-
-
-
-
-
In [6]:
-
-
-
data = np.loadtxt('ex0.csv', delimiter=',')
-data
-
- -
-
-
- -
-
- - -
Out[6]:
- - -
-
array([[  6.1101,  17.592 ],
-       [  5.5277,   9.1302],
-       [  8.5186,  13.662 ],
-       [  7.0032,  11.854 ],
-       [  5.8598,   6.8233]])
-
- -
- -
-
- -
-
-
-
-
-
-
Loading a Matlab formatted file
-
-
-
-
-
-
In [7]:
-
-
-
data = scipy.io.loadmat('ex3/ex3data1.mat')
-data
-
- -
-
-
- -
-
- - -
Out[7]:
- - -
-
{'X': array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
-        [ 0.,  0.,  0., ...,  0.,  0.,  0.],
-        [ 0.,  0.,  0., ...,  0.,  0.,  0.],
-        ..., 
-        [ 0.,  0.,  0., ...,  0.,  0.,  0.],
-        [ 0.,  0.,  0., ...,  0.,  0.,  0.],
-        [ 0.,  0.,  0., ...,  0.,  0.,  0.]]),
- '__globals__': [],
- '__header__': 'MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Sun Oct 16 13:09:09 2011',
- '__version__': '1.0',
- 'y': array([[10],
-        [10],
-        [10],
-        ..., 
-        [ 9],
-        [ 9],
-        [ 9]], dtype=uint8)}
-
- -
- -
-
- -
-
-
-
-
-
-

Manipulating matrices

Indexing and Slicing

-
-
-
-
-
-
-
-
-

a[start:end] - items start through end-1

a[start:] - items start through the rest of the array

a[:end] - items from the beginning through end-1

a[:] - a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:end:step] - start through not past end, by step

-
-
-
-
-
-
In [16]:
-
-
-
x = np.arange(10)
-x
-
- -
-
-
- -
-
- - -
Out[16]:
- - -
-
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
-
- -
- -
-
- -
-
-
-
In [119]:
-
-
-
x[:]
-
- -
-
-
- -
-
- - -
Out[119]:
- - -
-
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
-
- -
- -
-
- -
-
-
-
In [120]:
-
-
-
x[1:]
-
- -
-
-
- -
-
- - -
Out[120]:
- - -
-
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
-
- -
- -
-
- -
-
-
-
In [121]:
-
-
-
x[:5]
-
- -
-
-
- -
-
- - -
Out[121]:
- - -
-
array([0, 1, 2, 3, 4])
-
- -
- -
-
- -
-
-
-
In [122]:
-
-
-
x[2]
-
- -
-
-
- -
-
- - -
Out[122]:
- - -
-
2
-
- -
- -
-
- -
-
-
-
In [18]:
-
-
-
x[1:7:2]
-
- -
-
-
- -
-
- - -
Out[18]:
- - -
-
array([1, 3, 5])
-
- -
- -
-
- -
-
-
-
-
-
-

Negative indices

a[-1] - last item in the array
a[-2:] - last two items in the array
a[:-2] - everything except the last two items
-
-
-
-
-
-
In [17]:
-
-
-
x[:-2]
-
- -
-
-
- -
-
- - -
Out[17]:
- - -
-
array([0, 1, 2, 3, 4, 5, 6, 7])
-
- -
- -
-
- -
-
-
-
-
-
-
2d matrices are accessed in the row, column order
-
-
-
-
-
-
In [20]:
-
-
-
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
-arr2d
-
- -
-
-
- -
-
- - -
Out[20]:
- - -
-
array([[1, 2, 3],
-       [4, 5, 6],
-       [7, 8, 9]])
-
- -
- -
-
- -
-
-
-
In [21]:
-
-
-
arr2d[2]
-
- -
-
-
- -
-
- - -
Out[21]:
- - -
-
array([7, 8, 9])
-
- -
- -
-
- -
-
-
-
In [22]:
-
-
-
arr2d[0]
-
- -
-
-
- -
-
- - -
Out[22]:
- - -
-
array([1, 2, 3])
-
- -
- -
-
- -
-
-
-
In [132]:
-
-
-
arr2d[0,1]
-
- -
-
-
- -
-
- - -
Out[132]:
- - -
-
2
-
- -
- -
-
- -
-
-
-
-
-
-

Boolean indexing

Index selection can be done by filtering elements with boolean values

-
-
-
-
-
-
In [133]:
-
-
-
mat = np.array(['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']).reshape((3,3))
-mat
-
- -
-
-
- -
-
- - -
Out[133]:
- - -
-
array([['The', 'quick', 'brown'],
-       ['fox', 'jumped', 'over'],
-       ['the', 'lazy', 'dog']], 
-      dtype='|S6')
-
- -
- -
-
- -
-
-
-
In [134]:
-
-
-
rand = np.random.randn(3,3)>0
-rand
-
- -
-
-
- -
-
- - -
Out[134]:
- - -
-
array([[False, False,  True],
-       [ True,  True,  True],
-       [ True, False, False]], dtype=bool)
-
- -
- -
-
- -
-
-
-
In [135]:
-
-
-
mat[rand]
-
- -
-
-
- -
-
- - -
Out[135]:
- - -
-
array(['brown', 'fox', 'jumped', 'over', 'the'], 
-      dtype='|S6')
-
- -
- -
-
- -
-
-
-
-
-
-

Flattening

Reshaping from a higher dimensional to one dimensional order is called flattening

-
-
-
-
-
-
In [24]:
-
-
-
arr = np.arange(9).reshape((3,3))
-arr
-
- -
-
-
- -
-
- - -
Out[24]:
- - -
-
array([[0, 1, 2],
-       [3, 4, 5],
-       [6, 7, 8]])
-
- -
- -
-
- -
-
-
-
-
-
-
The flatten() function returns a copy of the array
-
-
-
-
-
-
In [25]:
-
-
-
arr.flatten()
-
- -
-
-
- -
-
- - -
Out[25]:
- - -
-
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
-
- -
- -
-
- -
-
-
-
-
-
-
flattening can be done columnwise
-
-
-
-
-
-
In [29]:
-
-
-
arr.flatten(1)
-
- -
-
-
- -
-
- - -
Out[29]:
- - -
-
array([0, 3, 6, 1, 4, 7, 2, 5, 8])
-
- -
- -
-
- -
-
-
-
-
-
-
the ravel() function doesn't return a copy of the underlying data
-
-
-
-
-
-
In [27]:
-
-
-
arr.ravel()
-
- -
-
-
- -
-
- - -
Out[27]:
- - -
-
array([0, 3, 6, 1, 4, 7, 2, 5, 8])
-
- -
- -
-
- -
-
-
-
-
-
-

Vector assignments

Python doesn't create copies of underlying data on assignment statements

-
-
-
-
-
-
In [31]:
-
-
-
arr = np.arange(10)
-arr
-
- -
-
-
- -
-
- - -
Out[31]:
- - -
-
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
-
- -
- -
-
- -
-
-
-
-
-
-
create a reference to some elements in the array and reassign them
-
-
-
-
-
-
In [32]:
-
-
-
slice=arr[4:8]
-slice
-
- -
-
-
- -
-
- - -
Out[32]:
- - -
-
array([4, 5, 6, 7])
-
- -
- -
-
- -
-
-
-
In [33]:
-
-
-
slice[:]=-5
-slice
-
- -
-
-
- -
-
- - -
Out[33]:
- - -
-
array([-5, -5, -5, -5])
-
- -
- -
-
- -
-
-
-
In [34]:
-
-
-
slice[1]=50
-slice
-
- -
-
-
- -
-
- - -
Out[34]:
- - -
-
array([-5, 50, -5, -5])
-
- -
- -
-
- -
-
-
-
In [35]:
-
-
-
arr
-
- -
-
-
- -
-
- - -
Out[35]:
- - -
-
array([ 0,  1,  2,  3, -5, 50, -5, -5,  8,  9])
-
- -
- -
-
- -
-
-
-
-
-
-
now create a copy of the array explicitly and reassign
-
-
-
-
-
-
In [145]:
-
-
-
arr_copy=arr.copy()
-arr_copy
-
- -
-
-
- -
-
- - -
Out[145]:
- - -
-
array([ 0,  1,  2,  3, -5, 50, -5, -5,  8,  9])
-
- -
- -
-
- -
-
-
-
In [146]:
-
-
-
arr_copy[4:8]=20
-arr_copy
-
- -
-
-
- -
-
- - -
Out[146]:
- - -
-
array([ 0,  1,  2,  3, 20, 20, 20, 20,  8,  9])
-
- -
- -
-
- -
-
-
-
-
-
-
The original array is unchanged
-
-
-
-
-
-
In [147]:
-
-
-
arr
-
- -
-
-
- -
-
- - -
Out[147]:
- - -
-
array([ 0,  1,  2,  3, -5, 50, -5, -5,  8,  9])
-
- -
- -
-
- -
-
-
-
-
-
-

Horizontal and vertical concatenation

There are two ways to concatenate

-
-
-
-
-
-
In [148]:
-
-
-
mat = np.array(['The', 'quick', 'brown', 'fox'])
-mat2 = np.array(['jumped', 'over', 'the', 'lazy'])
-
- -
-
-
- -
-
-
-
-
-
-
Method 1: Use stacking
-
-
-
-
-
-
In [149]:
-
-
-
 np.hstack((mat,mat2))
-
- -
-
-
- -
-
- - -
Out[149]:
- - -
-
array(['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy'], 
-      dtype='|S6')
-
- -
- -
-
- -
-
-
-
In [150]:
-
-
-
 np.vstack((mat,mat2))
-
- -
-
-
- -
-
- - -
Out[150]:
- - -
-
array([['The', 'quick', 'brown', 'fox'],
-       ['jumped', 'over', 'the', 'lazy']], 
-      dtype='|S6')
-
- -
- -
-
- -
-
-
-
In [151]:
-
-
-
 np.column_stack((mat,mat2))
-
- -
-
-
- -
-
- - -
Out[151]:
- - -
-
array([['The', 'jumped'],
-       ['quick', 'over'],
-       ['brown', 'the'],
-       ['fox', 'lazy']], 
-      dtype='|S6')
-
- -
- -
-
- -
-
-
-
-
-
-
Method 2: Use the concatenate() function applied to an axis
-
-
-
-
-
-
In [152]:
-
-
-
arr = np.arange(12).reshape((3, 4))
-arr
-
- -
-
-
- -
-
- - -
Out[152]:
- - -
-
array([[ 0,  1,  2,  3],
-       [ 4,  5,  6,  7],
-       [ 8,  9, 10, 11]])
-
- -
- -
-
- -
-
-
-
In [153]:
-
-
-
np.concatenate((arr,arr), axis=1)
-
- -
-
-
- -
-
- - -
Out[153]:
- - -
-
array([[ 0,  1,  2,  3,  0,  1,  2,  3],
-       [ 4,  5,  6,  7,  4,  5,  6,  7],
-       [ 8,  9, 10, 11,  8,  9, 10, 11]])
-
- -
- -
-
- -
-
-
-
In [154]:
-
-
-
np.concatenate((arr,arr), axis=0)
-
- -
-
-
- -
-
- - -
Out[154]:
- - -
-
array([[ 0,  1,  2,  3],
-       [ 4,  5,  6,  7],
-       [ 8,  9, 10, 11],
-       [ 0,  1,  2,  3],
-       [ 4,  5,  6,  7],
-       [ 8,  9, 10, 11]])
-
- -
- -
-
- -
-
-
-
In [155]:
-
-
-
arr = np.arange(5)
-np.concatenate((arr,arr), axis=0)
-
- -
-
-
- -
-
- - -
Out[155]:
- - -
-
array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4])
-
- -
- -
-
- -
-
-
-
-
-
-

Matrix multiplication

-
-
-
-
-
-
In [156]:
-
-
-
x=np.array([[1,2,3], [4,5,6], [7,8,9]])
-y=np.array([[1,2,3], [4,5,6], [7,8,9]])
-np.dot(x,y)
-
- -
-
-
- -
-
- - -
Out[156]:
- - -
-
array([[ 30,  36,  42],
-       [ 66,  81,  96],
-       [102, 126, 150]])
-
- -
- -
-
- -
-
-
-
-
-
-
Matrix multiplication is done using the dot() function
-
-
-
-
-
-
In [157]:
-
-
-
x.dot(y)
-
- -
-
-
- -
-
- - -
Out[157]:
- - -
-
array([[ 30,  36,  42],
-       [ 66,  81,  96],
-       [102, 126, 150]])
-
- -
- -
-
- -
-
-
-
-
-
-
Element-wise multiplication using the '*' operator
-
-
-
-
-
-
In [159]:
-
-
-
x*y
-
- -
-
-
- -
-
- - -
Out[159]:
- - -
-
array([[ 1,  4,  9],
-       [16, 25, 36],
-       [49, 64, 81]])
-
- -
- -
-
- -
-
-
-
-
-
-
Element-wise squaring
-
-
-
-
-
-
In [38]:
-
-
-
x**2
-
- -
-
-
- -
-
- - -
Out[38]:
- - -
-
array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])
-
- -
- -
-
- -
-
-
-
-
-
-
Element-wise reciprical
-
-
-
-
-
-
In [161]:
-
-
-
1./x
-
- -
-
-
- -
-
- - -
Out[161]:
- - -
-
array([[ 1.        ,  0.5       ,  0.33333333],
-       [ 0.25      ,  0.2       ,  0.16666667],
-       [ 0.14285714,  0.125     ,  0.11111111]])
-
- -
- -
-
- -
-
-
-
-
-
-
Element-wise logarithms/exponents
-
-
-
-
-
-
In [162]:
-
-
-
np.log(x)
-
- -
-
-
- -
-
- - -
Out[162]:
- - -
-
array([[ 0.        ,  0.69314718,  1.09861229],
-       [ 1.38629436,  1.60943791,  1.79175947],
-       [ 1.94591015,  2.07944154,  2.19722458]])
-
- -
- -
-
- -
-
-
-
In [163]:
-
-
-
np.exp(x)
-
- -
-
-
- -
-
- - -
Out[163]:
- - -
-
array([[  2.71828183e+00,   7.38905610e+00,   2.00855369e+01],
-       [  5.45981500e+01,   1.48413159e+02,   4.03428793e+02],
-       [  1.09663316e+03,   2.98095799e+03,   8.10308393e+03]])
-
- -
- -
-
- -
-
-
-
-
-
-
Element-wise addition
-
-
-
-
-
-
In [165]:
-
-
-
1+x
-
- -
-
-
- -
-
- - -
Out[165]:
- - -
-
array([[ 2,  3,  4],
-       [ 5,  6,  7],
-       [ 8,  9, 10]])
-
- -
- -
-
- -
-
-
-
-
-
-

Transpose of a matrix

-
-
-
-
-
-
In [167]:
-
-
-
x.T
-
- -
-
-
- -
-
- - -
Out[167]:
- - -
-
array([[1, 4, 7],
-       [2, 5, 8],
-       [3, 6, 9]])
-
- -
- -
-
- -
-
-
-
-
-
-

Maximum and minimum of matrix values

-
-
-
-
-
-
In [170]:
-
-
-
np.max(x)
-
- -
-
-
- -
-
- - -
Out[170]:
- - -
-
9
-
- -
- -
-
- -
-
-
-
In [171]:
-
-
-
np.min(x)
-
- -
-
-
- -
-
- - -
Out[171]:
- - -
-
1
-
- -
- -
-
- -
-
-
-
-
-
-

Sum and product of all elements

-
-
-
-
-
-
In [173]:
-
-
-
np.sum(x)
-
- -
-
-
- -
-
- - -
Out[173]:
- - -
-
45
-
- -
- -
-
- -
-
-
-
In [174]:
-
-
-
np.sum(x,axis=0)
-
- -
-
-
- -
-
- - -
Out[174]:
- - -
-
array([12, 15, 18])
-
- -
- -
-
- -
-
-
-
In [175]:
-
-
-
np.sum(x,axis=1)
-
- -
-
-
- -
-
- - -
Out[175]:
- - -
-
array([ 6, 15, 24])
-
- -
- -
-
- -
-
-
-
In [176]:
-
-
-
np.sum(x)
-
- -
-
-
- -
-
- - -
Out[176]:
- - -
-
45
-
- -
- -
-
- -
-
-
-
In [177]:
-
-
-
np.product(x)
-
- -
-
-
- -
-
- - -
Out[177]:
- - -
-
362880
-
- -
- -
-
- -
-
-
-
In [178]:
-
-
-
np.product(x,axis=0)
-
- -
-
-
- -
-
- - -
Out[178]:
- - -
-
array([ 28,  80, 162])
-
- -
- -
-
- -
-
-
-
In [179]:
-
-
-
np.product(x,axis=1)
-
- -
-
-
- -
-
- - -
Out[179]:
- - -
-
array([  6, 120, 504])
-
- -
- -
-
- -
-
-
-
-
-
-

Inverse and pseudo-inverse of a matrix

-
-
-
-
-
-
In [181]:
-
-
-
x=2*np.eye(3)
-np.linalg.inv(x)
-
- -
-
-
- -
-
- - -
Out[181]:
- - -
-
array([[ 0.5,  0. ,  0. ],
-       [ 0. ,  0.5,  0. ],
-       [ 0. ,  0. ,  0.5]])
-
- -
- -
-
- -
-
-
-
In [182]:
-
-
-
np.linalg.pinv(x)
-
- -
-
-
- -
-
- - -
Out[182]:
- - -
-
array([[ 0.5,  0. ,  0. ],
-       [ 0. ,  0.5,  0. ],
-       [ 0. ,  0. ,  0.5]])
-
- -
- -
-
- -
-
-
-
-
-
-

Plotting data

Plotting generated data

-
-
-
-
-
-
In [183]:
-
-
-
plt.plot(np.arange(10))
-
- -
-
-
- -
-
- - -
Out[183]:
- - -
-
[<matplotlib.lines.Line2D at 0x10dd05f50>]
-
- -
- -
- - -
- -
- -
- -
-
- -
-
-
-
-
-
-

Line color, labels, title and legend

Saving a graph

Subplots

Axis scaling

Creating/clearing figures

-
-
-
-
-
-
-
-
-

Control statements

For loops

-
-
-
-
-
-
In [184]:
-
-
-
li = ['a', 'b', 'e']
-for e in li:
-    print e
-
- -
-
-
- -
-
- - -
-
-
a
-b
-e
-
-
-
- -
-
- -
-
-
-
In [185]:
-
-
-
d = enumerate(li)
-for k,v in d:
-    print k,v
-
- -
-
-
- -
-
- - -
-
-
0 a
-1 b
-2 e
-
-
-
- -
-
- -
-
-
-
-
-
-

While loops

-
-
-
-
-
-
In [186]:
-
-
-
n = ''
-while n.strip() != 'hello':
-    n = raw_input("Please enter 'hello':")
-
- -
-
-
- -
-
- - -
-
-
Please enter 'hello':goodbye
-Please enter 'hello':hello
-
-
-
- -
-
- -
-
-
-
-
-
-

break statement

-
-
-
-
-
-
In [188]:
-
-
-
while True:
-    n = raw_input("Please enter 'hello':")
-    if n.strip() == 'hello':
-        break
-
- -
-
-
- -
-
- - -
-
-
Please enter 'hello':bye
-Please enter 'hello':hello
-
-
-
- -
-
- -
-
-
-
-
-
-

if-elif-else statement

-
-
-
-
-
-
In [189]:
-
-
-
x = int(raw_input("Please enter an integer: "))
-
-if x < 0:
-    x = 0
-    print 'Negative changed to zero'
-elif x == 0:
-    print 'Zero'
-elif x == 1:
-    print 'Single'
-else:
-    print 'More'
-
- -
-
-
- -
-
- - -
-
-
Please enter an integer: 42
-More
-
-
-
- -
-
- -
-
-
-
-
-
-

Functions

PYTHONPATH environment variable

Vectorization

Vectorized implementation

Unvectorized implementation

-
-
-
-
-
- -