IndexError: too many indices for array Numpy Plotting CCDF with CSV

IndexError: too many indices for array Numpy Plotting CCDF with CSV

我正在尝试使用 numpy 绘制 CCDF,输入为 csv#keywordscol[0],频率为 col[1] .

输入

#Car,45
#photo,4
#movie,6
#life,1

输入有超过 10K 行和两列,其中 col[0] 根本没有使用,只有来自 col[1] 的频率用于绘制 CCDF。数据中间没有空行,eof也没有空行。

代码:

import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
from pylab import*
import math
from matplotlib.ticker import LogLocator

data = np.genfromtxt('input.csv', delimiter=",")

d0=data[:,1]
X0 = np.sort(d0)
cdf0 = np.arange(len(X0))/float(len(X0))
#cumulative = np.cumsum(data)
ccdf0 = 1 - cdf0
plt.plot(X0,ccdf0, color='b', marker='.', label='Frequency')

plt.legend(loc='upper right')
plt.xlabel('Freq (x)')
plt.ylabel('ccdf(x)')
plt.gca().set_xscale("log")
#plt.gca().set_yscale("log")
plt.show()

错误

Traceback (most recent call last):
  File "00_plot_ccdf.py", line 17, in <module>
    d0=data[:,1]
IndexError: too many indices for array

提前致谢

genfromtxt 默认情况下将以 # 开头的行视为注释,所以实际上你的 data 是空的:

In [1]: genfromtxt('test.csv', delimiter=',')         
/usr/lib/python3/dist-packages/numpy/lib/npyio.py:1385: UserWarning: genfromtxt: Empty input file: "test.csv"
  warnings.warn('genfromtxt: Empty input file: "%s"' % fname)
Out[1]: array([], dtype=float64)

data 是一维空数组,因此 [:,1] 索引太多。

要禁用此传递 comments=Nonegenfromtxt:

In [20]: genfromtxt('test.csv', delimiter=',', comments=None)
Out[20]: 
array([[ nan,  45.],
       [ nan,   4.],
       [ nan,   6.],
       [ nan,   1.]])

由于您只需要第 2 列,您也可以直接将结果限制为:

In [21]: genfromtxt('test.csv', delimiter=',', comments=None, usecols=(1,))
Out[21]: array([ 45.,   4.,   6.,   1.])