diff --git a/Coursera Stanford ML Python wiki.html b/Coursera Stanford ML Python wiki.html new file mode 100644 index 0000000..6565455 --- /dev/null +++ b/Coursera Stanford ML Python wiki.html @@ -0,0 +1,5549 @@ + + +
+import numpy as np
+import scipy.io
+import scipy.misc
+import matplotlib.pyplot as plt
+
5+6
+
3-2
+
5*8
+
1/2
+
1./2
+
2**6
+
1 and 0 # AND
+
1 or 0 # OR
+
1 != 0 # XOR
+
bool([]) and True # False
+
a='foo'
+b='bar'
+bool(a) != bool(b)
+
b=None
+bool(a) != bool(b)
+
b=3
+b
+
from math import pi
+b=pi
+b
+
floating point numbers are formatted in two ways:
+The 'old' way (pre-python 2.7):
+ +print '%1.4f'%b
+
print '{:1.5}'.format(b)
+
a=np.array([[1,2],[3,4],[5,6]]) # 3x2 numpy matrix
+a
+
v=[1,2,3] # ordinary python list
+v
+
v=np.array([1,2,3]) # numpy array
+v
+
np.arange(start, stop, increment)
to generate a sequence of floats in a numpy array¶v=np.arange(1,2,0.1)
+v
+
tolist()
to convert a numpy array to a python list¶v.tolist()
+
range()
built-in function generates integer sequences in a list
¶v=range(1,6)
+v
+
linspace
function generates a non-integer sequence with a specific number of elements¶v=np.linspace(1,2,11)
+v
+
v=[1,2,3]
+[e**2 for e in v]
+
[e**2 for e in v if e%2 !=0]
+
[e**2 if e%2 != 0 else -1 for e in v]
+
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'}
+
{x**2 for x in [1, 1, 2]}
+set([1, 4])
+
ones=np.ones((3,2))
+ones
+
3*ones
+
np.zeros((3,2))
+
np.random.rand(3,2)
+
np.random.randn(3,2)
+
id=np.eye(3)
+id
+
3*id
+
a=np.random.rand(3,2)
+a
+
a.shape
+
a.size
+
file=open('ex6/emailSample1.txt', 'r')
+file_contents=file.read()
+file_contents
+
%pylab inline # this line works in ipython only
+data = scipy.misc.imread('ex7/bird_small.png')
+plt.imshow(data)
+
data = np.loadtxt('ex0.csv', delimiter=',')
+data
+
data = scipy.io.loadmat('ex3/ex3data1.mat')
+data
+
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¶a[start:end:step]
- start through not past end, by step¶x = np.arange(10)
+x
+
x[:]
+
x[1:]
+
x[:5]
+
x[2]
+
x[1:7:2]
+
x[:-2]
+
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
+arr2d
+
arr2d[2]
+
arr2d[0]
+
arr2d[0,1]
+
mat = np.array(['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']).reshape((3,3))
+mat
+
rand = np.random.randn(3,3)>0
+rand
+
mat[rand]
+
arr = np.arange(9).reshape((3,3))
+arr
+
flatten()
function returns a copy of the array¶arr.flatten()
+
arr.flatten(1)
+
ravel()
function doesn't return a copy of the underlying data¶arr.ravel()
+
arr = np.arange(10)
+arr
+
slice=arr[4:8]
+slice
+
slice[:]=-5
+slice
+
slice[1]=50
+slice
+
arr
+
arr_copy=arr.copy()
+arr_copy
+
arr_copy[4:8]=20
+arr_copy
+
arr
+
mat = np.array(['The', 'quick', 'brown', 'fox'])
+mat2 = np.array(['jumped', 'over', 'the', 'lazy'])
+
np.hstack((mat,mat2))
+
np.vstack((mat,mat2))
+
np.column_stack((mat,mat2))
+
concatenate()
function applied to an axis¶arr = np.arange(12).reshape((3, 4))
+arr
+
np.concatenate((arr,arr), axis=1)
+
np.concatenate((arr,arr), axis=0)
+
arr = np.arange(5)
+np.concatenate((arr,arr), axis=0)
+
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)
+
dot()
function¶x.dot(y)
+
x*y
+
x**2
+
1./x
+
np.log(x)
+
np.exp(x)
+
1+x
+
x.T
+
np.max(x)
+
np.min(x)
+
np.sum(x)
+
np.sum(x,axis=0)
+
np.sum(x,axis=1)
+
np.sum(x)
+
np.product(x)
+
np.product(x,axis=0)
+
np.product(x,axis=1)
+
x=2*np.eye(3)
+np.linalg.inv(x)
+
np.linalg.pinv(x)
+
plt.plot(np.arange(10))
+
li = ['a', 'b', 'e']
+for e in li:
+ print e
+
d = enumerate(li)
+for k,v in d:
+ print k,v
+
n = ''
+while n.strip() != 'hello':
+ n = raw_input("Please enter 'hello':")
+
while True:
+ n = raw_input("Please enter 'hello':")
+ if n.strip() == 'hello':
+ break
+
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'
+