相关疑难解决方法(0)

使用.corr获取两列之间的相关性

我有以下pandas数据帧Top15: 在此输入图像描述

我创建了一个列,用于估算每人可引用文档的数量:

Top15['PopEst'] = Top15['Energy Supply'] / Top15['Energy Supply per Capita']
Top15['Citable docs per Capita'] = Top15['Citable documents'] / Top15['PopEst']
Run Code Online (Sandbox Code Playgroud)

我想知道人均可引用文件数量与人均能源供应量之间的相关性.所以我使用.corr()方法(Pearson的相关性):

data = Top15[['Citable docs per Capita','Energy Supply per Capita']]
correlation = data.corr(method='pearson')
Run Code Online (Sandbox Code Playgroud)

我想返回一个数字,但结果是: 在此输入图像描述

python correlation pandas

101
推荐指数
5
解决办法
23万
查看次数

numpy corrcoef - 计算相关矩阵,同时忽略丢失的数据

我正在尝试计算几个值的相关矩阵.这些值包括一些'nan'值.我正在使用numpy.corrcoef.对于输出相关矩阵的元素(i,j),我希望使用对于变量i和变量j都存在的所有值来计算相关性.

这就是我现在拥有的:

In[20]: df_counties = pd.read_sql("SELECT Median_Age, Rpercent_2008, overall_LS, population_density FROM countyVotingSM2", db_eng)
In[21]: np.corrcoef(df_counties, rowvar = False)
Out[21]: 
array([[ 1.        ,         nan,         nan, -0.10998411],
       [        nan,         nan,         nan,         nan],
       [        nan,         nan,         nan,         nan],
       [-0.10998411,         nan,         nan,  1.        ]])
Run Code Online (Sandbox Code Playgroud)

太多的南瓜:(

python numpy correlation pandas

15
推荐指数
2
解决办法
2万
查看次数

python - 无法使corr工作

我正在努力完成简单的关联.我尝试过类似问题所提出的所有建议.

以下是代码的相关部分,我所做的各种尝试及其结果.

import numpy as np
import pandas as pd

try01 = data[['ESA Index_close_px', 'CCMP Index_close_px' ]].corr(method='pearson')

print (try01) 
Run Code Online (Sandbox Code Playgroud)

日期:

Empty DataFrame
Columns: []
Index: []
Run Code Online (Sandbox Code Playgroud)
try04 = data['ESA Index_close_px'][5:50].corr(data['CCMP Index_close_px'][5:50])
print (try04)
Run Code Online (Sandbox Code Playgroud)

日期:

**AttributeError: 'float' object has no attribute 'sqrt'**
Run Code Online (Sandbox Code Playgroud)

使用numpy

try05 = np.corrcoef(data['ESA Index_close_px'],data['CCMP Index_close_px'])
print (try05)
Run Code Online (Sandbox Code Playgroud)

日期:

AttributeError: 'float' object has no attribute 'sqrt'
Run Code Online (Sandbox Code Playgroud)

将列转换为列表

ESA_Index_close_px_list = list()
start_value = 1
end_value = len (data['ESA Index_close_px']) +1
for items in data['ESA Index_close_px']:
    ESA_Index_close_px_list.append(items)
    start_value = start_value+1    
    if …
Run Code Online (Sandbox Code Playgroud)

numpy correlation python-3.x pandas

8
推荐指数
1
解决办法
7352
查看次数

标签 统计

correlation ×3

pandas ×3

numpy ×2

python ×2

python-3.x ×1