def mesh_plot(plt, X, Y, Z, title): plt.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.7, cmap=cm.jet) # DEBUG plt.plot_wireframe(X, Y, Z, rstride=1, cstride=1) plt.contourf(X, Y, Z, zdir='z', cmap=cm.jet, offset=-400) # These used to use coolwarm plt.contourf(X, Y, Z, zdir='x', cmap=cm.jet, offset=41.6) plt.contourf(X, Y, Z, zdir='y', cmap=cm.jet, offset=-87.4) plt.set_xlabel('Lat') plt.set_xlim(41.6, 42.2) plt.set_ylabel('Long') plt.set_ylim(-88.0, -87.4) plt.set_zlabel('Number of Crimes') plt.set_zlim(-100, 1000) plt.set_title(f'{title}')
def surfacePlot(x, t, y, fOut=None, showPlot=True): X = np.outer(x, np.ones(len(t))) T = np.outer(np.ones(len(x)), t) fig = plt.figure() ax = plt.axes(projection='3d') ax.plot_surface(T, X, y, cmap='viridis', edgecolor='none') # ax.set_title('Surface plot') plt.grid(True) plt.set_xlabel('t') plt.set_ylabel('x') plt.set_zlabel('y') if fOut is not None: plt.savefig(fOut) if showPlot: plt.show() return fig
# -*- coding: utf-8 -*- """ Created on Thu Dec 14 12:02:50 2017 @author: vincentkao """ #PCA import matplotlib.pyplot as plt from sklearn import datasets from sklearn.decomposition import PCA import numpy as np iris = datasets.load_iris() X = iris.data[:, :2] Y = iris.target X_reduced = PCA(n_components=3).fit_transform(iris.data) plt.scatter(X_reduced[Y==0, 0], np.zeros((50,1))+0.02, color='red', marker='^') plt.scatter(X_reduced[Y==1, 0], np.zeros((50,1))-0.02, color='blue', marker='x') plt.scatter(X_reduced[Y==2, 0], np.zeros((50,1)), color='green', marker='o') plt.set_title("First three PCA directions") plt.set_xlabel("1st eigenvector") plt.w_xaxis.set_ticklabels([]) plt.set_ylabel("2nd eigenvector") plt.w_yaxis.set_ticklabels([]) plt.set_zlabel("3rd eigenvector") plt.w_zaxis.set_ticklabels([]) plt.show()
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D x = np.zeros(100) y = np.zeros(100) z = np.zeros(100) z_inc = 4.0 / 99.0 theta_inc = (8.0 * 3.14) / 99.0 for i in range(100): theta = -4.0 * 3.14 + theta_inc * i z[i] = -2.0 + z_inc * i r = z[i] * z[i] + 1 x[i] = (r * sin(theta)) y[i] = (r * cos(theta)) plt.plot(x, y, z, projection='3d') plt.xlabel("x label") plt.ylabel("y label") plt.set_zlabel("z label") plt.legend() plt.show()
theta_1 = np.linspace(-1, 4, 100) cost_values = np.zeros((len(theta_0), len(theta_1))) for i in range(len(theta_0)): for j in range(len(theta_1)): t = np.array([theta_0[i], theta_1[j]]) cost_values[i, j] = cost_function(X, y, t) # In[64]: fig = plt.figure(figsize=(12, 8)) ax = fig.gca(projection='3d') surf = ax.plot_surface(theta_0, theta_1, cost_values, cmap='viridis') fig.colorbar(surf, shrink=0.5, aspect=5) plt.xlabel("$\Theta_0$") plt.ylabel("$\Theta_1$") plt.set_zlabel("$J(\Theta)$") # ## # ### Task 7: Plotting the Convergence # --- # Plot $J(\theta)$ against the number of iterations of gradient descent: # In[67]: plt.plot(costs) plt.xlabel("Iterations") plt.ylabel("$Theta") #