numpy

介紹

NumPy 是一個 Python 包。 它代表 “Numeric Python”批销。 它是一個由多維數(shù)組對象和用于處理數(shù)組的例程集合組成的庫。
numpy手冊
NumPy 用戶指南
NumPy 參考資料
Scipy 講座
numpy教程

安裝與導(dǎo)入

pip install numpy
conda install numpy
為了跟課程匹配,怕有試驗時的版本差異金砍,課程中有要求numpy版本為1.13址否,
conda install numpy=1.13
使用前需要再py文件中導(dǎo)入庫:
import numpy as np

Ndarry對象

NumPy 中定義的最重要的對象是稱為 ndarray 的 N 維數(shù)組類型餐蔬。 它描述相同類型的元素集合。 可以使用基于零的索引訪問集合中的項目佑附。
ndarray 中的每個元素在內(nèi)存中使用相同大小的塊樊诺。 ndarray 中的每個元素是數(shù)據(jù)類型對象的對象(稱為 dtype )。
從 ndarray 對象提取的任何元素(通過切片)由一個數(shù)組標(biāo)量類型的 Python 對象表示音同。 下圖顯示了 ndarray 词爬,數(shù)據(jù)類型對象( dtype )和數(shù)組標(biāo)量類型之間的關(guān)系。


ndarray

ndarray 類的實例可以通過本教程后面描述的不同的數(shù)組創(chuàng)建例程來構(gòu)造权均。 基本的 ndarray 是使用 NumPy 中的數(shù)組函數(shù)創(chuàng)建的顿膨,如下所示:
numpy.array
numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)

上面的構(gòu)造器接受以下參數(shù):
NumPy - Ndarray 對象

序號 參數(shù)及描述
1. object 一個數(shù)組或任何(嵌套)序列。
2. dtype 數(shù)組的所需數(shù)據(jù)類型叽赊,可選恋沃。
3. copy 可選,默認為 true 必指,對象是否被復(fù)制囊咏。
4. order C (按行)、 F (按列)或 A (任意取劫,默認)匆笤。
5. subok 默認情況下,返回的數(shù)組被強制為基類數(shù)組谱邪。 如果為 true 炮捧,則返回子類。
6. ndimin 指定返回數(shù)組的最小維數(shù)惦银。

Examples

>>> np.array([1, 2, 3])
array([1, 2, 3])

>>> np.array([1, 2, 3.0])
array([ 1.,  2.,  3.])

>>> np.array([[1, 2], [3, 4]])
array([[1, 2],
       [3, 4]])

#最小維度
>>> np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])

#設(shè)置dtype
>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j,  2.+0.j,  3.+0.j])

#大于號小于號表示大小端存儲咆课,小于號是小端存儲
>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
>>> x['a']
array([1, 3])

>>> np.array(np.mat('1 2; 3 4'))
array([[1, 2],
       [3, 4]])

>>> np.array(np.mat('1 2; 3 4'), subok=True)
matrix([[1, 2],
        [3, 4]])
“”“
 Numpy matrices必須是2維的,但是 numpy arrays (ndarrays) 可以是多維的(1D末誓,2D,3D····ND). Matrix是Array的一個小的分支书蚪,包含于Array喇澡。所以matrix 擁有array的所有特性。
在numpy中matrix的主要優(yōu)勢是:相對簡單的乘法運算符號殊校。例如晴玖,a和b是兩個matrices,那么a*b为流,就是矩陣積呕屎。
”“”

NumPy - 數(shù)據(jù)類型

首先可以學(xué)習(xí)下自定義數(shù)據(jù)類型
arrays的每個項都必須是同一類型的敬察,那么如果輸入了不同類型的數(shù)據(jù)秀睛,就會轉(zhuǎn)換成同一類型,類型具體如下:

類型.png

Numpy- shape,zeros,ones,eye,full,diag莲祸,unique

首先來說簡單的:
首先引入概念蹂安,秩,我們將一維數(shù)組稱之為秩為 1 的數(shù)組锐帜。通常田盈,N 維數(shù)組的秩為 N。因此抹估,二維數(shù)組稱為秩為 2 的數(shù)組缠黍。數(shù)組的另一個重要特性是形狀。數(shù)組的形狀是指每個維度的大小药蜻。例如瓷式,秩為 2 的數(shù)組的形狀對應(yīng)于數(shù)組的行數(shù)和列數(shù)。

zeros
用法:zeros(shape, dtype=float, order='C')
返回:返回來一個給定形狀和類型的用0填充的數(shù)組语泽;
注意np.zeros() 函數(shù)默認地創(chuàng)建一個 dtype 為 float64 的數(shù)組贸典。

例子:
>>> np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])
>>> np.zeros((5,), dtype=np.int)
array([0, 0, 0, 0, 0])
>>> np.zeros((2, 1))
array([[ 0.],
       [ 0.]])
>>> s = (2,2)
>>> np.zeros(s)
array([[ 0.,  0.],
       [ 0.,  0.]])
>>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
array([(0, 0), (0, 0)],
       dtype=[('x', '<i4'), ('y', '<i4')])

ones
用法:ones(shape, dtype=None, order='C')
返回:返回來一個給定形狀和類型的用1填充的數(shù)組;
跟zeros其實很像的踱卵,區(qū)別只是0換成了1而已廊驼,注意np.ones() 函數(shù)默認地創(chuàng)建一個 dtype 為 float64 的數(shù)組。

>>>np.ones(5)
array([ 1.,  1.,  1.,  1.,  1.])
>>> np.ones((5,), dtype=np.int)
array([1, 1, 1, 1, 1])
>>> np.ones((2, 1))
array([[ 1.],
       [ 1.]])
>>> s = (2,2)
>>> np.ones(s)
array([[ 1.,  1.],
      [ 1.,  1.]])

full
用法:full(shape, fill_value, dtype=None, order='C'):
返回:返回一個給定形狀惋砂,類型和填充值的數(shù)組

# We create a 2 x 3 ndarray full of fives. 
X = np.full((2,3), 5) 

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)

#輸出
X =
[[5 5 5]
 [5 5 5]]

X has dimensions: (2, 3)
X is an object of type: class 'numpy.ndarray'
The elements in X are of type: int64

eye
用法:eye(N, M=None, k=0, dtype=<class 'float'>)
N是行妒挎,M是列,k是對角線的位置西饵,0的時候是正對角線酝掩,+1就是對角線向上移,-1就是對角線向下移
返回:返回一個類似單位矩陣的東西眷柔。

