import numpy as np import datasets import regression import importlib X, Y = datasets.load_nonlinear_example1() ex_X = datasets.polynomial3_features(X) samples = np.arange(0, 4, 0.1) x_samples = np.c_[ np.ones(len(samples)), samples ] ex_x_samples = datasets.polynomial3_features(x_samples) list = [0, 0.1, 0.5, 1.0, 10] import matplotlib.pyplot as plt plt.scatter(X[:,1], Y) for x in list: model = regression.RidgeRegression(x) model.fit(ex_X, Y) plt.plot(samples, model.predict(ex_x_samples)) plt.show()
import datasets X, Y = datasets.load_nonlinear_example1() ex_X = datasets.polynomial3_features(X) print(ex_X) print(X[0]) print(Y) import regression model = regression.RidgeRegression(alpha=0.5) model = regression.RidgeRegression() print(model.alpha) import importlib importlib.reload(regression) model = regression.LinearRegression() model.fit(ex_X, Y) print(model.theta) print(model.predict(ex_X)) print(model.score(ex_X, Y))
trafo = preprocessing.task1btransformation() X_train = trafo.transform(X_train) if BFinalPrediction == 0: X_test = trafo.transform(X_test) ## Linear Regression if BLinearRegression == 1: LinReg = regression.LinearRegression() LinReg.fit(X_train, y_train) if BFinalPrediction == 0: y_pred = LinReg.predict(X_test) w = LinReg.getcoeff() if BRidgeRegression == 1: l = 15 RidgeReg = regression.RidgeRegression(alpha=l) RidgeReg.fit(X_train, y_train) if BFinalPrediction == 0: y_pred = RidgeReg.predict(X_test) w = RidgeReg.getcoeff() if BLassoRegression == 1: l = 1 LassoReg = regression.LassoRegression(alpha=l) LassoReg.fit(X_train, y_train) if BFinalPrediction == 0: y_pred = LassoReg.predict(X_test) w = LassoReg.getcoeff() ## SVM if BSVClassification == 1:
import regression import datasets import numpy as np import matplotlib.pyplot as plt alpha = [0, 0.1, 0.5, 1.0, 10.0] importlib.reload(regression) X, Y = datasets.load_nonlinear_example1() ex_X = datasets.polynomial3_features(X) #model = regression.RidgeRegression(alpha=0) #model = regression.RidgeRegression() #model.fit(ex_X,Y) #print(model.theta) #print(model.predict(ex_X)) #print(model.score(ex_X,Y)) samples = np.arange(0, 4, 0.1) plt.scatter(X[:, 1], Y) for i in alpha: model = regression.RidgeRegression(alpha=i) model.fit(ex_X, Y) x_samples = np.c_[np.ones(len(samples)), samples] ex_x_samples = datasets.polynomial3_features(x_samples) plt.plot(samples, model.predict(ex_x_samples), label="a=" + str(i)) plt.legend(loc=0) plt.show()