Python × 資料分析

Numpy Part II


Kristen Chan

Agenda


  • N-dimensional ndarray
  • Create ndarray
  • Basic Indexing
  • Inspect
  • Reshaping
  • Mathematics

Note

  • 支援科學計算
    • 向量、矩陣運算。 e.g. 加法、減法、乘法

  • 主要的物件是 Numpy 陣列 (ndarray)
    • 是一個數學物件。 e.g. 一個向量、一個矩陣

  • 使用 Numpy 向量化操作比使用 Python list 更快
  • 匯入 Numpy
In [1]:
import numpy as np

Numpy


N-dimensional ndarray

N-dimensional ndarray

N-dimensional ndarray


  • 1d-ndarray
In [2]:
array_1d = np.array( [1,2,3] )
print(array_1d)
[1 2 3]

N-dimensional ndarray


  • 2d-ndarray
In [3]:
array_2d = np.array( [ [1,2,3],
                       [4,5,6],
                       [7,8,9] ])
print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

N-dimensional ndarray


  • 3d-ndarray
In [4]:
array_3d = np.array( [ [ [1,2,3],
                         [4,5,6],
                         [7,8,9] ],
                       [ [2,4,6],
                         [8,10,12],
                         [14,16,18] ] ] )
print(array_3d)
[[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[ 2  4  6]
  [ 8 10 12]
  [14 16 18]]]

Numpy


Create ndarray

Create ndarray ( 給定 )


  • np.array( x )
    x 為一個 list ( listndarray )

  • np.zeros( x )
    x 是維度 ( 建立一個由 0 組成的 ndarray )

  • np.ones( x )
    x 是維度 ( 建立一個由 1 組成的 ndarray )

  • np.empty( x )
    x 是維度 ( 建立一個空的 ndarray )

  • np.arange( start , stop , step )
    range

Create ndarray ( 給定 )


Review

np.array( x )

x 為一個 list ( listndarray )

In [5]:
import numpy as np
Taipei = [24,23,24,19,20,22,22]
Temperature_Taipei = np.array(Taipei)
In [6]:
print(Temperature_Taipei)
print(type(Temperature_Taipei))
[24 23 24 19 20 22 22]
<class 'numpy.ndarray'>

Create ndarray ( 給定 )


np.zeros( x )

x 是維度 ( 建立一個由 0 組成的 ndarray )

In [7]:
print( np.zeros( (2,3) ) )
[[ 0.  0.  0.]
 [ 0.  0.  0.]]

( 2 ,3 ) :表示一個 23二維的陣列

Create ndarray ( 給定 )


np.ones( x )

x 是維度 ( 建立一個由 1 組成的 ndarray )

In [8]:
print( np.ones( (2,3,4) ) )
[[[ 1.  1.  1.  1.]
  [ 1.  1.  1.  1.]
  [ 1.  1.  1.  1.]]

 [[ 1.  1.  1.  1.]
  [ 1.  1.  1.  1.]
  [ 1.  1.  1.  1.]]]

( 2 ,3 ,4 ) :表示一個 234 行的三維陣列

Create ndarray ( 給定 )


np.empty( x )

x 是維度 ( 建立一個空的 ndarray )

In [9]:
print( np.empty( (2,2) ) )
[[ -1.28822975e-231  -1.28822975e-231]
 [              nan               nan]]

陣列中的元素為一個初始化值

Create ndarray ( 給定 )


np.arange( start , stop , step ) : 似 range

In [10]:
print( np.arange(0,10,2) )
[0 2 4 6 8]

Review

In [11]:
print( list(range(0,10,2)) )
[0, 2, 4, 6, 8]

Create ndarray ( 隨機 )


  • np.random.random( x )
    x 是維度 ( 隨機產生 0~1 的值並組成 ndarray )

  • np.random.uniform( low , high , n )
    隨機產生 n 個介在 low~high 的值並服從 unifom

  • np.random.normal( mu , sigma , n )
    隨機產生 n 個服從 normal( $\mu$ , $\sigma$ ) )

Create ndarray ( 隨機 )


np.random.random( x )

x 是維度 ( 隨機產生 0~1 的值並組成 ndarray )

