Content:
1. 确定列表中出现最多的元素
2. 高效计算L2范数
3. numpy.array花式索引
4. numpy矩阵分块求和
1. 确定列表中出现最多的元素
源地址在StackOverflow:https://stackoverflow.com/questions/1518522/find-the-most-common-element-in-a-list
代码如下:
def most_common(lst):
return max(set(lst), key=lst.count)
方法:集合set函数确定列表lst中的个元素。builtin函数count计算元素出现次数,并作为max函数的依据。然后最后用max找 到最频繁出现的值。
此中集合set(lst)可以被替换为lst,对于unhashable的内容同样奏效,但是这样会让计算速度变慢,不过对于大部分应用无关紧要。
2. 高效计算L2范数(无for循环)
m = x.shape[0] # x has shape (m, d)
n = y.shape[0] # y has shape (n, d)
x2 = np.sum(x**2, axis=1).reshape((m, 1))
y2 = np.sum(y**2, axis=1).reshape((1, n))
xy = x.dot(y.T) # shape is (m, n)
dists = np.sqrt(x2 + y2 - 2*xy) # shape is (m, n)
计算范数,dists[m,n]最后存储的是x中m行于y中n行的L2距离
3. numpy.array花式索引
Fancy indexing is conceptually simple: it means passing an array of indices to access multiple array elements at once. For example, consider the following array:
import numpy as np
rand = np.random.RandomState(42)
x = rand.randint(100, size=10)
print(x)
Suppose we want to access three different elements. We could do it like this:
[x[3], x[7], x[2]]
Alternatively, we can pass a single list or array of indices to obtain the same result:
ind = [3, 7, 4]
x[ind]
When using fancy indexing, the shape of the result reflects the shape of the index arrays rather than the shape of the array being indexed:
ind = np.array([[3, 7],
[4, 5]])
x[ind]
Fancy indexing also works in multiple dimensions. Consider the following array:
X = np.arange(12).reshape((3, 4))
X
Like with standard indexing, the first index refers to the row, and the second to the column:
row = np.array([0, 1, 2])
col = np.array([2, 1, 3])
X[row, col]
Notice that the first value in the result is X[0, 2]
, the second is X[1, 1]
, and the third is X[2, 3]
. The pairing of indices in fancy indexing follows all the broadcasting rules that were mentioned in Computation on Arrays: Broadcasting. So, for example, if we combine a column vector and a row vector within the indices, we get a two-dimensional result:
X[row[:, np.newaxis], col]
row[:, np.newaxis] * col
It is always important to remember with fancy indexing that the return value reflects the broadcasted shape of the indices, rather than the shape of the array being indexed.
Combined Indexing
For even more powerful operations, fancy indexing can be combined with the other indexing schemes we've seen:
print(X)
We can combine fancy and simple indices:
X[2, [2, 0, 1]]
We can also combine fancy indexing with slicing:
X[1:, [2, 0, 1]]
And we can combine fancy indexing with masking:
mask = np.array([1, 0, 1, 0], dtype=bool)
X[row[:, np.newaxis], mask]
All of these indexing options combined lead to a very flexible set of operations for accessing and modifying array values.
4. numpy矩阵分块求和
用最简单的循环语句做出来的效果是:
In [1]: test=np.arange(81).reshape(9,9)
In [2]: res=np.zeros((3,3))
In [3]: for i in range(3):
...: for j in range(3):
...: res[i,j]=test[3*i:3*(i+1),3*j:3*(j+1)].mean()
In [4]: res
Out[5]:
array([[ 10., 13., 16.],
[ 37., 40., 43.],
[ 64., 67., 70.]])
等价于:
In []: test.reshape(3,3,3,3).mean(axis=(1,3))
Out[]:
array([[ 10., 13., 16.],
[ 37., 40., 43.],
[ 64., 67., 70.]])
是不是很神奇
或者这样也行,重复式的坐标轴表示法:
test.reshape(3,3,3,3).mean(3).mean(1)
如果是三维数组可以这样:
test.reshape( 2,3,3,3,3).mean((2,4))
test.reshape(-1,3,3,3,3).mean((2,4))