Python Array 的基本觀念
Python Array 觀念的了解是確定看懂 Google AI framework tensorflow 或 Adaptive Signal Processing 相關 source code 的基石. 如果了解下面基本的問題, 那至少可以開始看人家的 source code 並且修改成自己的了.
- What is the definitions of dimension, shape and size in Python array
- What is ()
- What is (1,)
- What is the difference between (1,) and (1,1)
- How to create an two-dimensional array with shape (0,0)
- How to create a two-dimensional array with shape (N, 1)
- How to write data to the column of array
- What is squeeze for array -- 把外面 (最左邊) 拔掉
- How to create an one-dimension array with shape (3, )
細節
- # 注意: dimenaion 跟 shape 不一樣, 有各自的定義
- [ndarray] the ndarray is the Numpy array
- [ndarray, shape] How to get the shape of an array -- print('d.shape = ', d.shape)
- [ndarray, size] The total number of elements of the array. This is equal to the product of the elements of shape
- NoisePool = ['a', 'b']
- print('NoisePool[0]=', NoisePool[0])
- np.arange(start, end, interval)
x = np.array(1) # create a zero dimension array, that can be imutable access
x =123
|
- [length, 1d] How to get the length of an array -- len(your_array)
- [create, 1d, (3,) ] How to create a 1D array with giving a list of value
- x = np.array([1, 2, 3])
x_1x=np.zeros(1)
Result
x_1x = [ 0.] x_1x shape= (1,)
|
x_1x=np.zeros(1)
Result
x_1x = [ 0.] x_1x shape= (1,)
|
- [create, 1d, (3,)] How to create an one-dimension array with shape (3, )
import numpy as np
x = np.array([1,2,3])
print('x.shape=', x.shape)
print('x.ndim = ', x.ndim)
Result
x.shape= (3,)
x.ndim = 1
|
obj=[]
# append element
obj.append(element)
|
- [create 1d, linespace] How to create a 1D array by linespace function -- linespace(start, stop, N) (ref)
In [6]: linspace(0, 10, 5)
Out[6]: array([ 0. , 2.5, 5. , 7.5, 10. ]) |
# 注意: dimenaion 跟 shape 不一樣
x = np.array([[1,2,3],[4,5,6], [1,2,3]]) # create two-dimensional array with shape (3, 3)
Result
x.shape= (3, 3)
|
- [create, 2d, (0, 0)] How to create an two-dimensional array with shape (0,0)
x = np.ones( shape=(0, 0) )
print('x.shape=', x.shape)
print('x.ndim = ', x.ndim)
print('x = ', x) # access demo
|
x_1x1=np.zeros((1,1))
Result
x_1x1 = [[ 0.]] x_1x1 shape= (1, 1)
|
x_2x3 =
[ [ 0. 0. 0.]
[ 0. 0. 0.] ]
x_2x3 shape= (2, 3)
|
- [create, ndarray, (N, 1)] How to create a two-dimensional array with shape (N, 1) in random nums
N = 10
import numpy as np
x = np.random.random((N, 1))
|
- [access, ndarray, N-by-1] How to access the element of N-by-1array
- kk[1,0] = 123
A=[[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]]
Updated A = [[ 0. 0. ]
[ 0.84147098 0. ]
[ 0.90929743 0. ]
[ 0.14112001 0. ]
[-0.7568025 0. ]
[-0.95892427 0. ]
[-0.2794155 0. ]
[ 0.6569866 0. ]
[ 0.98935825 0. ]
[ 0.41211849 0. ]]
|
- word[:2] # character from the beginning to position 2 (excluded)
- 'Py'
- word[4:] # characters from position 4 (included) to the end
- 'On'
# equivalent to a[0:6:2] = -1000;
# from start to position 6, exclusive, set every 2nd element to -1000
a[:6:2] = -1000
|
a[ : :-1]
|
for i in a:
... print(i**(1/3.))
|
- s = wave.ys[:N]
- s = wave.ys[N:]
x = points_2d[:,0] # retrive the column list from column 0
y = points-2d[:,1] # retrive the column list from column 1
|
- [length, numpy] How to get the length for a given numpy array -- var.size