自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

這八個NumPy函數(shù)可以解決90%的常見問題

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

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

Numpy快速而高效的原因是底層的C代碼,這比使用Python進(jìn)行數(shù)組的操作要快上幾百倍,并且隨著數(shù)據(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)建一個以零填充的數(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)建一個都是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:返回一個表示數(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ù)組的維度。它返回一個軸調(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:將多個數(shù)組垂直堆疊以創(chuàng)建一個新數(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ì)算元素的絕對值。

numpy.exp:計(jì)算所有元素的指數(shù)。

numpy.subtract:對兩個數(shù)組的對應(yīng)元素進(jìn)行減法運(yùn)算。

numpy.multiply:對兩個數(shù)組的對應(yīng)元素進(jìn)行乘法運(yùn)算。

numpy.divide:對兩個數(shù)組的對應(yīng)元素進(jìn)行除法運(yùn)算。

numpy.sin:計(jì)算數(shù)組中每個元素的正弦值。

numpy.cos:計(jì)算數(shù)組中每個元素的余弦值。

numpy.log:計(jì)算數(shù)組中每個元素的自然對數(shù)(以e為底的對數(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個百分位數(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ì)算兩個數(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ì)算兩個數(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ì)算一個方陣的逆, numpy.linalg.eig:一個方陣的特征值和特征向量。numpy.linalg.solve:求解一個線性方程組。

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、其他一些高級的函數(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:供對掩碼數(shù)組的支持。

  • numpy.ma.array:從現(xiàn)有的數(shù)組或序列創(chuàng)建一個掩碼數(shù)組。
  • numpy.ma.masked_array:從現(xiàn)有數(shù)組和掩碼中創(chuàng)建一個掩碼數(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:一個條件函數(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ù),希望對你有所幫助。

責(zé)任編輯:華軒 來源: DeepHub IMBA
相關(guān)推薦

2010-08-30 11:24:43

2019-10-18 15:16:10

Redis數(shù)據(jù)庫并發(fā)

2024-05-13 18:33:08

SQL日期函數(shù)

2011-02-22 14:00:16

vsftpd

2021-11-19 10:40:14

物聯(lián)網(wǎng)物聯(lián)網(wǎng)安全IoT

2024-11-07 15:55:22

PyTorchNumPyPython

2025-02-17 12:10:00

前端移動端適配開發(fā)

2025-02-19 16:00:00

前端開發(fā)移動端適配

2016-09-09 13:25:01

Linux

2017-03-30 11:20:59

云存儲服務(wù)供應(yīng)商

2025-03-17 00:33:00

2020-05-25 22:41:27

LoRaWAN物聯(lián)網(wǎng)技術(shù)物聯(lián)網(wǎng)

2022-10-10 09:00:35

ReactJSX組件

2009-08-24 10:37:11

Silverlight

2009-07-17 10:01:14

Swing和AWT

2015-09-06 16:27:34

PHP程序員級別

2024-08-22 14:49:49

系統(tǒng)設(shè)計(jì)數(shù)據(jù)庫

2014-06-17 09:51:57

Docker

2021-01-11 08:30:02

Dubbo服務(wù)

2010-05-17 10:01:09

MySql字符集
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號