亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

python學(xué)習(xí)——numpy練習(xí)題

系統(tǒng) 2463 0
                    
                      import 
                      
                        numpy 
                        
                          as 
                          
                            np

                          
                        
                      
                    
                  
In?[9]:
                    
                      #1 創(chuàng)建一個(gè)長(zhǎng)度為10的一維全為0的ndarray對(duì)象,然后讓第5個(gè)元素等于1

                      
                        n 
                        
                          = 
                          
                            np
                            
                              .
                              
                                zeros
                                
                                  (
                                  
                                    10
                                    
                                      ) 
                                      
                                        n
                                        
                                          [
                                          
                                            4
                                            
                                              ] 
                                              
                                                = 
                                                
                                                  1 
                                                  
                                                    print
                                                    
                                                      (
                                                      
                                                        n
                                                        
                                                          ) 
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
?
                    [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]

                  
In?[10]:
                    
                      #2 創(chuàng)建一個(gè)元素為從10到49的ndarray對(duì)象

                      
                        np
                        
                          .
                          
                            arange
                            
                              (
                              
                                10
                                
                                  ,
                                  
                                    50
                                    
                                      ) 
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[10]:
                    array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
       27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
       44, 45, 46, 47, 48, 49])
                  
In?[11]:
                    
                      #3 將第2題的所有元素位置反轉(zhuǎn)

                      
                        n3 
                        
                          = 
                          
                            np
                            
                              .
                              
                                arange
                                
                                  (
                                  
                                    10
                                    
                                      ,
                                      
                                        50
                                        
                                          ) 
                                          
                                            n3
                                            
                                              [::
                                              
                                                -
                                                
                                                  1
                                                  
                                                    ] 
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[11]:
                    array([49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33,
       32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
       15, 14, 13, 12, 11, 10])
                  
In?[29]:
                    
                      #4 使用np.random.random創(chuàng)建一個(gè)10*10的ndarray對(duì)象,并打印出最大最小元素

                      
                        n4 
                        
                          = 
                          
                            np
                            
                              .
                              
                                random
                                
                                  .
                                  
                                    random
                                    
                                      ((
                                      
                                        10
                                        
                                          ,
                                          
                                            10
                                            
                                              )) 
                                              
                                                print
                                                
                                                  (
                                                  
                                                    n4
                                                    
                                                      ) 
                                                      
                                                        print
                                                        
                                                          (
                                                          
                                                            np
                                                            
                                                              .
                                                              
                                                                max
                                                                
                                                                  (
                                                                  
                                                                    n4
                                                                    
                                                                      ),
                                                                      
                                                                        np
                                                                        
                                                                          .
                                                                          
                                                                            min
                                                                            
                                                                              (
                                                                              
                                                                                n4
                                                                                
                                                                                  )) 
                                                                                
                                                                              
                                                                            
                                                                          
                                                                        
                                                                      
                                                                    
                                                                  
                                                                
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
?
                    [[0.87111406 0.86476873 0.86391148 0.78816324 0.93838288 0.59607216
  0.84086973 0.48199116 0.11066871 0.11068288]
 [0.85201476 0.1501309  0.65682176 0.20674656 0.59219751 0.54345428
  0.87809034 0.35371227 0.87620721 0.97906846]
 [0.99045258 0.49207604 0.8109245  0.17304564 0.97809511 0.19944122
  0.35066104 0.57088987 0.59589159 0.90920154]
 [0.13962972 0.81358769 0.95650672 0.87961387 0.06530585 0.63410083
  0.16575957 0.84563097 0.85254561 0.15485708]
 [0.46523209 0.13314196 0.60749123 0.5805076  0.71714643 0.70276885
  0.76393749 0.1839967  0.99657554 0.24095804]
 [0.95328727 0.4174502  0.71184996 0.64976674 0.79155124 0.06004858
  0.42435983 0.9435027  0.43575042 0.96633344]
 [0.64044146 0.60840577 0.57431792 0.98093441 0.41788725 0.11308594
  0.98737901 0.1962544  0.01377043 0.39127853]
 [0.21463967 0.88801687 0.81726285 0.11641557 0.88568505 0.47643872
  0.26798048 0.54929047 0.62710841 0.0651055 ]
 [0.83702334 0.55580473 0.31937827 0.89550998 0.0812324  0.29505207
  0.34792795 0.00443295 0.70254115 0.70284487]
 [0.23769269 0.76225147 0.71931028 0.90641133 0.68028347 0.68391635
  0.50072569 0.5403078  0.26216243 0.8682864 ]]
