Cut将x的范围划分为区间,并根据x中的区间对x中的值进行编码,我可以用R中的Cut函数得到一个小的群,我怎么做呢 在python代码中工作?在
raw_data='
82 68 86 94 89 63 77 76 84 89
75 78 81 82 76 99 80 84 89 88
60 83 72 83 85 56 86 68 75 100
90 84 75 86 74 77 95 63 80 76
100 43 76 81 79 74 96 52 69 86'
data<-scan(textConnection(raw_data))
table(cut(data,include.lowest=TRUE,right=FALSE,breaks=c(0,60,70,80,90,100))
输出为:
^{pr2}$
我提议制定以下准则:
结果
^{pr2}$一。在
编辑
我对算法中的这些}都不满意。
if
、for
、else
和{所以我给出了一个新的代码,我认为这是一个改进,因为它更简短、更清晰。我觉得它也更快,但我没有测试速度。在
困难在于,当原始数据中的一个数字等于极限序列
tu = (0,60,70,80,90,100)
的最后一个值(样本中为100),这个数字必须被计算为属于最后一个跨度,这里是样本中的[90,100]
(数学表示法),而前面的跨度如[0,60[
,[60,70[
。。。不包括上限。但是我最终找到了正确的测试
x==tuLast==lim
来保存这个特定的情况,然后能够使用一个生成器来代替以前可怕的算法,它有几个测试if
和一个循环。在以下代码在Python3中是可执行的。在
结果
您可以使用
bisect
获取它所属的“范围”,并使用defaultdict
作为每个范围中出现次数的计数器,例如:结果
^{pr2}$count
是:正如苏姆建议的那样,你可以用熊猫来做这个。但是如果您想避免外部导入并且
raw_data
不是太大,可以使用:Acknowledgements
相关问题 更多 >
编程相关推荐