简体   繁体   English

使用for循环python对列表中的值求和时获取NaN

[英]Getting NaN when summing values in list with a for loop python

I am trying to obtain this sum:我正在尝试获得这笔款项: 在此处输入图像描述

But I have been having issues with the second term.但是我在第二个学期遇到了问题。

This is my code so far:到目前为止,这是我的代码:

hbar = 1.05457e-34
kB_si = 1.380649e-23
T = 3500
path = 'C:\\Users\\valit\\'  #path to file
file = 'data_175_phonopy_.dat'


k,f = np.loadtxt(path +file, unpack=True)

f = f//1e-12 #to convert THz to Hz
# print(f)

nondeg = []
for i in f:
    if i not in nondeg:
        nondeg.append(i)

svib = 0
for i in range(len(nondeg)):
    svib = svib + (-kB_si * (math.exp((-hbar * nondeg[i])/(kB_si * T))) - (1/T) * hbar * nondeg[i] / (math.exp((hbar * nondeg[i])/(kB_si * T)) - 1)) #nan

print(svib)

When I print svib I get Nan with the following message:当我打印 svib 时,我得到 Nan 并显示以下消息:

nan
C:\Users\valit\svib_modes.py:45: RuntimeWarning: invalid value encountered in double_scalars
  svib =  svib - ((1/T) * hbar * nondeg[i] / (math.exp((hbar * nondeg[i])/(kB_si * T)) - 1))

I know the issue is the second term because I added the two terms separately, and the first one gives me a really small number.我知道问题出在第二个词上,因为我分别添加了两个词,而第一个给了我一个非常小的数字。 Do you have any suggestions on how to get rid of the NaN?您对如何摆脱 NaN 有什么建议吗? I apologize if my question is really simple, but I have been stuck for a while and could not find a way to solve it.如果我的问题真的很简单,我深表歉意,但我已经卡了一段时间,找不到解决方法。

Thank y'all in advance.提前谢谢大家。

There are likely values in your file that are not numbers and performing arithmatic operations on nan result in nan .您的文件中可能存在不是数字的值,并且对nan执行算术运算会导致nan ie nan * 3 == nan and nan + 3 == nan .nan * 3 == nannan + 3 == nan

If you just want to ignore the nan values in your file, and only operate on the numbers, you could just filter them out:如果您只想忽略文件中的nan值,而只对数字进行操作,则可以将它们过滤掉:

f = f[~numpy.isnan(f)]

Just make sure you do want to ignore the nan values and they're not there due to some problem you cannot safely ignore.只需确保您确实想忽略nan值,并且由于某些您无法安全忽略的问题,它们不存在。

There are several causes for this error, as stated in 'invalid value encountered in double_scalars' warning, possibly numpy .此错误有多种原因,如“double_scalars 中遇到的无效值”警告中所述,可能是 numpy So it is very difficult to help you without the data.因此,如果没有数据,很难为您提供帮助。

However, I would recommend you to investigate if a division-by-zero is taking place, because it seems to be a common cause for this error.但是,我建议您调查是否发生被零除,因为这似乎是导致此错误的常见原因。

Also, you're calculating a very long expression in one shot.此外,您正在一次计算一个很长的表达式。 I should say this is a very bad practice.我应该说这是一个非常糟糕的做法。

Instead, split your expression in smaller parts and calculate it step by step, doing the required validations before each step.相反,将您的表达式拆分为更小的部分并逐步计算它,在每一步之前进行所需的验证。

Eg, instead of:例如,而不是:

x = a / b + c * d / e

it would be more rational to do something like:做类似的事情会更合理:

if (b == 0):
  # raise error, or do something about it

x = a / b

if (e == 0):
  # raise error, or do something about it

x += c * d / e

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

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