Nick Goble February 2016
### I have a gaussian function with two independent discrete variables. How do I create a matrix of all possible values?

Basically I have this:

```
from scip.stats import norm
import pandas as pd
r = pd.Series([1, 2, 3])
k = pd.Series([0.2, 0.3, 0.4, 0.5])
x = 2
mean = x + k
variance = k
# I'm feeding the gaussian function two vectors.
# I'd like to get a matrix back of all possible combinations. Quickly.
values = norm.pdf(r, mean, variance)
```

So I'm giving the function norm.pdf two vectors of data, and I'd like a (3x4) matrix returned to me that looks like:

```
values(1, 0.2) values(1, 0.3) values(1, 0.4) values(1, 0.5)
values(2, 0.2) ...
values(3, 0.2) ...
values(4, 0.2) ........... ........... values(4, 0.5)
```

I know I could iterate over all items in all arrays, but that takes a lot of time, and I plan on scaling this up quite a bit. I'd like to take advantage of numpy's speed. I've tried vectorizing, but that fails. Any ideas? Thanks!!!

Marius February 2016

You can apply the `pdf`

to each element of `r`

and automatically put the results in a matrix using:

```
r.apply(lambda x: pd.Series(norm.pdf(x, mean, variance), index=k))
```

If you return a `Series`

from `apply`

then the results are automatically unpacked into columns. Output:

```
0.2 0.3 0.4 0.5
0 3.037941e-08 0.000111 0.002182 0.008864
1 1.209854e+00 0.806569 0.604927 0.483941
2 6.691511e-04 0.087406 0.323794 0.483941
```

ebarr February 2016

Use `numpy.meshgrid`

to get all the combinations of inputs:

```
all_r,all_means = np.meshgrid(r,mean)
_,all_variances = np.meshgrid(r,variance)
values = norm.pdf(all_r, all_means, all_variances)
```

This will return the values in a 2-d grid:

```
print values
# outputs
array([[ 3.03794142e-08, 1.20985362e+00, 6.69151129e-04],
[ 1.11236208e-04, 8.06569082e-01, 8.74062970e-02],
[ 2.18170674e-03, 6.04926811e-01, 3.23793989e-01],
[ 8.86369682e-03, 4.83941449e-01, 4.83941449e-01]])
```

Asked in February 2016

Viewed 3,302 times

Voted 14

Answered 2 times

Viewed 3,302 times

Voted 14

Answered 2 times