python 映射和反映射_在python2和python3中映射

I'm a beginner python user and I've ran the following code on 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 like this:

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 this:

array(, 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 want to use an array instead.

Is there a way to get this to work with np.array?

解决方案

Convert the map object to list, then pass it to numpy.array.

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

Or use list comprehension instead of map to make a list instead of map object:

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

But, you don't need to use map or list comprehension is not requierd; just calling F_inverse directly is enough because the F_inverse uses vectorized operations:

v = F_inverse(u.rvs(n))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值