Skip to main content
 首页 » 编程设计

python之机器学习梯度下降python实现

2024年11月24日10telwanggs

问题

我已经写了这段代码,但是它给出了错误:

RuntimeWarning: overflow encountered in multiply

t2_temp = sum(x*(y_temp - y)) 

RuntimeWarning: overflow encountered in double_scalars

t1_temp =  sum(y_temp - y) 

我应该使用特征缩放还是我的代码有问题?

代码

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 
 
def gradient_descent(x,y,t1,t2,repeat,alpha): 
    n = x.size 
    for i in range(repeat): 
        y_temp = x*t2 + t1 
        t1_temp =  sum(y_temp - y) 
        t2_temp = sum(x*(y_temp - y)) 
        t1 = t1 - alpha * (t1_temp/n) 
        t2 = t2 - alpha * (t2_temp/n) 
 
    return [t1,t2] 
 
 
d = pd.read_csv("train.csv") 
 
x = d['GrLivArea'] 
y = d['SalePrice'] 
 
x = (np.array(x.values)) 
y = (np.array(y.values)) 
 
 
alpha = 0.047 
repeat = 3000 
 
theta = [1.23154644,1.654132398] 
 
tt = gradient_descent(x,y,theta[0],theta[1],repeat,alpha) 
 
print("FINISH") 

请您参考如下方法:

您的代码没问题; x 和 y 太大了。您可以执行标准化或某种其他形式的缩放。