为什么numpy.corrcoef()返回nan?

问题描述 投票:0回答:1

[试图建立回归模型,但是遇到了我无法解决的问题。用谷歌搜索并阅读有关它的所有内容,但没有用。具有此数据框:

<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

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

python numpy correlation
1个回答
0
投票

根据上传的文件,在inf列中有一些target值...就像第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

© www.soinside.com 2019 - 2024. All rights reserved.