網頁

2019年2月2日 星期六

Numpy

Numpy是一種Numerical Python,相較於python sequences, lists, sets, tuples,Numpy廣泛應用在進行數值運算,接下來要簡單介紹Numpy的功能。

Numpy array是一種多維陣列物件,由行列組合而成。


它可以利用Python的list來初始化,意思就是可以將list的element存取到Numpy陣列。

Single-dimensional Numpy Array:

1
2
3
import numpy as np
a=np.array([1,2,3])
print(a)
Output - [1 2 3

Multi-dimensional Array:

1
2
a=np.array([(1,2,3),(4,5,6)])
print(a)
Output - [[ 1 2 3][4 5 6]]

Python NumPy Array vs. List

通常利用Numpy取代list主要是考慮到以下三種因素:
1.記憶體資源使用率低
2.快速
3.方便
1
2
3
4
5
6
7
8
9
import numpy as np
 
import time
import sys
S= range(1000)
print(sys.getsizeof(5)*len(S))
 
D= np.arange(1000)
print(D.size*D.itemsize)
O/P -  14000

4000

上面例子證明list宣告用了14000,然而Numpy只用了4000。
下面例子是證明Numpy處理時間比list快很多。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import time
import sys
 
SIZE = 1000000
 
L1= range(SIZE)
L2= range(SIZE)
A1= np.arange(SIZE)
A2=np.arange(SIZE)
 
start= time.time()
result=[(x,y) for x,y in zip(L1,L2)]
print((time.time()-start)*1000)
 
start=time.time()
result= A1+A2
print((time.time()-start)*1000)
O/P - 380.9998035430908
49.99995231628418

Python NumPy Operations

ndim:維度 

1
2
3
import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(a.ndim)
Output - 2

itemsize:byte大小 
1
2
3
import numpy as np
a = np.array([(1,2,3)])
print(a.itemsize)
Output - 4

dtype:資料型態
1
2
3
import numpy as np
a = np.array([(1,2,3)])
print(a.dtype)
Output - int32

size, shape:陣列大小與dimension

1
2
3
4
import numpy as np
a = np.array([(1,2,3,4,5,6)])
print(a.size)
print(a.shape)
Output - 6 (1,6)

reshape:重新配置陣列dimension

1
2
3
4
5
import numpy as np
a = np.array([(8,9,10),(11,12,13)])
print(a)
a=a.reshape(3,2)
print(a)
Output - [[ 8 9 10] [11 12 13]] [[ 8 9] [10 11] [12 13]]

slicing:陣列切割


1
2
3
import numpy as np
a=np.array([(1,2,3,4),(3,4,5,6)])
print(a[0,2])
Output - 3

1
2
3
import numpy as np
a=np.array([(1,2,3,4),(3,4,5,6)])
print(a[0:,2])
Output - [3 5]

1
2
3
import numpy as np
a=np.array([(8,9),(10,11),(12,13)])
print(a[0:2,1])
Output - [9 11]

linspace:等差數列

1
2
3
import numpy as np
a=np.linspace(1,3,10)
print(a)
Output - [ 1. 1.22222222 1.44444444 1.66666667 1.88888889 2.11111111 2.33333333 2.55555556 2.77777778 3. ]

max/ min:最大最小值

1
2
3
4
5
6
import numpy as np
 
a= np.array([1,2,3])
print(a.min())
print(a.max())
print(a.sum())
Output - 1 3 6

axis:選擇某個維度

1
2
a= np.array([(1,2,3),(3,4,5)])
print(a.sum(axis=0))
Output - [4 6 8]

Square Root & Standard Deviation:平方根與標準差

1
2
3
4
import numpy as np
a=np.array([(1,2,3),(3,4,5,)])
print(np.sqrt(a))
print(np.std(a))
Output - [[ 1. 1.41421356 1.73205081]
 [ 1.73205081 2. 2.23606798]]
1.29099444874

Elementary arithmetic:基礎四則運算
1
2
3
4
import numpy as np
x= np.array([(1,2,3),(3,4,5)])
y= np.array([(1,2,3),(3,4,5)])
print(x+y)
Output - [[ 2 4 6] [ 6 8 10]]

1
2
3
4
5
6
import numpy as np
x= np.array([(1,2,3),(3,4,5)])
y= np.array([(1,2,3),(3,4,5)])
print(x-y)
print(x*y)
print(x/y)
Output - [[0 0 0] [0 0 0]]
[[ 1 4 9] [ 9 16 25]] 
[[ 1. 1. 1.] [ 1. 1. 1.]]

Vertical & Horizontal Stacking

1
2
3
4
5
import numpy as np
x= np.array([(1,2,3),(3,4,5)])
y= np.array([(1,2,3),(3,4,5)])
print(np.vstack((x,y)))
print(np.hstack((x,y)))

Output - [[1 2 3] [3 4 5] [1 2 3] [3 4 5]]
[[1 2 3 1 2 3] [3 4 5 3 4 5]]

ravel

1
2
3
import numpy as np
x= np.array([(1,2,3),(3,4,5)])
print(x.ravel())

Output - [ 1 2 3 3 4 5]

Python Numpy Special Functions

Exponential Function

1
2
a= np.array([1,2,3])
print(np.exp(a))
Output - [ 2.71828183   7.3890561   20.08553692]

Logarithmic Function

1
2
3
4
import numpy as np
import matplotlib.pyplot as plt
a= np.array([1,2,3])
print(np.log(a))
Output - [ 0.          0.69314718  1.09861229]

1
2
3
4
import numpy as np
import matplotlib.pyplot as plt
a= np.array([1,2,3])
print(np.log10(a))
Output - [ 0.        0.30103      0.47712125]

Matplotlib

1
2
3
4
5
6
import numpy as np
import matplotlib.pyplot as plt
x= np.arange(0,3*np.pi,0.1)
y=np.sin(x)
plt.plot(x,y)
plt.show()
Output – 


1
2
3
4
5
6
import numpy as np
import matplotlib.pyplot as plt
x= np.arange(0,3*np.pi,0.1)
y=np.tan(x)
plt.plot(x,y)
plt.show()
Output – 

沒有留言:

張貼留言