Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # read train data, init empty x/y arrays
  5. data_train = np.loadtxt("Train_Dataset.csv")
  6. train_shape_fst, train_shape_snd = data_train.shape
  7. x_train = np.empty([train_shape_fst, 1])  # all of the x values
  8. y_train = np.empty([train_shape_fst, 1])  # all of the y values
  9.  
  10. # fill x/y train data with the data from Train_Dataset.csv
  11. for i in range(0, train_shape_fst):
  12.     x_train[i] = data_train[i][0]
  13.     y_train[i] = data_train[i][1]
  14.  
  15. # read test data, init emtpy x/y arrays
  16. data_test = np.loadtxt("Test_Dataset.csv")
  17. test_shape_fst, test_shape_snd = data_test.shape
  18. x_test = np.empty([test_shape_fst, 1])  # all of the x values
  19. y_test = np.empty([test_shape_fst, 1])  # all of the y values
  20.  
  21. # fill x/y test data
  22. for i in range(0, test_shape_fst):
  23.     x_test[i] = data_test[i][0]
  24.     y_test[i] = data_test[i][1]
  25.  
  26.  
  27. class RegressionModel:
  28.     def __init__(self, degree):
  29.         self.degree = degree
  30.         self.model = [None, None, None]
  31.  
  32.     def vectorized_SLR(self, x, y):
  33.         XT = np.transpose(x)  # create XT
  34.         A = np.matmul(XT, x)  # A is now a squared matrix
  35.  
  36.         if np.linalg.det(A) > 0:  # Normal way
  37.             B = np.matmul(XT, y)  # right side of the formula
  38.             w = np.matmul(np.linalg.inv(A), B)  # create w
  39.             return w
  40.  
  41.         else:  # if the det is 0 then the above is not working
  42.             lam = np.identity(A.shape[0])  # create a lambda in the size of A
  43.  
  44.             while np.linalg.det(A) < 0:  # add lambda until condition is met
  45.                 A = np.add(A, lam)
  46.             B = np.matmul(XT, y)  # proceed as before
  47.             w = np.matmul(np.linalg.inv(A), B)
  48.  
  49.             return w
  50.  
  51.     def fit(self, x_values, y_values):  # creates the w with function above
  52.         generated_x_values = self._generate_features(x_values)
  53.         w = self.vectorized_SLR(generated_x_values, y_values)
  54.         self.model[1] = w  # add the w to the model
  55.         self.model[2] = np.matmul(self.model[0], self.model[1])  # add the y values to the model
  56.         return w
  57.  
  58.     def predict(self, x_test):
  59.         new_x_values = self._generate_features(x_test)  # create the new x values
  60.         more_y_values = np.matmul(new_x_values, self.model[1])  # use the w on new x values
  61.         return [x_test, more_y_values]  # return x and y values
  62.  
  63.     def _generate_features(self, x_values):  # create the matrix
  64.         shape_fst, shape_snd = x_values.shape  # only shape_fst is needed to read the number of x values
  65.         feature_matrix_x = np.empty((shape_fst, self.degree + 1))  # create the matrix uninitialized
  66.  
  67.         for i in range(0, feature_matrix_x.shape[0]):  # for every x value
  68.             for j in range(0, self.degree + 1):  # add columns to the number of degrees
  69.                 feature_matrix_x[i][j] = np.power(x_values[i], j)  # create the values in the matrix
  70.  
  71.         self.model[0] = feature_matrix_x  # add the x values to the model
  72.         return feature_matrix_x
  73.  
  74.  
  75. # ED Formula
  76. def ed(y, y_dach):
  77.     ed_value = 0.0
  78.  
  79.     for i in range(y.size):
  80.         ed_value += ((y[i] - y_dach[i]) ** 2)
  81.  
  82.     return np.sqrt(ed_value)
  83.  
  84.  
  85. # ED Testing
  86. y = np.array([0.8, 0.43, 1.74, 0.26, 4.06, 0.73, 2.8, 3.37])
  87. y_dach = np.array([3.49, 1.3, 1.49, 4.12, 2.19, 4.24, 4.67, 0.22])
  88. print(ed(y, y_dach))
  89.  
  90.  
  91. # Regression Model Testing
  92. plt.scatter(x_train, y_train)  # plot the training points
  93. plt.plot(x_test, y_test, color="black")  # plot the test data
  94.  
  95. # create the Objects
  96. p1 = RegressionModel(1)
  97. p2 = RegressionModel(2)
  98. p3 = RegressionModel(3)
  99. p4 = RegressionModel(4)
  100.  
  101. # train the w
  102. p1.fit(x_train, y_train)
  103. p2.fit(x_train, y_train)
  104. p3.fit(x_train, y_train)
  105. p4.fit(x_train, y_train)
  106.  
  107. # plot everything
  108. plt.plot(p1.predict(x_test)[0], p1.predict(x_test)[1], color="blue")
  109. plt.plot(p2.predict(x_test)[0], p2.predict(x_test)[1], color="red")
  110. plt.plot(p3.predict(x_test)[0], p3.predict(x_test)[1], color="yellow")
  111. plt.plot(p4.predict(x_test)[0], p4.predict(x_test)[1], color="green")
  112.  
  113. plt.show()
  114.  
  115.  
  116. """
  117. 2.4 c
  118.  
  119. Die Approximation von Grad 2 ist genauer als die von Grad 3 und 4, da Funktionen
  120. von hoeherem Grad dazu tendieren, nach Ablauf der Testwerte eine hoehere Varianz
  121. aufzuzeigen. Dies kommt daher da Funktionen mit hoeherem Grad meist mehr
  122. Schwingungen aufzeigen und diese dann im weiteren Verlauf den Graphen
  123. staerker beeinflussen, als es zum Besipiel der Graph vom Grad 2 tut.
  124. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement