[英]Fastest way to compute vector in Python
I have the following (using Python's pandas): 我有以下(使用Python的熊猫):
y: n by 1 dataframe y:n由1个数据帧组成
x: n by k dataframe x:n由k数据帧组成
theta: k by 1 dataframe theta:k乘以1个数据帧
Each of the elements in the above dataframes contains a real number. 上述数据帧中的每个元素都包含一个实数。
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. 我需要一个数据帧w,其中w = y'x('表示转置),但w只包含y乘以元素的乘以(x * theta)小于1的观察值。换句话说,w的维数最多n乘以k,如果有一些观察结果不符合标准,则行数会减少。
What's the fastest way (in terms of time) to get w? 什么是获得w的最快方式(在时间方面)?
Use .values
to access underlying numpy arrays 使用.values
访问底层的numpy数组
Y = y.values
X = x.values
Th = theta.values
W = Y.T.dot(X)
mask = Y * X.dot(Th) < 1
w = pd.DataFrame(W[mask], y.index[mask])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.