3

我试图显示两个单独列表之间的相关性。在安装 Numpy 之前,我解析了世界银行数据中的 GDP 值和互联网用户数量,并将它们存储在两个单独的列表中。这是代码片段。这仅适用于 gdp07。实际上,我有更多的清单和其他数据,例如失业。

import numpy as np

file = open('final_gdpnum.txt', 'r')
gdp07 = []
for line in file:
    fields = line.strip().split()
    gdp07.append(fields [0])    

file2 = open('internetnum.txt', 'r')
netnum07 = []
for line in file2:
    fields2 = line.strip().split()
    nnetnum07.append(fields2 [0])

print np.correlate(gdp07,netnum07,"full")

我得到的错误是这样的:

Traceback (most recent call last):
  File "Project3,py", line 83, in ,module.
    print np.correlate(gdp07, netnum07, "full")
  File "/usr/lib/python2.6/site-packages/numpy/core/numeric.py", line 645, in correlate
    return multiarray.correlate2(a,v,mode))
ValueError: data type must provide an itemsize

仅作记录,我在 Windows 计算机上使用 Cygwin 和 Python 2.6。我只使用 Numpy 及其依赖项及其构建的其他部分(gcc 编译器)。任何帮助都会很棒。谢谢

4

2 回答 2

3

当您尝试将数据作为字符串输入时,这可能是错误,因为根据 python 文档 strip() 返回一个字符串

http://docs.python.org/library/stdtypes.html

尝试将数据解析为您想要的任何类型

正如你在这里看到的

In [14]:np.correlate(["3", "2","1"], [0, 1, 0.5])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/dog/<ipython-input-14-a0b588b9af44> in <module>()
----> 1 np.correlate(["3", "2","1"], [0, 1, 0.5])

/usr/lib64/python2.7/site-packages/numpy/core/numeric.pyc in correlate(a, v, mode, old_behavior)
    643         return multiarray.correlate(a,v,mode)
    644     else:
--> 645         return multiarray.correlate2(a,v,mode)
    646 
    647 def convolve(a,v,mode='full'):

ValueError: data type must provide an itemsize

尝试解析值

In [15]: np.correlate([int("3"), int("2"),int("1")], [0, 1, 0.5])
Out[15]: array([ 2.5])



import numpy as np

file = open('final_gdpnum.txt', 'r')
gdp07 = []
for line in file:
    fields = line.strip().split()
    gdp07.append(int(fields [0]))    

file2 = open('internetnum.txt', 'r')
netnum07 = []
for line in file2:
    fields2 = line.strip().split()
    nnetnum07.append(int(fields2 [0]))

print np.correlate(gdp07,netnum07,"full")

您的另一个错误是字符结尾问题,我希望这可行,因为我认为我无法重现它,因为我有一个默认支持 utf-8 的 linux 机器。我通过 ipython 帮助(编解码器)文档 http://code.google.com/edu/languages/google-python-class/dict-files.html

import codecs

f =  codecs.open(file, "r", codecs.BOM_UTF8)
for line in f:
    fields = line.strip().split()
    gdp07.append(int(fields [0]))
于 2012-06-26T00:02:00.367 回答
0

尝试将数据转换为浮点类型。这个对我有用!

于 2017-03-18T16:16:50.050 回答