vectorization

Numpy vectorize wrongly converts the output to be integer

三世轮回 提交于 2020-06-29 05:28:45
问题 I am struggling with the following code: import numpy as np e = np.linspace(0, 4, 10) def g(x): if x > 1: return x else: return 0 vg = np.vectorize(g) print(vg(e)) the result looks like this: [0 0 0 1 1 2 2 3 3 4] I also checked the dtype. It seems that the vectorize function is conveting the type to int64 from float64! 回答1: The documentation for np.vectorize explains: The data type of the output of vectorized is determined by calling the function with the first element of the input. This can

Numpy vectorize wrongly converts the output to be integer

瘦欲@ 提交于 2020-06-29 05:28:35
问题 I am struggling with the following code: import numpy as np e = np.linspace(0, 4, 10) def g(x): if x > 1: return x else: return 0 vg = np.vectorize(g) print(vg(e)) the result looks like this: [0 0 0 1 1 2 2 3 3 4] I also checked the dtype. It seems that the vectorize function is conveting the type to int64 from float64! 回答1: The documentation for np.vectorize explains: The data type of the output of vectorized is determined by calling the function with the first element of the input. This can

Pytorch error “RuntimeError: index out of range: Tried to access index 512 out of table with 511 rows”

折月煮酒 提交于 2020-06-29 03:43:43
问题 I have sentences that I vectorize using sentence_vector() method of BiobertEmbedding python module (https://pypi.org/project/biobert-embedding/). For some group of sentences I have no problem but for some others I have the following error message : File "/home/nobunaga/.local/lib/python3.6/site-packages/biobert_embedding/embedding.py", line 133, in sentence_vector encoded_layers = self.eval_fwdprop_biobert(tokenized_text) File "/home/nobunaga/.local/lib/python3.6/site-packages/biobert

Pytorch error “RuntimeError: index out of range: Tried to access index 512 out of table with 511 rows”

僤鯓⒐⒋嵵緔 提交于 2020-06-29 03:42:38
问题 I have sentences that I vectorize using sentence_vector() method of BiobertEmbedding python module (https://pypi.org/project/biobert-embedding/). For some group of sentences I have no problem but for some others I have the following error message : File "/home/nobunaga/.local/lib/python3.6/site-packages/biobert_embedding/embedding.py", line 133, in sentence_vector encoded_layers = self.eval_fwdprop_biobert(tokenized_text) File "/home/nobunaga/.local/lib/python3.6/site-packages/biobert

R supplying arguments while using case_when (R vectorization)

为君一笑 提交于 2020-06-27 15:48:08
问题 This is a follow up question to a question that I asked before (R apply multiple functions when large number of categories/types are present using case_when (R vectorization)). Unfortunately I have not been able to figure out the problem. I think I may have narrowed down the source of the problem an wanted to check if someone with a better understanding than me could help me figure out a solution. Suppose I have the following dataset: set.seed(100) City=c("City1","City2","City2","City1")

R supplying arguments while using case_when (R vectorization)

孤人 提交于 2020-06-27 15:48:08
问题 This is a follow up question to a question that I asked before (R apply multiple functions when large number of categories/types are present using case_when (R vectorization)). Unfortunately I have not been able to figure out the problem. I think I may have narrowed down the source of the problem an wanted to check if someone with a better understanding than me could help me figure out a solution. Suppose I have the following dataset: set.seed(100) City=c("City1","City2","City2","City1")

Factorial of a matrix elementwise with Numpy

拥有回忆 提交于 2020-06-27 07:36:32
问题 I'd like to know how to calculate the factorial of a matrix elementwise. For example, import numpy as np mat = np.array([[1,2,3],[2,3,4]]) np.the_function_i_want(mat) would give a matrix mat2 such that mat2[i,j] = mat[i,j]! . I've tried something like np.fromfunction(lambda i,j: np.math.factorial(mat[i,j])) but it passes the entire matrix as argument for np.math.factorial . I've also tried to use scipy.vectorize but for matrices larger than 10x10 I get an error. This is the code I wrote:

Efficient computation of similarity matrix in Python (NumPy)

寵の児 提交于 2020-06-11 04:00:46
问题 Let X be a Bxn numpy matrix, i.e., import numpy as np B = 10 n = 2 X = np.random.random((B, n)) Now, I'm interested in computing the so-called kernel (or even similarity) matrix K , which is of shape BxB , and its {i,j} -th element is given as follows: K(i,j) = fun(x_i, x_j) where x_t denotes the t -th row of matrix X and fun is some function of x_i , x_j . For instance, this function could be the so-called RBF function, i.e., K(i,j) = exp(-|x_i - x_j|^2). For doing so, a naive way would be

Numpy: argmax over multiple axes without loop

我只是一个虾纸丫 提交于 2020-06-07 04:33:16
问题 I have a N-dimensional array (Named A). For each row of the first axis of A, I want to obtain the coordinates of the maximum value along the other axes of A. Then I would return a 2-dimensional array with the coordinates of the maximum value for each row of the first axis of A. I already solved my problem using a loop, but I was wondering whether there is a more efficient way of doing this. My current solution (for an example array A) is as follows: import numpy as np A=np.reshape(np

Numpy: argmax over multiple axes without loop

点点圈 提交于 2020-06-07 04:33:14
问题 I have a N-dimensional array (Named A). For each row of the first axis of A, I want to obtain the coordinates of the maximum value along the other axes of A. Then I would return a 2-dimensional array with the coordinates of the maximum value for each row of the first axis of A. I already solved my problem using a loop, but I was wondering whether there is a more efficient way of doing this. My current solution (for an example array A) is as follows: import numpy as np A=np.reshape(np