Map in python2 vs python3

I am a novice python user and I have executed the following code in both python2.7 and python3.4.3

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

alpha = 1
n = 100

u = stats.uniform(0,1)
F_inverse = lambda u: 1/alpha*np.log(1/(1-u))
v = np.array(map(F_inverse, u.rvs(n)))
print(v)

fig, ax = plt.subplots(1,1)
stats.probplot(v, (1,), dist='expon', plot=ax)
plt.show()

On python2, I get a nice array as follows:

array([  2.29133808e+00,   1.63236151e+00,   6.77776227e-01,
         3.33668250e-01,   1.77830890e+00,   3.06193068e-01,
         2.10677775e+00,   1.30525788e-01,   2.97056775e-01,
                           ...
         1.31463775e+00,   1.41840428e-03,   8.60594737e-01,
         1.80644880e-01])

On python3, I get the following:

array(<map object at 0x7f8aab6f3ef0>, dtype=object)

If I change this:

v = np.array(map(F_inverse, u.rvs(n)))

to

v = list(map(F_inverse, u.rvs(n)))

It works fine on both, but I would like to use an array. Is there a way to get this to work with np.array?

+4
source share
1 answer

Convert map objectto a list, then pass it on numpy.array.

v = np.array(list(map(F_inverse, u.rvs(n))))

Or use a list instead of a map to create a list instead of a map object:

v = np.array([F_inverse(x) for x in u.rvs(n)])

map, ; F_inverse , F_inverse :

v = F_inverse(u.rvs(n))
+3

Source: https://habr.com/ru/post/1670133/


All Articles