如何修复错误,是使一个矩阵单列吗?这是多元线性回归。这是正确的方法梯度下降/误差在多元线性回归?对每个x做还是对整个x做?
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
%matplotlib inline
import os
path = os.getcwd() + 'farmExpense.txt'
x1=[]
x2=[]
x3=[]
x4=[]
y=[]
file = open("farmExpense.txt","r")
while True:
line = file.readline()
if not line:
break
else:
values = line.split(",")
x1.append(float(values[0]))
x2.append(float(values[1]))
x3.append(float(values[2]))
x4.append(float(values[3]))
y.append(float(values[4].rstrip("n")))
file.close()
x1 = np.matrix(x1).T
x2 = np.matrix(x2).T
x3 = np.matrix(x3).T
x4 = np.matrix(x4).T
y = np.matrix(y).T
x = np.hstack((x1,x2,x3,x4))
x = np.matrix(x)
print(x)
lm.fit(x,y)
theta0 = lm.intercept_[0]
theta1 = lm.coef_[0][0]
theta2 = lm.coef_[0][1]
theta3 = lm.coef_[0][2]
theta4 = lm.coef_[0][3]
print("The equation that best models our data is:")
print("y=",theta0,"+",theta1,"*X1 +",theta2,"*X2 +",theta3,"*X3 +",theta4, "*X4")
#Initial Guess for Weights in hypothesis function
theta0 = 1
theta1 = 0.5
theta2 = 0.2
theta3 = 0.3
theta4 = 0.6
#learning rate
alpha = 0.011
#convergence so we know when to stop (0.1 -> converage after 1 decimal spot, 0.01 converage after 2 decimal spots)
conv = 0.001
m = len(x)
print(len(x))
x1 = np.matrix(x1).T
x2 = np.matrix(x2).T
x3 = np.matrix(x3).T
x4 = np.matrix(x4).T
y = np.matrix(y).T
theta = np.array([[1],[0.5],[0.2],[0.3],[0.6]])
print(theta)
ones = np.ones((m,1))
print(ones)
x = np.hstack((ones,x))
previousCost = 0
numSteps = 0
while True:
#Counts how many times the gradient descent takes to reach the minimum cost
numSteps = numSteps + 1
#create the hypothesis matrix
#Its the dot product betwween the x matrix and the Theta matrix
hypothesis = x.dot(theta)
#create the error matrix
#Its the hypothesis matrix subtracted by the y matrix
error = (hypothesis-y)
#Calculate the sum of the errors squared
#Its the dot product between the transpose of the error matrix and the non transposed error matrix
totalError = error.T.dot(error)
#Calculate the cost
#Its 1/(2m) multiplied by the total error
cost = (1/(2*m)*totalError)
#Checking if the cost has converged to less than the conversion parameter
if abs(previousCost - cost) <= conv:
break
else:
#Calculate new alpha error for theta0
#Its the dot product of the transposed error matrix with the first column of the x matrix x[:,0]
alphaError0 = error.T.dot(x[:, 0])
#Calculate new alpha error for theta1
#Its the dot product of the transposed error matrix with the second column of the x matrix x[:,1]
alphaError1 = error.T.dot(x[:, 1])
#Calculate the new theta0 value
#Its the theta0 value - alpha/m multiplied by alphaError0
theta0 = (theta0-alpha/m*alphaError0)
#Calculate the new theta1 value
#Its the theta1 value - alpha/m multiplied by alphaError1
theta1 = (theta1-alpha/m*alphaError1)
#Put the new theta values into the theta matrix
theta = np.matrix([[theta0[0,0]],[theta1[0,0]]])
#reset the previous cost for next comparison
previousCost = cost
不,您不需要将矩阵设置为单列。
实际情况是你有一个变量shape(19,11)和另一个变量shape(5,1)。
当取点积时基本上当你用*
或.dot
将两个变量乘在一起时,为了使它工作,第一个变量的最后一个索引和第二个变量的第一个索引必须相同所以.dot
的形状(19,11)和(11,1)可以工作或者(19,5)和(51)可以工作但是一旦这些中间维度不匹配你就有问题了
您没有指定错误在哪里,但我会对使用。dot或*的变量的形状进行检查。