def part1_1_2_a_ii(): x1 = np.random.uniform(0, 1, 30) epsilon = np.random.normal(0, 0.07, 30) y1 = (np.sin(2 * np.pi * x1)**2) + epsilon model_d2 = lr.fit(x1, y1, 2) model_d5 = lr.fit(x1, y1, 5) model_d10 = lr.fit(x1, y1, 10) model_d14 = lr.fit(x1, y1, 14) model_d18 = lr.fit(x1, y1, 18) x_axis1 = np.linspace(0, 1, num=1000) # configuration of plot plt.xlim(0, 1) plt.ylim(-0.5, 1.5) plt.xlabel('x') plt.ylabel('y') estimate_y2 = lr.estimate(lr.pl_featured(x_axis1, 2), model_d2) l1, = plt.plot(x_axis1, estimate_y2, color='green', label='dimension 2') plt.scatter(x1, y1) # Place a legend to the right of this smaller subplot. #plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) #plt.show() estimate_y5 = lr.estimate(lr.pl_featured(x_axis1, 5), model_d5) l2, = plt.plot(x_axis1, estimate_y5, color='blue', label='dimension 5') #plt.scatter(x1, y1) # Place a legend to the right of this smaller subplot. #plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) #plt.show() estimate_y10 = lr.estimate(lr.pl_featured(x_axis1, 10), model_d10) l3, = plt.plot(x_axis1, estimate_y10, color='black', label='dimension 10') #plt.scatter(x1, y1) # Place a legend to the right of this smaller subplot. #plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) #plt.show() estimate_y14 = lr.estimate(lr.pl_featured(x_axis1, 14), model_d14) l4, = plt.plot(x_axis1, estimate_y14, color='red', label='dimension 14') #plt.scatter(x1, y1) # Place a legend to the right of this smaller subplot. #plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) #plt.show() estimate_y18 = lr.estimate(lr.pl_featured(x_axis1, 18), model_d18) l5, = plt.plot(x_axis1, estimate_y18, color='purple', label='dimension 18') #plt.scatter(x1, y1) # Place a legend to the right of this smaller subplot. plt.legend(handles=[l1, l2, l3, l4, l5], labels=['k=2', 'k=5', 'k=10', 'k=14', 'k=18'], bbox_to_anchor=(1.13, 1.025), loc='upper right') plt.show()
def part1_1_1_c(): #calculate estimate value for different dimension estimate_y1 = lr.estimate(lr.pl_featured(x, 1), model_d1) estimate_y2 = lr.estimate(lr.pl_featured(x, 2), model_d2) estimate_y3 = lr.estimate(lr.pl_featured(x, 3), model_d3) estimate_y4 = lr.estimate(lr.pl_featured(x, 4), model_d4) #calculate MSEs mse1 = lr.mean_squared_error(y, estimate_y1) mse2 = lr.mean_squared_error(y, estimate_y2) mse3 = lr.mean_squared_error(y, estimate_y3) mse4 = lr.mean_squared_error(y, estimate_y4) print("MSE 1D: %f, MSE 2D: %f, MSE 3D: %f, MSE 4D: %f" % (mse1, mse2, mse3, mse4))
def mse_1d_to_18d(x1, y1, xt, yt): mse = np.zeros(shape=(18, 1)) for k in range(1, 19): #calculate w for dimension 1 to 18 model_dk = lr.fit(x1, y1, k) #calculate estimate value for different dimension estimate_yt = lr.estimate(lr.pl_featured(xt, k), model_dk) #calculate MSEs mse[k - 1] = lr.mean_squared_error(yt, estimate_yt) return mse
def part1_1_1_a(): #define all variables as global variables global model_d1, model_d2, model_d3, model_d4 #calculate w for dimension 1 to 4 model_d1 = lr.fit(x, y, 1) model_d2 = lr.fit(x, y, 2) model_d3 = lr.fit(x, y, 3) model_d4 = lr.fit(x, y, 4) #configuration of plot plt.xlim(0, 5) plt.ylim(-4, 8) plt.xlabel('x') plt.ylabel('y') #draw plot x_axis1 = np.linspace(0, 5, num=1000) y_axis1 = np.linspace(2.5, 2.5, num=1000) ''' y_axis2 = 1.5 + 0.4 * x_axis1 y_axis3 = 9 - 7.1 * x_axis1 + 1.5 * x_axis1 ** 2 y_axis4 = -5 + 15.17 * x_axis1 - 8.5 * x_axis1 ** 2 + 1.33 * x_axis1 ** 3 ''' y_axis2 = lr.estimate(lr.pl_featured(x_axis1, 2), model_d2) y_axis3 = lr.estimate(lr.pl_featured(x_axis1, 3), model_d3) y_axis4 = lr.estimate(lr.pl_featured(x_axis1, 4), model_d4) plt.scatter([1, 2, 3, 4], [3, 2, 0, 5]) l1, = plt.plot(x_axis1, y_axis1, color='green', label='dimension 1') l2, = plt.plot(x_axis1, y_axis2, color='blue', label='dimension 2') l3, = plt.plot(x_axis1, y_axis3, color='black', label='dimension 3') l4, = plt.plot(x_axis1, y_axis4, color='red', label='dimension 4') # Place a legend to the right of this smaller subplot. plt.legend(handles=[l1, l2, l3, l4], labels=['k=1', 'k=2', 'k=3', 'k=4'], loc='best') plt.show()