解決90%的常見問題的8個(gè)python NumPy函數(shù)
NumPy
NumPy是一個(gè)用于科學(xué)計(jì)算和數(shù)據(jù)分析的Python庫,也是機(jī)器學(xué)習(xí)的支柱??梢哉fNumPy奠定了Python在機(jī)器學(xué)習(xí)中的地位。NumPy提供了一個(gè)強(qiáng)大的多維數(shù)組對(duì)象,以及廣泛的數(shù)學(xué)函數(shù),可以對(duì)大型數(shù)據(jù)集進(jìn)行有效的操作。這里的“大”是指數(shù)百萬行。

Numpy快速而高效的原因是底層的C代碼,這比使用Python進(jìn)行數(shù)組的操作要快上幾百倍,并且隨著數(shù)據(jù)量級(jí)的上升而上升。
本文中整理了一些可以解決常見問題的主要的NumPy函數(shù)。
1、創(chuàng)建數(shù)組
numpy.array:創(chuàng)建新的NumPy數(shù)組
# Create an array using np.array() arr = np.array([1, 2, 3, 4, 5]) print(arr) Ouput: [1 2 3 4 5]
numpy.zeros:創(chuàng)建一個(gè)以零填充的數(shù)組。
# Create a 2-dimensional array of zeros arr = np.zeros((3, 4)) [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]
類似的還有numpy.ones:創(chuàng)建一個(gè)都是1的數(shù)組 / numpy.empty:在不初始化數(shù)組元素的情況下創(chuàng)建數(shù)組。
使用numpy.random:生成隨機(jī)數(shù)組的函數(shù)。
# Generate a random integer between 0 and 9 rand_int = np.random.randint(10) print(rand_int)
numpy.linspace:在指定范圍內(nèi)生成均勻間隔的數(shù)字。
# Generate an array of 5 values from 0 to 10 (inclusive) arr = np.linspace(0, 10, 5) # Print the array print(arr) [ 0. 2.5 5. 7.5 10. ]
numpy.range:用間隔的值創(chuàng)建數(shù)組。
# Generate an array from 0 to 10 (exclusive) with step size 1 arr = np.arange(0, 10, 2) # Print the array print(arr) [1 3 5 7 9]
2、查看數(shù)組信息
numpy.shape:返回一個(gè)表示數(shù)組形狀的元組。
numpy.ndim:返回?cái)?shù)組的維度數(shù)。
numpy.dtype:獲取數(shù)組中元素的數(shù)據(jù)類型??梢允莍nt型,float型,bool型等等。
3、數(shù)組操作函數(shù)
numpy.reshape:改變數(shù)組的形狀。
# Create a 1-dimensional array arr = np.array([1, 2, 3, 4, 5, 6]) # Reshape the array to a 2x3 matrix reshaped_arr = np.reshape(arr, (2, 3)) [[1 2 3] [4 5 6]]
numpy.transpose:用于排列數(shù)組的維度。它返回一個(gè)軸調(diào)換后的新數(shù)組。
# Create a 2-dimensional array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Transpose the array
transposed_arr = np.transpose(arr)
[[1 4]
[2 5]
[3 6]]numpy.concatate:沿現(xiàn)有軸連接數(shù)組。
# Create two 1-dimensional arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) # Concatenate the arrays along axis 0 (default) concatenated_arr = np.concatenate((arr1, arr2)) [1 2 3 4 5 6]
numpy.split:分割數(shù)據(jù),numpy.resize:改變數(shù)組的形狀和大小。
numpy.vstack:將多個(gè)數(shù)組垂直堆疊以創(chuàng)建一個(gè)新數(shù)組。
# Create two 1-dimensional arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) # Vertically stack the arrays stacked_arr = np.vstack((arr1, arr2)) [[1 2 3] [4 5 6]]
numpy.hstack:與vstack類似,但是是水平堆疊數(shù)組。
4、數(shù)學(xué)函數(shù)
numpy.sum:計(jì)算數(shù)組元素的和。
numpy.mean:計(jì)算數(shù)組的算術(shù)平均值。
numpy.max:返回?cái)?shù)組中的最大值。
numpy.min:返回?cái)?shù)組中的最小值。
numpy.abs:計(jì)算元素的絕對(duì)值。
numpy.exp:計(jì)算所有元素的指數(shù)。
numpy.subtract: 對(duì)兩個(gè)數(shù)組的對(duì)應(yīng)元素進(jìn)行減法運(yùn)算。
numpy.multiply: 對(duì)兩個(gè)數(shù)組的對(duì)應(yīng)元素進(jìn)行乘法運(yùn)算。
numpy.divide: 對(duì)兩個(gè)數(shù)組的對(duì)應(yīng)元素進(jìn)行除法運(yùn)算。
numpy.sin: 計(jì)算數(shù)組中每個(gè)元素的正弦值。
numpy.cos: 計(jì)算數(shù)組中每個(gè)元素的余弦值。
numpy.log: 計(jì)算數(shù)組中每個(gè)元素的自然對(duì)數(shù)(以e為底的對(duì)數(shù))。
5、統(tǒng)計(jì)函數(shù)
numpy.std:計(jì)算數(shù)組的標(biāo)準(zhǔn)差。
# Create a 1-dimensional array arr = np.array([1, 2, 3, 4, 5]) # Compute the standard deviation of the array std = np.std(arr) 1.4142135623730951
numpy.var:計(jì)算數(shù)組的方差。
numpy.histogram:計(jì)算一組數(shù)據(jù)的直方圖。
numpy.percentile:計(jì)算數(shù)組的第n個(gè)百分位數(shù)。它返回低于給定百分比的數(shù)據(jù)的值。
data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # Calculate the 50th percentile (median) of the data median = np.percentile(data, 50) # Calculate the 25th and 75th percentiles (quartiles) of the data q1 = np.percentile(data, 25) q3 = np.percentile(data, 75) Median: 5.5 Q1: 3.25 Q3: 7.75
numpy.corcoef:計(jì)算兩個(gè)數(shù)組之間的相關(guān)系數(shù)。numpy.mean: 計(jì)算數(shù)組元素的平均值。numpy.median: 計(jì)算數(shù)組元素的中位數(shù)。
numpy.random.rand:在區(qū)間[0,1]內(nèi)從均勻分布生成隨機(jī)數(shù)數(shù)組
# Generate a 1-dimensional array of random numbers random_array = np.random.rand(5) [0.35463311 0.67659889 0.5865293 0.77127035 0.13949178]
numpy.random.normal:從正態(tài)(高斯)分布生成隨機(jī)數(shù)
# Generate a random number from a normal distribution random_number = np.random.normal() -0.6532785285205665
6、線性代數(shù)函數(shù)
numpy.dot:計(jì)算兩個(gè)數(shù)組的點(diǎn)積。
# Create two arrays a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) # Compute the dot product of the arrays dot_product = np.dot(a, b) 32
numpy.linalg.inv:計(jì)算一個(gè)方陣的逆, numpy.linalg.eig:一個(gè)方陣的特征值和特征向量。numpy.linalg.solve:求解一個(gè)線性方程組。
7、排序函數(shù)
numpy.sort:沿指定軸返回?cái)?shù)組的排序副本
# Create a 2D array arr = np.array([[3, 1, 5], [2, 4, 6]]) # Sort the array along the second axis (columns) sorted_arr = np.sort(arr, axis=1) [[1 3 5] [2 4 6]]
numpy.argsort:返回按升序?qū)?shù)組排序的索引
# Create an array arr = np.array([3, 1, 5, 2, 4]) # Get the indices that would sort the array sorted_indices = np.argsort(arr) [1 3 0 4 2]
8、其他一些高級(jí)的函數(shù)
numpy.unique:在數(shù)組中查找唯一的元素。
arr = np.array([2, 1, 3, 2, 1, 4, 5, 4]) # Get the unique elements of the array unique_values = np.unique(arr) [1 2 3 4 5]
numpy.fft:傅里葉變換的函數(shù)。
numpy.ma:供對(duì)掩碼數(shù)組的支持。
- numpy.ma.array:從現(xiàn)有的數(shù)組或序列創(chuàng)建一個(gè)掩碼數(shù)組。
- numpy.ma.masked_array:從現(xiàn)有數(shù)組和掩碼中創(chuàng)建一個(gè)掩碼數(shù)組。
- numpy.ma.mask:表示掩碼數(shù)組中的掩碼值。
- numpy.ma.masked_invalid:屏蔽數(shù)組中無效的(NaN, Inf)元素。
- numpy.ma.masked_greate, numpy.ma.masked_less:掩碼大于或小于給定值的元素。
arr = np.array([1, 2, 3, np.nan, 5]) # Create a masked array by masking the invalid values masked_arr = ma.masked_invalid(arr) [1 2 3 5]
numpy.apply_along_axis:沿著數(shù)組的特定軸應(yīng)用函數(shù)。
numpy.wheres:一個(gè)條件函數(shù),根據(jù)給定條件返回?cái)?shù)組中滿足條件的元素的索引或值。
condition = np.array([True, False, True, False]) # Create two arrays array_true = np.array([1, 2, 3, 4]) array_false = np.array([5, 6, 7, 8]) result = np.where(condition, array_true, array_false) [1 6 3 8]
以上就是Numpy最經(jīng)常被使用的函數(shù),希望對(duì)你有所幫助,更多關(guān)于python NumPy函數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一文帶你解密Python迭代器的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Python中迭代器的實(shí)現(xiàn)原理,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,需要的可以參考一下2022-12-12
Python成功解決ZeroDivisionError:?division?by?zero的方法過程
在Python編程中,ZeroDivisionError:divisionbyzero是因?yàn)閲L試除以零所導(dǎo)致的常見錯(cuò)誤,這篇文章詳細(xì)介紹了錯(cuò)誤的原因、解決方案,需要的朋友可以參考下2024-09-09
Python輕松實(shí)現(xiàn)圖片文字提取的高效技巧分享
隨著數(shù)字化轉(zhuǎn)型的加速,從圖片中提取文字(OCR,光學(xué)字符識(shí)別)的需求日益增長,Python憑借其豐富的庫和易用性,成為實(shí)現(xiàn)OCR的首選工具之一,本文將深入探討如何利用Python從圖片中提取文字,涵蓋基本原理、常用工具、代碼實(shí)現(xiàn)及優(yōu)化技巧,需要的朋友可以參考下2025-07-07
python中os和sys模塊的區(qū)別與常用方法總結(jié)
這篇文章主要給大家介紹了關(guān)于python中os和sys模塊的區(qū)別與常用方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
OpenCV+Python--RGB轉(zhuǎn)HSI的實(shí)現(xiàn)
今天小編就為大家分享一篇OpenCV+Python--RGB轉(zhuǎn)HSI的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Python實(shí)現(xiàn)網(wǎng)頁數(shù)據(jù)提取完整指南
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)網(wǎng)頁數(shù)據(jù)提取的相關(guān)方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2026-04-04
Python 實(shí)現(xiàn)自動(dòng)化Excel報(bào)表的步驟
這篇文章主要介紹了Python 實(shí)現(xiàn)自動(dòng)化Excel報(bào)表的步驟,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
python將Dataframe格式的數(shù)據(jù)寫入opengauss數(shù)據(jù)庫并查詢
這篇文章主要介紹了python將Dataframe格式的數(shù)據(jù)寫入opengauss數(shù)據(jù)庫并查詢,文章介紹詳細(xì)具有一定的參考價(jià)值,希望對(duì)你的學(xué)習(xí)有所幫助2022-04-04

