“希言自然。
故飘风不终朝,骤雨不终日。
孰为此者?
天地,天地尚不能久,而况于人乎?
故从事于道者同于道,德者同于德,失者同于失。
故同于道者,道亦得之;同于失,道亦失之。
信不足,焉有不信。”1
本文对如下方法进行记录:(持续更新)
- zip
- bincount
- enumerate
zip
zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表。
1 | import numpy as np |
2 | x = [2,5,3,5] |
3 | y = [1,3,2,5] |
4 | z = [8,7,9,0] |
5 | # i = (3,4,5,6) |
6 | i = list(zip(x,y,z)) |
7 | i |
output:
1 | [(2, 1, 8), (5, 3, 7), (3, 2, 9), (5, 5, 0)] |
bincount
1 | numpy.bincount(x, weights=None, minlength=None) |
数组x以及返回的数据bins,bins的数量比x中的最大值+1,bins[i]=x[i]在x中出现的次数。
比如:
1 | import numpy as np |
2 | x = [2,5,3,5] |
3 | c = np.bincount(x) |
4 | c |
output:
1 | array([0, 0, 1, 1, 0, 2]) |
2 | # 0在x中出现0次 |
3 | # 1在x中出现0次 |
4 | # 2在x中出现1次 |
5 | # 3在x中出现1次 |
6 | # 4在x中出现0次 |
7 | # 5在x中出现1次 |
看一下设置了weights的情况:
bins[i]=sum(x[i]*w[i])
1 | import numpy as np |
2 | x = [2,5,3,5] |
3 | w = [.4,.3,.7,.1] |
4 | c = np.bincount(x,w) |
5 | c |
output:
1 | array([ 0. , 0. , 0.4, 0.7, 0. , 0.4]) |
参数minlength貌似只有在minlength>x.len的时候有效,表示bins数组的最小长度。
这篇文章可以移步去看看 - bincount详解
enumerate
- enumerate()是python的内置函数
- enumerate在字典上是枚举、列举的意思
- 对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值
- enumerate多用于在for循环中得到计数output:
1enum = ['我','爱','北京','天安门']2for index,s in enumerate(enum):3print('index=%d s=%s'% (index,s))1index=0 s=我2index=1 s=爱3index=2 s=北京4index=3 s=天安门
1 :老子《道德经》,老子故里,中国鹿邑。