Numpy基礎(chǔ)(二)

choose函數(shù)實(shí)現(xiàn)條件篩選

choose函數(shù)主要利用下標(biāo)以及下標(biāo)所在的位置對(duì)相關(guān)的數(shù)組進(jìn)行篩選,看下面的示例:

In [129]: control
Out[129]: 
array([[1, 0, 1],
       [2, 1, 0],
       [1, 2, 2]])

In [130]: choose(control, [10, 11, 12]) # control中,1對(duì)應(yīng)11,0對(duì)弈10,2對(duì)應(yīng)12
Out[130]: 
array([[11, 10, 11],
       [12, 11, 10],
       [11, 12, 12]])
--------------------------------------------------------
In [131]: i0
Out[131]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

In [132]: i2
Out[132]: 
array([[20, 21, 22],
       [23, 24, 25],
       [26, 27, 28]])

In [133]: choose(control, [i0, 10, i2]) # control中,第一個(gè)元素1對(duì)應(yīng)10,第二個(gè)元素[0, 1]0對(duì)應(yīng)i0中的[0, 1]1,以此類推。
Out[133]: 
array([[10,  1, 10],
       [23, 10,  5],
       [10, 27, 28]])
------------------------------------------------------------
In [135]: a = array([[0, 1, 2], [10, 11, 12], [20, 21, 22]])

In [136]: a
Out[136]: 
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22]])

In [137]: a < 10
Out[137]: 
array([[ True,  True,  True],
       [False, False, False],
       [False, False, False]], dtype=bool)

In [138]: choose(a < 10, [a, 10]) # True可以看成是1,對(duì)應(yīng)10;False看成0,對(duì)應(yīng)a中相同位置的元素。
Out[138]: 
array([[10, 10, 10],
       [10, 11, 12],
       [20, 21, 22]])
------------------------------------------------------------
# 下面的例子將數(shù)組中所有小于10的值變成了10, 大于15的值變成了15.
In [139]: a
Out[139]: 
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22]])

In [140]: a < 10
Out[140]: 
array([[ True,  True,  True],
       [False, False, False],
       [False, False, False]], dtype=bool)

In [141]: a > 15
Out[141]: 
array([[False, False, False],
       [False, False, False],
       [ True,  True,  True]], dtype=bool)

In [142]: lt = a < 10

In [143]: a > 15
Out[143]: 
array([[False, False, False],
       [False, False, False],
       [ True,  True,  True]], dtype=bool)

In [144]: gt = a > 15

In [145]: gt
Out[145]: 
array([[False, False, False],
       [False, False, False],
       [ True,  True,  True]], dtype=bool)

In [146]: 2 * gt
Out[146]: 
array([[0, 0, 0],
       [0, 0, 0],
       [2, 2, 2]])

In [147]: gt * 2
Out[147]: 
array([[0, 0, 0],
       [0, 0, 0],
       [2, 2, 2]])

In [148]: a
Out[148]: 
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22]])

In [149]: lt = a < 10

In [150]: lt
Out[150]: 
array([[ True,  True,  True],
       [False, False, False],
       [False, False, False]], dtype=bool)

In [151]: gt = a > 15

In [152]: gt
Out[152]: 
array([[False, False, False],
       [False, False, False],
       [ True,  True,  True]], dtype=bool)

In [153]: choice = lt + 2 * gt

In [154]: choice
Out[154]: 
array([[1, 1, 1],
       [0, 0, 0],
       [2, 2, 2]])

In [155]: choose(choice, [a, 10, 15])
Out[155]: 
array([[10, 10, 10],
       [10, 11, 12],
       [15, 15, 15]])

transpose實(shí)現(xiàn)轉(zhuǎn)置

np.transpose實(shí)現(xiàn)數(shù)組的轉(zhuǎn)置,并且可以定制轉(zhuǎn)置的維度。示例如下:

In [2]: a = np.arange(6).reshape(1, 2, 3)

In [3]: a
Out[3]: 
array([[[0, 1, 2],
        [3, 4, 5]]])

In [5]: np.transpose(a)
Out[5]: 
array([[[0],
        [3]],

       [[1],
        [4]],

       [[2],
        [5]]])

In [6]: np.transpose(a).shape
Out[6]: (3, 2, 1)

In [7]: np.transpose(a, axes=(1, 2, 0))  # 沿著指定維度的轉(zhuǎn)置
Out[7]: 
array([[[0],
        [1],
        [2]],

       [[3],
        [4],
        [5]]])

In [8]: np.transpose(a, axes=(1, 2, 0)).shape
Out[8]: (2, 3, 1)

squeeze壓縮數(shù)組維度

np.squeeze可以壓縮數(shù)組的維度,去掉維度等于1的那一維。示例如下:

In [9]: a
Out[9]: 
array([[[0, 1, 2],
        [3, 4, 5]]])

In [10]: a.shape
Out[10]: (1, 2, 3)

In [12]: np.squeeze(a)
Out[12]: 
array([[0, 1, 2],
       [3, 4, 5]])

In [13]: np.squeeze(a).shape
Out[13]: (2, 3)

repeat復(fù)制數(shù)組元素

np.repeat實(shí)現(xiàn)數(shù)組元素的復(fù)制,并且可以定制維度,示例如下:

In [14]: w = np.array([[-1,-1,-1], [-1,8,-1], [-1,-1,-1]])

In [15]: w.shape
Out[15]: (3, 3)

In [16]: w = w.reshape(1, 1, 3, 3)

In [17]: w
Out[17]: 
array([[[[-1, -1, -1],
         [-1,  8, -1],
         [-1, -1, -1]]]])

In [18]: w.shape
Out[18]: (1, 1, 3, 3)

In [19]: np.repeat(w, 3)
Out[19]: 
array([-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  8,  8,  8, -1, -1,
       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1])

In [21]: np.repeat(w, 3).shape
Out[21]: (27,)

In [22]: np.repeat(w, 3, axis=1)  # 定制復(fù)制維度,沿著第一維進(jìn)行復(fù)制
Out[22]: 
array([[[[-1, -1, -1],
         [-1,  8, -1],
         [-1, -1, -1]],

        [[-1, -1, -1],
         [-1,  8, -1],
         [-1, -1, -1]],

        [[-1, -1, -1],
         [-1,  8, -1],
         [-1, -1, -1]]]])

In [23]: np.repeat(w, 3, axis=1).shape
Out[23]: (1, 3, 3, 3)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 來源:NumPy Tutorial - TutorialsPoint 譯者:飛龍 協(xié)議:CC BY-NC-SA 4...
    布客飛龍閱讀 33,513評(píng)論 6 97
  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對(duì)象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學(xué)一百閱讀 3,667評(píng)論 0 4
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 北京八達(dá)嶺野生動(dòng)物園一位女游客跟自己的丈夫爭吵,私自下車被東北虎拖走,受了重傷,她的媽媽也下車去救她,被老虎殘忍咬...
    羅peipei閱讀 362評(píng)論 0 0
  • タイトル :江戸怪奇標(biāo)本箱 著者 : 藤巻一保[著].花輪和一[畫] 出版社 :柏書房 初版発行日 : (平成20...
    花輪和一閱讀 266評(píng)論 0 0

友情鏈接更多精彩內(nèi)容