new_train[:, i] = minmax(new_train[:, i]) #for plotting of polynomial curves - cotinuous data set over range of original data #then add polynomial features and normalise continuous_x = np.arange(1, 7.01, 0.01).reshape(-1, 1) x_ = add_polynomial_features(continuous_x, 10) for i in range(10): x_[:, i] = minmax(x_[:, i]) thetas = np.ones(11).reshape(-1, 1) cost_values = [] thetas_list = [] mlr = MLR(thetas, alpha=0.009, n_cycle=5000) for degree in range(2, 11): mlr.thetas = thetas[:degree + 1] thetas_list.append(mlr.fit_(new_train[:, :degree], y_train)) cost_values.append( mlr.mse_(y_train, mlr.predict_(new_train[:, :degree])[1])) i = 2 for elem in thetas_list: mlr.thetas = elem y_hat = mlr.predict_(x_[:, :i])[1] plt.plot(continuous_x, y_hat, '--') plt.title(str(degree)) plt.title(('degree = ' + str(i) + ' cost: ' + str(cost_values[i - 2]))) plt.plot(x_train, y_train, 'go') plt.plot(x_test, y_test, 'ro') plt.show()
data = pd.read_csv("../subjects/day01/resources/are_blue_pills_magics.csv") Xpill = np.array(data["Micrograms"]).reshape(-1,1) Yscore = np.array(data["Score"]).reshape(-1,1) thetas = np.array([1, 1]) #plt.plot(Xpill, Yscore, 'o') mlr = MyLR(thetas, alpha=0.05, n_cycle=5000) #th = mlr.fit_(Xpill, Yscore) #print(th) #plt.plot(Xpill, (th[1] * Xpill + th[0]), '-r') #plt.show() for j in range(80, 100, 5): res = [] for i in range(-11, -7, 1): mlr.thetas = np.array([j, i]) dummy, y_hat = mlr.predict_(Xpill) res.append(mlr.mse_(Yscore, y_hat)) np.array(res) plt.plot(np.arange(-11, -7), res) plt.show() for j in range(80, 100, 5): res = [] for i in range(-11, -7, 1): mlr.thetas = np.array([j, i]) dummy, y_hat = mlr.predict_(Xpill) res.append(mean_squared_error(Yscore, y_hat)) np.array(res) plt.plot(np.arange(-11, -7), res, '-r') plt.show()