ndarray
)list
更快import numpy as np
array_1d = np.array( [1,2,3] )
print(array_1d)
[1 2 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]]
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]]]
ndarray
( 給定 )¶np.array(
x )
list
( list
轉 ndarray
)np.zeros(
x )
0
組成的 ndarray
)np.ones(
x )
1
組成的 ndarray
)np.empty(
x )
ndarray
)np.arange(
start , stop , step )
range
ndarray
( 給定 )¶np.array(
x )
¶x 為一個 list
( list
轉 ndarray
)
import numpy as np
Taipei = [24,23,24,19,20,22,22]
Temperature_Taipei = np.array(Taipei)
print(Temperature_Taipei)
print(type(Temperature_Taipei))
[24 23 24 19 20 22 22] <class 'numpy.ndarray'>
print( np.zeros( (2,3) ) )
[[ 0. 0. 0.] [ 0. 0. 0.]]
( 2 ,3 ) :表示一個
2
列3
行二維的陣列
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 ) :表示一個
2
頁3
列4
行的三維陣列
print( np.empty( (2,2) ) )
[[ -1.28822975e-231 -1.28822975e-231] [ nan nan]]
陣列中的元素為一個初始化值
print( np.arange(0,10,2) )
[0 2 4 6 8]
print( list(range(0,10,2)) )
[0, 2, 4, 6, 8]
ndarray
( 隨機 )¶np.random.random(
x )
0~1
的值並組成 ndarray
) np.random.uniform(
low , high , n )
np.random.normal(
mu , sigma , n )
隨機產生 0~1 的數值
print( np.random.random( (2,3) ) )
[[ 0.75378759 0.08643416 0.17476439] [ 0.77896588 0.42930252 0.75001892]]
隨機產生 0~10 的數值
print( np.random.random( (2,3) ) *10 )
[[ 5.3338003 2.33693967 7.68192917] [ 7.70993591 4.83310922 8.98952634]]
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]
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]
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]
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]
array_2d[1 ,1]
5
array_2d[0 ,:]
array([1, 2, 3])
array_2d[: ,2]
array([3, 6, 9])
array_2d>5
array([[False, False, False], [False, False, True], [ True, True, True]], dtype=bool)
array_2d[(array_2d > 5) & (array_2d < 8)]
array([6, 7])
ndarray
中¶九九乘法表
for i in range(1,10): for j in range(1,10): k=i*j print (k , end=' ') print()
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
multiplication = np.empty( (9 , 9) )
for i in range(1,10):
for j in range(1,10):
multiplication[(i-1),(j-1)]= i*j
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.]]
.size
: 這個 ndarray
中總共有幾個元素.shape
: 這個 ndarray
的外觀;e.g. 若 2d-ndarray
shape : (m × n).ndim
: 這個 ndarray
的維度.dtype
: 定義或檢查 ndarray
的資料型態print( array_2d.size )
9
print( array_3d.size )
18
len
¶my_list = [0, 2, 4, 6, 8]
print( len(my_list) )
5
print( array_2d.shape )
(3, 3)
print( array_3d.shape )
(2, 3, 3)
print( array_2d.ndim )
2
print( array_3d.ndim )
3
print( array_2d.dtype )
int64
print( array_3d.dtype )
int64
x = np.array([1 ,2 ,3] ,dtype=np.int)
print(x)
[1 2 3]
print( type(x) )
<class 'numpy.ndarray'>
x.dtype
dtype('int64')
默認是 64 位元的 int,也有 16, 32 位元 。
位元越小佔用的空間越小,若想要精確就必須用大一點。
x = np.array([1 ,2 ,3] ,dtype=np.float)
print(x)
[ 1. 2. 3.]
x.dtype
dtype('float64')
x = np.array([1 ,2 ,3] ,dtype=np.str)
print(x)
['1' '2' '3']
x.dtype
dtype('<U1')
.reshape(m,n)
: 更改 ndarray
的外觀.T
: 將 ndarray
轉置np.ravel()
: 將 ndarray
攤平np.concatenate( (x1,x2) ,axis=0 )
np.vstack( (x1,x2) )
np.concatenate( (x1,x2) ,axis=1 )
np.hstack( (x1,x2) )
np.split( x1 ,分幾個 ,axis=0 )
np.vsplit( x1 ,分幾個 )
np.split( x1 ,分幾個 ,axis=1 )
np.hsplit( x1 ,分幾個 )
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]
normal_distrbution.ndim
1
normal_distrbution.shape
(20,)
.reshape
將 normal_distrbution 資料轉成 2d-ndarray
(4X5)¶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]]
normal_distrbution_new.shape
(4, 5)
array_2d.T
array([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
np.ravel(array_2d)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.ravel(array_2d).shape
(9,)
x1 = np.array([[1,2,3],[4,5,6]])
print(x1)
[[1 2 3] [4 5 6]]
x2 = np.array([[1,1,1],[2,2,2]])
print(x2)
[[1 1 1] [2 2 2]]
np.concatenate( (x1,x2) ,axis=0)
array([[1, 2, 3], [4, 5, 6], [1, 1, 1], [2, 2, 2]])
np.vstack( (x1,x2) )
array([[1, 2, 3], [4, 5, 6], [1, 1, 1], [2, 2, 2]])
np.concatenate( (x1,x2) ,axis=1)
array([[1, 2, 3, 1, 1, 1], [4, 5, 6, 2, 2, 2]])
np.hstack( (x1,x2) )
array([[1, 2, 3, 1, 1, 1], [4, 5, 6, 2, 2, 2]])
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]]
np.split( x1,4,axis=0 )
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]]), array([[10, 11, 12]])]
np.vsplit( x1 ,4 )
[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]]), array([[10, 11, 12]])]
np.split( x1 ,3 ,axis=1 )
[array([[ 1], [ 4], [ 7], [10]]), array([[ 2], [ 5], [ 8], [11]]), array([[ 3], [ 6], [ 9], [12]])]
np.hsplit( x1 ,3 )
[array([[ 1], [ 4], [ 7], [10]]), array([[ 2], [ 5], [ 8], [11]]), array([[ 3], [ 6], [ 9], [12]])]
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
最大值的位置A = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(A)
[[1 2 3] [4 5 6] [7 8 9]]
B = np.array([[1,1,1],[2,2,2],[3,3,3]])
print(B)
[[1 1 1] [2 2 2] [3 3 3]]
將 A 乘上一個常數
A * 2
array([[ 2, 4, 6], [ 8, 10, 12], [14, 16, 18]])
將 A , B 兩矩陣元素相乘
A * B
array([[ 1, 2, 3], [ 8, 10, 12], [21, 24, 27]])
np.dot(A , B )
: 兩 ndarray
相乘 (兩矩陣相乘)¶Ref : http://www.stoimen.com/blog/wp-content/uploads/2012/11/4.-Rect-Matrix-Multiplication.png
將 A , B 兩矩陣相乘
np.dot(A ,B)
array([[14, 14, 14], [32, 32, 32], [50, 50, 50]])
.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]
normal_distrbution.min()
-1.6906168263836041
.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]
normal_distrbution.max()
1.6189816606752596
.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]
normal_distrbution.sum()
1.0538213057199428
.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]
normal_distrbution.mean()
0.052691065285997138
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]
np.std(normal_distrbution)
1.0222357238176645
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]
np.median(normal_distrbution)
0.12009735270935988
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]
np.argmin(normal_distrbution)
17
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]
np.argmax(normal_distrbution)
0
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]]
normal_distrbution_new.mean(axis=0)
array([ 0.58062914, 0.5047527 , -0.33432304, -0.42715471, -0.06044876])
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]]
np.median(normal_distrbution_new,axis=1)
array([ 0.18451869, 0.73100034, 0.22239961, -1.23243451])