为什么 numpy.corrcoef() returns nan?

Why does numpy.corrcoef() returns nan?

正在尝试构建回归模型,但遇到了无法解决的问题。 用谷歌搜索并阅读了有关它的所有内容,但没有任何效果。有这个数据框:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 334195 entries, 0 to 334194
Data columns (total 12 columns):
type         334195 non-null int64
zipcode      334195 non-null int64
sqft         334195 non-null float64
lotsize      334195 non-null float64
beds         334195 non-null float64
baths        334195 non-null float64
year         334195 non-null float64
s_num        334195 non-null int64
s_rate       334195 non-null float64
s_dist       334195 non-null float64
crimes       334195 non-null float64
target       334195 non-null float64
dtypes: float64(9), int64(3)

正在尝试这样做:

data = pd.read_csv('data_prep_sale')
df = pd.DataFrame(data)

x = df.drop(['target'],axis=1)
y = pd.Series(df['target'])

trimmed_feature_names = []
for i in range(x.shape[1]):
    correlation = np.corrcoef(x.iloc[:,i],y)[0,1]
    if abs(correlation) > 0.5:
        feature_name = x.columns[i]
        print(feature_name, correlation)
        trimmed_feature_names.append(feature_name)

并继续为所有 x:

获取此矩阵

array([[ 1., nan],
       [nan, nan]])

这是一个数据样本:

type zipcode sqft lotsize beds baths year s_num s_rate s_dist crimes target
4 28387 2900.0 0.0 4.0 3.5 2019.0 8 5.20 5.54 6.0 144.137931
4 99216 1947.0 5828.0 3.0 3.0 2019.0 3 4.00 1.33 3.1 159.219312
3 90049 3000.0 8626.0 3.0 2.0 1967.0 3 6.67 1.96 4.4 965.000000
1 75205 6457.0 8220.0 5.0 8.0 2006.0 4 9.25 0.75 4.6 370.915286

Link to the complete data file

请帮帮我!需要任何想法!

根据上传的文件,target 列中有一些 inf 值...例如第 43、283、372 行...等。因此,要解决此问题,您必须删除所有 inf 行。此外,还有一种更好的方法可以找到 target 和其他特征之间的相关性。两者都显示在以下代码中:

import numpy as np
import pandas as pd

data = pd.read_csv('data_prep_sale.csv')
df = pd.DataFrame(data)

# remove any (inf, -inf, nan) values
df = df.replace([np.inf, -np.inf], np.nan).dropna()

# find the correlation between target other features
print(df.corr()["target"])

正如您从相关输出中看到的那样,所有值都远低于 0.5