<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Numpy核心語(yǔ)法和代碼整理匯總!

          共 3776字,需瀏覽 8分鐘

           ·

          2020-09-10 11:16

          用戶2769421 | 作者

          騰訊云 云+社區(qū)?|?來(lái)源




          Numpy是一個(gè)用python實(shí)現(xiàn)的科學(xué)計(jì)算的擴(kuò)展程序庫(kù),包括:

          • 一個(gè)強(qiáng)大的N維數(shù)組對(duì)象Array;

          • 比較成熟的(廣播)函數(shù)庫(kù);

          • 用于整合C/C++和Fortran代碼的工具包;

          • 實(shí)用的線性代數(shù)、傅里葉變換和隨機(jī)數(shù)生成函數(shù)。numpy和稀疏矩陣運(yùn)算包scipy配合使用更加方便。


          NumPy(Numeric Python)提供了許多高級(jí)的數(shù)值編程工具,如:矩陣數(shù)據(jù)類型、矢量處理,以及精密的運(yùn)算庫(kù)。專為進(jìn)行嚴(yán)格的數(shù)字處理而產(chǎn)生。多為很多大型金融公司使用,以及核心的科學(xué)計(jì)算組織如:Lawrence Livermore,NASA用其處理一些本來(lái)使用C++,F(xiàn)ortran或Matlab等所做的任務(wù)。


          本文整理了一個(gè)Numpy的小抄表,總結(jié)了Numpy的常用操作,可以收藏慢慢看。


          (圖片可以點(diǎn)開大圖查看哦~)


          1


          安裝Numpy


          可以通過(guò) Pip 或者 Anaconda安裝Numpy:


          $?pip install numpy



          $?conda install numpy


          2


          基礎(chǔ)


          NumPy最常用的功能之一就是NumPy數(shù)組:列表和NumPy數(shù)組的最主要區(qū)別在于功能性和速度。


          列表提供基本操作,但NumPy添加了FTTs、卷積、快速搜索、基本統(tǒng)計(jì)、線性代數(shù)、直方圖等。


          兩者數(shù)據(jù)科學(xué)最重要的區(qū)別是能夠用NumPy數(shù)組進(jìn)行元素級(jí)計(jì)算。


          • axis 0:通常指行

          • axis 1:通常指列



          1.占位符



          舉例:


          import numpy as np

          #
          ?1 dimensional
          x = np.array([1,2,3])
          #?2 dimensional
          y = np.array([(1,2,3),(4,5,6)])

          x = np.arange(3)
          >>> array([0, 1, 2])

          y = np.arange(3.0)
          >>> array([ 0., 1., 2.])

          x = np.arange(3,7)
          >>> array([3, 4, 5, 6])

          y = np.arange(3,7,2)
          >>> array([3, 5])


          2.數(shù)組屬性



          3.拷貝 /排序



          舉例:


          import?numpy as?np
          # Sort sorts in ascending order
          y = np.array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
          y.sort()
          print(y)
          >>> [ 1??2??3??4??5??6??7??8??9??10]


          4.數(shù)組操作例程


          ?增加或減少元素?



          舉例:


          import numpy as?np
          # Append items?to?array
          a?= np.array([(1, 2, 3),(4, 5, 6)])
          b?= np.append(a, [(7, 8, 9)])
          print(b)
          >>> [1?2?3?4?5?6?7?8?9]

          # Remove index?2?from previous?array
          print(np.delete(b, 2))
          >>> [1?2?4?5?6?7?8?9]


          ?組合數(shù)組?



          舉例:


          import?numpy as?np
          a = np.array([1, 3, 5])
          b = np.array([2, 4, 6])

          # Stack two arrays row-wise
          print(np.vstack((a,b)))
          >>> [[1?3?5]
          ?????[2?4?6]]

          # Stack two arrays column-wise
          print(np.hstack((a,b)))
          >>> [1?3?5?2?4?6]


          ?分割數(shù)組?



          舉例:


          # Split array into groups of ~3
          a = np.array([1, 2, 3, 4, 5, 6, 7, 8])
          print(np.array_split(a, 3))
          >>> [array([1, 2, 3]), array([4, 5, 6]), array([7, 8])]


          ?數(shù)組形狀變化?


          • 操作



          • 其他



          舉例:


          # Find?inverse?of?a?given?matrix
          >>> np.linalg.inv([[3,1],[2,4]])
          array([[ 0.4, -0.1],
          ???????[-0.2, 0.3]])


          5.數(shù)學(xué)計(jì)算


          ?操作?



          舉例:


          #?If a 1d array is added to a 2d array (or the other way), NumPy
          #?chooses the array with smaller dimension and adds it to the one
          #?with bigger dimension
          a = np.array([1, 2, 3])
          b = np.array([(1, 2, 3), (4, 5, 6)])
          print(np.add(a, b))
          >>> [[2 4 6]
          ?????[5 7 9]]
          ?????
          #?Example of np.roots
          #?Consider a polynomial function?(x-1)^2 = x^2 - 2*x + 1
          #?Whose roots are 1,1
          >>> np.roots([1,-2,1])
          array([1., 1.])
          #?Similarly x^2 - 4 = 0 has roots as x=±2
          >>> np.roots([1,0,-4])
          array([-2., 2.])


          ?比較?



          舉例:


          # Using comparison operators will create boolean NumPy arrays
          z = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
          c = z < 6
          print(c)
          >>> [ True??True??True??True??True?False?False?False?False?False]


          ?基本的統(tǒng)計(jì)?



          舉例:


          #?Statistics of an array
          a = np.array([1, 1, 2, 5, 8, 10, 11, 12])

          #
          ?Standard deviation
          print(np.std(a))
          >>> 4.2938910093294167

          #
          ?Median
          print(np.median(a))
          >>> 6.5


          ?更多?



          6.切片和子集



          舉例:


          b = np.array([(1, 2, 3), (4, 5, 6)])

          # The index *before* the comma refers to *rows*,
          # the index *after* the comma refers to *columns*
          print(b[0:1, 2])
          >>> [3]

          print(b[:len(b), 2])
          >>> [3?6]

          print(b[0, :])
          >>> [1?2?3]

          print(b[0, 2:])
          >>> [3]

          print(b[:, 0])
          >>> [1?4]

          c = np.array([(1, 2, 3), (4, 5, 6)])
          d = c[1:2, 0:2]
          print(d)
          >>> [[4?5]]


          切片舉例:


          import?numpy as?np
          a1 = np.arange(0, 6)
          a2 = np.arange(10, 16)
          a3 = np.arange(20, 26)
          a4 = np.arange(30, 36)
          a5 = np.arange(40, 46)
          a6 = np.arange(50, 56)
          a = np.vstack((a1, a2, a3, a4, a5, a6))


          生成矩陣和切片圖示




          7.小技巧


          ?布爾索引?


          # Index trick when working with two np-arrays
          a?= np.array([1,2,3,6,1,4,1])
          b?= np.array([5,6,7,8,3,1,2])

          # Only saves a at index where b == 1
          other_a?= a[b == 1]
          #Saves every spot in a except at index where b != 1
          other_other_a?= a[b != 1]


          import?numpy as?np
          x = np.array([4,6,8,1,2,6,9])
          y = x > 5
          print(x[y])
          >>> [6?8?6?9]

          # Even shorter
          x = np.array([1, 2, 3, 4, 4, 35, 212, 5, 5, 6])
          print(x[x < 5])
          >>> [1?2?3?4?4]


          參考:

          https://github.com/juliangaal/python-cheat-sheet

          戀習(xí)Python

          關(guān)注戀習(xí)Python,Python都好練


          好文章,我在看??
          瀏覽 27
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  台湾成人无码 | 成人网免费三级 | 视频网站国产日本 | 成人性生活影视av | 蜜桃久久久久 |