Linear Regression_Intro page 10

生成数据

NumberObservations=100
minVal=1
maxVal=20
X = np.random.uniform(minVal,maxVal,(NumberObservations,1))
print(X.shape)
#Add you code below to define error and Y based on the information above

def generateY(x):
    Y = np.array(100)
    Y = 10 + 5*x
    #print(Y)
    gaussian_noise = np.random.normal(0, 1, 100).reshape(100,1)
    #print(gaussian_noise)
    Y = Y + gaussian_noise
    return Y
    

Y = generateY(X)
print(Y)

线性回归

def calculate_RSS(B0, B1, testX, testY):
    testX = np.array(testX)
    testY = np.array(testY)
    predictY = testX*B1+B0
    RSS = sum((testY-predictY)*(testY-predictY))
    return RSS

def linear_regression(B0, B1, trainX, trainY, iteration=10000, learning_rate=0.0002, e=0.0001):
    N = len(trainX)
    trainX = np.array(trainX)
    trainY = np.array(trainY)
    old_RSS = 20
    current_RSS = None
    for i in range(iteration):
        #print(B0, B1)
        predictY = B0 + B1*trainX
        B0 = B0 - (-(2/N) * sum(trainY-predictY) * learning_rate)
        B1 = B1 - (-(2/N) * sum(trainX * (trainY-predictY)) * learning_rate)
        current_RSS = calculate_RSS(B0, B1, trainX, trainY)
        if abs(current_RSS-old_RSS) < e:
            break
        #print('rss',old_RSS, current_RSS, old_RSS-current_RSS)
        old_RSS = current_RSS
    return B0, B1

b0, b1 = linear_regression(1, 2, X, Y, 100000)
print(b0, b1)