隨機產生 0~1 的數值

In [12]:
print( np.random.random( (2,3) ) )
[[ 0.75378759  0.08643416  0.17476439]
 [ 0.77896588  0.42930252  0.75001892]]

隨機產生 0~10 的數值

In [13]:
print( np.random.random( (2,3) ) *10 )
[[ 5.3338003   2.33693967  7.68192917]
 [ 7.70993591  4.83310922  8.98952634]]

Create ndarray ( 隨機 )


np.random.uniform( low , high , n )

隨機產生 n 個介在 low~high 的值並服從 unifom

In [14]:
unifom_distrbution = np.random.uniform(-1,1,25)
print(unifom_distrbution)
[-0.80185851  0.81497357  0.62891691  0.5286932  -0.13747851  0.02991847
  0.42097931  0.71500292 -0.47950983  0.39914824 -0.58390657 -0.20947911
 -0.39016756 -0.26251339 -0.66299936 -0.26117429  0.52557675  0.02243784
 -0.86338     0.94588199 -0.12273873  0.46230174 -0.89655545 -0.04291599
 -0.25827873]

Create ndarray ( 隨機 )


np.random.uniform( low , high , n )

隨機產生 n 個介在 low~high 的值並服從 unifom

Note

設定 seed 可以固定每次 random 產生的結果

In [15]:
np.random.seed(100)
unifom_distrbution = np.random.uniform(-1,1,25)
print(unifom_distrbution)
[ 0.08680988 -0.44326123 -0.15096482  0.68955226 -0.99056229 -0.75686176
  0.34149817  0.65170551 -0.72658682  0.15018666  0.78264391 -0.58159576
 -0.62934356 -0.78324622 -0.56060501  0.95724757  0.6233663  -0.65611797
  0.6324495  -0.45185251 -0.13659163  0.88005964  0.63529876 -0.3277761
 -0.64917909]

Note

Uniform Probability Density Function

$$ f(x)=\begin{cases} \frac{1}{b-a} &\text{,for } a \leq x \leq b \\ 0 & \text{,otherwise} \end{cases} $$

Uniform Probability Density Function

Create ndarray ( 隨機 )


np.random.normal( mu , sigma , n )

隨機產生 n 個服從 Normal( $\mu$ , $\sigma$ )

In [16]:
normal_distrbution = np.random.normal( 0 , 1 , 20 )
print(normal_distrbution)
[ 0.78148842 -0.65438103  0.04117247 -0.20191691 -0.87081315  0.22893207
 -0.40803994 -0.10392514  1.56717879  0.49702472  1.15587233  1.83861168
  1.53572662  0.25499773 -0.84415725 -0.98294346 -0.30609783  0.83850061
 -1.69084816  1.15117366]

Create ndarray ( 隨機 )


np.random.normal( mu , sigma , n )

隨機產生 n 個服從 Normal( $\mu$ , $\sigma$ )

Note

設定 seed 可以固定每次 random 產生的結果

In [17]:
np.random.seed(100)
normal_distrbution = np.random.normal( 0 , 1 , 20 )
print(normal_distrbution)
[-1.74976547  0.3426804   1.1530358  -0.25243604  0.98132079  0.51421884
  0.22117967 -1.07004333 -0.18949583  0.25500144 -0.45802699  0.43516349
 -0.58359505  0.81684707  0.67272081 -0.10441114 -0.53128038  1.02973269
 -0.43813562 -1.11831825]

Note

Normal Density Function

$$ f(x)= \frac{1}{\sqrt{2 \sigma^2 \pi}}e^{-\frac{(x-\mu)^2}{2\sigma^2}} \text{ for } \mu \in R\text{,}\sigma^2 > 0 $$

Uniform Probability Density Function

Numpy


Basic Indexing

Basic Indexing


Example

print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

取出 array_2d 中第二列第二行的元素

In [18]:
array_2d[1 ,1]
Out[18]:
5

Basic Indexing


Example

print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

取出 array_2d 中第一列的所有元素

In [19]:
array_2d[0 ,:]
Out[19]:
array([1, 2, 3])

Basic Indexing


Example

print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

取出 array_2d 中第三行的所有元素

In [20]:
array_2d[: ,2]
Out[20]:
array([3, 6, 9])

Basic Indexing


Example

print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

取出 array_2d 中大於 5 的元素

In [21]:
array_2d>5
Out[21]:
array([[False, False, False],
       [False, False,  True],
       [ True,  True,  True]], dtype=bool)

Basic Indexing


Example

print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

取出 array_2d 中大於 5 且小於 8 的元素

In [22]:
array_2d[(array_2d > 5) & (array_2d < 8)]
Out[22]:
array([6, 7])

Exercise

Q. 將九九乘法表存入一個 2d-ndarray

九九乘法表

for i in range(1,10):
    for j in range(1,10):
        k=i*j
        print (k , end=' ')
    print()

Note

In [23]:
multiplication = np.empty( (9 , 9) )
for i in range(1,10):
    for j in range(1,10):
        multiplication[i,j]= i*j
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-23-6a60e7889134> in <module>()
      2 for i in range(1,10):
      3     for j in range(1,10):
----> 4         multiplication[i,j]= i*j

IndexError: index 9 is out of bounds for axis 1 with size 9

Answers

In [24]:
multiplication = np.empty( (9 , 9) )
for i in range(1,10):
    for j in range(1,10):
        multiplication[(i-1),(j-1)]= i*j
In [25]:
print(multiplication)
[[  1.   2.   3.   4.   5.   6.   7.   8.   9.]
 [  2.   4.   6.   8.  10.  12.  14.  16.  18.]
 [  3.   6.   9.  12.  15.  18.  21.  24.  27.]
 [  4.   8.  12.  16.  20.  24.  28.  32.  36.]
 [  5.  10.  15.  20.  25.  30.  35.  40.  45.]
 [  6.  12.  18.  24.  30.  36.  42.  48.  54.]
 [  7.  14.  21.  28.  35.  42.  49.  56.  63.]
 [  8.  16.  24.  32.  40.  48.  56.  64.  72.]
 [  9.  18.  27.  36.  45.  54.  63.  72.  81.]]

Numpy


Inspect

Inspect


  • .size : 這個 ndarray 中總共有幾個元素
  • .shape : 這個 ndarray 的外觀;e.g. 若 2d-ndarray shape : (m × n)
  • .ndim : 這個 ndarray 的維度
  • .dtype : 定義或檢查 ndarray 的資料型態

Inspect


.size : 這個 ndarray 中總共有幾個元素

Example

print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

確認 array_2d 中元素個數

In [26]:
print( array_2d.size )
9

Inspect


.size : 這個 ndarray 中總共有幾個元素

Example

print(array_3d)
[[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[ 2  4  6]
  [ 8 10 12]
  [14 16 18]]]

確認 array_3d 中元素個數

In [27]:
print( array_3d.size )
18

Inspect


.size : 這個 ndarray 中總共有幾個元素

Review

len

In [28]:
my_list = [0, 2, 4, 6, 8]
print( len(my_list) )
5

Inspect


.shape : 這個 ndarray 的外觀

Example

print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

確認 array_2d 中矩陣形狀

In [29]:
print( array_2d.shape )
(3, 3)

Inspect


.shape : 這個 ndarray 的外觀

Example

print(array_3d)
[[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[ 2  4  6]
  [ 8 10 12]
  [14 16 18]]]

確認 array_3d 中矩陣形狀

In [30]:
print( array_3d.shape )
(2, 3, 3)

Inspect


.ndim : 這個 ndarray 的維度

Example

print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

確認 array_2d 的維度

In [31]:
print( array_2d.ndim )
2

Inspect


.ndim : 這個 ndarray 的維度

Example

print(array_3d)
[[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[ 2  4  6]
  [ 8 10 12]
  [14 16 18]]]

確認 array_3d 的維度

In [32]:
print( array_3d.ndim )
3

Inspect


.dtype : 定義檢查 ndarray 的資料型態

Example

print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

檢查 array_2d 中元素的型態

In [33]:
print( array_2d.dtype )
int64

Inspect