>>> np.eye(2, dtype=int)
array([[1, 0],
      [0, 1]])
>>> np.eye(3, k=1)
array([[ 0.,  1.,  0.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  0.]])

diag
用法:diag(v, k=0)
v : array_like
If v is a 2-D array, return a copy of its k-th diagonal.
If v is a 1-D array, return a 2-D array with v on the k-th
diagonal.
k : int, optional
Diagonal in question. The default is 0. Use k>0 for diagonals
above the main diagonal, and k<0 for diagonals below the main
diagonal.
v和k的理解看下面的例子期虾,k和eye里的k作用其實是一樣的原朝。

返回:一個對角矩陣。對角矩陣是僅在主對角線上有值的方形矩陣镶苞。
np.diag() 函數(shù)會創(chuàng)建一個對應(yīng)于對角矩陣的 ndarray

# Create a 4 x 4 diagonal matrix that contains the numbers 10,20,30, and 50
# on its main diagonal
X = np.diag([10,20,30,50])

# We print X
print()
print('X = \n', X)
print()
#輸出:
X =
[[10 0 0 0]
 [ 0 20 0 0]
 [ 0 0 30 0]
 [ 0 0 0 50]]

>>> x = np.arange(9).reshape((3,3))
>>> x
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> np.diag(x)
array([0, 4, 8])
>>> np.diag(x, k=1)
array([1, 5])
>>> np.diag(x, k=-1)
array([3, 7])
>>> np.diag(np.diag(x))
array([[0, 0, 0],
      [0, 4, 0],
      [0, 0, 8]])

unique
可以理解為返回array的set值
從 ndarray 中提取唯一的元素喳坠。我們可以使用 np.unique() 函數(shù)查找 ndarray 中的唯一元素。np.unique(ndarray) 函數(shù)會返回給定 ndarray 中的 唯一元素

# Create 3 x 3 ndarray with repeated values
X = np.array([[1,2,3],[5,2,8],[1,2,3]])
# We print X
print('X = \n', X)
# We print the unique elements of X 
print('The unique elements in X are:',np.unique(X))
#輸出:
X =
[[1 2 3]
 [5 2 8]
 [1 2 3]]

The unique elements in X are: [1 2 3 5 8]

shape
shape函數(shù)功能是讀取矩陣的長度茂蚓,比如shape[0]就是讀取矩陣第一維度的長度壕鹉。
shape的輸入?yún)?shù)可以是一個整數(shù)(表示維度),也可以是一個矩陣聋涨。

>>>a = np.array([[2,2,2],[3,3,3]])  
>>> print(a)  
[[ 2  2  2 ]  
  [ 3  3  3 ]]  
>>> a.shape  
(2, 3)

總結(jié):采用np.array()創(chuàng)建時需要幾個維度就要用幾個[ ]括起來御板,這種創(chuàng)建方式要給定數(shù)據(jù);采用np.ones()或np.zeros()創(chuàng)建分別產(chǎn)生全1或全0的數(shù)據(jù)牛郑,用a.shape會輸出你創(chuàng)建時的輸入,shape的返回值是一個元組敬鬓,里面每個數(shù)字表示每一維的長度

Numpy- arrange淹朋,linspace,reshape/resize钉答,random

arrange
其實跟我們常用的range很像啊
arange([start,] stop[, step,], dtype=None)
其中包含位于半開區(qū)間 [start, stop) 內(nèi)并均勻分布的值础芍。也就是說,均勻分布的數(shù)字將包括 start 數(shù)字数尿,但是不包括 stop 數(shù)字仑性,根據(jù)start與stop指定的范圍以及step設(shè)定的步長,生成一個 ndarray右蹦。
step可以不是整數(shù)诊杆,但是精度上不是很好,所以需要引入linspace

>>> np.arange(3)  
array([0, 1, 2])  
>>> np.arange(3.0)  
array([ 0.,  1.,  2.])  
>>> np.arange(3,7)  
array([3, 4, 5, 6])  
>>> np.arange(3,7,2)  
array([3, 5]) 

linspace
用法: linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):
np.linspace(start, stop, N) 函數(shù)返回 N 個在閉區(qū)間 [start, stop] 內(nèi)均勻分布的數(shù)字何陆。即 start 和 stop 值都包括在內(nèi)晨汹。此外注意,start, stop是必須參數(shù)。
endpoint是說是否包含stop的值贷盲,默認是包含的淘这。
restep如果是true,返回(samples, step)巩剖。

>>> np.linspace(2.0, 3.0, num=5)
array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ])
>>> np.linspace(2.0, 3.0, num=5, endpoint=False)
array([ 2. ,  2.2,  2.4,  2.6,  2.8])
>>> np.linspace(2.0, 3.0, num=5, retstep=True)
(array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)

reshape/resize
用法:reshape(a, newshape, order='C')
resize使用方法也一樣
newshape:新形狀應(yīng)與原始形狀兼容铝穷。 如果是整數(shù),那么結(jié)果將是該長度的一維數(shù)組佳魔。 一個形狀尺寸可以是-1曙聂。 在這種情況下,該值是從數(shù)組長度和剩余維度推斷出來的吃引。

-1的用法
reshape和resize區(qū)別

  • reshape:有返回值筹陵,所謂有返回值刽锤,即不對原始多維數(shù)組進行修改;
  • resize:無返回值朦佩,所謂有返回值并思,即會對原始多維數(shù)組進行修改;
>>> a = np.arange(6).reshape((3, 2))
>>> a
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> a = np.array([[1,2,3], [4,5,6]])
#變?yōu)橐痪S數(shù)組
>>> np.reshape(a, 6)
array([1, 2, 3, 4, 5, 6])
>>> np.reshape(a, 6, order='F')
array([1, 4, 2, 5, 3, 6])
#不知道變成幾列,但是行數(shù)固定為3行
>>> np.reshape(a, (3,-1))       # the unspecified value is inferred to be 2
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> np.resize(a,(3,-1))
>>> print(a)
array([[1, 2],
       [3, 4],
       [5, 6]])

random

  • numpy.random.random() 生成隨機浮點數(shù),默認為生成一個隨機的浮點數(shù)语稠,范圍是在0.0~1.0之間宋彼,也可以通過參數(shù)size設(shè)置返回數(shù)據(jù)的size;
    注意是半開區(qū)間 [0.0, 1.0) 內(nèi)的隨機浮點數(shù)