0.996575541946171 0.004432950968736971

                  
In?[41]:
                    
                      #5 創(chuàng)建一個(gè)10*10的ndarray對(duì)象,且矩陣邊界全為1,里面全為0

                      
                        # n5 = np.zeros((10,10))

                        
                          # for i in n5:

                          
                            #     i[0],i[9] = 1,1

                            
                              # n5[0] = 1

                              
                                # n5[9] = 1 
                                
                                  # print(n5) 
                                  
                                    n5 
                                    
                                      = 
                                      
                                        np
                                        
                                          .
                                          
                                            ones
                                            
                                              ((
                                              
                                                10
                                                
                                                  ,
                                                  
                                                    10
                                                    
                                                      )) 
                                                      
                                                        n5
                                                        
                                                          [
                                                          
                                                            1
                                                            
                                                              :
                                                              
                                                                -
                                                                
                                                                  1
                                                                  
                                                                    ,
                                                                    
                                                                      1
                                                                      
                                                                        :
                                                                        
                                                                          -
                                                                          
                                                                            1
                                                                            
                                                                              ] 
                                                                              
                                                                                = 
                                                                                
                                                                                  0 
                                                                                  
                                                                                    n5 
                                                                                  
                                                                                
                                                                              
                                                                            
                                                                          
                                                                        
                                                                      
                                                                    
                                                                  
                                                                
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[41]:
                    array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
                  
In?[61]:
                    
                      #6 創(chuàng)建一個(gè)每一行都是從0到4的5*5矩陣

                      
                        np
                        
                          .
                          
                            ones
                            
                              ((
                              
                                5
                                
                                  ,
                                  
                                    5
                                    
                                      ))
                                      
                                        *
                                        
                                          np
                                          
                                            .
                                            
                                              arange
                                              
                                                (
                                                
                                                  5
                                                  
                                                    ) 
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[61]:
                    array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24])
                  
In?[72]:
                    
                      #7 創(chuàng)建一個(gè)范圍在(0,1)之間的長(zhǎng)度為12的等差數(shù)列

                      
                        np
                        
                          .
                          
                            linspace
                            
                              (
                              
                                0
                                
                                  ,
                                  
                                    1
                                    
                                      ,
                                      
                                        12
                                        
                                          ) 
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[72]:
                    array([0.        , 0.09090909, 0.18181818, 0.27272727, 0.36363636,
       0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,
       0.90909091, 1.        ])
                  
In?[15]:
                    
                      #8 創(chuàng)建一個(gè)長(zhǎng)度為10的隨機(jī)數(shù)組并排序

                      
                        n8 
                        
                          = 
                          
                            np
                            
                              .
                              
                                random
                                
                                  .
                                  
                                    randint
                                    
                                      (
                                      
                                        1
                                        
                                          ,
                                          
                                            10
                                            
                                              ,
                                              
                                                10
                                                
                                                  ) 
                                                  
                                                    n8
                                                    
                                                      .
                                                      
                                                        sort
                                                        
                                                          () 
                                                          
                                                            print
                                                            
                                                              (
                                                              
                                                                n8
                                                                
                                                                  ) 
                                                                
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
?
                    [1 1 2 5 5 5 5 6 6 8]

                  
In?[14]:
                    
                      #9 創(chuàng)建一個(gè)長(zhǎng)度為10的隨機(jī)數(shù)組并將最大值替換為0

                      
                        n9 
                        
                          = 
                          
                            np
                            
                              .
                              
                                random
                                
                                  .
                                  
                                    randint
                                    
                                      (
                                      
                                        0
                                        
                                          ,
                                          
                                            150
                                            
                                              ,
                                              
                                                10
                                                
                                                  ) 
                                                  
                                                    index 
                                                    
                                                      = 
                                                      
                                                        n9
                                                        
                                                          .
                                                          
                                                            argmax
                                                            
                                                              () 
                                                              
                                                                n9
                                                                
                                                                  [
                                                                  
                                                                    index
                                                                    
                                                                      ] 
                                                                      
                                                                        = 
                                                                        
                                                                          0 
                                                                          
                                                                            n9 
                                                                          
                                                                        
                                                                      
                                                                    
                                                                  
                                                                
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[14]:
                    array([43, 33, 48, 50, 72, 27,  3, 33,  9,  0])
                  