.dtype : 定義或檢查 ndarray 的資料型態

Example

print(array_3d)
[[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[ 2  4  6]
  [ 8 10 12]
  [14 16 18]]]

檢查 array_3d 中元素的型態

In [34]:
print( array_3d.dtype )
int64

Inspect


.dtype : 定義或檢查 ndarray 的資料型態

利用 dtype 定義一個 int 的 ndarray

In [35]:
x = np.array([1 ,2 ,3] ,dtype=np.int)
In [36]:
print(x)
[1 2 3]
In [37]:
print( type(x) )
<class 'numpy.ndarray'>
In [38]:
x.dtype
Out[38]:
dtype('int64')

默認是 64 位元的 int,也有 16, 32 位元 。
位元越小佔用的空間越小,若想要精確就必須用大一點。

Inspect


.dtype : 定義或檢查 ndarray 的資料型態

利用 dtype 定義一個 float 的 ndarray

In [39]:
x = np.array([1 ,2 ,3] ,dtype=np.float)
In [40]:
print(x)
[ 1.  2.  3.]
In [41]:
x.dtype
Out[41]:
dtype('float64')

Inspect


.dtype : 定義或檢查 ndarray 的資料型態

利用 dtype 定義一個 str 的 ndarray

In [42]:
x = np.array([1 ,2 ,3] ,dtype=np.str)
In [43]:
print(x)
['1' '2' '3']
In [44]:
x.dtype
Out[44]:
dtype('<U1')

Numpy


Reshaping

Reshaping


  • .reshape(m,n) : 更改 ndarray 的外觀
  • .T : 將 ndarray 轉置
  • np.ravel() : 將 ndarray 攤平
  • vertical stack : 縱向合併
    • np.concatenate( (x1,x2) ,axis=0 )
    • np.vstack( (x1,x2) )
  • horizontal stack : 橫向合併
    • np.concatenate( (x1,x2) ,axis=1 )
    • np.hstack( (x1,x2) )
  • vertical split : 縱向合併
    • np.split( x1 ,分幾個 ,axis=0 )
    • np.vsplit( x1 ,分幾個 )
  • horizontal split : 橫向合併
    • np.split( x1 ,分幾個 ,axis=1 )
    • np.hsplit( x1 ,分幾個 )

Reshaping


.reshape(m,n) : 更改 ndarray 的外觀

Example
隨機生成 一個名為 normal_distrbution (20×1) 的ndarray,服從 normal(0,1)

In [45]:
normal_distrbution = np.random.normal(0,1,20)
print(normal_distrbution)
[ 1.61898166  1.54160517 -0.25187914 -0.84243574  0.18451869  0.9370822
  0.73100034  1.36155613 -0.32623806  0.05567601  0.22239961 -1.443217
 -0.75635231  0.81645401  0.75044476 -0.45594693  1.18962227 -1.69061683
 -1.35639905 -1.23243451]
In [46]:
normal_distrbution.ndim
Out[46]:
1
In [47]:
normal_distrbution.shape
Out[47]:
(20,)

Reshaping


.reshape(m,n) : 更改 ndarray 的外觀

Example
隨機生成 一個名為 normal_distrbution (20,) 的1d-ndarray,服從 normal(0,1)

利用 .reshape 將 normal_distrbution 資料轉成 2d-ndarray(4X5)

In [48]:
normal_distrbution_new=normal_distrbution.reshape(4,5)
print(normal_distrbution_new)
[[ 1.61898166  1.54160517 -0.25187914 -0.84243574  0.18451869]
 [ 0.9370822   0.73100034  1.36155613 -0.32623806  0.05567601]
 [ 0.22239961 -1.443217   -0.75635231  0.81645401  0.75044476]
 [-0.45594693  1.18962227 -1.69061683 -1.35639905 -1.23243451]]
In [49]:
normal_distrbution_new.shape
Out[49]:
(4, 5)

Reshaping


.T : 將 ndarray 轉置

Example

print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

將 array_2d 轉置

In [50]:
array_2d.T
Out[50]:
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])

Reshaping


np.ravel( ) : 將 ndarray 攤平

Example

print(array_2d)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

將 array_2d 攤平

(3×3) → (9×1)

In [51]:
np.ravel(array_2d)
Out[51]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
In [52]:
np.ravel(array_2d).shape
Out[52]:
(9,)

Reshaping


vertical stack

Example

In [53]:
x1 = np.array([[1,2,3],[4,5,6]])
print(x1)
[[1 2 3]
 [4 5 6]]
In [54]:
x2 = np.array([[1,1,1],[2,2,2]])
print(x2)
[[1 1 1]
 [2 2 2]]

Reshaping


vertical stack : 縱向合併

Method 1 : np.concatenate( (x1,x2) ,axis=0 )

Example

print(x1)          print(x2)
[[1 2 3]          [[1 1 1]
 [4 5 6]]          [2 2 2]]
In [55]:
np.concatenate( (x1,x2) ,axis=0)
Out[55]:
array([[1, 2, 3],
       [4, 5, 6],
       [1, 1, 1],
       [2, 2, 2]])

Reshaping


vertical stack : 縱向合併

Method 2 : np.vstack( (x1,x2) )

In [56]:
np.vstack( (x1,x2) )
Out[56]:
array([[1, 2, 3],
       [4, 5, 6],
       [1, 1, 1],
       [2, 2, 2]])

Reshaping


horizontal stack : 橫向合併

Method 1 : np.concatenate( (x1,x2) ,axis=1 )

Example

print(x1)          print(x2)
[[1 2 3]          [[1 1 1]
 [4 5 6]]          [2 2 2]]
In [57]:
np.concatenate( (x1,x2) ,axis=1)
Out[57]:
array([[1, 2, 3, 1, 1, 1],
       [4, 5, 6, 2, 2, 2]])

Reshaping


horizontal stack : 橫向合併

Method 2 : np.hstack( (x1,x2) )

In [58]:
np.hstack( (x1,x2) )
Out[58]:
array([[1, 2, 3, 1, 1, 1],
       [4, 5, 6, 2, 2, 2]])

Reshaping


vertical split

Example

In [59]:
x1 = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(x1)
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

Reshaping


vertical split : 縱向分割

Method 1 : np.split( x1 ,分幾個 ,axis=0 )

Example

print(x1)
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
In [60]:
np.split( x1,4,axis=0 )
Out[60]:
[array([[1, 2, 3]]),
 array([[4, 5, 6]]),
 array([[7, 8, 9]]),
 array([[10, 11, 12]])]

Reshaping


vertical split : 縱向分割

Method 2 : np.vsplit( x1 ,分幾個 )

Example

print(x1)
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
In [61]:
np.vsplit( x1 ,4 )
Out[61]:
[array([[1, 2, 3]]),
 array([[4, 5, 6]]),
 array([[7, 8, 9]]),
 array([[10, 11, 12]])]

Reshaping


horizontal split : 縱向分割

Method 1 : np.split( x1 ,分幾個 ,axis=1 )

Example

print(x1)
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
In [62]:
np.split( x1 ,3 ,axis=1 ) 
Out[62]:
[array([[ 1],
        [ 4],
        [ 7],
        [10]]), array([[ 2],
        [ 5],
        [ 8],
        [11]]), array([[ 3],
        [ 6],
        [ 9],
        [12]])]

Reshaping


horizontal split : 縱向分割

Method 2 : np.hsplit( x1 ,分幾個 )

Example

print(x1)
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
In [63]:
np.hsplit( x1 ,3 )
Out[63]:
[array([[ 1],
        [ 4],
        [ 7],
        [10]]), array([[ 2],
        [ 5],
        [ 8],
        [11]]), array([[ 3],
        [ 6],
        [ 9],
        [12]])]

Numpy


Mathematics

Mathematics


  • A * B : 兩 ndarray 元素相乘 (兩矩陣元素相乘)
  • np.dot(A , B ) : 兩 ndarray 相乘 (兩矩陣相乘)
  • .min( ) : 找出 ndarray 中最小值
  • .max( ) : 找出 ndarray 中最大值
  • .sum( ) : 計算這個 ndarray 的加總
  • .mean( ) : 計算這個 ndarray 的平均數
  • np.std( ) : 計算這個 ndarray 的標準差
  • np.median( ) : 計算這個 ndarray 的中位數
  • np.argmin( ) : 找出這個 ndarray 最小值的位置
  • np.argmax( ) : 計算這個 ndarray 最大值的位置

