Я должен реализовать многомерную линейную регрессию с нуля. Функция стоимости (MSE) продолжает расти после нескольких итераций - PullRequest
0 голосов
/ 15 марта 2020

Я должен реализовать многомерную линейную регрессию с нуля. Проблема в том, что функция затрат (MSE) продолжает увеличиваться (иногда достигает бесконечности) после итераций, тогда как она должна уменьшаться. Задача состояла в том, чтобы реализовать многомерный LR, используя MSE в качестве функции стоимости и Gradient Descent для обновления весов. Вот мой код:

'' '

dataset = pd.read_csv("multivariateLRData.csv")
dataset = dataset.to_numpy()

x = dataset[:,0:-1]#seperating the x and y
x=np.insert(x, 0, 1, axis=1)#inserting column of ones
# x=np.append(x,one)

y = dataset[:, -1]
#learning rate
lr=0.01
m=x.shape[0] #size of examples

#initializing weights
term = np.matmul(x.T,y)
#t is the array of weights
t = np.matmul(np.linalg.pinv(np.matmul(x.T,x)),term)

#hypothesis
hyp = t[0] + (t[1] * x[:,1]) + (t[2] * x[:,2]) + (t[3] * x[:,3]) + (t[4] * x[:,4])

#helping array to store all cost function's values
k=[]
count=0
count2=0
for i in range(0,100):
    for l in range(0,145):
        count+=(hyp[l]-y[l])**2

    mse = count/(2*m) #cost function of every iteration
    k.append(mse)
    #gradient descent
    for j in range(0,t.shape[0]):
        for l in range(0,145):
            count2 += (hyp[l] - y[l]) * x[l,j] # computing values of weights
        t[j] = t[j]-((lr/m)*count2)

    hyp = t[0] + (t[1] * x[:, 1]) + (t[2] * x[:, 2]) + (t[3] * x[:, 3]) + (t[4] * x[:, 4]) #updating the hypothesis

print(k)

' ''

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...