RuntimeWarning:在square中遇到溢出

时间:2017-03-14 12:23:13

标签: python numpy machine-learning linear-regression

我是机器学习和numpy的新手,我一直试图在sklearn上运行渐变下降来自sklearn,我的实现适用于小型随机数据集,但在波士顿数据集中它正在产生这些警告

<string>:12: RuntimeWarning: overflow encountered in square
<string>:15: RuntimeWarning: invalid value encountered in subtract

然后输出

Output
array([ nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
    nan,  nan,  nan])

这是我的梯度下降代码

import numpy as np
from sklearn.datasets import load_boston
from matplotlib import pyplot as plt
def gradient_descent(x,y,alpha,theta):
    m=y.shape[0]
    xtranspose = x.transpose()
    i=0
    cost =488
    while cost>0.5:
        hyp = np.dot(x, theta)
        loss = hyp - y
        cost = np.sum(loss ** 2)/(2*m)
        plt.scatter(i,cost)
        gradient = np.dot(xtranspose, loss)/m
        theta = theta - alpha * gradient
        i=i+1
    plt.show()
    return theta


    dataset = load_boston()
    m,n = dataset['data'].shape
    x = np.ones((m,n+1))
    x[:,:-1] = dataset['data']
    y= dataset['target']
    alpha=0.005
    theta=np.ones(x.shape[1])
    theta = gradient_descent(x,y,alpha,theta)

1 个答案:

答案 0 :(得分:0)

当输入为整数时,看到此类溢出警告并不罕见。首先尝试将它们转换为浮点数。如果损失是一个数组,则可以使用loss = np.array(loss,dtype = float)。如果损失是整数,则可以使用loss = float(loss)。