python学习笔记15 模块numpy函数

版权声明: https://blog.csdn.net/qq_40025335/article/details/83184184

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
>>>
小结:多维数据提取一维数据后还原为多维数组

例子form:https://blog.csdn.net/xtingjie/article/details/72510834

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. >>>  
  8.   
  9. >>> a = np.array([[1], [2], [3]])  
  10. >>> b = np.array([[2], [3], [4]])  
  11. >>> np.vstack((a,b))  
  12. array([[1],  
  13.        [2],  
  14.        [3],  
  15.        [2],  
  16.        [3],  
  17.        [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]])  

类似有:Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()

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中类似于函数参数,运行时必须传入值。

猜你喜欢

转载自blog.csdn.net/qq_40025335/article/details/83184184