# Start matplotlib inline mode, so figures will appear in the notebook %matplotlib inline # Import the example plot from the figures directory from fig_code import plot_sgd_separator plot_sgd_separator() #Uncomment the %load command to load the contents of the file # %load fig_code/sgd_separator.py from fig_code import plot_linear_regression plot_linear_regression() import numpy as np # Generating a random array X = np.random.random((3, 5)) # a 3 x 5 array print X # Accessing elements # get a single element print X[0, 0] # get a row print X[1] # get a column print X[:, 1] # Transposing an array print X.T # Turning a row vector into a column vector y = np.linspace(0, 12, 5) print y # make into a column vector print y[:, np.newaxis] # Create a random array with a lot of zeros X = np.random.random((10, 5)) print X X[X < 0.7] = 0 print X from scipy import sparse # turn X into a csr (Compressed-Sparse-Row) matrix X_csr = sparse.csr_matrix(X) print X_csr # convert the sparse matrix to a dense array print X_csr.toarray() # Sparse matrices support linear algebra: y = np.random.random(X_csr.shape[1]) z1 = X_csr.dot(y) z2 = X.dot(y) np.allclose(z1, z2) # Create an empty LIL matrix and add some items X_lil = sparse.lil_matrix((5, 5)) for i, j in np.random.randint(0, 5, (15, 2)): X_lil[i, j] = i + j print X_lil print X_lil.toarray() X_csr = X_lil.tocsr() print X_csr %matplotlib inline import matplotlib.pyplot as plt import numpy as np # plotting a line x = np.linspace(0, 10, 100) plt.plot(x, np.sin(x)); # scatter-plot points x = np.random.normal(size=500) y = np.random.normal(size=500) plt.scatter(x, y); # showing images x = np.linspace(1, 12, 100) y = x[:, np.newaxis] im = y * np.sin(x) * np.cos(y) print(im.shape) # imshow - note that origin is at the top-left! plt.imshow(im); # Contour plot - note that origin here is at the bottom-left! plt.contour(im);