Home > Net >  Python apply element-wise operation between to series where every element is a list
Python apply element-wise operation between to series where every element is a list

Time:03-22

I have to series, where every element of the series is a list:

s1 = pd.Series([[1,2,3],[4,5,6],[7,8,9]])
s2 = pd.Series([[1,2,3],[1,1,1],[7,8,8]])

And I want to calculate element-wise sklearn.metrics.mean_squared_error, so I will get:

[0, 16.666, 0.33]

What is the best way to do it?

Thanks

CodePudding user response:

With this setup,

import pandas as pd
from sklearn.metrics import mean_squared_error

s1 = pd.Series([[1,2,3],[4,5,6],[7,8,9]])
s2 = pd.Series([[1,2,3],[1,1,1],[7,8,8]])

here's two options, first with map,

>>> pd.Series(map(mean_squared_error, s1, s2))
0     0.000000
1    16.666667
2     0.333333
dtype: float64

and second with np.vectorize:

>>> import numpy as np
>>> mean_squared_error_vec = np.vectorize(mean_squared_error)
>>> mean_squared_error_vec(s1, s2)
array([ 0.        , 16.66666667,  0.33333333])

CodePudding user response:

First of all, you can't construct the Series like that, it will throw an error. What you probably meant was this:

s1 = pd.Series([[1,2,3],[4,5,6],[7,8,9]])
s2 = pd.Series([[1,2,3],[1,1,1],[7,8,8]])

With these Series, you have a few options. You can use zip to create an object in which the corresponding elements are chunked together and use map to apply the function to these chunks. the * is needed to pass the chunk as two separate arguments:

from sklearn.metrics import mean_squared_error
list(map(lambda x: mean_squared_error(*x), zip(s1, s2)))

Or simpler, you can use list iterations to loop over the elements (again using zip):

[mean_squared_error(x, y) for x, y in zip(s1, s2)]
  • Related