In?[32]:
                    
                      #10 如何根據(jù)第3列來(lái)對(duì)一個(gè)5*5矩陣排序?

                      
                        n10 
                        
                          = 
                          
                            np
                            
                              .
                              
                                random
                                
                                  .
                                  
                                    randint
                                    
                                      (
                                      
                                        0
                                        
                                          ,
                                          
                                            20
                                            
                                              ,(
                                              
                                                5
                                                
                                                  ,
                                                  
                                                    5
                                                    
                                                      )) 
                                                      
                                                        n10_sort 
                                                        
                                                          = 
                                                          
                                                            np
                                                            
                                                              .
                                                              
                                                                argsort
                                                                
                                                                  (
                                                                  
                                                                    n10
                                                                    
                                                                      [:,
                                                                      
                                                                        3
                                                                        
                                                                          ])
                                                                          
                                                                            #對(duì)第三列排序并返回索引值 
                                                                            
                                                                              n10
                                                                              
                                                                                [
                                                                                
                                                                                  n10_sort
                                                                                  
                                                                                    ] 
                                                                                  
                                                                                
                                                                              
                                                                            
                                                                          
                                                                        
                                                                      
                                                                    
                                                                  
                                                                
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[32]:
                    array([[ 8,  6,  6,  3, 12],
       [14,  2, 17,  4, 13],
       [19, 15,  4, 14,  9],
       [13,  3, 12, 14,  1],
       [10,  4,  1, 15, 12]])
                  
In?[80]:
                    
                      #11 給定一個(gè)4維矩陣,如何得到最后兩維的和?

                      
                        n11 
                        
                          = 
                          
                            np
                            
                              .
                              
                                random
                                
                                  .
                                  
                                    randint
                                    
                                      (
                                      
                                        0
                                        
                                          ,
                                          
                                            100
                                            
                                              ,(
                                              
                                                2
                                                
                                                  ,
                                                  
                                                    2
                                                    
                                                      ,
                                                      
                                                        2
                                                        
                                                          ,
                                                          
                                                            2
                                                            
                                                              )) 
                                                              
                                                                n11 
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[80]:
                    array([[[[ 6, 52],
         [15, 41]],

        [[90, 34],
         [41,  8]]],


       [[[45,  8],
         [71, 38]],

        [[82,  8],
         [95, 46]]]])
                  
In?[90]:
                    
                      n11
                      
                        .
                        
                          sum
                          
                            (
                            
                              axis
                              
                                =
                                
                                  (
                                  
                                    2
                                    
                                      ,
                                      
                                        3
                                        
                                          )) 
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[90]:
                    array([[114, 173],
       [162, 231]])
                  
In?[110]:
                    
                      #12  給定數(shù)組[1, 2, 3, 4, 5],如何得到在這個(gè)數(shù)組的每個(gè)元素之間插入3個(gè)0后的新數(shù)組?

                      
                        n12 
                        
                          = 
                          
                            np
                            
                              .
                              
                                arange
                                
                                  (
                                  
                                    1
                                    
                                      ,
                                      
                                        6
                                        
                                          ) 
                                          
                                            n12_1 
                                            
                                              = 
                                              
                                                np
                                                
                                                  .
                                                  
                                                    zeros
                                                    
                                                      (
                                                      
                                                        17
                                                        
                                                          ,
                                                          
                                                            dtype
                                                            
                                                              =
                                                              
                                                                int
                                                                
                                                                  ) 
                                                                  
                                                                    n12_1
                                                                    
                                                                      [::
                                                                      
                                                                        4
                                                                        
                                                                          ] 
                                                                          
                                                                            = 
                                                                            
                                                                              n12 
                                                                              
                                                                                n12_1 
                                                                              
                                                                            
                                                                          
                                                                        
                                                                      
                                                                    
                                                                  
                                                                
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[110]:
                    array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5])
                  
