def randomLine(): seed(1) alpha = 0.000003 numOfSteps = 1000 numOfLoops = 250 model = LinearRegression() #generating feature x x = np.random.uniform(low=-4, high=6, size=(500, )) #generating coeffient and constant a = float(np.random.uniform(low=-5, high=10, size=(1, ))) b = float(np.random.uniform(low=-5, high=5, size=(1, ))) #adding gaussian noise noise = np.random.normal(0, 1, 500) # labels y = [] for i in range(len(x)): t = a * x[i] + b + noise[i] y.append(t) for i in range(len(x)): model.addSample(x[i], y[i]) Samples = model.getSamples() Labels = model.getValues() for i in range(numOfLoops + 1): model.fit(alpha, numOfSteps) plt.show()
def setPlane(): alpha = 0.000000003 numOfSteps = 1000 numOfLoops = 10 model = LinearRegression() noOfFeatures = 2 x = list(range(0, 1000)) #calculating points for points((x,2x),5x)and ((2x,x),4x) x1, y1 = x, [i * 2 for i in x] x2, y2 = [i * 2 for i in x], x x = x1 + x2 y = y1 + y2 #calculating z = x + 2y z = [(x[i] + 2 * y[i]) for i in range(len(x))] #array of features as (x1,x2) features = list(zip(x, y)) for i in range(len(z)): model.addSample(features[i], z[i]) Samples = model.getSamples(noOfFeatures) Labels = model.getValues() for i in range(numOfLoops + 1): current_Hypothesis, cost = model.fit(alpha, numOfSteps) print("Current hypothesis: ", current_Hypothesis, ", cost = ", "{0:.4f}".format(cost))
def rabndomLine(): seed(1) alpha = 0.003 numOfSteps = 1000 numOfLoops = 100 noise = 20 noOfFeatures = 2 model = LinearRegression() x = [] #generationg the values for variables given in assignment x1 = np.random.uniform(low=0, high=1, size=(5000, )) x2 = np.random.uniform(low=0, high=1, size=(5000, )) #generationg the values for coeffients given in assignment a = float(np.random.uniform(low=-100, high=100, size=(1, ))) b = float(np.random.uniform(low=-100, high=100, size=(1, ))) c = float(np.random.uniform(low=-20, high=20, size=(1, ))) #generationg the value for noise given in assignment delta = np.random.uniform(low=-100, high=100, size=(5000, )) #calculating the label y = [(a * x1[i] + b * x2[i] + c + delta[i]) for i in range(len(x1))] #array of features as (x1,x2) features = list(zip(x1, x2)) for i in range(len(y)): model.addSample(features[i], y[i]) Samples = model.getSamples(noOfFeatures) Labels = model.getValues() for i in range(numOfLoops + 1): current_Hypothesis, cost = model.fit(alpha, numOfSteps) print("Current hypothesis: ", current_Hypothesis, ", cost = ", "{0:.4f}".format(cost))
def randomDimension(noOfFeatures): seed(1) alpha = 0.00003 numOfSteps = 1000 numOfLoops = 100 noise = 20 model = LinearRegression() examples = 5000 #array of coefficient t = [] #array of all features of each 5000 examples x = [] # array of r - label r = [] #array of product of coefficient and x for each dimention tx = [] #array of features as (x1,x2...xn) features = [] #generating random variable for each coefficient t0 in assignment t0 = float(np.random.uniform(low=-100, high=100, size=(1, ))) for i in range(1, noOfFeatures + 1): #generating random variable for each coefficient t in assignment a = float(np.random.uniform(low=-100, high=100, size=(1, ))) t.append(a) #generating random variable for each feature x in assignment x1 = np.random.uniform(low=0, high=1, size=(5000, )) x.append(list(x1)) #poduct of coefficient and x p = list(a * x1) tx.append(p) for j in range(examples): #summation of poduct of coefficient and x for each sample z = sum(i[j] for i in tx) #adding noise and t0 to find r to the summation y = z + t0 + noise r.append(y) g = [i[j] for i in x] features.append(g) for i in range(len(r)): model.addSample(features[i], r[i]) Samples = model.getSamples(noOfFeatures) Labels = model.getValues() for i in range(numOfLoops + 1): current_Hypothesis, cost = model.fit(alpha, numOfSteps) print("Current hypothesis: ", current_Hypothesis, ", cost = ", "{0:.4f}".format(cost))
def setLine(): alpha = 0.000000003 numOfSteps = 100 numOfLoops = 50 model = LinearRegression() #feature x = list(range(0, 1000)) #label y = x for i in x: y = i model.addSample(i, y) Samples = model.getSamples() Labels = model.getValues() for i in range(numOfLoops + 1): model.fit(alpha, numOfSteps) plt.show()
def setLine(): alpha = 0.000000003 numOfSteps = 100 numOfLoops = 50 model = LinearRegression() #feature x = list(range(0, 1000)) #label y = x for i in x: y = i model.addSample(i, y) Samples = model.getSamples() Labels = model.getValues() for i in range(numOfLoops + 1): current_Hypothesis, cost = model.fit(alpha, numOfSteps) print("Current hypothesis: ", current_Hypothesis, ", cost = ", "{0:.4f}".format(cost))