Questions tagged [elementwise-operations]
204 questions
4
votes
3 answers
Python: Element-wise mean of lists in a dataframe conditioned on another column
I have a dataframe looking like this with three columns (10 different stimuli, 16 trials and a data column containing lists of equal lengths). I would simply like to get the element-wise mean of the data column based on the stimulus. As I have 10…

Svenno Nito
- 635
- 1
- 6
- 22
4
votes
1 answer
Fastest way to set elements of Pandas Dataframe based on a function with index and column value as input
I have a single column Pandas dataframe:
s =
VALUE
INDEX
A 12
B 21
C 7
...
Y 21
Z 7
I want to make it into a square matrix mask with the same index and columns as s.index, with each element either True if the value of…

Zhang18
- 4,800
- 10
- 50
- 67
4
votes
1 answer
How to multiply a vector by an array/matrix element-wise in numpy?
I have a multidimensional array a whose shape is (32,3,5,5) and an array v with a shape of (32,). How could I multiply (i,3,5,5) with (i,) for each i using numpy other than a for-loop?

Xu Zhang
- 43
- 3
4
votes
1 answer
theano (python): elementwise gradient
I'm trying to perform elementwise gradient with
e.g.,
output-f(x): 5 by 1 vector,
with respect to
input-X: 5 by 1 vector
I can do this like,
import theano
import theano.tensor as T
X = T.vector('X')
f = X*3
[rfrx, []] =…

eunsuk jung
- 43
- 4
3
votes
5 answers
Elementwise ifs in matlab - do they exist?
Say I have the following basic if-statement:
if (A ~= 0)
% do something like divide your favorite number by A
else
% do something like return NaN or infinity
end
The problem is that A is not a simple number but a vector. Matlab returns true…

AnnaR
- 3,166
- 6
- 35
- 39
3
votes
3 answers
Elementwise multiplication of dataframes in Python
I have a dataframe which represents features of a linear regression model.
df1 = pd.DataFrame({'yyyyww': ['2022-01','2022-02','2022-03', '2022-04','2022-05','2022-06','2022-07','2022-08','2022-09','2022-10'],
'feature1':…

jimiclapton
- 775
- 3
- 14
- 42
3
votes
2 answers
elementwise operation on array of arrays in Julia
I am new to Julia and I am trying to migrate from existing code in Mathematica. I am trying to do: with an array of vectors, subtract a constant vector from it. Here is what I want:
a=[[1, 2], [1, 3]]
println(a)
b=a.-[1,1]
println(b)
I want…

wooohooo
- 576
- 1
- 4
- 15
3
votes
2 answers
Using Julia's dot notation and in place operation
How do I use both Julia's dot notation to do elementwise operations AND ensure that the result is saved in an already existing array?
function myfun(x, y)
return x + y
end
a = myfun(1, 2) # Results in a == 3
a = myfun.([1 2], [3; 4]) #…

Fredrik P
- 682
- 1
- 8
- 21
3
votes
1 answer
how to use elementwise dataframe manipulation?
I have 2 dataframes, representing the estimate mean and standard error.
import numpy as np
import scipy.stats as st
rows = (1, 2)
col1 = ["mean_x", "mean_y"]
col2 = ["std_x", "std_y"]
data1 = ([10, 20], [5, 10])
data2 = ([1, 2], [0.5, 1])
df1 =…

Cooper
- 81
- 5
3
votes
2 answers
Elementwise comparison between two large vectors, high degree of sparsity
Need a function that performs similarly to numpy.where function, but that doesn't run into memory issues caused by the dense representation of the Boolean array. The function should therefore be able to return an extremely sparse Boolean…

WillTheGeek
- 35
- 5
3
votes
1 answer
numpy elementwise outer product with sparse matrices
I want to do the element-wise outer product of three (or four) large 2D arrays in python (values are float32 rounded to 2 decimals). They all have the same number of rows "n", but different number of columns "i", "j", "k".
The resulting array…

plancton
- 33
- 5
3
votes
1 answer
Element-wise matrix multiplication for multi-dimensional array
I want to realize component-wise matrix multiplication in MATLAB, which can be done using numpy.einsum in Python as below:
import numpy as np
M = 2
N = 4
I = 2000
J = 300
A = np.random.randn(M, M, I)
B = np.random.randn(M, M, N, J, I)
C =…

Yuki
- 33
- 3
3
votes
1 answer
How to get element wise intersection from two Series in python pandas
My question is for python pandas.
I have two Series and each Series has elements of string as follows:
To simplify, I've concatenated two Series in DataFrame.
import pandas as pd
import numpy as np
my_df = pd.DataFrame([['ab', 'bz', 'b'], ['cd',…

Sang-il Ahn
- 113
- 7
3
votes
1 answer
Julia - Multiplication of values in an array
If I have an array A with random values, I would like define an array B with, for each i in length A, B[i] = (A[i])²
First, I tried the following code:
using Distributions
A = rand(Uniform(1,10),1,20)
B = A
for i in 1:20
B[i] =…

Julien
- 763
- 8
- 32
3
votes
1 answer
Create element wise dictionary from python arrays
From these arrays:
t = ["A","B","C"]
a = [1,2,3]
b = [4,5,6]
c = [7,8,9]
how can I obtain a list like this
[
{ 'A':1, 'B':4, 'C':7},
{ 'A':2, 'B':5, 'C':8},
{ 'A':3, 'B':6, 'C':9},
]
so that it is more useful one dumped in JSON?

leonard vertighel
- 1,058
- 1
- 18
- 37