In?[113]:
                    
                      n12 
                      
                        = 
                        
                          np
                          
                            .
                            
                              arange
                              
                                (
                                
                                  1
                                  
                                    ,
                                    
                                      6
                                      
                                        )
                                        
                                          .
                                          
                                            reshape
                                            
                                              (
                                              
                                                5
                                                
                                                  ,
                                                  
                                                    1
                                                    
                                                      ) 
                                                      
                                                        n12_2 
                                                        
                                                          = 
                                                          
                                                            np
                                                            
                                                              .
                                                              
                                                                zeros
                                                                
                                                                  ((
                                                                  
                                                                    5
                                                                    
                                                                      ,
                                                                      
                                                                        3
                                                                        
                                                                          )) 
                                                                          
                                                                            np
                                                                            
                                                                              .
                                                                              
                                                                                concatenate
                                                                                
                                                                                  ([
                                                                                  
                                                                                    n12
                                                                                    
                                                                                      ,
                                                                                      
                                                                                        n12_2
                                                                                        
                                                                                          ],
                                                                                          
                                                                                            axis
                                                                                            
                                                                                              =
                                                                                              
                                                                                                1
                                                                                                
                                                                                                  ) 
                                                                                                
                                                                                              
                                                                                            
                                                                                          
                                                                                        
                                                                                      
                                                                                    
                                                                                  
                                                                                
                                                                              
                                                                            
                                                                          
                                                                        
                                                                      
                                                                    
                                                                  
                                                                
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[113]:
                    array([[1., 0., 0., 0.],
       [2., 0., 0., 0.],
       [3., 0., 0., 0.],
       [4., 0., 0., 0.],
       [5., 0., 0., 0.]])
                  
In?[132]:
                    
                      #13 給定一個(gè)二維矩陣,如何交換其中兩行的元素?

                      
                        n13 
                        
                          = 
                          
                            np
                            
                              .
                              
                                random
                                
                                  .
                                  
                                    randint
                                    
                                      (
                                      
                                        0
                                        
                                          ,
                                          
                                            10
                                            
                                              ,(
                                              
                                                2
                                                
                                                  ,
                                                  
                                                    2
                                                    
                                                      )) 
                                                      
                                                        n13 
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[132]:
                    array([[5, 6],
       [3, 7]])
                  
In?[133]:
                    
                      n13
                      
                        [[
                        
                          1
                          
                            ,
                            
                              0
                              
                                ]] 
                              
                            
                          
                        
                      
                    
                  
Out[133]:
                    array([[3, 7],
       [5, 6]])
                  
In?[142]:
                    
                      #14 創(chuàng)建一個(gè)100000長(zhǎng)度的隨機(jī)數(shù)組,使用三種方法對(duì)其求三次方,并比較所用時(shí)間

                      
                        n14 
                        
                          = 
                          
                            np
                            
                              .
                              
                                random
                                
                                  .
                                  
                                    randint
                                    
                                      (
                                      
                                        0
                                        
                                          ,
                                          
                                            1000000
                                            
                                              ,
                                              
                                                100000
                                                
                                                  ) 
                                                  
                                                    n14 
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[142]:
                    array([469994, 274462, 901213, ..., 821177, 810961,  33090])
                  
In?[143]:
                    
                      %
                      
                        timeit n14**3

                      
                    
                  
?
                    227 μs ± 8.17 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

                  
In?[144]:
                    
                      %
                      
                        timeit np.power(n14,3)

                      
                    
                  
?
                    219 μs ± 3.23 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

                  
In?[153]:
                    
                      n14_1 
                      
                        = 
                        
                          np
                          
                            .
                            
                              dot
                              
                                (
                                
                                  n14
                                  
                                    ,
                                    
                                      n14
                                      
                                        ) 
                                        
                                          %
                                          
                                            timeit np.dot(n14_1,n14) 
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
?
                    94.1 μs ± 1.73 μs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

                  
