简体   繁体   English

在 Python 中控制 output 的梯度下降 class

[英]Controlling output of gradient descent class in Python

I've created a LinearRegression class as part of a homework problem, but the outputs I get display crazy amounts of decimal points along with scientific notation.我创建了一个 LinearRegression class 作为家庭作业的一部分,但是我得到的输出显示疯狂的小数点以及科学记数法。 I would really love if my output only displayed values to just a few decimal points.如果我的 output 只显示小数点后几位的值,我会非常喜欢。

I've tried giving f-string and format a try.我试过给 f-string 和 format 一个尝试。 Those do reduce the number of decimals displayed, but the scientific notation remains in the output, and occasionally some values just display as 'inf'.这些确实减少了显示的小数位数,但科学记数法仍保留在 output 中,有时某些值仅显示为“inf”。 Examples below.下面的例子。

If I test this out with the following (as per the assignment):如果我使用以下内容(根据作业)对此进行测试:

from model import LinearRegression # model.py is my file

model = LinearRegression()
examples = list(range(1, 1000))
values = list(range(1, 1000))

for i in range(len(examples)):
    model.addSample(examples[i], values[i])

print(model.fit(0.0006, 100))

I get the output:我得到 output:

(2.1803197675124753549e+229, 1.4528186498977718021e+232, 1.7775211633675145435e+465)

When I add formatting (eg, returning '{:.4}'.format(self.theta_0) in my class's fit() method), I get:当我添加格式(例如,在我的类的 fit() 方法中返回 '{:.4}'.format(self.theta_0) )时,我得到:

('2.18e+229', '1.453e+232', 'inf')

I'm really not sure why it's being so finicky.我真的不知道为什么它如此挑剔。 There must be something I'm missing.一定有什么我错过了。 Any help is greatly appreciated!任何帮助是极大的赞赏!

'{:.4f}'.format(self.theta_0) '{:.4f}'.format(self.theta_0)

OR或者

import numpy as np导入 numpy 作为 np

np.set_printoptions(suppress=True) np.set_printoptions(抑制=真)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM