-
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 of a matrix ###size of a matrix ###Loading files in python ##Manipulating matrices ###Slicing ###Flattening ###Vector assignments ###Concatenating ####Horizontal concatenation ####Vertical concatenation ##Computing on data ###Matrix multiplication ###Element-wise operations ####Element-wise multiplication ####Element-wise squaring ####Element-wise reciprical ####Element-wise logarithms/exponents ####Element-wise addition ###Transpose of a matrix ###Maximum and minimum of matrix values ###'magic' matrix ###Logical filtering ###sum and product of all elements ###Identity matrix for selection ###Inverse and pseudo-inverse of a matrix ##Plotting data ###Plotting generated data ###Line color, labels, title and legend ###Saving a graph ###Subplots ###Axis scaling ###Creating/clearing figures ##Control statements ###For loops ###While loops ###break statement ###if-elif-else statement ##Functions ###PYTHONPATH environment variable ##Vectorization ###Vectorized implementation ###Unvectorized implementation