numpy

multiple numpy dot products without a loop

北战南征 提交于 2021-02-07 03:38:13
问题 Is it possible to compute several dot products without a loop? say you have the following: a = randn(100, 3, 3) b = randn(100, 3, 3) I want to get an array z of shape (100, 3, 3) such that for all i z[i, ...] == dot(a[i, ...], b[i, ...]) in other words, which verifies: for va, vb, vz in izip(a, b, z): assert (vq == dot(va, vb)).all() The straightforward solution would be: z = array([dot(va, vb) for va, vb in zip(a, b)]) which uses an implicit loop (list comprehension + array). Is there a more

multiple numpy dot products without a loop

ⅰ亾dé卋堺 提交于 2021-02-07 03:34:31
问题 Is it possible to compute several dot products without a loop? say you have the following: a = randn(100, 3, 3) b = randn(100, 3, 3) I want to get an array z of shape (100, 3, 3) such that for all i z[i, ...] == dot(a[i, ...], b[i, ...]) in other words, which verifies: for va, vb, vz in izip(a, b, z): assert (vq == dot(va, vb)).all() The straightforward solution would be: z = array([dot(va, vb) for va, vb in zip(a, b)]) which uses an implicit loop (list comprehension + array). Is there a more

Writing a formated binary file from a Pandas Dataframe

試著忘記壹切 提交于 2021-02-07 03:28:38
问题 I've seen some ways to read a formatted binary file in Python to Pandas, namely, I'm using this code that read using NumPy fromfile formatted with a structure given using dtype. import numpy as np import pandas as pd input_file_name = 'test.hst' input_file = open(input_file_name, 'rb') header = input_file.read(96) dt_header = np.dtype([('version', 'i4'), ('copyright', 'S64'), ('symbol', 'S12'), ('period', 'i4'), ('digits', 'i4'), ('timesign', 'i4'), ('last_sync', 'i4')]) header = np

Writing a formated binary file from a Pandas Dataframe

删除回忆录丶 提交于 2021-02-07 03:28:01
问题 I've seen some ways to read a formatted binary file in Python to Pandas, namely, I'm using this code that read using NumPy fromfile formatted with a structure given using dtype. import numpy as np import pandas as pd input_file_name = 'test.hst' input_file = open(input_file_name, 'rb') header = input_file.read(96) dt_header = np.dtype([('version', 'i4'), ('copyright', 'S64'), ('symbol', 'S12'), ('period', 'i4'), ('digits', 'i4'), ('timesign', 'i4'), ('last_sync', 'i4')]) header = np

Replace elements in numpy array avoiding loops

好久不见. 提交于 2021-02-07 03:00:16
问题 I have a quite large 1d numpy array Xold with given values. These values shall be replaced according to the rule specified by a 2d numpy array Y: An example would be Xold=np.array([0,1,2,3,4]) Y=np.array([[0,0],[1,100],[3,300],[4,400],[2,200]]) Whenever a value in Xold is identical to a value in Y[:,0], the new value in Xnew should be the corresponding value in Y[:,1]. This is accomplished by two nested for loops: Xnew=np.zeros(len(Xold)) for i in range(len(Xold)): for j in range(len(Y)): if

Replace elements in numpy array avoiding loops

瘦欲@ 提交于 2021-02-07 02:59:44
问题 I have a quite large 1d numpy array Xold with given values. These values shall be replaced according to the rule specified by a 2d numpy array Y: An example would be Xold=np.array([0,1,2,3,4]) Y=np.array([[0,0],[1,100],[3,300],[4,400],[2,200]]) Whenever a value in Xold is identical to a value in Y[:,0], the new value in Xnew should be the corresponding value in Y[:,1]. This is accomplished by two nested for loops: Xnew=np.zeros(len(Xold)) for i in range(len(Xold)): for j in range(len(Y)): if

Replace elements in numpy array avoiding loops

走远了吗. 提交于 2021-02-07 02:59:35
问题 I have a quite large 1d numpy array Xold with given values. These values shall be replaced according to the rule specified by a 2d numpy array Y: An example would be Xold=np.array([0,1,2,3,4]) Y=np.array([[0,0],[1,100],[3,300],[4,400],[2,200]]) Whenever a value in Xold is identical to a value in Y[:,0], the new value in Xnew should be the corresponding value in Y[:,1]. This is accomplished by two nested for loops: Xnew=np.zeros(len(Xold)) for i in range(len(Xold)): for j in range(len(Y)): if

一文讲解图像插值算法原理!附Python实现

﹥>﹥吖頭↗ 提交于 2021-02-06 21:21:42
在图像处理中,几何变换是将一幅图像映射到另外一幅图像内的操作,可以大概分为放缩、翻转、仿射(平移、旋转)、透视、重映射几部分。 在几何变换时,无法给有些像素点直接赋值,例如,将图像放大两倍,必然会多出一些无法被直接映射的像素点,对于这些像素点,通过插值决定它们的值。且不同插值方式的结果不同。 在一幅输入图像[u,v]中,灰度值仅在整数位置上有定义。然而,输出图像的坐标映射回原图像后,一般为非整数的坐标。所以输出图像[x,y]的灰度值,一般由非整数坐标来决定,非整数坐标的像素值,就需要插值算法来进行处理。常见的插值算法有最近邻插值、双线性插值和三次样条插值。 本文目标 了解插值算法与常见几何变换之间的关系 理解插值算法的原理 掌握OpenCV框架下插值算法API的使用 插值算法原理介绍 近邻插值算法 1. 原理简介 将目标图像中的点,对应到原图像中后,找到最相邻的整数坐标点的像素值,作为该点的像素值输出。 如上图所示,目标图像中的某点投影到原图像中的位置为点P,与P距离最近的点为Q11,此时易知,f(P )=f(Q11)。 2. 例子说明 如图所示: 将一幅3*3图像放大到4*4,用f(x , y)表示原图像,h(x ,y)表示目标图像,我们有如下公式: 3. 缺点 由最邻近插值法,放大后的图像有很严重的马赛克,会出现明显的块状效应;缩小后的图像有很严重的失真。 这是一种最基本

Rearranging axes in numpy?

折月煮酒 提交于 2021-02-06 20:05:28
问题 I have an ndarray such as >>> arr = np.random.rand(10, 20, 30, 40) >>> arr.shape (10, 20, 30, 40) whose axes I would like to swap around into some arbitrary order such as >>> rearranged_arr = np.swapaxes(np.swapaxes(arr, 1,3), 0,1) >>> rearranged_arr.shape (40, 10, 30, 20) Is there a function which achieves this without having to chain together a bunch of np.swapaxes ? 回答1: There are two options: np.moveaxis and np.transpose. np.moveaxis(a, sources, destinations) docs This function can be

Rearranging axes in numpy?

十年热恋 提交于 2021-02-06 20:04:07
问题 I have an ndarray such as >>> arr = np.random.rand(10, 20, 30, 40) >>> arr.shape (10, 20, 30, 40) whose axes I would like to swap around into some arbitrary order such as >>> rearranged_arr = np.swapaxes(np.swapaxes(arr, 1,3), 0,1) >>> rearranged_arr.shape (40, 10, 30, 20) Is there a function which achieves this without having to chain together a bunch of np.swapaxes ? 回答1: There are two options: np.moveaxis and np.transpose. np.moveaxis(a, sources, destinations) docs This function can be