简体   繁体   English

在Python中计算向量的最快方法

[英]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.

相关问题 python:计算向量到矩阵每一行的欧氏距离的最快方法? - python: fastest way to compute euclidean distance of a vector to every row of a matrix? 在 Python 中计算熵的最快方法 - Fastest way to compute entropy in Python 在python中计算RBF内核的最快方法是什么? - What is the fastest way to compute an RBF kernel in python? 计算几何级数上三角矩阵的最快方法(Python) - Fastest way to compute upper-triangular matrix of geometric series (Python) 在 python 中计算大量不动点的最快方法? - Fastest way to compute large amount of fixed points in python? 计算python中每个点之间的距离的最快方法 - Fastest way to compute distance beetween each points in python 使用Python代码并行计算两点之间距离的最快方法 - Fastest way with parallelization to compute distances between two points with Python code 在 Python(和 Cython)中计算两个矩阵的点积的最快方法是什么 - What is the fastest way to compute the dot product of two matrices in Python (and Cython) 在 Python 中计算稀疏 Gram 矩阵的最快方法是什么? - What is the fastest way to compute a sparse Gram matrix in Python? 计算数据帧列的最快方法 - Fastest way to compute a column of a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM