1. 程式人生 > >python學習筆記15 模組numpy函式

python學習筆記15 模組numpy函式

Time:20181019

NumPy是Python語言的一個擴充程式庫。支援高階大量的維度陣列與矩陣運算,此外也針對陣列運算提供大量的數學函式庫。

1、np.newaxis:

np.newaxis:放在第幾個位置,就會在shape裡面看到相應的位置增加了一個維數 >>> import numpy as np >>> x = np.random.randint(1, 8, size=5) >>> x array([7, 2, 3, 5, 2]) - >>> x1 = x[np.newaxis, :] >>> x1 array([[7, 2, 3, 5, 2]])-->1x5

>>> x2 = x[:, np.newaxis] >>> x2 array([[7],        [2],        [3],        [5],        [2]])--->5x1 >>> 小結:多維資料提取一維資料後還原為多維陣列

1.1 x_data , y_label= produceData(10,6,-4,1000)

X_Col1 = np.random.uniform( r1*np.cos(theta1),r2*np.cos(theta1),num)[:, np.newaxis]

[[ 6.43537340e+00]  [ 6.82978402e+00]  [ 2.09355610e+00]  [-7.97087355e+00]  [-1.11236278e+01]  [-4.22396005e+00]  [-5.26542737e+00]  [-6.68204222e+00]  [ 8.44659647e+00]  [-3.28716816e+00]  [ 1.04212395e+01] ....

X_Col = np.random.uniform(r1 * np.cos(theta1), r2 * np.cos(theta1), num)

float64\

[ 4.00186233e+00  1.11634938e+01  1.60572965e+00 -7.76467945e+00  -1.17435194e+01 -5.57723441e+00 -6.79744608e+00 -1.07945486e+01   5.70181510e+00 -4.77393238e+00  7.95908523e+00  1.92325205e+00  -7.18504140e+00 -1.22510116e+01  2.55674780e+00  4.64671047e+00  -8.01048001e+00  1.08157827e+01 -1.94160498e+00 -6.17413987e+00  -1.11653391e+01  6.05140733e+00  7.07141974e+00 -6.10921790e+00  -3.80019914e+00 -1.02909642e+01  1.96867941e+00  5.24443249e+00   4.78028897e+00  1.19571914e+01  6.28946614e+00  4.79423174e+00   7.97336424e+00 -1.07988410e+00  1.12222201e+01 -6.46459059e+00   4.29360952e-01  9.53635479e+00  8.88714479e+00 -4.82616647e+00  -9.49782749e+00  8.26966741e+00 -8.47419249e+00 -1.06866070e+00   1.01734433e+00 -7.81109968e+00 -3.93336388e+00 -1.20250022e+01   9.40833887e+00  1.09078915e+01  6.83920543e+00 -1.16427596e+01   1.03922819e+01 -7.88152648e+00 -1.14732064e+01 -1.06747656e+01   8.80438740e+00 -8.43821282e+00  7.70779315e+00 -3.28956946e+00 .......

資料對不上:??(見下)

----------------------------

X_Col1 =np.random.uniform( r1*np.cos(theta1),r2*np.cos(theta1),num)

X_Col1 =X_Col[:, np.newaxis]

float64 [ 9.63057718e+00 -8.89313282e+00  3.86946000e+00  4.95165355e-01   2.45909802e+00 -1.05677993e+00  6.11050453e+00 -5.50608221e+00  -8.02435169e+00 -9.48476236e+00  1.09073492e+01 -7.26462285e+00   1.08027047e+01 -1.25535557e+01  1.04945933e+01 -1.14269865e+01  -2.67661388e+00 -6.55422847e+00 -2.84605786e+00  5.59903410e+00   5.21495147e+00  9.19405019e+00 -1.08143459e+01  5.35552559e-01 ......]

[[ 9.63057718e+00]  [-8.89313282e+00]  [ 3.86946000e+00]  [ 4.95165355e-01]  [ 2.45909802e+00]  [-1.05677993e+00]  [ 6.11050453e+00]  [-5.50608221e+00]  [-8.02435169e+00]  [-9.48476236e+00]  [ 1.09073492e+01] ..........]

把1x1000資料轉換為1000x1資料

2、

numpy.vstack()函式

函式原型:numpy.vstack(tup)

等價於:np.concatenate(tup, axis=0) if tup contains arrays thatare at least 2-dimensional.

  1. >>> a = np.array([1, 2, 3])  
  2. >>> b = np.array([2, 3, 4])  
  3. >>> np.vstack((a,b))  
  4. array([[1, 2, 3],  
  5.        [2, 3, 4]])  
  6. >>>  
  7. >>> a = np.array([[1], [2], [3]])  
  8. >>> b = np.array([[2], [3], [4]])  
  9. >>> np.vstack((a,b))  
  10. array([[1],  
  11.        [2],  
  12.        [3],  
  13.        [2],  
  14.        [3],  
  15.        [4]])  

numpy.hstack()函式 函式原型:numpy.hstack(tup) 按列合併

  1. >>> a = np.array((1,2,3))  
  2. >>> b = np.array((2,3,4))  
  3. >>> np.hstack((a,b))  
  4. array([1, 2, 3, 2, 3, 4])  
  5. >>> a = np.array([[1],[2],[3]])  
  6. >>> b = np.array([[2],[3],[4]])  
  7. >>> np.hstack((a,b))  
  8. array([[1, 2],  
  9.        [2, 3],  
  10.        [3, 4]])  

3、transpose:

def produce_random_data(r,w,d,num):
    X1 = np.random.uniform(-r-w/2,2*r+w/2, num)
    X2 = np.random.uniform(-r - w / 2-d, r+w/2, num)
    X = np.vstack((X1, X2))
    print( X.transpose())

>>> a = array([[[ 0,  1,  2,  3],                 [ 4,  5,  6,  7]],                [[ 8,  9, 10, 11],                 [12, 13, 14, 15]]]) >>> b = a.transpose(1,0,2) array([[[ 0,  1,  2,  3],         [ 8,  9, 10, 11]],        [[ 4,  5,  6,  7],         [12, 13, 14, 15]]]) 原文:https://blog.csdn.net/zhangleaimeiling/article/details/78052235 

陣列a中10的座標為a(1,0,3),經過transpose(1,0,2)轉置後的陣列b中的10的座標為b(0,1,3)。原始的transpose引數(預設的引數)為(0,1,2),這個轉置相當於將第一個座標與第二座標進行了互換。

4、tensorflow中的placeholder及用法。placeholder,中文意思是佔位符,在tensorflow中類似於函式引數,執行時必須傳入值。