【问题标题】:Multiple linear regression for real estate dataset prediction房地产数据集预测的多元线性回归
【发布时间】:2021-07-13 13:20:14
【问题描述】:

我学习了线性回归,所以我决定在这个房地产数据集https://archive.ics.uci.edu/ml/datasets/Real+estate+valuation+data+set进行测试

我使用梯度下降来计算我的权重,但它们不会收敛到最优值。如果我将学习率设置为 0.01 并且迭代次数等于 100,我会得到 nan 和 inf 值,如果我将学习率设置为 0.0001 之类的值,它将不会收敛,或者我会得到 nan 和 inf 值。

import openpyxl
from pathlib import Path
import numpy as np

file = Path(r'C:\Users\Cata\Desktop', 'lini.xlsx')
file = openpyxl.load_workbook(file)

fila = file.active

def Loss(x, w, y):
    pred = np.dot(x, w)
    m = len(y)
    return 1/(2*m) * np.sum(np.square(pred - y))

def dLoss(x, w, y):
    pred = np.dot(x, w)
    m = len(y)
    return  1/m * np.dot(np.transpose(x), (pred - y))

def Gradient(x, y ,learning = 0.000001, iterations = 1000):
    w = np.random.rand(7)
    m = len(y)

    for i in range(iterations):
        w = w - learning*dLoss(x,w,y)

    return w

x = []
y = []

for row in fila.iter_rows(min_row=2,max_row=400):
    acm = []
    acm.append(1)
    for i in range(1, 7):
        acm.append(float(row[i].value))
        #print(row[i].value, end=" ")
    x.append(acm)
    y.append(float(row[7].value))

y = np.array(y)

sa = Gradient(x,y)
print(sa)
test = [1, 2013.500, 6.5,   90.45606,   9,  24.97433,   121.5431]
print(test)
print(np.dot(test, sa))

我也收到这个错误

RuntimeWarning: invalid value encountered in subtract
  w = w - learning*dLoss(x,w,y)

【问题讨论】:

    标签: python machine-learning linear-regression


    【解决方案1】:

    这是你可以做的,首先你更新体重的公式不正确。试试

    w = w + learning*dLoss(x,w,y)
    

    将学习率取为0.01或0.001(不要让学习率太小),现在应该收敛了。

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 2021-01-04
      • 2015-06-21
      • 2014-05-01
      • 2019-06-11
      • 2017-10-31
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      相关资源
      最近更新 更多