def test_MyLinearRegressing(): X = np.array([[1., 1., 2., 3.], [5., 8., 13., 21.], [34., 55., 89., 144.]]) Y = np.array([[23.], [48.], [218.]]) mylr = MyLR([[1.], [1.], [1.], [1.], [1]], 5e-5, 320000) # Example 0: print(mylr.predict_(X), end="\n\n") # Output: # array([[8.], [48.], [323.]]) # Example 1: print(mylr.cost_elem_(X, Y), end="\n\n") # Output: # array([[37.5], [0.], [1837.5]]) # Example 2: print(mylr.cost_(X, Y), end="\n\n") # Output: # 1875.0 # Example 3: mylr.fit_(X, Y) print(mylr.thetas, end="\n\n") # Output: # array([[18.023..], [3.323..], [-0.711..], [1.605..], [-0.1113..]]) # Example 4: print(mylr.predict_(X), end="\n\n") # Output: # array([[23.499..], [47.385..], [218.079...]]) # Example 5: print(mylr.cost_elem_(X, Y), end="\n\n") # Output: # array([[0.041..], [0.062..], [0.001..]]) # Example 6: print(mylr.cost_(X, Y), end="\n\n")
import numpy as np from mylinearregression import MyLinearRegression as MyLR X = np.array([[1., 1., 2., 3.], [5., 8., 13., 21.], [34., 55., 89., 144.]]) Y = np.array([[23.], [48.], [218.]]) theta = np.array([[1.], [1.], [1.], [1.], [1]]) print("X", X.shape) print("Y", Y.shape) print("theta", theta.shape) # MyLR(). mylr = MyLR(theta) mylr.predict_(X) mylr.cost_elem_(X, Y) mylr.cost_(X, Y) mylr.fit_(X, Y, alpha=1.6e-4, n_cycle=200000) mylr.theta mylr.predict_(X) mylr.cost_elem_(X, Y) mylr.cost_(X, Y)
""" if X.size == 0 or self.theta.size == 0 or \ (X.size != 0 and X.shape[1] + 1 != self.theta.shape[0]): return None # on commence par rajouter une colonne x0 qui vaut 1 X_1 = np.insert(X, 0, [1.], axis=1) return np.dot(X_1, self.theta) if __name__ == '__main__': import numpy as np from mylinearregression import MyLinearRegression as MyLR X = np.array([[1., 1., 2., 3.], [5., 8., 13., 21.], [34., 55., 89., 144.]]) Y = np.array([[23.], [48.], [218.]]) mylr = MyLR([[1.], [1.], [1.], [1.], [1]]) print(mylr.predict_(X)) # => array([[8.], [48.], [323.]]) print(mylr.cost_elem_(X, Y)) # => array([[37.5], [0.], [1837.5]]) print(mylr.cost_(X, Y)) # => 1875.0 mylr.fit_(X, Y, alpha=1.6e-4, n_cycle=200000) print(mylr.theta) # => array([[18.023..], [3.323..], [-0.711..], [1.605..], [-0.1113..]]) print(mylr.predict_(X)) # => array([[23.499..], [47.385..], [218.079...]]) print(mylr.cost_elem_(X, Y)) # => array([[0.041..], [0.062..], [0.001..]]) print(mylr.cost_(X, Y)) # => 0.1056..
x = np.array([[12.4956442], [21.5007972], [31.5527382], [48.9145838], [57.5088733]]) y = np.array([[37.4013816], [36.1473236], [45.7655287], [46.6793434], [59.5585554]]) lr1 = MyLR([2, 0.7]) print(lr1.predict_(x)) # Output: # array([[10.74695094], # [17.05055804], # [24.08691674], # [36.24020866], # [42.25621131]]) # Example 0.1: print(lr1.cost_elem_(lr1.predict_(x), y)) # Output: # array([[77.72116511], # [49.33699664], # [72.38621816], # [37.29223426], # [78.28360514]]) # Example 0.2: print(lr1.cost_(lr1.predict_(x), y)) # Output: # 315.0202193084312 # Example 1.0: lr2 = MyLR([0, 0]) lr2.fit_(x, y) print(lr2.thetas) # Output:
import numpy as np from mylinearregression import MyLinearRegression as MyLR X = np.array([[1., 1., 2., 3.], [5., 8., 13., 21.], [34., 55., 89., 144.]]) Y = np.array([[23.], [48.], [218.]]) mylr = MyLR([[1.], [1.], [1.], [1.], [1]]) res = mylr.predict_(X) print(res) res = mylr.cost_elem_(X, Y) print(res) res = mylr.cost_(X, Y) print(res) res = mylr.fit_(X, Y, alpha=1.6e-4, n_cycle=200000) print(res) res = mylr.predict_(X) print(res) res = mylr.cost_elem_(X, Y) print(res) res = mylr.cost_(X, Y) print(res)