官术网_书友最值得收藏!

Indexing

Part of the power of NumPy comes from the versatile ways in which its arrays can be accessed.

In addition to normal list indexing, it allows you to use arrays themselves as indices by performing the following:

>>> a[np.array([2,3,4])] 
array([77, 3, 4])

Coupled with the fact that conditions are also propagated to individual elements, we gain a very convenient way to access our data, using the following:

>>> a>4
array([False, False, True, False, False, True], dtype=bool)
>>> a[a>4]
array([77, 5])

By performing the following command, we can trim outliers:

>>> a[a>4] = 4
>>> a
array([0, 1, 4, 3, 4, 4])

As this is a frequent use case, there is a special clip function for it, clipping the values at both ends of an interval with one function call:

>>> a.clip(0,4)
array([0, 1, 4, 3, 4, 4])
主站蜘蛛池模板: 宣威市| 石泉县| 洪江市| 舞阳县| 彭山县| 甘德县| 潞西市| 洱源县| 潼关县| 临颍县| 平陆县| 祁连县| 开封市| 平遥县| 台山市| 呼玛县| 云霄县| 朝阳区| 阆中市| 宜兰县| 湖口县| 固始县| 锦屏县| 吉隆县| 邵阳市| 文登市| 阳谷县| 武乡县| 大荔县| 驻马店市| 叙永县| 黄浦区| 麻江县| 宝丰县| 申扎县| 沂水县| 白玉县| 宽甸| 漯河市| 昌宁县| 自治县|