缺少值的numpy dot产品

时间:2017-06-20 17:08:39

标签: numpy

如果两个矢量可能缺少值,你如何做一个numpy dot产品?这似乎需要许多额外的步骤,有更简单的方法吗?:

  <div class="description">
    <dl> <dt>Engine:</dt> <dd>2.5L I-5 cyl<span class="separator">,</span>
    </dd> <dt>Transmission:</dt> <dd>Manual<span class="separator">,</span></dd> <dt>Mileage:</dt> <dd>37,171 miles<span class="separator">,</span></dd> <dt>MPG Range:</dt> <dd>22/31<span class="separator">,</span></dd></dl><dl class="last"> <dt>Exterior Color:</dt> <dd>Reflex Silver Metallic<span class="separator">,</span></dd> <dt>Interior Color:</dt> <dd>Titan Black<span class="separator">,</span></dd> <dt>Stock #:</dt> <dd>P3229</dd></dl> <span class="ddc-more">More<span class="hellip">…</span></span> 
<div class="calloutDetails">
<ul class="list-unstyled">
<li class="certified" style="margin-bottom: 10px;"><div class="badge "><img class="align-center" src="https://static.dealer.com/v8/global/images/franchise/white/en_US/logo-certified-volkswagen.gif?r=1356028132000" alt="Certified"></div></li><li class="carfax" style="margin-bottom: 10px;"><a href="http://www.carfax.com/cfm/ccc_displayhistoryrpt.cfm?partner=DLR_3&amp;vin=3VWHX7AT1EM600723" class="badge carfax-one-owner pointer" target="_blank"><img class="align-center" src="https://static.dealer.com/v8/global/images/franchise/white/logo-certified-carfax-one-owner-lrg.png?r=1405027620000" alt="Carfax One Owner"></a></li>
</ul>
</div>
<div class="hproductDynamicArea"></div>
</div>

2 个答案:

答案 0 :(得分:4)

我们可以使用np.nansum来总结在元素乘法后忽略unescaped_str的值 -

אפרת

示例运行 -

NaNs

答案 1 :(得分:0)

另一种解决方案是使用掩码数组:

v1 = np.array([1,4,2,np.nan,3])
v2 = np.array([np.nan,np.nan,2,4,1])

v1_m = numpy.ma.array(v1, mask=numpy.isnan(v1))
v2_m = numpy.ma.array(v2, mask=numpy.isnan(v2))

numpy.ma.dot(v1_m, v2_m)