def plotcontour(traceJ, timegap, eta, imagename, converged): plt.ion() pltfig = plt.figure() #create a mesh using numpy T0, T1 = np.mgrid[0:2:50j, -1:1:50j] mesh = np.c_[T0.flatten(), T1.flatten()] reshapevalue = T0.shape Jvalue = (np.array([J(point) for point in mesh]).reshape(reshapevalue)) plt.xlabel(r'$\theta 0$') plt.ylabel(r'$\theta 1$') plt.contour(T0, T1, Jvalue, cmap=cm.RdBu_r) plt.title("Contour Plot (eta = " + str(eta) + ")") plt.show() i = 0 for value in traceJ: i += 1 plt.plot([value[0]], [value[1]], color='b', marker='x', markersize=2) if (timegap > 0.0): plt.pause(timegap) if (not converged and i > 99): break save_plots(plt, imagename) plt.close()
def plotlinearboundary(mu, sigma, show=True): sigma_inverse = la.pinv(sigma) # parameters of line equation : Ax = B tempmu = np.transpose(mu[0] - mu[1]) A = 2 * tempmu @ sigma_inverse B = np.transpose(mu[0]) @ sigma_inverse @ mu[0] - np.transpose( mu[1]) @ sigma_inverse @ mu[1] - 2 * np.log(len(canada) / len(alaska)) # plot data points plt.scatter(alaska[:, 0], alaska[:, 1], marker='o', color='b') plt.scatter(canada[:, 0], canada[:, 1], marker='x', color='r') classalaska = plotpatches.Patch(color='b', label="Alaska") classcanada = plotpatches.Patch(color='r', label="Canada") # plotting line x0 = x[:, 0] x1 = x[:, 1] x1 = (B - A[0] * x0) / (A[1]) if (show): line, = plt.plot(x0, x1, color="g", label="Decision Boundary") plt.legend(handles=[classalaska, classcanada, line]) plt.xlabel(r'Feature 0 ($X_0)$') plt.ylabel(r'Feature 1 ($X_1)$') plt.title("Linear decision boundary") save_plots(plt, "ques4_part(c).png") plt.show() return x0, x1
def plotmesh(traceJ, timegap): plt.ion() pltfig = plt.figure() #create a mesh using numpy T0, T1 = np.mgrid[0:2:50j, -1:1:50j] xt = T0.flatten() yt = T1.flatten() mesh = np.c_[xt, yt] #meshplot = pltfig.add_subplot(111, projection = '3d') meshplot = pltfig.gca(projection='3d') #compute Jvalues for the grid according to our mesh points Jvalue = (np.array([J(point) for point in mesh]).reshape(50, 50)) meshplot.set_xlabel(r'$\theta 0$', labelpad=15) meshplot.set_ylabel(r'$\theta 1$', labelpad=15) meshplot.set_zlabel(r'$J(\theta)$', labelpad=15) meshplot.plot_surface(T0, T1, Jvalue, cmap=cm.RdBu_r) plt.show() for value in traceJ: meshplot.plot([value[0]], [value[1]], [value[2]], color='b', marker='x', markersize=2) if (timegap > 0.0): plt.pause(timegap) save_plots(plt, "ques_1c.png") plt.close()
def plotting(theta): def colorslist(): colors = [] for cls in y: if cls: colors.append("r") else: colors.append("b") return colors color = colorslist() plt.scatter(x[:, 1], x[:, 2], c=color) # finding x2 value using line equation --> x2 = (-theta0 -theta1 x1) / theta2 for each x yy = [] for xi in x: value = (-theta[0] - theta[1] * xi[1]) / theta[2] yy.append(value) cls0 = plotpatches.Patch(color='blue', label='Class 0') cls1 = plotpatches.Patch(color='red', label='Class 1') line, = plt.plot(x[:, 1], yy, 'g', label="Decision Boundary") plt.xlabel(r'Feature 0 ($X_0)$') plt.ylabel(r'Feature 1 ($X_1)$') plt.legend(handles=[cls0, cls1, line]) save_plots(plt, "ques3_b.png") plt.show()
def weightedregression(tau, imagename): #generating points for plot maxx = np.amax(x[:, 1]) minx = np.amin(x[:, 1]) pointsforplot = np.linspace(minx, maxx) result = np.array([]) for point in pointsforplot: w = np.exp(-1 * ((point - x[:, 1])**2 / (2 * tau**2))) W = np.diag(w) transposex = np.transpose(x) inversematrix = la.inv(transposex @ W @ x) theta = inversematrix @ transposex @ W @ y result = np.append(result, [theta @ np.array([1, point])]) # plot of the result obtained from weighted regression line, = plt.plot(pointsforplot, result, 'b', label="Hypothesis for " + r'$\tau = ' + str(tau)) data, = plt.plot(x[:, 1], y, 'rx', label="Data") plt.xlabel("input x") plt.ylabel("output y") plt.legend(handles=[data, line]) plt.title("for value tau = " + str(tau)) save_plots(plt, imagename) plt.show()
def plotdata(): plt.scatter(alaska[:, 0], alaska[:, 1], marker='o', color='b') plt.scatter(canada[:, 0], canada[:, 1], marker='x', color='r') classalaska = plotpatches.Patch(color='b', label="Alaska") classcanada = plotpatches.Patch(color='r', label="Canada") plt.title("Plot of training data") #plt.legend(handles=[classalaska, classcanada]) plt.legend(handles=[classalaska, classcanada]) save_plots(plt, "ques4_part(b).png") plt.show()
def plotdata(theta): data, = plt.plot(x[:, 1], y, 'rx', label="Data") hy = list(map(lambda y: theta @ y, x)) plt.xlabel("Acidity of Wine (Normalised) ") plt.ylabel("Density Of Wine ") line, = plt.plot(x[:, 1], hy, "b", label="Hypothesis") plt.legend(handles=[data, line]) save_plots(plt, "ques_1b.png") plt.show()
def plotquadgda(mu, sigma, line0, line1): print("Part E") # plot data points plt.scatter(alaska[:, 0], alaska[:, 1], marker='o', color='b') plt.scatter(canada[:, 0], canada[:, 1], marker='x', color='r') classalaska = plotpatches.Patch(color='b', label="Alaska") classcanada = plotpatches.Patch(color='r', label="Canada") # plotting line line, = plt.plot(line0, line1, color="g", label="Linear Decision Boundary") sigma_inverse0 = la.pinv(sigma[0]) sigma_det0 = la.det(sigma[0]) sigma_inverse1 = la.pinv(sigma[1]) sigma_det1 = la.det(sigma[1]) # computing parameters of equation x'Ax + BX + C = 0 A = sigma_inverse0 - sigma_inverse1 mu_transpose0 = np.transpose(mu[0]) mu_transpose1 = np.transpose(mu[1]) B = -2 * (mu_transpose0 @ sigma_inverse0 - mu_transpose1 @ sigma_inverse1) C = (mu_transpose0 @ sigma_inverse0 @ mu[0] - mu_transpose1 @ sigma_inverse1 @ mu[1] - 2 * np.log( (len(canada) / len(alaska)) * (sigma_det0 / sigma_det1))) #create a mesh using numpy T0, T1 = np.mgrid[-5:5:50j, -7.5:7.5:50j] xt = T0.flatten() yt = T1.flatten() mesh = np.c_[xt, yt] # quadratic boundary quadboundary = np.array([]) for point in mesh: value = np.transpose(point) @ A @ point + B @ point + C quadboundary = np.append(quadboundary, [value]) quadboundary = quadboundary.reshape(T0.shape) plt.contour(T0, T1, quadboundary, [0], colors="y") quadlabel = lines.Line2D(color='yellow', label="quadratic decision boundary", xdata=[], ydata=[]) plt.title("Quadratic Gaussian Discriminant analysis") plt.xlabel("Input Feature(x1)") plt.ylabel("Input Feature(x2)") plt.legend(handles=[classalaska, classcanada, line, quadlabel]) save_plots(plt, "ques4_part(e).png") plt.show()
def unweightedregression(): # computing theta using normal equation transposex = np.transpose(x) inversematrix = la.inv(transposex @ x) theta = inversematrix @ transposex @ y print("theta parameters : ", theta, "\n") hy = list(map(lambda y: theta @ y, x)) line, = plt.plot(x[:, 1], hy, "b", label="Hypothesis") data, = plt.plot(x[:, 1], y, 'rx', label="Data") plt.legend(handles=[data, line]) plt.xlabel("input x") plt.ylabel("output y") save_plots(plt, "ques_2a.png") plt.show()