我正试图使用numpy
绘制CCDF
,输入为csv
,#keywords
为col[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)
CCD_ 13是一维空数组,因此CCD_。
要禁用此传递comments=None
到genfromtxt
:
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.])