# We create a 3 x 3 ndarray with random floats in the half-open interval [0.0, 1.0).
X = np.random.random((3,3))

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in x are of type:', X.dtype)

#輸出
X =
[[ 0.12379926 0.52943854 0.3443525 ]
 [ 0.11169547 0.82123909 0.52864397]
 [ 0.58244133 0.21980803 0.69026858]]

X has dimensions: (3, 3)
X is an object of type: class 'numpy.ndarray' The elements in X are of type: float64
  • np.random.randint(start, stop, size = shape) 會創(chuàng)建一個具有給定形狀的 ndarray,其中包含在半開區(qū)間 [start, stop) 內(nèi)的隨機整數(shù)仙畦。
# We create a 3 x 2 ndarray with random integers in the half-open interval [4, 15).
X = np.random.randint(4,15,size=(3,2))

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)

#輸出
X =
[[ 7 11]
 [ 9 11]
 [ 6 7]]

X has dimensions: (3, 2)
X is an object of type: class 'numpy.ndarray' The elements in X are of type: int64
  • np.random.normal(mean, standard deviation, size=shape)會創(chuàng)建一個具有給定形狀的 ndarray输涕,其中包含從正態(tài)高斯分布(具有給定均值和標(biāo)準(zhǔn)差)中抽樣的隨機數(shù)字。我們來創(chuàng)建一個 1,000 x 1,000 ndarray慨畸,其中包含從正態(tài)分布(均值為 0莱坎,標(biāo)準(zhǔn)差為 0.1)中隨機抽樣的浮點數(shù)。
# We create a 1000 x 1000 ndarray of random floats drawn from normal (Gaussian) distribution
# with a mean of zero and a standard deviation of 0.1.
X = np.random.normal(0, 0.1, size=(1000,1000))

# We print X
print()
print('X = \n', X)
print()

# We print information about X
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)
print('The elements in X have a mean of:', X.mean())
print('The maximum value in X is:', X.max())
print('The minimum value in X is:', X.min())
print('X has', (X < 0).sum(), 'negative numbers')
print('X has', (X > 0).sum(), 'positive numbers')

#輸出
X =
[[ 0.04218614 0.03247225 -0.02936003 ..., 0.01586796 -0.05599115 -0.03630946]
 [ 0.13879995 -0.01583122 -0.16599967 ..., 0.01859617 -0.08241612 0.09684025]
 [ 0.14422252 -0.11635985 -0.04550231 ..., -0.09748604 -0.09350044 0.02514799]
 ...,
 [-0.10472516 -0.04643974 0.08856722 ..., -0.02096011 -0.02946155 0.12930844]
 [-0.26596955 0.0829783 0.11032549 ..., -0.14492074 -0.00113646 -0.03566034]
 [-0.12044482 0.20355356 0.13637195 ..., 0.06047196 -0.04170031 -0.04957684]]

X has dimensions: (1000, 1000)
X is an object of type: class 'numpy.ndarray' The elements in X are of type: float64
The elements in X have a mean of: -0.000121576684405
The maximum value in X is: 0.476673923106
The minimum value in X is: -0.499114224706 X 具有 500562 個負數(shù) X 具有 499438 個正數(shù)

ndarray 中的隨機數(shù)字的平均值接近 0寸士,X 中的最大值和最小值與 0(平均值)保持對稱檐什,正數(shù)和負數(shù)的數(shù)量很接近。

  • np.random.permutation()
    輸入一個數(shù)N或者數(shù)組弱卡,生成一個隨機序列(如果是數(shù)的話生成的內(nèi)容是[1, N))乃正,對多維數(shù)組來說是多維隨機打亂而不是1維
>>np.random.permutation([1, 4, 9, 12, 15])
array([15,  1,  9,  4, 12])

>>arr = np.arange(9).reshape((3, 3))
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>np.random.permutation(arr)
array([[6, 7, 8],
       [0, 1, 2],
       [3, 4, 5]]) 

>>permutation = list(np.random.permutation(10))
[5, 1, 7, 6, 8, 9, 4, 0, 2, 3]
>>Y = np.array([[1,1,1,1,0,0,0,0,0,0]])
>>Y_new = Y[:, permutation]
array([[0, 1, 0, 0, 0, 0, 0, 1, 1, 1]])
  • np.random.shuffle()
    shuffle與permutatio作用相似,他們的區(qū)別是:
    函數(shù)shuffle與permutation都是對原來的數(shù)組進行重新洗牌(即隨機打亂原來的元素順序)婶博;區(qū)別在于shuffle直接在原來的數(shù)組上進行操作瓮具,改變原來數(shù)組的順序,無返回值凡人。而permutation不直接在原來的數(shù)組上進行操作名党,而是返回一個新的打亂順序的數(shù)組,并不改變原來的數(shù)組划栓。

Numpy- 讀取兑巾,修改,添加忠荞,刪除蒋歌,堆疊,切片

讀取委煤,修改
array的讀取堂油,修改操作和列表非常的類似--通過索引,但是多維的情況和列表會有些區(qū)別碧绞,列表的話[0][0]來讀取第一行第一列的元素,而array是用[0,0]來讀雀颉(格式為 [row, column])。
那么修改就是正常的賦值手段 ‘=’ 讥邻。

# We create a 3 x 3 rank 2 ndarray that contains integers from 1 to 9
X = np.array([[1,2,3],[4,5,6],[7,8,9]])

# We print X
print()
print('X = \n', X)
print()

# Let's access some elements in X
print('This is (0,0) Element in X:', X[0,0])
print('This is (0,1) Element in X:', X[0,1])
print('This is (2,2) Element in X:', X[2,2])

#輸出
X =
[[1 2 3]
 [4 5 6]
 [7 8 9]]

This is (0,0) Element in X: 1
This is (0,1) Element in X: 2
This is (2,2) Element in X: 9

刪除
np.delete(ndarray, elements, axis=None)
此函數(shù)會沿著指定的軸從給定 ndarray 中刪除給定的元素列表迫靖。對于秩為 1 的 ndarray院峡,不需要使用關(guān)鍵字 axis。對于秩為 2 的 ndarray系宜,axis = 0 表示選擇行照激,axis = 1 表示選擇列。

