为了完整起见,您还应考虑:重复:您可以在数据中多次拥有同一点。
点之间可以有不同的距离
点可以浮动
您可以使用numpy.histogram,以这样的方式设置箱子边缘,即每个箱子只收集一个点的所有出现。
您应该保留density=False,因为根据文档:Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen
您可以规范化每个bin中的元素数除以数据大小。import numpy as np
import matplotlib.pyplot as plt
def cdf(data):
data_size=len(data)
# Set bins edges
data_set=sorted(set(data))
bins=np.append(data_set, data_set[-1]+1)
# Use the histogram function to bin the data
counts, bin_edges = np.histogram(data, bins=bins, density=False)
counts=counts.astype(float)/data_size
# Find the cdf
cdf = np.cumsum(counts)
# Plot the cdf
plt.plot(bin_edges[0:-1], cdf,linestyle='--', marker="o", color