寒假学习进度7:tensorflow2.0

tf.where语句

tf.where(条件语句,真返回A,假返回B)

import tensorflow as tf
a = tf.constant([1,2,3,4,5,6,7])
b = tf.constant([2,3,4,6,7,89,8])
c = tf.where(tf.greater(a,b),a,b) #tf.greater(a,b)比较a,b元素间的大小
print(c)
>>tf.Tensor([ 2  3  4  6  7 89  8], shape=(7,), dtype=int32)

返回一个【0,1)之间的随机数

np.random.RandomState().rand(维度)        #若维度为空则返回【0,1)之间的标量

import numpy as np
a = np.random.RandomState()
b= a.rand()
c = a.rand()
d = a.rand(2,5)
print(b,c,d)
>>0.5136594424255404 0.025006978847597727 [[0.50668218 0.45570855 0.78719687 0.92962176 0.2428056 ]
 [0.01391677 0.69533238 0.14489499 0.33269286 0.54619019]]

将两个数组按垂直方向叠加

np.vstack(数组1,数组2)

import numpy as np
a = np.array([[1,2,3],[3,4,6]])
b = np.array([4,5,6])
c = np.vstack((a,b))
print(c)
>>[[1 2 3]
 [3 4 6]
 [4 5 6]]

np.mgrid[]

np.mgrid[起始值:结束值:步长,起始值:结束值,步长...]

x.ravel()  将x变为一维数组

np.c_[]使返回的间隔数值点配对

np.c_[数值1,数值2,...]

import numpy as np
x,y = np.mgrid[1:3:1,2:4:0.5]   #包含1,不包含3,步长为1;包含2,不包含4,步长为0.5
grid = np.c_[x.ravel(),y.ravel()]   #将x,y转换为一维数组,并配对,生成grid
print("x=",x)
print("y=",y)
print("grid=",grid)
>>x= [[1. 1. 1. 1.]
 [2. 2. 2. 2.]]
y= [[2.  2.5 3.  3.5]
 [2.  2.5 3.  3.5]]
grid= [[1.  2. ]
 [1.  2.5]
 [1.  3. ]
 [1.  3.5]
 [2.  2. ]
 [2.  2.5]
 [2.  3. ]
 [2.  3.5]]   #构成网格坐标点

 

posted @ 2021-01-11 19:44  yangqqq  阅读(83)  评论(0编辑  收藏  举报