日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

【Python】简约而不简单|值得收藏的Numpy小抄表(含主要语法、代码)

發(fā)布時(shí)間:2025/3/12 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python】简约而不简单|值得收藏的Numpy小抄表(含主要语法、代码) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

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

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

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

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

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

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

安裝Numpy

可以通過 Pip 或者 Anaconda安裝Numpy:

$ pip install numpy

$ conda install numpy

本文目錄

  • 基礎(chǔ)

    • 占位符

    • 數(shù)組

      • 增加或減少元素

      • 合并數(shù)組

      • 分割數(shù)組

      • 數(shù)組形狀變化

      • 拷貝 /排序

      • 數(shù)組操作

      • 其他

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

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

      • 比較

      • 基礎(chǔ)統(tǒng)計(jì)

      • 更多

    • 切片和子集

    • 小技巧

    • 基礎(chǔ)

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

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

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

      axis 0 通常指行

      axis 1 通常指列

      操作描述文檔
      np.array([1,2,3])一維數(shù)組https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array
      np.array([(1,2,3),(4,5,6)])二維數(shù)組https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array
      np.arange(start,stop,step)等差數(shù)組https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html

      占位符

      操作描述文檔
      np.linspace(0,2,9)數(shù)組中添加等差的值https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
      np.zeros((1,2))創(chuàng)建全0數(shù)組docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html
      np.ones((1,2))創(chuàng)建全1數(shù)組https://docs.scipy.org/doc/numpy/reference/generated/numpy.ones.html#numpy.ones
      np.random.random((5,5))創(chuàng)建隨機(jī)數(shù)的數(shù)組https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random.html
      np.empty((2,2))創(chuàng)建空數(shù)組https://numpy.org/doc/stable/reference/generated/numpy.empty.html

      舉例:

      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])

      數(shù)組屬性

      數(shù)組屬性

      語法描述文檔
      array.shape維度(行,列)https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html
      len(array)數(shù)組長度https://docs.python.org/3.5/library/functions.html#len
      array.ndim數(shù)組的維度數(shù)https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.ndim.html
      array.size數(shù)組的元素?cái)?shù)https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.size.html
      array.dtype數(shù)據(jù)類型https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
      array.astype(type)轉(zhuǎn)換數(shù)組類型https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html
      type(array)顯示數(shù)組類型https://numpy.org/doc/stable/user/basics.types.html

      拷貝?/排序

      操作描述文檔
      np.copy(array)創(chuàng)建數(shù)組拷貝https://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html
      other = array.copy()創(chuàng)建數(shù)組深拷貝https://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html
      array.sort()排序一個(gè)數(shù)組https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html
      array.sort(axis=0)按照指定軸排序一個(gè)數(shù)組https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html

      舉例

      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]

      數(shù)組操作例程

      增加或減少元素

      操作描述文檔
      np.append(a,b)增加數(shù)據(jù)項(xiàng)到數(shù)組https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html
      np.insert(array, 1, 2, axis)沿著數(shù)組0軸或者1軸插入數(shù)據(jù)項(xiàng)https://docs.scipy.org/doc/numpy/reference/generated/numpy.insert.html
      np.resize((2,4))將數(shù)組調(diào)整為形狀(2,4)https://docs.scipy.org/doc/numpy/reference/generated/numpy.resize.html
      np.delete(array,1,axis)從數(shù)組里刪除數(shù)據(jù)項(xiàng)https://numpy.org/doc/stable/reference/generated/numpy.delete.html

      舉例

      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ù)組

      操作描述文檔
      np.concatenate((a,b),axis=0)連接2個(gè)數(shù)組,添加到末尾https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html
      np.vstack((a,b))按照行堆疊數(shù)組https://numpy.org/doc/stable/reference/generated/numpy.vstack.html
      np.hstack((a,b))按照列堆疊數(shù)組docs.scipy.org/doc/numpy/reference/generated/numpy.hstack.html#numpy.hstack

      舉例

      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ù)組

      操作描述文檔
      numpy.split()分割數(shù)組
      https://docs.scipy.org/doc/numpy/reference/generated/numpy.split.html
      np.array_split(array, 3)將數(shù)組拆分為大小(幾乎)相同的子數(shù)組https://docs.scipy.org/doc/numpy/reference/generated/numpy.array_split.html#numpy.array_split
      numpy.hsplit(array, 3)在第3個(gè)索引處水平拆分?jǐn)?shù)組
      https://numpy.org/doc/stable/reference/generated/numpy.hsplit.html#numpy.hsplit

      舉例

      # 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ù)組形狀變化

      操作

      操作描述文檔
      other = ndarray.flatten()平鋪一個(gè)二維數(shù)組到一維數(shù)組https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flatten.html
      numpy.flip()翻轉(zhuǎn)一維數(shù)組中元素的順序https://docs.scipy.org/doc/stable/reference/generated/numpy.flip.html
      np.ndarray[::-1]翻轉(zhuǎn)一維數(shù)組中元素的順序
      reshape改變數(shù)組的維數(shù)https://docs.scipy.org/doc/stable/reference/generated/numpy.reshape.html
      squeeze從數(shù)組的形狀中刪除單維度條目https://numpy.org/doc/stable/reference/generated/numpy.squeeze.html
      expand_dims擴(kuò)展數(shù)組維度
      https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.expand_dims.html

      其他

      操作描述文檔
      other = ndarray.flatten()平鋪2維數(shù)組到1維數(shù)組https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flatten.html
      array = np.transpose(other)
      array.T
      數(shù)組轉(zhuǎn)置https://numpy.org/doc/stable/reference/generated/numpy.transpose.html
      inverse = np.linalg.inv(matrix)求矩陣的逆矩陣https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.inv.html


      舉例

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

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

      操作

      操作描述文檔
      np.add(x,y)
      x + y
      https://docs.scipy.org/doc/numpy/reference/generated/numpy.add.html
      np.substract(x,y)
      x - y
      https://docs.scipy.org/doc/numpy/reference/generated/numpy.subtract.html#numpy.subtract
      np.divide(x,y)
      x / y
      https://docs.scipy.org/doc/numpy/reference/generated/numpy.divide.html#numpy.divide
      np.multiply(x,y)
      x @ y
      https://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html#numpy.multiply
      np.sqrt(x)平方根https://docs.scipy.org/doc/numpy/reference/generated/numpy.sqrt.html#numpy.sqrt
      np.sin(x)元素正弦https://docs.scipy.org/doc/numpy/reference/generated/numpy.sin.html#numpy.sin
      np.cos(x)元素余弦https://docs.scipy.org/doc/numpy/reference/generated/numpy.cos.html#numpy.cos
      np.log(x)元素自然對數(shù)https://docs.scipy.org/doc/numpy/reference/generated/numpy.log.html#numpy.log
      np.dot(x,y)點(diǎn)積https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html
      np.roots([1,0,-4])給定多項(xiàng)式系數(shù)的根https://docs.scipy.org/doc/numpy/reference/generated/numpy.roots.html

      舉例

      # 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.])

      比較

      操作描述文檔
      ==等于https://docs.python.org/2/library/stdtypes.html
      !=不等于
      https://docs.python.org/2/library/stdtypes.html
      <小于https://docs.python.org/2/library/stdtypes.html
      >大于https://docs.python.org/2/library/stdtypes.html
      <=小于等于https://docs.python.org/2/library/stdtypes.html
      >=大于等于https://docs.python.org/2/library/stdtypes.html
      np.array_equal(x,y)數(shù)組比較https://numpy.org/doc/stable/reference/generated/numpy.array_equal.html

      舉例:

      # 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ì)

      操作描述文檔
      np.mean(array)Meanhttps://numpy.org/doc/stable/reference/generated/numpy.mean.html#numpy.mean
      np.median(array)Medianhttps://numpy.org/doc/stable/reference/generated/numpy.median.html#numpy.median
      array.corrcoef()Correlation Coefficienthttps://numpy.org/doc/stable/reference/generated/numpy.corrcoef.html#numpy.corrcoef
      np.std(array)Standard Deviationhttps://docs.scipy.org/doc/numpy/reference/generated/numpy.std.html#numpy.std

      舉例

      # 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

      更多

      操作描述文檔
      array.sum()數(shù)組求和https://numpy.org/doc/stable/reference/generated/numpy.sum.html
      array.min()數(shù)組求最小值https://numpy.org/doc/stable/reference/generated/numpy.ndarray.min.html
      array.max(axis=0)數(shù)組求最大值(沿著0軸)
      array.cumsum(axis=0)指定軸求累計(jì)和https://numpy.org/doc/stable/reference/generated/numpy.cumsum.html


      切片和子集

      操作描述文檔
      array[i]索引i處的一維數(shù)組https://numpy.org/doc/stable/reference/arrays.indexing.html
      array[i,j]索引在[i][j]處的二維數(shù)組https://numpy.org/doc/stable/reference/arrays.indexing.html
      array[i<4]布爾索引https://numpy.org/doc/stable/reference/arrays.indexing.html
      array[0:3]選擇索引為 0, 1和?2https://numpy.org/doc/stable/reference/arrays.indexing.html
      array[0:2,1]選擇第0,1行,第1列https://numpy.org/doc/stable/reference/arrays.indexing.html
      array[:1]選擇第0行數(shù)據(jù)項(xiàng)?(與[0:1, :]相同)https://numpy.org/doc/stable/reference/arrays.indexing.html
      array[1:2, :]選擇第1行https://numpy.org/doc/stable/reference/arrays.indexing.html
      [comment]: <> "array[1,...]等同于 array[1,:,:]
      array[ : :-1]反轉(zhuǎn)數(shù)組同上

      舉例

      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))

      生成矩陣和切片圖示

      小技巧

      例子將會越來越多的,歡迎大家提交。

      布爾索引?

      # 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

      往期精彩回顧適合初學(xué)者入門人工智能的路線及資料下載機(jī)器學(xué)習(xí)及深度學(xué)習(xí)筆記等資料打印機(jī)器學(xué)習(xí)在線手冊深度學(xué)習(xí)筆記專輯《統(tǒng)計(jì)學(xué)習(xí)方法》的代碼復(fù)現(xiàn)專輯 AI基礎(chǔ)下載機(jī)器學(xué)習(xí)的數(shù)學(xué)基礎(chǔ)專輯黃海廣老師《機(jī)器學(xué)習(xí)課程》課件合集 本站qq群851320808,加入微信群請掃碼:

    總結(jié)

    以上是生活随笔為你收集整理的【Python】简约而不简单|值得收藏的Numpy小抄表(含主要语法、代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。