线性方程的Numpy语法

时间:2016-11-01 15:50:45

标签: python numpy linear-regression

我是numpy但不是python的新手。对numpy这样做的方法有疑问,请考虑:

修改:更正功能**

def _my_function(weights, features, bias):
    # the pure python way
    value = 0.
    for i in range(len(weights)):
         value += (weights[i]*features[i])

    return value+bias

这样做的最直接的方法是什么?

2 个答案:

答案 0 :(得分:3)

方法#1:dot-productnp.dot一起使用 -

weights.dot(features) + bias*len(weights)

方法#2:带来np.einsum以执行sum-reduction -

np.einsum('i,i->',weights,features) + bias*len(weights)

我认为方法#1会更好。

答案 1 :(得分:1)

如果weigthsfeatures是相同大小的numpy数组,则可以进行元素乘法运算:

values = np.sum(weights * features + bias)

如果您想避免向产品阵列的每个元素添加偏见,可以执行

values = np.sum(weights * features) + bias * weights.size

你甚至可以使用Python的内置sum函数:

values = sum(weights * features + bias)

values = sum(weights * features) + bias * weights.size

这样做的原因是表达式weights * featuresweights * features + bias都是临时的numpy数组,它们可以沿着它们的第一个维度进行迭代,因此可以传递给sum

<强>时序

我在IPython中运行了以下时序测试:

%timeit weights.dot(features)
The slowest run took 145.86 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 649 ns per loop

%timeit np.sum(weights * features)
The slowest run took 17.09 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 2.83 µs per loop

使用ndarray.dot比使用产品上的np.sum快五倍。但是,警告表明第一次运行代码时可能不是这样。 0.649μs* 145.86 =94.66μs,而2.83μs* 17.09 =48.36μs。