我正在使用Windows。我只想输入数组并获取数组的cdf。Python:如何绘制一个给定数组数组的cdf函数
3
A
回答
3
首先,可以实现CDF这样的:
from bisect import bisect_left
class discrete_cdf:
def __init__(data):
self._data = data # must be sorted
self._data_len = float(len(data))
def __call__(point):
return (len(self._data[:bisect_left(self._data, point)])/
self._data_len)
使用上述类,则可以这样绘图:
from scipy.stats import norm
import matplotlib.pyplot as plt
cdf = discrete_cdf(your_data)
xvalues = range(0, max(your_data))
yvalues = [cdf(point) for point in xvalues]
plt.plot(xvalues, yvalues)
编辑:arange
在这里没有意义,对于x和x + 1之间的所有点,cdf将始终相同。
+0
不错的解决方案。您可以将len(self._data [:bisect_left(self._data,point)])'简化为'bisect_left(self._data,point)',因为slice已经指定了长度。也许'bisect_right'也会更好,因为CDF是用于P(X <= x)的点。 – 2012-03-14 06:08:38
2
这是你在追求什么? 我提供了一个函数来近似cdf并绘制它。 (假设要输入与y值的PDF阵列)
import matplotlib.pyplot as plt
from math import exp
xmin=0
xmax=5
steps=1000
stepsize=float(xmax-xmin)/float(steps)
xpoints=[i*stepsize for i in range(int(xmin/stepsize),int(xmax/stepsize))]
print xpoints,int(xmin/stepsize),int(xmax/stepsize)
ypoints=map(lambda x: exp(-x),xpoints)
def get_cdf(pdf_array):
ans=[0]
for i in range(0,len(pdf_array)-1):
ans.append(ans[i]+(pdf_array[i]+pdf_array[i+1])/2.0*stepsize)
return ans
cdfypoints=get_cdf(ypoints)
plt.plot(xpoints,ypoints)
plt.plot(xpoints,cdfypoints)
plt.show()
相关问题
- 1. Matlab:如何创建一个CDF数组
- 2. MATLAB - 如何使用负指数数组绘制一个函数?
- 3. 在Python中绘制一个数组
- 4. 在给定一个数组的数组
- 5. 如何计算python中的给定数组中的子数组?
- 6. 如何在python中绘制一个数组?
- 7. 将一组函数作为参数传递给Python中的另一个函数
- 8. 在matplotlib中绘制离散计数数据的有界CDF python
- 9. 运行一个函数,更新数组虽然Matplotlib绘制它
- 10. 如何将一个数组和一个键传递给一个函数的参数? (使用多维数组)
- 11. 如何将一个php数组分配给一个javascript数组?
- 12. 如何使用gadfly绘制函数返回一个数组的元素
- 13. 如何将数组传递给Javascript中的另一个函数?
- 14. qwt:绘制一维数组?
- 15. 对另一个数组给定值的数组计数发生
- 16. 创建一个总和给定数字的数组数组
- 17. 如何将对象数组传递给P5.js绘图函数?
- 18. 如何选择和绘制python中的特定数组?
- 19. 使用数组参数声明一个python函数并将数组参数传递给函数调用?
- 20. 绘制一个已定义的函数
- 21. Python(3.4.2)如何绘制具有给定半径的二维数组作为圆?
- 22. 制作一个javascript数组函数
- 23. 在R中绘制数据集的CDF?
- 24. 给定两个数组数组
- 25. 给出一个数组,它将返回给定数组中的唯一数字
- 26. 如何创建从给定的二维数组一维数组
- 27. 从一个数组中绘制图像
- 28. 在healpy中绘制一个numpy数组
- 29. 给定一个数组找到m个奇数的子数组的数量?
- 30. 如何在JavaScript中将数组数组传递给函数?
你有Matplotlib和numpy/scipy吗? – 2011-05-27 08:49:57
@ Space_C0wb0y:是 – Bruce 2011-05-27 09:09:31
@Bruce:另外,你的意思是* cdf的数组* *? cdf必须根据某个分布进行计算。 – 2011-05-27 09:25:58