我在下面编写了python代码,用梯度下降法找到多元线性回归函数。我已经在输入特性上使用了特性规范化,并且能够得出w1、w2、w0,从而使损失最小化。然而,这些w1、w2、w0的数量是在对x1、x2和y进行归一化之后计算的权重
我的目标是得到x1,x2的系数(w1,w2)和截距(w0)的简单数目,从而形成一个线性方程,该方程根据原始输入特征(x1,x2)预测y。我试过将w1,w2,w0与std相乘,然后加上平均值。然而,结果并不正确。我想我需要缩小系数以得到我想要的。然而,我被困住了,希望有人能帮我解决这个问题
下面是代码和结果
import pandas as pd
import numpy as np
x1 = np.array([1,2,3,4,5,6,7,8,9,10],dtype='float64')
x2 = np.array([5,8,11,14,17,20,23,26,29,32],dtype='float64')
y = np.array([36,61,86,111,136,161,186,211,236,261],dtype='float64')
def featureNormalize(X):
"""
X : The dataset of shape (m x n)
"""
X_norm = X.copy()
mu = np.zeros(len(X))
sigma = np.zeros(len(X))
mu = np.mean(X, axis=0)
sigma = np.std(X, axis=0)
X_norm = (X-mu)/ sigma
return X_norm
def multivar_gradient_descent(x1,x2,y):
np.random.seed(3)
w1=np.random.randn(1) # random weight initialization
w2=np.random.randn(1)
w0=0
iteration=2000
n=len(x1)
learning_rate=0.01
x1 = featureNormalize(x1)
x2= featureNormalize(x2)
y = featureNormalize(y)
for i in range(iteration):
y_predicted = w1 * x1 + w2 * x2 +w0
cost = (1*(2/n))*float(sum((y_predicted-y)**2)) # cost function
x1d = sum(x1*(y_predicted-y))/n # derivative for feature x1
x2d = sum(x2*(y_predicted-y))/n # derivative for feature x2
cd = sum(1*(y-y_predicted))/n # derivative for bias
w1 = w1 - learning_rate * x1d
w2 = w2 - learning_rate * x2d
w0 = w0 - learning_rate * cd
print(f"Iteration {i}: w1= {w1}, w2 = {w2}, w0 = {w0}, cost = {cost} ")
return w1,w2, w0
w1,w2,w0 = multivar_gradient_descent(x1,x2,y)
w1,w2,w0
结果:
Iteration 0: w1= [1.77637709], w2 = [0.42425847], w0 = 0.0, cost = 3.0019278255843362
Iteration 1: w1= [1.76437073], w2 = [0.41225211], w0 = -4.440892098500626e-19, cost = 2.883051483691196
Iteration 2: w1= [1.75260451], w2 = [0.40048588], w0 = -4.440892098500626e-19, cost = 2.7688826449370247
Iteration 3: w1= [1.7410736], w2 = [0.38895498], w0 = -8.881784197001253e-19, cost = 2.659234892197519
Iteration 4: w1= [1.72977332], w2 = [0.37765469], w0 = -4.440892098500626e-19, cost = 2.553929190466498
:
:
Iteration 1787: w1= [1.17605931], w2 = [-0.17605931], w0 = 1.443289932012704e-18, cost = 4.560910257100077e-30
Iteration 1788: w1= [1.17605931], w2 = [-0.17605931], w0 = 1.443289932012704e-18, cost = 3.850935442401166e-30
Iteration 1789: w1= [1.17605931], w2 = [-0.17605931], w0 = 1.443289932012704e-18, cost = 3.850935442401166e-30
Iteration 1790: w1= [1.17605931], w2 = [-0.17605931], w0 = 1.443289932012704e-18, cost = 3.850935442401166e-30
Iteration 1791: w1= [1.17605931], w2 = [-0.17605931], w0 = 1.443289932012704e-18, cost = 3.850935442401166e-30
目前没有回答
相关问题 更多 >
编程相关推荐