# Start pylab inline mode, so figures will appear in the notebook %matplotlib inline 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]) from scipy import sparse # Create a random array with a lot of zeros X = np.random.random((10, 5)) print(X) # set the majority of elements to zero X[X < 0.7] = 0 print(X) # 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()) %matplotlib inline # Here we import the plotting functions import matplotlib.pyplot as plt # 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 by default! plt.imshow(im) # Contour plot - note that origin here is at the bottom-left by default! plt.contour(im) %load http://matplotlib.org/mpl_examples/pylab_examples/ellipse_collection.py # 3D plotting from mpl_toolkits.mplot3d import Axes3D ax = plt.axes(projection='3d') xgrid, ygrid = np.meshgrid(x, y.ravel()) ax.plot_surface(xgrid, ygrid, im, cmap=plt.cm.jet, cstride=2, rstride=2, linewidth=0)