Mathematics


A * B : 兩 ndarray 元素相乘 (兩矩陣元素相乘)

Example

In [64]:
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(A)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
In [65]:
B = np.array([[1,1,1],[2,2,2],[3,3,3]])
print(B)
[[1 1 1]
 [2 2 2]
 [3 3 3]]

Mathematics


A * B : 兩 ndarray 元素相乘 (兩矩陣元素相乘)

Example

print(A)          print(B)
[[1 2 3]          [[1 1 1]
 [4 5 6]           [2 2 2]
 [7 8 9]]          [3 3 3]]

將 A 乘上一個常數

In [66]:
A * 2
Out[66]:
array([[ 2,  4,  6],
       [ 8, 10, 12],
       [14, 16, 18]])

Mathematics


A * B : 兩 ndarray 元素相乘 (兩矩陣元素相乘)

Example

print(A)          print(B)
[[1 2 3]          [[1 1 1]
 [4 5 6]           [2 2 2]
 [7 8 9]]          [3 3 3]]

將 A , B 兩矩陣元素相乘

In [67]:
A * B
Out[67]:
array([[ 1,  2,  3],
       [ 8, 10, 12],
       [21, 24, 27]])

Mathematics


A * B : 兩 ndarray 元素相乘 (兩矩陣元素相乘)

Note

矩陣元素相乘

Mathematics


np.dot(A , B ) : 兩 ndarray 相乘 (兩矩陣相乘)

矩陣元素相乘

Ref : http://www.stoimen.com/blog/wp-content/uploads/2012/11/4.-Rect-Matrix-Multiplication.png

Mathematics


np.dot(A , B ) : 兩 ndarray 相乘 (兩矩陣相乘)

Example

print(A)          print(B)
[[1 2 3]          [[1 1 1]
 [4 5 6]           [2 2 2]
 [7 8 9]]          [3 3 3]]

將 A , B 兩矩陣相乘

In [68]:
np.dot(A ,B)
Out[68]:
array([[14, 14, 14],
       [32, 32, 32],
       [50, 50, 50]])

Mathematics


np.dot(A , B ) : 兩 ndarray 相乘 (兩矩陣相乘)

Note

矩陣元素相乘

Mathematics


.min( ) : 找出 ndarray 中最小值

Example

print(normal_distrbution)
[-1.74976547  0.3426804   1.1530358  -0.25243604  0.98132079  0.51421884
  0.22117967 -1.07004333 -0.18949583  0.25500144 -0.45802699  0.43516349
 -0.58359505  0.81684707  0.67272081 -0.10441114 -0.53128038  1.02973269
 -0.43813562 -1.11831825]
In [69]:
normal_distrbution.min()
Out[69]:
-1.6906168263836041

Mathematics


.max( ) : 找出 ndarray 中最大值

Example

print(normal_distrbution)
[-1.74976547  0.3426804   1.1530358  -0.25243604  0.98132079  0.51421884
  0.22117967 -1.07004333 -0.18949583  0.25500144 -0.45802699  0.43516349
 -0.58359505  0.81684707  0.67272081 -0.10441114 -0.53128038  1.02973269
 -0.43813562 -1.11831825]
In [70]:
normal_distrbution.max()
Out[70]:
1.6189816606752596

Mathematics


.sum( ) : 計算這個 ndarray 的加總

Example

print(normal_distrbution)
[-1.74976547  0.3426804   1.1530358  -0.25243604  0.98132079  0.51421884
  0.22117967 -1.07004333 -0.18949583  0.25500144 -0.45802699  0.43516349
 -0.58359505  0.81684707  0.67272081 -0.10441114 -0.53128038  1.02973269
 -0.43813562 -1.11831825]
In [71]:
normal_distrbution.sum()
Out[71]:
1.0538213057199428

Mathematics


.mean( ) : 計算這個 ndarray 的平均數