# We create a rank 1 ndarray 
x = np.array([1, 2, 3, 4, 5])
# We create a rank 2 ndarray
Y = np.array([[1,2,3],[4,5,6],[7,8,9]])
# We print x
print('Original x = ', x)
# We delete the first and last element of x
x = np.delete(x, [0,4])
# We print x with the first and last element deleted
print('Modified x = ', x)
# We print Y
print('Original Y = \n', Y)
# We delete the first row of y
w = np.delete(Y, 0, axis=0)
# We delete the first and last column of y
v = np.delete(Y, [0,2], axis=1)
# We print w
print('w = \n', w)
# We print v
print('v = \n', v)

#輸出
Original x = [1 2 3 4 5]
Modified x = [2 3 4]
Original Y =
[[1 2 3]
 [4 5 6]
 [7 8 9]]
w =
[[4 5 6]
 [7 8 9]]
v =
[[2]
 [5]
 [8]]

添加
np.append(ndarray, elements, axis)
大致上跟列表追加一樣盹牧。
函數(shù)向 ndarray 中附加值俩垃。該函數(shù)會將給定的元素列表沿著指定的軸附加到 ndarray 中,對于秩為 1 的 ndarray汰寓,不需要使用關(guān)鍵字 axis口柳。對于秩為 2 的 ndarray,axis = 0 表示選擇行有滑,axis = 1 表示選擇列跃闹。

# We create a rank 1 ndarray 
x = np.array([1, 2, 3, 4, 5])
# We create a rank 2 ndarray 
Y = np.array([[1,2,3],[4,5,6]])
# We print x
print('Original x = ', x)
# We append the integer 6 to x
x = np.append(x, 6)
# We print x
print('x = ', x)
# We append the integer 7 and 8 to x
x = np.append(x, [7,8])
# We print x
print('x = ', x)
# We print Y
print('Original Y = \n', Y)
# We append a new row containing 7,8,9 to y
v = np.append(Y, [[7,8,9]], axis=0)
# We append a new column containing 9 and 10 to y
q = np.append(Y,[[9],[10]], axis=1)
# We print v
print('v = \n', v)
# We print q
print('q = \n', q)
Original x = [1 2 3 4 5]
#輸出
x = [1 2 3 4 5 6]
x = [1 2 3 4 5 6 7 8]
Original Y =
[[1 2 3]
 [4 5 6]]
v =
[[1 2 3]
 [4 5 6]
 [7 8 9]]
q =
[[ 1 2 3 9]
 [ 4 5 6 10]]

np.insert(ndarray, index, elements, axis)
向 ndarray 中插入值。此函數(shù)會將給定的元素列表沿著指定的軸插入到 ndarray 中毛好,并放在給定的索引前面辣卒。對于秩為 1 的 ndarray,不需要使用關(guān)鍵字 axis睛榄。對于秩為 2 的 ndarray,axis = 0 表示選擇行想帅,axis = 1 表示選擇列场靴。

# We create a rank 1 ndarray 
x = np.array([1, 2, 5, 6, 7])
# We create a rank 2 ndarray 
Y = np.array([[1,2,3],[7,8,9]])
# We print x
print('Original x = ', x)
# We insert the integer 3 and 4 between 2 and 5 in x. 
x = np.insert(x,2,[3,4])
# We print x with the inserted elements
print('x = ', x)
# We print Y
print('Original Y = \n', Y)
# We insert a row between the first and last row of y
w = np.insert(Y,1,[4,5,6],axis=0)
# We insert a column full of 5s between the first and second column of y
v = np.insert(Y,1,5, axis=1)
# We print w
print('w = \n', w)
# We print v
print('v = \n', v)
#輸出
Original x = [1 2 5 6 7]
x = [1 2 3 4 5 6 7]
Original Y =
[[1 2 3]
 [7 8 9]]
w =
[[1 2 3]
 [4 5 6]
 [7 8 9]]
v =
[[1 5 2 3]
 [7 5 8 9]]

堆疊
Numpy中stack(),hstack()港准,vstack()旨剥,這三個函數(shù)有些相似性,都是堆疊數(shù)組浅缸,里面最難理解的應(yīng)該就是stack()函數(shù)了轨帜。

  • stack()函數(shù)
    函數(shù)原型為:stack(arrays, axis=0),arrays可以傳數(shù)組和列表衩椒。
import numpy as np
a=[[1,2,3],
   [4,5,6]]
print("列表a如下:")
print(a)

print("增加一維蚌父,新維度的下標(biāo)為0")
c=np.stack(a,axis=0)
print(c)

print("增加一維,新維度的下標(biāo)為1")
c=np.stack(a,axis=1)
print(c)

輸出:
列表a如下:
[[1, 2, 3], [4, 5, 6]]
增加一維毛萌,新維度下標(biāo)為0
[[1 2 3]
 [4 5 6]]
增加一維苟弛,新維度下標(biāo)為1
[[1 4]
 [2 5]
 [3 6]]

首先這里arrays我傳的是一個列表,現(xiàn)在我開始講解這個stack()函數(shù)的意思阁将,它就是對arrays里面的每個元素(可能是個列表膏秫,元組,或者是個numpy的數(shù)組)變成numpy的數(shù)組后做盅,再對每個元素增加一維(至于維度加在哪里缤削,是靠axis控制的)窘哈,然后再把這些元素串起來(至于怎么串,我下面會說)亭敢。

arrays里面的每個元素必須形狀是一樣的滚婉,例如本例中列表a中的兩個元素[1,2,3]和[4,5,6]的形狀是一樣的,如果把[4,5,6]換成[4,5] 吨拗,那么程序會報錯满哪!而axis代表的是在哪個維度上加一維,例如axis=0(它是默認的)代表的就是增加的這一維的下標(biāo)為0劝篷,axis等于多少不是隨便亂寫的哨鸭,如果參數(shù)arrays里面的每個元素是個1維的,那么調(diào)用stack()函數(shù)增加一維后會變成2維的娇妓,所以axis只能等于0和1(維度的下標(biāo)是從0開始的)像鸡,而參數(shù)axis=0和axis=1得到的結(jié)果是不一樣的。

