如何解决RunTimeWarning:double_scalars中遇到的无效值?

时间:2018-07-31 14:28:00

标签: python

使用此代码运行时警告,请帮助我 我不知道为什么它不起作用的问题:

RuntimeWarning: invalid value encountered in double_scalars;

这是我每次执行此代码时发生的警告。

简单线性回归;

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


file = 'usedcars.csv'
points = np.array(np.genfromtxt(file, delimiter=',', skip_header=1))
learning_rate = 0.0000001
x = points[:,3]  
y = points[:,2]  

plt.scatter(x,y)
plt.xlabel('mileage')
plt.ylabel('price')
plt.show()

bias=0
weight=0
n=150
iters=200
learning_rate=0.001

def step_descent(b,k,x,y,learning_rate):
    b_grad=0
    k_grad=0
    for d in range(149):
        x_=x[d]
        y_=y[d]
        b_grad=b_grad + 1/n * (((k*x_)+b)-y_)
        k_grad=k_grad + 1/n * x_ * (((k*x_)+b)-y_)
    new_b=b-(learning_rate*b_grad)
    new_k=k-(learning_rate*k_grad)
    return [new_b,new_k]


def gradient_descent(x,y,iters,learning_rate,bias,weight):
    b=bias
    print(bias)
    k=weight
    print(weight)
    for i in range(iters):
        b,k=step_descent(b,k,x,y,learning_rate)
    return [b,k]    




bias,weight=gradient_descent(x,y,iters,learning_rate,bias,weight)

enter image description here

C:/Users/vicky viper/Downloads/Practice ML/ULR.py:30: RuntimeWarning: overflow encountered in double_scalars
  new_k=k-(learning_rate*k_grad)
C:/Users/vicky viper/Downloads/Practice ML/ULR.py:32: RuntimeWarning: invalid value encountered in double_scalars

0 个答案:

没有答案