@hanxiaoyang
2016-11-14T10:20:22.000000Z
字数 850
阅读 3092
机器学习
By @寒小阳
我们来看几个简单的例子,for循环和numpy矢量化运算的对比。
对于x向量的每个x[i]和y向量的每个y[j],计算x[i] * y[j],并求和
# 非矢量化运算
def sum_products(x, y):
"""
>>> sum_products(np.arange(3000), np.arange(3000))
20236502250000
"""
result = 0
for i in range(len(x)):
for j in range(len(y)):
result += x[i] * y[j]
return result
#矢量化运算
np.sum(x) * np.sum(y)
返回所有满足x[i] < y[j]的个数
# 非矢量化运算
def count_lower(x, y):
"""
>>> count_lower(np.arange(0, 200, 2), np.arange(40, 140))
4500
"""
result = 0
for i in range(len(x)):
for j in range(len(y)):
if x[i] < y[j]:
result += 1
return result
#矢量化运算
np.sum(np.searchsorted(np.sort(x), y))
如果一个数组里有x[i] == missing,用value做替代,最后返回这个数组.
def clean_up(x, missing=-1, value=0):
"""
>>> clean_up(np.arange(-3, 3), value=10)
... # doctest: +NORMALIZE_WHITESPACE
array([-3, -2, 10, 0, 1, 2])
"""
result = []
for i in range(len(x)):
if x[i] == missing:
result.append(value)
else:
result.append(x[i])
return np.array(result)
#矢量化运算
np.where(x == missing, value, x)