例如上面的代碼中a列表中的第一個元素為[1,2,3]哈恰,那么當(dāng)axis=0的時候只估,就是在它的中括號外面再加一個中括號,變成[ [1,2,3] ](其實1着绷,2蛔钙,3之間是沒有逗號的,因為stack()函數(shù)會先把參數(shù)arrays中的每個元素變成numpy的數(shù)組荠医,數(shù)組之間是沒有逗號的吁脱,看看上面的代碼輸出就知道了,這里大家明白就行彬向,我為了方便講解兼贡,下面還會加上逗號),這樣最外面那層中括號才代表維度下標(biāo)為0的那維娃胆;當(dāng)axis=1的時候遍希,就是在里面加個中括號,變成了[ [1]里烦,[2]凿蒜,[3] ],這樣里面加的那層中括號才代表維度下標(biāo)為1的那維。同理當(dāng)axis=0的時候[4,5,6]變成[ [ 4胁黑,5篙程,6] ],當(dāng)axis=1的時候别厘,變成[ [4]虱饿,[5],[6] ]。下面我們講如何把增加一維度后的每個元素串起來氮发。

怎么把上面那兩個元素增加維度后的結(jié)果串起來呢渴肉,其實很簡單。現(xiàn)在我們已經(jīng)知道了增加維度無非是增加中括號的意思爽冕,至于在哪里加中括號仇祭,取決于axis等于幾。我們把增加的中括號想像成一個個的箱子颈畸。還拿上面的代碼來說乌奇,當(dāng)axis=0的時候,我們把套在[1,2,3]外面的中括號(就是[ [1,2,3] ]最外層的那個中括號)看做是箱子A眯娱,這個箱子A也會套在[4,5,6]的外面礁苗,所以我們就先把[1,2,3]和[4,5,6]放在一起,變成[1,2,3],[4,5,6]徙缴,然后再一起套上箱子A试伙,變成[ [1,2,3],[4,5,6] ]這就是當(dāng)axis=0的時候程序的輸出結(jié)果。

現(xiàn)在再來看當(dāng)axis=1的時候于样,對于[1疏叨,2,3]穿剖,我們把套在1外面的箱子(就是上面講的[ [1]蚤蔓,[2],[3] ]中1外面的那層中括號)看做A糊余,套在2外面的看做B昌粤,套在3外面的看做C,同理啄刹,箱子A也會套在4的外面,箱子B也會套在5的外面酒奶,箱子C也會套在6的外面记焊。那么我們就把1和4放一起捆毫,2和5放一起,3和6放一起昵时,變成[ 1,4 ,2,5 椒丧,3,6 ]然后把箱子A,B,C分別套在1,4 壹甥, 2,5 , 3,6的外面壶熏,變成[ [1,4] , [2,5] , [3,6] ]這就是程序中axis=1的時候程序的輸出結(jié)果句柠。

大家發(fā)現(xiàn)了沒有,串起來的時候其實就是把arrays中每個元素在相同的位置套箱子的一些小塊(這里叫小塊這個名詞可能不洽當(dāng),但是大家明白就行)放在一起后溯职,再套箱子精盅,就是外面套個中括號,這就是堆疊谜酒。

再看下面的代碼的輸出叹俏,測試下你理解的沒有。

import numpy as np
a=[[1,2,3,4],
   [5,6,7,8],
   [9,10,11,12]]
print("列表a如下:")
print(a)

print("增加一維僻族,新維度的下標(biāo)為0")
c=np.stack(a,axis=0)
print(c)

print("增加一維粘驰,新維度的下標(biāo)為1")
c=np.stack(a,axis=1)
print(c)

輸出:
列表a如下:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
增加一維,新維度的下標(biāo)為0
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
增加一維述么,新維度的下標(biāo)為1
[[ 1  5  9]
 [ 2  6 10]
 [ 3  7 11]
 [ 4  8 12]]

不知道和你想象的輸出一樣不一樣蝌数,還有另一種情況,先看下面的代碼碉输。

import numpy as np
a=[1,2,3,4]
b=[5,6,7,8]
c=[9,10,11,12]
print("a=",a)
print("b=",b)
print("c=",c)

print("增加一維籽前,新維度的下標(biāo)為0")
d=np.stack((a,b,c),axis=0)
print(d)

print("增加一維,新維度的下標(biāo)為1")
d=np.stack((a,b,c),axis=1)
print(d)

輸出:
('a=', [1, 2, 3, 4])
('b=', [5, 6, 7, 8])
('c=', [9, 10, 11, 12])
增加一維敷钾,新維度的下標(biāo)為0
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
增加一維枝哄,新維度的下標(biāo)為1
[[ 1  5  9]
 [ 2  6 10]
 [ 3  7 11]
 [ 4  8 12]]

你會發(fā)現(xiàn)輸出結(jié)果和上面的代碼一樣,其實它倆就是一樣的阻荒。只不過當(dāng)你對arrays傳參的時候挠锥,如果你傳的參數(shù)是類似于(a,b,c)這種,它會把(a,b,c)當(dāng)做一個元組來看侨赡,a,b,c都是元組的每個元素蓖租。然后分別對每個元素處理,上面我已經(jīng)說了羊壹,arrays傳的參數(shù)可以是列表蓖宦,元組,或者numpy數(shù)組油猫。所以傳(a,b,c)和傳[a,b,c]或者當(dāng)x=[a,b,c]的時候傳x稠茂,效果都是一樣的。

上面的代碼處理的arrays元素都是一維變二維的情況情妖,下面我們看看二維變?nèi)S是什么樣的睬关。

import numpy as np
a=[[1,2,3],
   [4,5,6]]
b=[[1,2,3],
   [4,5,6]]
c=[[1,2,3],
   [4,5,6]]
print("a=",a)
print("b=",b)
print("c=",c)

print("增加一維,新維度的下標(biāo)為0")
d=np.stack((a,b,c),axis=0)
print(d)

print("增加一維毡证,新維度的下標(biāo)為1")
d=np.stack((a,b,c),axis=1)
print(d)
print("增加一維电爹,新維度的下標(biāo)為2")
d=np.stack((a,b,c),axis=2)
print(d)

輸出:
('a=', [[1, 2, 3], [4, 5, 6]])
('b=', [[1, 2, 3], [4, 5, 6]])
('c=', [[1, 2, 3], [4, 5, 6]])
增加一維,新維度的下標(biāo)為0
[[[1 2 3]
  [4 5 6]]

 [[1 2 3]
  [4 5 6]]

 [[1 2 3]
  [4 5 6]]]
增加一維料睛,新維度的下標(biāo)為1
[[[1 2 3]
  [1 2 3]
  [1 2 3]]

 [[4 5 6]
  [4 5 6]
  [4 5 6]]]