In?[146]:
                    
                      #15 創(chuàng)建一個(gè)5*3隨機(jī)矩陣和一個(gè)3*2隨機(jī)矩陣,求矩陣積

                      
                        n15_1 
                        
                          = 
                          
                            np
                            
                              .
                              
                                random
                                
                                  .
                                  
                                    randint
                                    
                                      (
                                      
                                        0
                                        
                                          ,
                                          
                                            100
                                            
                                              ,(
                                              
                                                5
                                                
                                                  ,
                                                  
                                                    3
                                                    
                                                      )) 
                                                      
                                                        n15_2 
                                                        
                                                          = 
                                                          
                                                            np
                                                            
                                                              .
                                                              
                                                                random
                                                                
                                                                  .
                                                                  
                                                                    randint
                                                                    
                                                                      (
                                                                      
                                                                        0
                                                                        
                                                                          ,
                                                                          
                                                                            100
                                                                            
                                                                              ,(
                                                                              
                                                                                3
                                                                                
                                                                                  ,
                                                                                  
                                                                                    2
                                                                                    
                                                                                      )) 
                                                                                      
                                                                                        display
                                                                                        
                                                                                          (
                                                                                          
                                                                                            n15_1
                                                                                            
                                                                                              ,
                                                                                              
                                                                                                n15_2
                                                                                                
                                                                                                  ) 
                                                                                                
                                                                                              
                                                                                            
                                                                                          
                                                                                        
                                                                                      
                                                                                    
                                                                                  
                                                                                
                                                                              
                                                                            
                                                                          
                                                                        
                                                                      
                                                                    
                                                                  
                                                                
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
?
                    array([[21, 95, 95],
       [63, 18,  5],
       [19,  9, 36],
       [52, 56, 38],
       [57, 32, 70]])
                  
?
                    array([[64, 57],
       [57, 54],
       [20, 54]])
                  
In?[149]:
                    
                      np
                      
                        .
                        
                          dot
                          
                            (
                            
                              n15_1
                              
                                ,
                                
                                  n15_2
                                  
                                    ) 
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[149]:
                    array([[ 8659, 11457],
       [ 5158,  4833],
       [ 2449,  3513],
       [ 7280,  8040],
       [ 6872,  8757]])
                  
In?[155]:
                    
                      #16 矩陣的每一行的元素都減去該行的平均值

                      
                        n16 
                        
                          = 
                          
                            np
                            
                              .
                              
                                random
                                
                                  .
                                  
                                    randint
                                    
                                      (
                                      
                                        0
                                        
                                          ,
                                          
                                            100
                                            
                                              ,(
                                              
                                                2
                                                
                                                  ,
                                                  
                                                    2
                                                    
                                                      )) 
                                                      
                                                        n16 
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[155]:
                    array([[71, 15],
       [80, 47]])
                  
In?[166]:
                    
                      n16_mean 
                      
                        = 
                        
                          np
                          
                            .
                            
                              mean
                              
                                (
                                
                                  n16
                                  
                                    ,
                                    
                                      axis
                                      
                                        =
                                        
                                          1
                                          
                                            )
                                            
                                              .
                                              
                                                reshape
                                                
                                                  (
                                                  
                                                    2
                                                    
                                                      ,
                                                      
                                                        1
                                                        
                                                          ) 
                                                          
                                                            n16_mean 
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[166]:
                    array([[43. ],
       [63.5]])
                  
In?[167]:
                    
                      n16 
                      
                        - 
                        
                          n16_mean

                        
                      
                    
                  
Out[167]:
                    array([[ 28. , -28. ],
       [ 16.5, -16.5]])
                  
