为什么我在尝试打印此变量时得到 Nan 值?

Why am i getting Nan value when trying to print this variable?

这是我的代码:

import numpy as np
import pandas as pd

df = pd.read_csv('dataset2.csv')
x = []
y = []

# Populate x and y values from csv :

for z in df['x'][0:]:
    x.append(float(z))

for z in df['y'][0:]:
    y.append(float(z))

x_mean = float(np.array(x).mean())
y_mean = float(np.array(y).mean())

num = 0.0
den = 0.0

print("type of num",type(num))

for z in range(len(x)):
    num += float(y[z]) - float(y_mean)
    den += float(x[z]) - float(x_mean)

print("type of num",type(num))

print("Numerator is",num)
print("Denominator is",den)

在我的整个代码中进一步说明这一点,我得到的是 Nan 值。

Output :
type of num <class 'float'>
type of num <class 'float'>
Numerator is nan
Denominator is 1.8836487925000256e-11

Process finished with exit code 0

dataset2.csv 文件:dataset2.csv

我试过在所有地方强制执行 float 类型转换,但无济于事。

根据你的源代码:

num += float(y[z]) - float(y_mean)

num依赖于两个变量,你应该把它们打印出来或者加一个检查:

if math.isnan(y[z]) or math.isnan(y_mean) :
    # sound the alarm

您的数据集中似乎有一个 NaN 值。 df.info() 产量:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 700 entries, 0 to 699
Data columns (total 2 columns):
x    700 non-null float64
y    699 non-null float64
dtypes: float64(2)
memory usage: 11.0 KB

如果您可以将 NaN 替换为零,您可以添加:

y = np.nan_to_num(y)

这一步之后:

for z in df['y'][0:]:
    y.append(float(z))

我在此更改后测试了您的代码,得到以下输出:

type of num <class 'float'>
type of num <class 'float'>
Numerator is -2.4726887204451486e-12
Denominator is 1.8836487925000256e-11