增加一維丐箩,新維度的下標(biāo)為2
[[[1 1 1]
  [2 2 2]
  [3 3 3]]

 [[4 4 4]
  [5 5 5]
  [6 6 6]]]

當(dāng)axis=0的時候摇邦,列表a,b雏蛮,c最外面都需要套箱子(就是加中括號)涎嚼,那么我把你們先放一起,變成下面這樣

[[1,2,3],[4,5,6]],
[[1,2,3],[4,5,6]],
[[1,2,3],[4,5,6]]

然后在最外面套箱子挑秉,變成

[
[[1,2,3],[4,5,6]],
[[1,2,3],[4,5,6]],
[[1,2,3],[4,5,6]]
]

當(dāng)axis=1的時候法梯,列表a,b犀概,c中的[1,2,3]需要套同樣的箱子立哑,列表a,b姻灶,c中的[4,5,6]需要套同樣的箱子铛绰,好,我先把你們放一塊變成下面這樣

[
    [1,2,3],[1,2,3],[1,2,3]
    ,
    [4,5,6],[4,5,6],[4,5,6]

]

然后開始分別在 [1,2,3],[1,2,3],[1,2,3]的外面和[4,5,6],[4,5,6],[4,5,6]的外面套箱子产喉,變成下面這樣

[
    [[1,2,3],[1,2,3],[1,2,3]]
    ,
    [[4,5,6],[4,5,6],[4,5,6]]
]

當(dāng)axis=2的時候捂掰,列表a,b曾沈,c中的1这嚣,2,3塞俱,4姐帚,5,6都需要套箱子障涯,我把你們先放一起變成:

[
    [1,1,1  ,  2,2,2  , 3,3,3],
    [4,4,4  ,  5,5,5  , 6,6,6]
]

然后在1,1,1 ………6,6,6的外面分別套箱子變成:

[
    [[1,1,1]  ,  [2,2,2]  , [3,3,3]],
    [[4,4,4]  ,  [5,5,5]  , [6,6,6]]
]

關(guān)于stack()函數(shù)就講這么多罐旗,這也是我全部理解的部分。

  • hstack()函數(shù)
    函數(shù)原型:hstack(tup) 唯蝶,參數(shù)tup可以是元組九秀,列表,或者numpy數(shù)組粘我,返回結(jié)果為numpy的數(shù)組鼓蜒。這個是進行水平堆疊,看下面的代碼體會它的含義
import numpy as np
a=[1,2,3]
b=[4,5,6]
print(np.hstack((a,b)))

輸出:[1 2 3 4 5 6 ]

a=[[1],[2],[3]]
b=[[1],[2],[3]]
c=[[1],[2],[3]]
d=[[1],[2],[3]]
print(np.hstack((a,b,c,d)))

輸出:
[[1 1 1 1]
 [2 2 2 2]
 [3 3 3 3]]
  • vstack()函數(shù)
    函數(shù)原型:vstack(tup) 涂滴,參數(shù)tup可以是元組,列表晴音,或者numpy數(shù)組柔纵,返回結(jié)果為numpy的數(shù)組。這個是進行垂直堆疊锤躁,看下面的代碼體會它的含義
import numpy as np
a=[1,2,3]
b=[4,5,6]
print(np.vstack((a,b)))

輸出:
[[1 2 3]
 [4 5 6]]

import numpy as np
a=[[1],[2],[3]]
b=[[1],[2],[3]]
c=[[1],[2],[3]]
d=[[1],[2],[3]]
print(np.vstack((a,b,c,d)))

輸出:
[[1]
 [2]
 [3]
 [1]
 [2]
 [3]
 [1]
 [2]
 [3]
 [1]
 [2]
 [3]]

總結(jié)搁料,stack可以進行任意堆疊,np.vstack() 函數(shù)進行垂直堆疊,或使用 np.hstack() 函數(shù)進行水平堆疊郭计。請務(wù)必注意霸琴,為了堆疊 ndarray,ndarray 的形狀必須相符昭伸。

切片
其實理解了列表切片梧乘,ndarray的切片差不多,切片方式都是在方括號里用冒號 : 分隔起始和結(jié)束索引庐杨。
start和end同樣也是含頭不含尾选调,如果冒號兩遍空著的就代表最開頭和最結(jié)尾。
區(qū)別僅僅是注意維度上的切割灵份,這里就不再舉例仁堪。
另外注意,你切割沒有用冒號填渠,而是用一個整數(shù)弦聂,切片返回一個秩為 1 的 ndarray,而不是秩為 2 的 ndarray(其實列表也是這樣的呢氛什,直接索引和切片莺葫,得出來的即使是同一個索引的值,出來的效果不同屉更,切片出來的也是列表哦)徙融。舉個例子。

# We create a 4 x 5 ndarray that contains integers from 0 to 19
X = np.arange(20).reshape(4, 5)

