- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要使用 numexpr 重写这段代码,它正在计算矩阵数据 [行 x 列] 和向量 [1 x 列] 的欧几里德范数矩阵。
d = ((data-vec)**2).sum(axis=1)
如何实现?或许还有另一种更快的方法?
我使用 hdf5 并从中读取数据矩阵的问题。例如,此代码给出错误:对象未对齐。
#naive numpy solution, can be parallel?
def test_bruteforce_knn():
h5f = tables.open_file(fileName)
t0= time.time()
d = np.empty((rows*batches,))
for i in range(batches):
d[i*rows:(i+1)*rows] = ((h5f.root.carray[i*rows:(i+1)*rows]-vec)**2).sum(axis=1)
print (time.time()-t0)
ndx = d.argsort()
print ndx[:k]
h5f.close()
#using some tricks (don't work error: objects are not aligned )
def test_bruteforce_knn():
h5f = tables.open_file(fileName)
t0= time.time()
d = np.empty((rows*batches,))
for i in range(batches):
d[i*rows:(i+1)*rows] = (np.einsum('ij,ij->i', h5f.root.carray[i*rows:(i+1)*rows],
h5f.root.carray[i*rows:(i+1)*rows])
+ np.dot(vec, vec)
-2 * np.dot(h5f.root.carray[i*rows:(i+1)*rows], vec))
print (time.time()-t0)
ndx = d.argsort()
print ndx[:k]
h5f.close()
Using numexpr: 似乎 numexpr 不理解 h5f.root.carray[i*rows:(i+1)*rows] 它必须重新分配?
import numexpr as ne
def test_bruteforce_knn():
h5f = tables.open_file(fileName)
t0= time.time()
d = np.empty((rows*batches,))
for i in range(batches):
ne.evaluate("sum((h5f.root.carray[i*rows:(i+1)*rows] - vec) ** 2, axis=1)")
print (time.time()-t0)
ndx = d.argsort()
print ndx[:k]
h5f.close()
最佳答案
有一种仅使用 NumPy 的潜在快速方法(对于非常大的数组),它在 scikit-learn 中使用:
def squared_row_norms(X):
# From http://stackoverflow.com/q/19094441/166749
return np.einsum('ij,ij->i', X, X)
def squared_euclidean_distances(data, vec):
data2 = squared_row_norms(data)
vec2 = squared_row_norms(vec)
d = np.dot(data, vec.T).ravel()
d *= -2
d += data2
d += vec2
return d
这是基于 (x - y)² = x² + y² - 2xy 这一事实,即使对于向量也是如此。
测试:
>>> data = np.random.randn(10, 40)
>>> vec = np.random.randn(1, 40)
>>> ((data - vec) ** 2).sum(axis=1)
array([ 96.75712686, 69.45894306, 100.71998244, 80.97797154,
84.8832107 , 82.28910021, 67.48309433, 81.94813371,
64.68162331, 77.43265692])
>>> squared_euclidean_distances(data, vec)
array([ 96.75712686, 69.45894306, 100.71998244, 80.97797154,
84.8832107 , 82.28910021, 67.48309433, 81.94813371,
64.68162331, 77.43265692])
>>> from sklearn.metrics.pairwise import euclidean_distances
>>> euclidean_distances(data, vec, squared=True).ravel()
array([ 96.75712686, 69.45894306, 100.71998244, 80.97797154,
84.8832107 , 82.28910021, 67.48309433, 81.94813371,
64.68162331, 77.43265692])
简介:
>>> data = np.random.randn(1000, 40)
>>> vec = np.random.randn(1, 40)
>>> %timeit ((data - vec)**2).sum(axis=1)
10000 loops, best of 3: 114 us per loop
>>> %timeit squared_euclidean_distances(data, vec)
10000 loops, best of 3: 52.5 us per loop
使用 numexpr 也是可能的,但它似乎并没有为 1000 点提供任何加速(并且在 10000 时,它也好不了多少):
>>> %timeit ne.evaluate("sum((data - vec) ** 2, axis=1)")
10000 loops, best of 3: 142 us per loop
关于python - 使用 numexpr 的欧几里德范数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19653951/
与在 SVM 的相同成本函数中使用 2-范数权重相比,我们如何通过在成本函数中使用 1-范数权重来提高稀疏性。 对于 1-范数:成本函数 - 最小化 ||w||_1 对于 2-范数:成本函数 - 最小
我有几个与 l1 规范相关的问题。和TV(Total Variation)一样吗?如何在matlab中计算图像的L1范数?我确实阅读了电视的 wiki 页面,但它对我来说太复杂了。 最佳答案 L1范数
我想最小化实现一组给定的非负整数值 b 的成本,这些非负整数值 b 是从 GEKKO 中的两组非负整数变量 x、y 线性计算出来的。 如果以某种方式说明了我的问题,则 b 是 x 和 y 的约束。我的
我有一个 TensorFlow 占位符,它有 4 个维度,代表一批图像。每个图像为 32 x 32 像素,每个像素有 3 个颜色 channel 。第一个维度表示图像的数量。 X = tf.place
我想取变量列表的 2-范数。如何将序列转换为 CVXPY 兼容的变量“列表”?有办法处理这个问题吗?提前致谢。例如, test_a=cvxpy.Variable(1) test_b=c
我想知道 Python 中是否有一个函数可以完成与 scipy.linalg.lstsq 相同的工作,但使用“最小绝对偏差”回归而不是“最小二乘”回归 (OLS)。我想使用 L1 规范,而不是 L2
如何在 Python 中计算两个向量的差的 1-范数 ||a - b||_1 = sum(|a_i - b_i|)? a = [1,2,3,4] b = [2,3,4,5] ||a - b||_1
我有一个矢量 e <- c(0.1, -0.1, 0.1)我想计算 L1 和 L2 范数。我正在使用 norm(e, type="2")这适用于 L2 规范,但当我将其更改为 norm(e, type
我正在尝试将 Boost.Units 与 Eigen 3.3.1 一起使用,但在遵循说明后 here , 和一些信息 found around ,我仍然不知道如何使 norm() 工作。 这是我到目前
在 Eigen 库中,我知道有 visitors 和 reductions 用于密集的 Eigen::Matrix 类,我可以有效地使用它们来计算他们的 1-norm, inf-norm, etc.
我有一个矩阵 X,它在 d 维空间中有 n 列数据 vector 。给定一个 vector xj,v[j]是它的l1范数(所有abs(xji)的总和), w[j] 是它的 l2 范数的平方(所有 xj
我是一名优秀的程序员,十分优秀!