2018年1月25日 星期四

Python Array 的基本觀念

Python Array 的基本觀念



Python Array 觀念的了解是確定看懂 Google AI framework tensorflow 或 Adaptive Signal Processing 相關 source code 的基石. 如果了解下面基本的問題, 那至少可以開始看人家的 source code 並且修改成自己的了.




  1. What is the definitions of dimension, shape and size in Python array
  2. What is ()
  3. What is (1,)
  4. What is the difference between (1,) and (1,1)
  5. How to create an two-dimensional array with shape (0,0)
  6. How to create a two-dimensional array with shape (N, 1)
  7. How to write data to the column of array
  8. What is squeeze for array -- 把外面 (最左邊) 拔掉
  9. How to create an one-dimension array with shape (3, )

細節
  1. [scipy array, survey] Lots of array example (ref) (basic)
  2. # 注意: dimenaion 跟 shape 不一樣, 有各自的定義
  3. [ndarray] the ndarray is the Numpy array
  4. [ndarray, axis] the order of indexing into the array (ref)
  5. [ndarray, dimension] the number of axes (dimensions) of the array. the rank of axis (ref)
  6. [ndarray, dimension] How to get the dimension of an array -- x.ndim (view)
  7. [ndarray, shape] a tube indicating the number of elements along each axis of array (ref)
  8. [ndarray, shape] How to get the shape of an array --  print('d.shape = ', d.shape)
  9. [ndarray, size] The total number of elements of the array. This is equal to the product of the elements of shape
  10. [ndarray, len] How to get array length (ref)
  11. [create, ndarray, string] How to create a string array (view)
    1. NoisePool = ['a', 'b']
    2. print('NoisePool[0]=', NoisePool[0])
  12. [create, ndarray, linspace] How to setup a linespace array (view)
  13. [create, ndarray, arange] How to get an array of evenly spaced values (view)
    1. np.arange(start, end, interval)
  14. [create, 0d, ()] How to create a zero-dimensional array (view)

x = np.array(1)   # create a zero dimension array, that can be imutable access
x =123

  1. [length, 1d] How to get the length of an array -- len(your_array)
  2. [create, 1d, (3,) ] How to create a 1D array with giving a list of value
    1. x = np.array([1, 2, 3])
  3. [create, 1d, (1,) zero] How to create an one-dimensional array with zero values (view)

x_1x=np.zeros(1)
Result
x_1x =  [ 0.]  x_1x shape= (1,)


  1. [create, 1d, (1,)] How to create an one-dimension array with shape (1, )  (view)

x_1x=np.zeros(1)  

x = np.array([1])   (ref)
Result
x_1x =  [ 0.]  x_1x shape= (1,)
  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

  1. [create ,1d, append] How to create a 1D array with 40 elements (view)

obj=[]

# append element
obj.append(element)
  1. [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. ])
  1. [create, 2d] How to create a 2D array (view)

# 注意: 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)

  1. [create, 2d] How to construct an 2d array from two Python lists (view)
  2. [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

  1. [create, 2d, (1, 1)] How to create an array with shape (1,1)  (view)

x_1x1=np.zeros((1,1))
Result
x_1x1 =  [[ 0.]]  x_1x1 shape= (1, 1)
  1. [create, 2d, (2, 3)] How to create an array with shape (2, 3) (view)

x_2x3 =
[ [ 0.  0.  0.]
[ 0.  0.  0.]  ]
x_2x3 shape= (2, 3)
  1. [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))

  1. [access, ndarray, N-by-1] How to access the element of N-by-1array
    1. kk[1,0] = 123
  2. [access, ndarray, column] How to write data to the column of array (view)

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.        ]]

  1. [squeeze] What is squeeze for sci array -- 把外面 (最左邊) 拔掉 (view)
  2. [foreach, 1d] How to evaluate the elements for an array (view)
  3. [slice, strip] How to strip the last elemnt from the array? -- [:-1] (view)
    1. word[:2]   # character from the beginning to position 2 (excluded)
      1. 'Py'
    2. word[4:]   # characters from position 4 (included) to the end
      1. 'On'
  4. [set, value, multi] How to setup a valutes to multiple indexs in array (ref)

# equivalent to a[0:6:2] = -1000;
# from start to position 6, exclusive, set every 2nd element to -1000
a[:6:2] = -1000

  1. [reverse] How to reverse the permutation of the value of array (ref)

a[ : :-1]

  1. [iterate] How to iterate all members of array  (ref)

for i in a:
...     print(i**(1/3.))

  1. [subarray, extract] How to extract elements from the begining to the position N (excluded) (view)
    1. s = wave.ys[:N]
  2. [subarray, extract] How to extract elements from the position N (include) to End (view)
    1. s = wave.ys[N:]
  3. [col] How to grab an entire column, column vector (view)
  1. [col] How to get the column vector from 2D numpy array (view)
  2. [col, 20] How to get the column list from a 2D array (view)

x = points_2d[:,0]      # retrive the column list from column 0
y = points-2d[:,1]      # retrive the column list from column 1

  1. [transpose] How to transpose an 2d array  (view)
  2. [mean] How to get the mean for a giving array (view)

  1. [array, sum of square] 用內積來做串列的平方和 (view)
  2. [array, rms] How to calcuating the power value of a signal, python (view)
  3. [array, normalized] How to normalize a wave array so the maximum amplitude is +amp or -amp (view)
  4. [length, numpy] How to get the length for a given numpy array -- var.size