解決90%的常見(jiàn)問(wèn)題的8個(gè)python NumPy函數(shù)
NumPy
NumPy是一個(gè)用于科學(xué)計(jì)算和數(shù)據(jù)分析的Python庫(kù),也是機(jī)器學(xué)習(xí)的支柱??梢哉f(shuō)NumPy奠定了Python在機(jī)器學(xué)習(xí)中的地位。NumPy提供了一個(gè)強(qiáng)大的多維數(shù)組對(duì)象,以及廣泛的數(shù)學(xué)函數(shù),可以對(duì)大型數(shù)據(jù)集進(jìn)行有效的操作。這里的“大”是指數(shù)百萬(wàn)行。
Numpy快速而高效的原因是底層的C代碼,這比使用Python進(jìn)行數(shù)組的操作要快上幾百倍,并且隨著數(shù)據(jù)量級(jí)的上升而上升。
本文中整理了一些可以解決常見(jiàn)問(wèn)題的主要的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ù)組中無(wú)效的(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中元組的基礎(chǔ)介紹及常用操作總結(jié)
元組是一種不可變序列。元組變量的賦值要在定義時(shí)就進(jìn)行,這就像C語(yǔ)言中的const變量或是C++的引用,定義時(shí)賦值之后就不允許有修改。元組存在的意義是:元組在映射中可以作為鍵使用,因?yàn)橐WC鍵的不變性。元組作為很多內(nèi)置函數(shù)和方法的返回值存在2021-09-09機(jī)器學(xué)習(xí)之?dāng)?shù)據(jù)清洗及六種缺值處理方式小結(jié)
本文主要介紹了機(jī)器學(xué)習(xí)之?dāng)?shù)據(jù)清洗及六種缺值處理方式小結(jié),包括刪除空行、填充平均值、中位數(shù)、眾數(shù)、線性插值和隨機(jī)森林填充,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03淺析python表達(dá)式4+0.5值的數(shù)據(jù)類型
在本篇文章里小編給大家整理的是一篇關(guān)于python表達(dá)式4+0.5值的數(shù)據(jù)類型的知識(shí)點(diǎn)內(nèi)容,需要的的朋友們學(xué)習(xí)下。2020-02-02pytest實(shí)現(xiàn)多種調(diào)用方式
pytest是一個(gè)非常成熟的全功能的Python測(cè)試框架,本文主要介紹了pytest多種調(diào)用方式,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12Python爬蟲(chóng)實(shí)戰(zhàn)之12306搶票開(kāi)源
今天小編就為大家分享一篇關(guān)于Python爬蟲(chóng)實(shí)戰(zhàn)之12306搶票開(kāi)源,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01python寫一個(gè)隨機(jī)點(diǎn)名軟件的實(shí)例
今天小編就為大家分享一篇python寫一個(gè)隨機(jī)點(diǎn)名軟件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11利用Python中的pandas庫(kù)對(duì)cdn日志進(jìn)行分析詳解
這篇文章主要介紹了利用Python中的pandas庫(kù)進(jìn)行cdn日志分析的相關(guān)資料,文中分享了pandas對(duì)cdn日志分析的完整示例代碼,然后詳細(xì)介紹了關(guān)于pandas庫(kù)的相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-03-03python實(shí)現(xiàn)翻譯word表格小程序
這篇文章主要為大家詳細(xì)介紹了python翻譯word表格小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02如何利用Opencv實(shí)現(xiàn)圖像的加密解密
一般情況下,圖像的加密和解密過(guò)程是通過(guò)按位異或運(yùn)算實(shí)現(xiàn)的,下面這篇文章主要給大家介紹了關(guān)于如何利用Opencv實(shí)現(xiàn)圖像加密解密的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10