Fastest way to compute vector in Python
<p>I have the following (using Python's pandas):</p>

<p>y: n by 1 dataframe</p>

<p>x: n by k dataframe</p>

<p>theta: k by 1 dataframe</p>

<p>Each of the elements in the above dataframes contains a real number.</p>

<p>I need a dataframe w, where w = y'x (' denotes transpose), but w only contains the observations for which y multiplied element-wise by (x * theta) is less than 1. In other words, the dimension of w is at most n by k, and there will be fewer rows if there are some observations that do not meet the criteria.</p>

<p>What's the fastest way (in terms of time) to get w?</p>

<p>Use <code>.values</code> to access underlying numpy arrays</p>

<pre><code>Y = y.values
X = x.values
Th = theta.values

W = Y.T.dot(X)

mask = Y * X.dot(Th) &lt; 1

w = pd.DataFrame(W[mask], y.index[mask])
</code></pre>

python pandas