In?[211]:
                    
                      #17 打印出以下函數(shù)(要求使用np.zeros創(chuàng)建8*8的矩陣):

                      
                        # [[0 1 0 1 0 1 0 1]

                        
                          #  [1 0 1 0 1 0 1 0]

                          
                            #  [0 1 0 1 0 1 0 1]

                            
                              #  [1 0 1 0 1 0 1 0]

                              
                                # [0 1 0 1 0 1 0 1] 
                                
                                  # [1 0 1 0 1 0 1 0] 
                                  
                                    # [0 1 0 1 0 1 0 1] 
                                    
                                      # [1 0 1 0 1 0 1 0]] 
                                      
                                        n17 
                                        
                                          = 
                                          
                                            np
                                            
                                              .
                                              
                                                zeros
                                                
                                                  ((
                                                  
                                                    8
                                                    
                                                      ,
                                                      
                                                        8
                                                        
                                                          ),
                                                          
                                                            dtype
                                                            
                                                              =
                                                              
                                                                int
                                                                
                                                                  ) 
                                                                  
                                                                    n17
                                                                    
                                                                      [::
                                                                      
                                                                        2
                                                                        
                                                                          ,
                                                                          
                                                                            1
                                                                            
                                                                              ::
                                                                              
                                                                                2
                                                                                
                                                                                  ]
                                                                                  
                                                                                    =
                                                                                    
                                                                                      1 
                                                                                      
                                                                                        n17
                                                                                        
                                                                                          [
                                                                                          
                                                                                            1
                                                                                            
                                                                                              ::
                                                                                              
                                                                                                2
                                                                                                
                                                                                                  ,::
                                                                                                  
                                                                                                    2
                                                                                                    
                                                                                                      ]
                                                                                                      
                                                                                                        =
                                                                                                        
                                                                                                          1 
                                                                                                          
                                                                                                            n17 
                                                                                                          
                                                                                                        
                                                                                                      
                                                                                                    
                                                                                                  
                                                                                                
                                                                                              
                                                                                            
                                                                                          
                                                                                        
                                                                                      
                                                                                    
                                                                                  
                                                                                
                                                                              
                                                                            
                                                                          
                                                                        
                                                                      
                                                                    
                                                                  
                                                                
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[211]:
                    array([[0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0]])
                  
In?[219]:
                    
                      #18 正則化一個(gè)5*5隨機(jī)矩陣

                      
                        # 正則的概念:假設(shè)a是矩陣中的一個(gè)元素,max/min分別是矩陣元素的最大最小值,

                        
                          # 則正則化后a = (a - min)/(max - min)

                          
                            n18 
                            
                              = 
                              
                                np
                                
                                  .
                                  
                                    random
                                    
                                      .
                                      
                                        randint
                                        
                                          (
                                          
                                            0
                                            
                                              ,
                                              
                                                100
                                                
                                                  ,(
                                                  
                                                    5
                                                    
                                                      ,
                                                      
                                                        5
                                                        
                                                          )) 
                                                          
                                                            n18_max
                                                            
                                                              ,
                                                              
                                                                n18_min 
                                                                
                                                                  = 
                                                                  
                                                                    n18
                                                                    
                                                                      .
                                                                      
                                                                        max
                                                                        
                                                                          (),
                                                                          
                                                                            n18
                                                                            
                                                                              .
                                                                              
                                                                                min
                                                                                
                                                                                  () 
                                                                                  
                                                                                    n18_re 
                                                                                    
                                                                                      = 
                                                                                      
                                                                                        (
                                                                                        
                                                                                          n18
                                                                                          
                                                                                            -
                                                                                            
                                                                                              n18_min
                                                                                              
                                                                                                )
                                                                                                
                                                                                                  /
                                                                                                  
                                                                                                    (
                                                                                                    
                                                                                                      n18_max
                                                                                                      
                                                                                                        -
                                                                                                        
                                                                                                          n18_min
                                                                                                          
                                                                                                            ) 
                                                                                                            
                                                                                                              n18_re 
                                                                                                            
                                                                                                          
                                                                                                        
                                                                                                      
                                                                                                    
                                                                                                  
                                                                                                
                                                                                              
                                                                                            
                                                                                          
                                                                                        
                                                                                      
                                                                                    
                                                                                  
                                                                                
                                                                              
                                                                            
                                                                          
                                                                        
                                                                      
                                                                    
                                                                  
                                                                
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[219]:
                    array([[0.68888889, 0.07777778, 0.85555556, 0.36666667, 0.13333333],
       [0.1       , 0.91111111, 0.67777778, 0.24444444, 0.37777778],
       [0.04444444, 0.45555556, 0.64444444, 0.04444444, 0.04444444],
       [0.86666667, 0.91111111, 0.4       , 0.7       , 0.94444444],
       [0.        , 0.95555556, 0.22222222, 1.        , 0.16666667]])
                  
In?[241]:
                    
                      # 19 將一個(gè)一維數(shù)組轉(zhuǎn)化為二進(jìn)制表示矩陣。例如

                      
                        # [1,2,3]

                        
                          # 轉(zhuǎn)化為

                          
                            # [[0,0,1],

                            
                              # [0,1,0],

                              
                                # [0,1,1]] 
                                
                                  display
                                  
                                    (
                                    
                                      1 
                                      
                                        and 
                                        
                                          0 
                                          
                                            ,
                                            
                                              1 
                                              
                                                or 
                                                
                                                  1
                                                  
                                                    , 
                                                    
                                                      1 
                                                      
                                                        & 
                                                        
                                                          2
                                                          
                                                            ) 
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
?
                    0
                  
