-
Notifications
You must be signed in to change notification settings - Fork 227
Home
#Welcome to the Coursera Stanford ML Python wiki!
##Python and package installation ##Basic idle usage ##Basic Python operations ###Basic algebra in python ####Elementary arithmetic operation
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 5+6
11
>>> 3-2
1
>>> 5*8
40
>>> 1/2
0
>>> 1./2 # Explain this
0.5
>>> 2**6
64
####Logical operations
>>> 1 != 2 # True
True
>>> 1 and 0 # False
0
>>> 1 or 0 # True
1
>>> 1 != 0 # XOR
True
>>> a='foo'
>>> b='bar'
>>> bool(a) != bool(b)
False
>>> b=None
>>> bool(a) != bool(b) # Explain this
True
####Python variables and types ####Displaying variables
>>> b=3
>>> b
3
>>> from math import pi
>>> b=pi
>>> b
3.141592653589793
>>> '%1.4f'%b
'3.1416'
>>> '{:1.5}'.format(b)
'3.1416'
##Numpy basics : Vectors and matrices
>>> import numpy as np
>>> a=np.array([[1,2],[3,4],[5,6]])
>>> a
array([[1, 2],
[3, 4],
[5, 6]])
>>> v=[1,2,3] # list
>>> v
[1, 2, 3]
>>> v=np.array([1,2,3]) # numpy array
>>> v
array([1, 2, 3])
>>> v=np.arange(1,2,0.1)
>>> v
array([ 1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9])
>>> v.tolist()
[1.0, 1.1, 1.2000000000000002, 1.3000000000000003, 1.4000000000000004, 1.5000000000000004, 1.6000000000000005, 1.7000000000000006, 1.8000000000000007, 1.9000000000000008]
>>> v=range(1,6)
>>> v
[1, 2, 3, 4, 5]
>>> v=np.linspace(1,2,11)
>>> v
array([ 1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. ])
###Comprehensions ####list comprehension
>>> v=[1,2,3]
>>> [e**2 for e in v]
[1, 4, 9]
>>> [e**2 for e in v if e%2 !=0]
[1, 9]
>>> [e**2 if e%2 != 0 else -1 for e in v]
[1, -1, 9]
####dictionary comprehension
>>> 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'}
####set comprehension
>>> {x**2 for x in [1, 1, 2]}
set([1, 4])
###Special matrix functions
>>> np.ones((3,2))
array([[ 1., 1.],
[ 1., 1.],
[ 1., 1.]])
>>> ones=np.ones((3,2))
>>> 3*ones
array([[ 3., 3.],
[ 3., 3.],
[ 3., 3.]])
>>> np.zeros((3,2))
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
>>> np.random.rand(3,2)
array([[ 0.0999543 , 0.54626042],
[ 0.34575433, 0.17310526],
[ 0.03811936, 0.40766806]])
>>> np.random.randn(3,2)
array([[ 0.2463843 , -0.89374849],
[-0.82852495, -0.0141292 ],
[-0.19124246, -0.00364112]])
>>> np.random.randn(3,2)
array([[-0.19823872, -1.07640824],
[-0.41855259, -1.0848794 ],
[ 0.37521868, -0.90956295]])
>>> id=np.eye(3)
>>> id
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> 3*id
array([[ 3., 0., 0.],
[ 0., 3., 0.],
[ 0., 0., 3.]])
##Moving data around ###shape/size of a matrix
a=np.random.rand(3,2)
>>> a.shape
(3, 2)
>>> a.size
6
###Loading files in python
data = np.loadtxt('ex1data1.txt', delimiter=',')
data = scipy.io.loadmat('ex3data1.mat')
data = scipy.misc.imread('bird_small.png')
# If imread does not work for you, you can try instead
data = load ('bird_small.mat')
##Manipulating matrices ###Indexing and Slicing
>>> x = np.arange(10)
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x[:]
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x[1:]
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x[:1]
array([0])
>>> x[:9]
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> x[2]
2
>>> x[-2]
8
>>> x=x.reshape((2,5))
>>> x
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> x[1,3]
8
>>> x[0]
array([0, 1, 2, 3, 4])
>>> x = np.arange(10)
>>> x[2:5]
array([2, 3, 4])
>>> x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x[2:5]
array([2, 3, 4])
>>> x[1:7:2]
array([1, 3, 5])
>>> arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> arr2d
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> arr2d[2]
array([7, 8, 9])
>>> arr2d[0][1]
2
>>> arr2d[0,1]
2
###Boolean indexing
>>> mat = np.array(['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']).reshape((3,3))
>>> mat
array([['The', 'quick', 'brown'],
['fox', 'jumped', 'over'],
['the', 'lazy', 'dog']],
dtype='|S6')
>>> rand = np.random.randn(3,3)>0
>>> rand
array([[ True, False, True],
[False, False, True],
[ True, True, True]], dtype=bool)
>>> mat[rand]
array(['The', 'brown', 'over', 'the', 'lazy', 'dog'],
dtype='|S6')
###Flattening
>>> arr = np.arange(9).reshape((3,3))
>>> arr
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> arr.flatten()
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> arr.ravel()
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
arr.flatten(1)
array([0, 3, 6, 1, 4, 7, 2, 5, 8])
###Vector assignments
>>> arr = np.arange(10)
>>> arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> arr[4:8]=10
>>> arr
array([ 0, 1, 2, 3, 10, 10, 10, 10, 8, 9])
>>> slice=arr[4:8]
>>> slice
array([10, 10, 10, 10])
>>> slice[:]=-5
>>> slice
array([-5, -5, -5, -5])
>>> slice[1]=50
>>> arr
array([ 0, 1, 2, 3, -5, 50, -5, -5, 8, 9])
>>> arr_copy=arr.copy()
>>> arr_copy
array([ 0, 1, 2, 3, -5, 50, -5, -5, 8, 9])
>>> arr_copy[4:8]=20
>>> arr_copy
array([ 0, 1, 2, 3, 20, 20, 20, 20, 8, 9])
>>> arr
array([ 0, 1, 2, 3, -5, 50, -5, -5, 8, 9])
###Concatenating
>>> mat = np.array(['The', 'quick', 'brown', 'fox'])
>>> mat2 = np.array(['jumped', 'over', 'the', 'lazy'])
>>> np.hstack((mat,mat2))
array(['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy'],
dtype='|S6')
>>> np.vstack((mat,mat2))
array([['The', 'quick', 'brown', 'fox'],
['jumped', 'over', 'the', 'lazy']],
dtype='|S6')
>>> np.column_stack((mat,mat2))
array([['The', 'jumped'],
['quick', 'over'],
['brown', 'the'],
['fox', 'lazy']],
dtype='|S6')
>>> arr = np.arange(12).reshape((3, 4))
>>> np.concatenate((arr,arr), axis=1)
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]])
>>> np.concatenate((arr,arr), axis=0)
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]])
>>> arr = np.arange(12)
>>> np.concatenate((arr,arr), axis=0)
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])
####Horizontal concatenation ####Vertical concatenation ##Computing on data ###Matrix multiplication
>>> 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)
array([[ 30, 36, 42],
[ 66, 81, 96],
[102, 126, 150]])
>>> x.dot(y)
array([[ 30, 36, 42],
[ 66, 81, 96],
[102, 126, 150]])
####Element-wise multiplication
>>> x*y
array([[ 1, 4, 9],
[16, 25, 36],
[49, 64, 81]])
####Element-wise squaring
>>> x**2
array([[ 1, 4, 9],
[16, 25, 36],
[49, 64, 81]])
####Element-wise reciprical
>>> 1./x
array([[ 1. , 0.5 , 0.33333333],
[ 0.25 , 0.2 , 0.16666667],
[ 0.14285714, 0.125 , 0.11111111]])
####Element-wise logarithms/exponents
>>> np.log(x)
array([[ 0. , 0.69314718, 1.09861229],
[ 1.38629436, 1.60943791, 1.79175947],
[ 1.94591015, 2.07944154, 2.19722458]])
>>> np.exp(x)
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
>>> 1+x
array([[ 2, 3, 4],
[ 5, 6, 7],
[ 8, 9, 10]])
###Transpose of a matrix
###Maximum and minimum of matrix values
>>> np.max(x)
9
>>> np.min(x)
1
###sum and product of all elements
>>> np.sum(x)
45
>>> np.sum(x,axis=0)
array([12, 15, 18])
>>> np.sum(x,axis=1)
array([ 6, 15, 24])
>>> np.sum(x)
45
>>> np.product(x)
362880
>>> np.product(x,axis=0)
array([ 28, 80, 162])
>>> np.product(x,axis=1)
array([ 6, 120, 504])
###Identity matrix for selection
>>> np.identity(3)
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> np.eye(3)
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
###Inverse and pseudo-inverse of a matrix
x=2*np.eye(3)
np.linalg.inv(x)
array([[ 0.5, 0. , 0. ],
[ 0. , 0.5, 0. ],
[ 0. , 0. , 0.5]])
np.linalg.pinv(x)
array([[ 0.5, 0. , 0. ],
[ 0. , 0.5, 0. ],
[ 0. , 0. , 0.5]])
##Plotting data ###Plotting generated data
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> plt.plot(np.arange(10))
>>> plt.show(block=False)
###Line color, labels, title and legend ###Saving a graph ###Subplots ###Axis scaling ###Creating/clearing figures ##Control statements ###For loops
>>> li = ['a', 'b', 'e']
>>> for e in li:
... print e
>>> d = enumerate(li)
>>> for k,v in d:
... print k,v
...
0 a
1 b
2 e
###While loops
>>> n = ''
>>> while n.strip() != 'hello':
... n = raw_input("Please enter 'hello':")
...
Please enter 'hello':lkdsdf
Please enter 'hello':hello
>>>
###break statement
>>> while True:
... n = raw_input("Please enter 'hello':")
... if n.strip() == 'hello':
... break
...
Please enter 'hello':hello
>>>
###if-elif-else statement
>>> x = int(raw_input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'
...
More
##Functions
###PYTHONPATH environment variable ##Vectorization ###Vectorized implementation ###Unvectorized implementation