gpt4 book ai didi

python - 在 Python 中处理/显示极大的值

转载 作者:行者123 更新时间:2023-11-30 09:09:20 24 4
gpt4 key购买 nike

我目前正在尝试对使用 scikit-learn 库获得的名为 load_boston 的玩具数据集执行批量梯度下降。数据集的尺寸为506 x 13,由100个数量级的数据组成。下面是我的 Python 脚本,后面是运行该脚本时出现的错误。

boston_data_regression.py

import scipy
import numpy

from sklearn.datasets import load_boston

def generateGradient (X, Y, m, alpha, theta, num_iterations) :

X_transpose = X.transpose()

for i in range(0, num_iterations) :
hypothesis = numpy.dot(X, theta)
delta = hypothesis - Y
cost = numpy.sum(delta ** 2) / (2 * m)

print ("No. iteration : %d | Cost : %ld" % ((i + 1), cost))

gradient = numpy.dot(X_transpose, delta) / m
theta = theta - alpha * gradient

return (theta)

if __name__ == '__main__' :

boston_data = load_boston()
X = boston_data.data[:, 0:11]
Y = boston_data.data[:,12]

print (boston_data.data)

print (numpy.shape(X))
print (numpy.shape(Y))

num_iterations = 100000
alpha = 0.0005
m, n = numpy.shape(X)

theta = numpy.ones(n)
theta = generateGradient(X, Y, m, alpha, theta, num_iterations)

print (theta)

错误:

No. iteration : 75 | Cost : 5107568749643583921695342267251134617186569132604666005559083886757991071451800270203896531093730395389956630990780914914913406418422174358389131741568461360913005557192743665544540413282512755425657295941969706284629047517505070375172805106443882740219842668724638239205198801815953626988648840822784
No. iteration : 76 | Cost : 50304231336916560424319335120140228744355885776376593114754676052001428477104842266241766923801372402675185672996149747402542290566577918714034301765248577735574592772115140169849029676464020678156657455729204985429508262045621361912203426365153327346440580108502094724090338985744326599309593512431845376
boston_data_regression.py:13: RuntimeWarning: overflow encountered in square
cost = numpy.sum(delta ** 2) / (2 * m)
Traceback (most recent call last):
File "boston_data_regression.py", line 38, in <module>
theta = generateGradient(X, Y, m, alpha, theta, num_iterations)
File "boston_data_regression.py", line 15, in generateGradient
print ("No. iteration : %d | Cost : %ld" % ((i + 1), cost))
TypeError: %d format: a number is required, not numpy.float64

我可以知道如何对这个错误进行排序吗?是否有更好/更优化的方式来执行批量梯度下降?

最佳答案

你的问题源于你的值(value)观。您的值稳步增加到 5.e+304,并且在后续时间步骤中会引发错误,该错误可能来自溢出。

您可以通过以下方式检查 numpy.float64 值的限制:

import numpy
numpy.finfo('d')
finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64)

如您所见,最大值约为 1.8e+308。解决此问题的方法是缩小值。

关于python - 在 Python 中处理/显示极大的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44153628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com