Python-无法使Corr工作 [英] python - cannot make corr work

查看:147
本文介绍了Python-无法使Corr工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm struggling with getting a simple correlation done. I've tried all that was suggested under similar questions.

这是代码的相关部分,我进行的各种尝试及其结果.

Here are the relevant parts of the code, the various attempts I've made and their results.

import numpy as np
import pandas as pd

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

print (try01) 

出局:

Empty DataFrame
Columns: []
Index: []


try04 = data['ESA Index_close_px'][5:50].corr(data['CCMP Index_close_px'][5:50])
print (try04)

出局:

**AttributeError: 'float' object has no attribute 'sqrt'**


使用numpy

try05 = np.corrcoef(data['ESA Index_close_px'],data['CCMP Index_close_px'])
print (try05)

出局:

AttributeError: 'float' object has no attribute 'sqrt'


将列转换为列表

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 start_value == end_value:
        break
    else:
        continue

CCMP_Index_close_px_list = list()
start_value = 1
end_value = len (data['CCMP Index_close_px']) +1
for items in data['CCMP Index_close_px']:
    CCMP_Index_close_px_list.append(items)
    start_value = start_value+1    
    if start_value == end_value:
        break
    else:
        continue

try06 = np.corrcoef(['ESA_Index_close_px_list','CCMP_Index_close_px_list'])
print (try06)

出局:

****TypeError: cannot perform reduce with flexible type****


还尝试了.astype,但没有任何区别.


Also tried .astype but not made any difference.

data['ESA Index_close_px'].astype(float)

data['CCMP Index_close_px'].astype(float)

使用Python 3.5,pandas 0.18.1和numpy 1.11.1

Using Python 3.5, pandas 0.18.1 and numpy 1.11.1

真的很感谢任何建议.

*** 数据来自Excel电子表格 data = pd.read_excel('C:\\Users\\Ako\\Desktop\\ako_files\\for_corr_‌​tool.xlsx')进行关联尝试之前,只有列重命名和

**edit1:* Data is coming from an excel spreadsheet data = pd.read_excel('C:\\Users\\Ako\\Desktop\\ako_files\\for_corr_‌​tool.xlsx') prior to the correlation attempts, there are only column renames and

data = data.drop(data.index[0]) 

摆脱一行

关于类型:

print (type (data['ESA Index_close_px']))



print (type (data['ESA Index_close_px'][1]))

出:

**编辑2 * 部分数据:

**edit2* parts of the data:

print (data['ESA Index_close_px'][1:10])

print (data['CCMP Index_close_px'][1:10])

出局:

2        2137
3        2138
4        2132
5        2123
6        2127
7     2126.25
8      2131.5
9      2134.5
10       2159
Name: ESA Index_close_px, dtype: object
2     5241.83
3     5246.41
4     5243.84
5     5199.82
6     5214.16
7     5213.33
8     5239.02
9     5246.79
10    5328.67
Name: CCMP Index_close_px, dtype: object

推荐答案

好,我今天也遇到了同样的问题. 请尝试使用.astype('float64')来帮助正确设置类型.
data['ESA Index_close_px'][5:50].astype('float64').corr(data['CCMP Index_close_px'][5:50].astype('float64'))

Well, I've encountered the same problem today. try use .astype('float64') to help make the type correct.
data['ESA Index_close_px'][5:50].astype('float64').corr(data['CCMP Index_close_px'][5:50].astype('float64'))

这对我来说很好.希望它也能为您提供帮助.

This works well for me. Hope it can help you as well.

这篇关于Python-无法使Corr工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