Python中波士顿数据集的多变量线性回归

时间:2019-02-10 00:30:43

标签: python machine-learning linear-regression gradient-descent

我正在尝试对Boston数据集执行多变量线性回归。我在成本函数中观察到一些奇怪的行为。在最初的150-200次迭代期间,成本函数从(5-7)降低到1.something,但在大约200次迭代后,成本仍在增加。在第1000次迭代结束时,成本值达到120左右。我不明白为什么成本函数会显示这种行为,因为据我所知,一旦成本函数找到最佳值,它几乎就保持不变。我应该从中得出什么?

data = datasets.load_boston(return_X_y=False)
df = pd.DataFrame(data.data, columns=data.feature_names)
df = (df - df.mean())/df.std()

 X = df
 Y = data.target
 Y = (Y-Y.mean())/Y.std()

 X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.26, 
 random_state = 17)
 X_train = np.matrix(X_train)

 X0 = np.ones((len(X_train),1)) #adding column of 1's
 X_train= np.hstack((X0, X_train))
 X01 = np.ones((len(X_test),1))
 X_test= np.hstack((X01,X_test))

 np.random.seed(10)
 theta = np.random.rand(14)
 theta = theta.reshape(-1,1)

 theta = theta.reshape(-1,1)

def  computeCost(X, Y, theta):

    m = len(X)
    yp = np.dot(X, theta)


    error = yp - Y
    cost = 1/(2*m) * np.dot(error.T, error)


    return cost

def GradientDescent(X, Y, theta, alpha,itera):
    J = np.zeros((itera,1))
    m = len(X)
    E = (np.dot(X, theta)) - Y


for i in range(1,itera):

        theta = theta - (alpha * (1/m) * np.dot(X.T, E))
        print("shape of theta", theta.shape)
        J[i]   = computeCost(X,Y, theta)
        print("cost at ", i, " || ", J[i])
return J, theta

我使用公式 data = (data-data.mean())/data.std()

对数据进行了归一化

不重复次数与费用的关系图

0 个答案:

没有答案