?
                    1
                  
?
                    0
                  
In?[237]:
                    
                      I 
                      
                        = 
                        
                          np
                          
                            .
                            
                              array
                              
                                ([
                                
                                  1
                                  
                                    ,
                                    
                                      2
                                      
                                        ,
                                        
                                          3
                                          
                                            ]) 
                                            
                                              A 
                                              
                                                = 
                                                
                                                  I
                                                  
                                                    .
                                                    
                                                      reshape
                                                      
                                                        (
                                                        
                                                          -
                                                          
                                                            1
                                                            
                                                              ,
                                                              
                                                                1
                                                                
                                                                  ) 
                                                                  
                                                                    A 
                                                                  
                                                                
                                                              
                                                            
                                                          
                                                        
                                                      
                                                    
                                                  
                                                
                                              
                                            
                                          
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[237]:
                    array([[1],
       [2],
       [3]])
                  
In?[230]:
                    
                      B 
                      
                        = 
                        
                          2
                          
                            **
                            
                              np
                              
                                .
                                
                                  arange
                                  
                                    (
                                    
                                      3
                                      
                                        ) 
                                        
                                          B 
                                        
                                      
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[230]:
                    array([1, 2, 4], dtype=int32)
                  
In?[231]:
                    
                      M 
                      
                        = 
                        
                          A
                          
                            &
                            
                              B

                              
                                M 
                              
                            
                          
                        
                      
                    
                  
Out[231]:
                    array([[1, 0, 0],
       [0, 2, 0],
       [1, 2, 0]], dtype=int32)
                  
In?[232]:
                    
                      M 
                      
                        != 
                        
                          0

                        
                      
                    
                  
Out[232]:
                    array([[ True, False, False],
       [False,  True, False],
       [ True,  True, False]])
                  
In?[235]:
                    
                      M 
                      
                        [
                        
                          M
                          
                            !=
                            
                              0
                              
                                ] 
                                
                                  = 
                                  
                                    1 
                                    
                                      M 
                                    
                                  
                                
                              
                            
                          
                        
                      
                    
                  
Out[235]:
                    array([[1, 0, 0],
       [0, 1, 0],
       [1, 1, 0]], dtype=int32)
                  
In?[236]:
                    
                      M
                      
                        [:,::
                        
                          -
                          
                            1
                            
                              ]

                            
                          
                        
                      
                    
                  
Out[236]:
                    array([[0, 0, 1],
       [0, 1, 0],
       [0, 1, 1]], dtype=int32)
                  

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。。?/p>

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 久久久影院 | jazz欧美人免费xxxxxx | 日本免费在线视频 | 首页 动漫 亚洲 欧美 日韩 | 国产高清一级毛片在线不卡 | 亚洲综合图色 | 亚洲视频二 | 欧美一级毛片片免费 | 一区二区三区在线播放视频 | 亚洲综合成人网在线观看 | 精品成人免费一区二区在线播放 | 国产首页精品 | 成人中文字幕一区二区三区 | 国产成人a∨麻豆精品 | 日韩亚洲精品不卡在线 | 色婷婷精品综合久久狠狠 | 玖玖草在线观看 | 亚洲七七久久综合桃花 | 在线免费福利 | 国产在线视频99 | 亚洲福利社| 999精品久久久中文字幕蜜桃 | www.亚洲一区二区三区 | 日本高清免费不卡在线播放 | 免费福利在线视频 | a级做人爱免费播放 | 91精品日本久久久久久牛牛 | 亚洲va国产日韩欧美精品色婷婷 | 日本爽快片100色毛片 | 九九热精品在线 | 色老头成人免费视频天天综合 | 国产最新网站 | 四虎影库网址 | 在线免费观看中文字幕 | 狠狠色成人综合网图片区 | 亚洲视频高清 | 又粗又硬又大久久久 | 狠狠插天天干 | 亚洲第成色999久久网站 | 免费国产黄线在线观看视频 | 一级毛片全部免费播放 |