栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

快速python numpy的功能在哪里?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

快速python numpy的功能在哪里?

事实证明,在这种情况下,纯Python循环比NumPy索引(或对np.where的调用)要快得多。

考虑以下替代方案:

import numpy as npimport collectionsimport itertools as ITshape = (2600,5200)# shape = (26,52)emiss_data = np.random.random(shape)obj_data = np.random.random_integers(1, 800, size=shape)UNIQ_IDS = np.unique(obj_data)def using_where():    max = np.max    where = np.where    MAX_EMISS = [max(emiss_data[where(obj_data == i)]) for i in UNIQ_IDS]    return MAX_EMISSdef using_index():    max = np.max    MAX_EMISS = [max(emiss_data[obj_data == i]) for i in UNIQ_IDS]    return MAX_EMISSdef using_max():    MAX_EMISS = [(emiss_data[obj_data == i]).max() for i in UNIQ_IDS]    return MAX_EMISSdef using_loop():    result = collections.defaultdict(list)    for val, idx in IT.izip(emiss_data.ravel(), obj_data.ravel()):        result[idx].append(val)    return [max(result[idx]) for idx in UNIQ_IDS]def using_sort():    uind = np.digitize(obj_data.ravel(), UNIQ_IDS) - 1    vals = uind.argsort()    count = np.bincount(uind)    start = 0    end = 0    out = np.empty(count.shape[0])    for ind, x in np.ndenumerate(count):        end += x        out[ind] = np.max(np.take(emiss_data, vals[start:end]))        start += x    return outdef using_split():    uind = np.digitize(obj_data.ravel(), UNIQ_IDS) - 1    vals = uind.argsort()    count = np.bincount(uind)    return [np.take(emiss_data, item).max() for item in np.split(vals, count.cumsum())[:-1]]for func in (using_index, using_max, using_loop, using_sort, using_split):    assert using_where() == func()

以下是基准测试shape = (2600,5200):

In [57]: %timeit using_loop()1 loops, best of 3: 9.15 s per loopIn [90]: %timeit using_sort()1 loops, best of 3: 9.33 s per loopIn [91]: %timeit using_split()1 loops, best of 3: 9.33 s per loopIn [61]: %timeit using_index()1 loops, best of 3: 63.2 s per loopIn [62]: %timeit using_max()1 loops, best of 3: 64.4 s per loopIn [58]: %timeit using_where()1 loops, best of 3: 112 s per loop

因此,using_loop(纯Python)的运行速度比快11倍以上using_where。

我不完全确定为什么纯Python比NumPy更快。我的猜测是纯Python版本一次通过两个数组(是,是双关语)。它利用了这样一个事实,即尽管进行了所有花哨的索引编制,但实际上我们只想访问每个值一次。因此,它避免了必须确定每个值所属的组的问题emiss_data。但这只是模糊的推测。我不知道在进行基准测试之前会更快。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/638535.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号