# We print X
print('X = \n', X)
# We select all the elements in the 3rd row
v = X[2,:]
# We print v
print('v = ', v)
# We select all the elements in the 3rd column
q = X[:,2]
# We print q
print('q = ', q)
# We select all the elements in the 3rd column but return a rank 2 ndarray
R = X[:,2:3]
# We print R
print('R = \n', R)
#輸出
X =
[[ 0 1 2 3 4]
 [ 5 6 7 8 9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
v = [10 11 12 13 14]

q = [ 2 7 12 17]

R =
[[ 2]
 [ 7]
 [12]
 [17]]

注意例子中的區(qū)別瑰谜。

另外注意切片賦值是引用傳遞欺冀,傳遞的是地址,即萨脑,如果改變z的值隐轩,x的值也會發(fā)生改變,所以如果想切片生成新的array變量渤早,則需要使用到np.copy()函數(shù)职车,特別注意哦
正確使用的方法:

# We create a 4 x 5 ndarray that contains integers from 0 to 19
X = np.arange(20).reshape(4, 5)
# We print X
print('X = \n', X)
# create a copy of the slice using the np.copy() function
Z = np.copy(X[1:4,2:5])
#  create a copy of the slice using the copy as a method
W = X[1:4,2:5].copy()
# We change the last element in Z to 555
Z[2,2] = 555
# We change the last element in W to 444
W[2,2] = 444
# We print X
print('X = \n', X)
# We print Z
print('Z = \n', Z)
# We print W
print('W = \n', W)
X =
[[ 0 1 2 3 4]
 [ 5 6 7 8 9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
X =
[[ 0 1 2 3 4]
 [ 5 6 7 8 9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
Z =
[[ 7 8 9]
 [ 12 13 14]
 [ 17 18 555]]
W =
[[ 7 8 9]
 [ 12 13 14]
 [ 17 18 444]]

布爾型索引
中括號內(nèi)不再是冒號和start end這種實際指示的行列或下標(biāo),而是一個布爾表達式鹊杖,用來篩選滿足條件的項悴灵,非常好用。
可以用這種布爾型索引篩選讀取值骂蓖,也可以用來直接賦值积瞒。
舉例:

# We create a 5 x 5 ndarray that contains integers from 0 to 24
X = np.arange(25).reshape(5, 5)
print('Original X = \n', X)
print('The elements in X that lees than or equal to 7:', X[X <= 7])
X[(X > 10) & (X < 17)] = -1
# We print X
print('X = \n', X)
#輸出:
Original X =
[[ 0 1 2 3 4]
 [ 5 6 7 8 9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]
The elements in X that lees than or equal to 7: [0 1 2 3 4 5 6 7]
X =
[[ 0 1 2 3 4]
 [ 5 6 7 8 9]
 [10 -1 -1 -1 -1]
 [-1 -1 17 18 19]
 [20 21 22 23 24]]

集合運算和排序

排序函數(shù) 說明
np.sort( ndarray, axis) 排序,返回副本登下,axis跟之前用法一樣
np.unique(ndarray) 返回ndarray中的元素茫孔,排除重復(fù)元素之后叮喳,并進行排序
np.intersect1d( ndarray1, ndarray2) 返回二者的交集并排序。
np.union1d( ndarray1, ndarray2) 返回二者的并集并排序缰贝。
np.setdiff1d( ndarray1, ndarray2) 返回二者的差馍悟。
np.setxor1d( ndarray1, ndarray2) 返回二者的對稱差

課程中有例子,不再舉例剩晴。想看其他例子锣咒,可以看這里

另外sort的使用有兩種李破,一種作為函數(shù)使用宠哄,即np.sort(array),這樣的array自身是不會變的嗤攻,只是返回一個排序后的結(jié)果毛嫉,而如果一個array.sort()這樣當(dāng)方法使用的話,會在np原地進行排序妇菱。

原地不變:
# We create an unsorted rank 1 ndarray
x = np.random.randint(1,11,size=(10,))
# We print x
print('Original x = ', x)
# We sort x and print the sorted array using sort as a function.
print('Sorted x (out of place):', np.sort(x))
# When we sort out of place the original array remains intact. To see this we print x again
print('x after sorting:', x)
#輸出
Original x = [9 6 4 4 9 4 8 4 4 7]
Sorted x (out of place): [4 4 4 4 4 6 7 8 9 9]
x after sorting: [9 6 4 4 9 4 8 4 4 7]
原地變:
# We create an unsorted rank 1 ndarray
x = np.random.randint(1,11,size=(10,))
# We print x
print('Original x = ', x)
# We sort x and print the sorted array using sort as a method.
x.sort()
# When we sort in place the original array is changed to the sorted array. To see this we print x again
print('x after sorting:', x)
#輸出
Original x = [9 9 8 1 1 4 3 7 2 8]
x after sorting: [1 1 2 3 4 7 8 8 9 9]

算術(shù)運算
首先了解下numpy廣播規(guī)則
其實就是滿足這兩個條件
對兩個陣進行操作時承粤,NumPy逐元素地比較他們的形狀,從后面的維度向前執(zhí)行闯团。當(dāng)以下情形出現(xiàn)時辛臊,兩個維度是兼容的:
1,它們相等
2房交,其中一個是1
如果這些條件都沒有達到彻舰,將會拋出錯誤:frames are not aligned exception,表示兩個陣列形狀不兼容候味。結(jié)果陣列的尺寸與輸入陣列的各維度最大尺寸相同刃唤。
舉例子:

可以的:
A      (2d array):  5 x 4
B      (1d array):      1
Result (2d array):  5 x 4

A      (2d array):  5 x 4
B      (1d array):      4
Result (2d array):  5 x 4

A      (3d array):  15 x 3 x 5
B      (3d array):  15 x 1 x 5
Result (3d array):  15 x 3 x 5

A      (3d array):  15 x 3 x 5
B      (2d array):       3 x 5
Result (3d array):  15 x 3 x 5

A      (3d array):  15 x 3 x 5
B      (2d array):       3 x 1
Result (3d array):  15 x 3 x 5

A      (4d array):  8 x 1 x 6 x 1
B      (3d array):      7 x 1 x 5
Result (4d array):  8 x 7 x 6 x 5

不可以的:
A      (1d array):  3
B      (1d array):  4 # trailing dimensions do not match  #維度尺寸不兼容

A      (2d array):      2 x 1
B      (3d array):  8 x 4 x 3 # second from last dimensions mismatched #倒數(shù)第二個維度不兼容
# We create two rank 1 ndarrays
x = np.array([1,2,3,4])
y = np.array([5.5,6.5,7.5,8.5])
# We print x
print('x = ', x)
# We print y
print('y = ', y)
# We perfrom basic element-wise operations using arithmetic symbols and functions
print('x + y = ', x + y)
print('add(x,y) = ', np.add(x,y))
print('x - y = ', x - y)
print('subtract(x,y) = ', np.subtract(x,y))
print('x * y = ', x * y)
print('multiply(x,y) = ', np.multiply(x,y))
print('x / y = ', x / y)
print('divide(x,y) = ', np.divide(x,y))
#輸出
x = [1 2 3 4]
y = [ 5.5 6.5 7.5 8.5]
x + y = [ 6.5 8.5 10.5 12.5]
add(x,y) = [ 6.5 8.5 10.5 12.5]
x - y = [-4.5 -4.5 -4.5 -4.5]
subtract(x,y) = [-4.5 -4.5 -4.5 -4.5]
x * y = [ 5.5 13. 22.5 34. ]
multiply(x,y) = [ 5.5 13. 22.5 34. ]
x / y = [ 0.18181818 0.30769231 0.4 0.47058824]
divide(x,y) = [ 0.18181818 0.30769231 0.4 0.47058824]
  • numpy.reciprocal()
    此函數(shù)返回參數(shù)逐元素的倒數(shù)尚胞,。 由于 Python 處理整數(shù)除法的方式帜慢,對于絕對值大于 1 的整數(shù)元素笼裳,結(jié)果始終為 0, 對于整數(shù) 0粱玲,則發(fā)出溢出警告躬柬。
  • numpy.power()
    此函數(shù)將第一個輸入數(shù)組中的元素作為底數(shù),計算它與第二個輸入數(shù)組中相應(yīng)元素的冪抽减。
    例如np.power(a,2)
  • numpy.mod()
    此函數(shù)返回輸入數(shù)組中相應(yīng)元素的除法余數(shù)允青。 函數(shù)numpy.remainder()也產(chǎn)生相同的結(jié)果。
  • 三角函數(shù)https://www.yiibai.com/numpy/numpy_mathematical_functions.html
    不同角度的正弦值胯甩,可以通過乘 pi/180 轉(zhuǎn)化為弧度
    np.sin(anp.pi/180)
    數(shù)組中角度的余弦值:
    np.cos(a
    np.pi/180)
    '數(shù)組中角度的正切值:'
    np.tan(a*np.pi/180)
    arcsin昧廷,arccos,和arctan函數(shù)返回給定角度的sin偎箫,cos和tan的反三角函數(shù)木柬。 這些函數(shù)的結(jié)果可以通過numpy.degrees()函數(shù)通過將弧度制轉(zhuǎn)換為角度制來驗證。
  • numpy.around()
    這個函數(shù)返回四舍五入到所需精度的值淹办。 該函數(shù)接受以下參數(shù)眉枕。
    numpy.around(a,decimals)
    其中:
    序號 參數(shù)及描述1. a 輸入數(shù)組2. decimals 要舍入的小數(shù)位數(shù)。 默認值為0怜森。 如果為負速挑,整數(shù)將四舍五入到小數(shù)點左側(cè)的位置
  • numpy.floor()
    此函數(shù)返回不大于輸入?yún)?shù)的最大整數(shù)。 即標(biāo)量x 的下限是最大的整數(shù)i 副硅,使得i <= x姥宝。 注意在Python中,向下取整總是從 0 舍入恐疲。
  • numpy.ceil()
    ceil()函數(shù)返回輸入值的上限腊满,即,標(biāo)量x的上限是最小的整數(shù)i 培己,使得i> = x碳蛋。
  • 統(tǒng)計運算https://www.yiibai.com/numpy/numpy_statistical_functions.html
    numpy.amin() 和 numpy.amax()
    這些函數(shù)從給定數(shù)組中的元素沿指定軸返回最小值和最大值。
    numpy.median()
    中值定義為將數(shù)據(jù)樣本的上半部分與下半部分分開的值省咨。 numpy.median()函數(shù)的用法如下面的程序所示肃弟。
    numpy.mean()
    算術(shù)平均值是沿軸的元素的總和除以元素的數(shù)量。 numpy.mean()函數(shù)返回數(shù)組中元素的算術(shù)平均值零蓉。 如果提供了軸笤受,則沿其計算。
    numpy.average()
    加權(quán)平均值是由每個分量乘以反映其重要性的因子得到的平均值壁公。 numpy.average()函數(shù)根據(jù)在另一個數(shù)組中給出的各自的權(quán)重計算數(shù)組中元素的加權(quán)平均值感论。 該函數(shù)可以接受一個軸參數(shù)。 如果沒有指定軸紊册,則數(shù)組會被展開比肄。
    考慮數(shù)組[1,2,3,4]和相應(yīng)的權(quán)重[4,3,2,1],通過將相應(yīng)元素的乘積相加囊陡,并將和除以權(quán)重的和芳绩,來計算加權(quán)平均值。
    加權(quán)平均值 = (14+23+32+41)/(4+3+2+1)
    numpy.std()
    標(biāo)準(zhǔn)差是與均值的偏差的平方的平均值的平方根撞反。 標(biāo)準(zhǔn)差公式如下:
    std = sqrt(mean((x - x.mean())^2))
    如果數(shù)組是[1妥色,2,3遏片,4]嘹害,則其平均值為2.5撮竿。 因此,差的平方是[2.25,0.25,0.25,2.25]笔呀,并且其平均值的平方根除以4幢踏,即sqrt(5/4)是1.1180339887498949。
    np.var()
    方差是偏差的平方的平均值许师,即mean((x - x.mean())^ 2)房蝉。 換句話說,標(biāo)準(zhǔn)差是方差的平方根微渠。
  • 線性代數(shù)https://www.yiibai.com/numpy/numpy_linear_algebra.html
序號 函數(shù)及描述
1. dot 兩個數(shù)組的點積
2. vdot 兩個向量的點積
3. inner 兩個數(shù)組的內(nèi)積
4. matmul 兩個數(shù)組的矩陣積
5. determinant 數(shù)組的行列式
6. solve 求解線性矩陣方程
7. inv 尋找矩陣的乘法逆矩陣
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末搭幻,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子逞盆,更是在濱河造成了極大的恐慌檀蹋,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件云芦,死亡現(xiàn)場離奇詭異续扔,居然都是意外死亡,警方通過查閱死者的電腦和手機焕数,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門纱昧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人堡赔,你說我怎么就攤上這事识脆。” “怎么了善已?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵灼捂,是天一觀的道長。 經(jīng)常有香客問我换团,道長悉稠,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任艘包,我火速辦了婚禮的猛,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘想虎。我一直安慰自己卦尊,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布舌厨。 她就那樣靜靜地躺著岂却,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上躏哩,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天署浩,我揣著相機與錄音,去河邊找鬼扫尺。 笑死瑰抵,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的器联。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼婿崭,長吁一口氣:“原來是場噩夢啊……” “哼拨拓!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起氓栈,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤渣磷,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后授瘦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體醋界,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年提完,在試婚紗的時候發(fā)現(xiàn)自己被綠了形纺。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡徒欣,死狀恐怖逐样,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情打肝,我是刑警寧澤脂新,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站粗梭,受9級特大地震影響争便,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜断医,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一滞乙、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鉴嗤,春花似錦酷宵、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至荣挨,卻和暖如春男韧,著一層夾襖步出監(jiān)牢的瞬間朴摊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工此虑, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留甚纲,地道東北人。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓朦前,卻偏偏與公主長得像介杆,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子韭寸,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容