Example

print(normal_distrbution)
[-1.74976547  0.3426804   1.1530358  -0.25243604  0.98132079  0.51421884
  0.22117967 -1.07004333 -0.18949583  0.25500144 -0.45802699  0.43516349
 -0.58359505  0.81684707  0.67272081 -0.10441114 -0.53128038  1.02973269
 -0.43813562 -1.11831825]
In [72]:
normal_distrbution.mean()
Out[72]:
0.052691065285997138

Mathematics


np.std( ) : 計算這個 ndarray 的標準差

Example

print(normal_distrbution)
[-1.74976547  0.3426804   1.1530358  -0.25243604  0.98132079  0.51421884
  0.22117967 -1.07004333 -0.18949583  0.25500144 -0.45802699  0.43516349
 -0.58359505  0.81684707  0.67272081 -0.10441114 -0.53128038  1.02973269
 -0.43813562 -1.11831825]
In [73]:
np.std(normal_distrbution)
Out[73]:
1.0222357238176645

Mathematics


np.median( ) : 計算這個 ndarray 的中位數

Example

print(normal_distrbution)
[-1.74976547  0.3426804   1.1530358  -0.25243604  0.98132079  0.51421884
  0.22117967 -1.07004333 -0.18949583  0.25500144 -0.45802699  0.43516349
 -0.58359505  0.81684707  0.67272081 -0.10441114 -0.53128038  1.02973269
 -0.43813562 -1.11831825]
In [74]:
np.median(normal_distrbution)
Out[74]:
0.12009735270935988

Mathematics


np.argmin( ) : 找出這個 ndarray 最小值的位置

Example

print(normal_distrbution)
[-1.74976547  0.3426804   1.1530358  -0.25243604  0.98132079  0.51421884
  0.22117967 -1.07004333 -0.18949583  0.25500144 -0.45802699  0.43516349
 -0.58359505  0.81684707  0.67272081 -0.10441114 -0.53128038  1.02973269
 -0.43813562 -1.11831825]
In [75]:
np.argmin(normal_distrbution)
Out[75]:
17

Mathematics


np.argmax( ) : 找出這個 ndarray 最大值的位置

Example

print(normal_distrbution)
[-1.74976547  0.3426804   1.1530358  -0.25243604  0.98132079  0.51421884
  0.22117967 -1.07004333 -0.18949583  0.25500144 -0.45802699  0.43516349
 -0.58359505  0.81684707  0.67272081 -0.10441114 -0.53128038  1.02973269
 -0.43813562 -1.11831825]
In [76]:
np.argmax(normal_distrbution)
Out[76]:
0

Mathematics


Note

可以加入 axis 這個參數,分別計算行、列的結果

  • axis=0 : 針對行 (column)
  • axis=1 : 針對列 (row)

Example

print(normal_distrbution_new)
[[-1.74976547  0.3426804   1.1530358  -0.25243604  0.98132079]
 [ 0.51421884  0.22117967 -1.07004333 -0.18949583  0.25500144]
 [-0.45802699  0.43516349 -0.58359505  0.81684707  0.67272081]
 [-0.10441114 -0.53128038  1.02973269 -0.43813562 -1.11831825]]
In [77]:
normal_distrbution_new.mean(axis=0)
Out[77]:
array([ 0.58062914,  0.5047527 , -0.33432304, -0.42715471, -0.06044876])

Mathematics


Note

可以加入 axis 這個參數,分別計算行、列的結果

  • axis=0 : 針對行 (column)
  • axis=1 : 針對列 (row)

Example

print(normal_distrbution_new)
[[-1.74976547  0.3426804   1.1530358  -0.25243604  0.98132079]
 [ 0.51421884  0.22117967 -1.07004333 -0.18949583  0.25500144]
 [-0.45802699  0.43516349 -0.58359505  0.81684707  0.67272081]
 [-0.10441114 -0.53128038  1.02973269 -0.43813562 -1.11831825]]
In [78]:
np.median(normal_distrbution_new,axis=1)
Out[78]:
array([ 0.18451869,  0.73100034,  0.22239961, -1.23243451])