常遗忘的函数,f.reduce_sum,np.argsort ,np.concatenate,np.argmax,
1)
其中,tf.reduce_sum用法如下:
# 'x' is [[1, 1, 1]
# [[1, 1, 1]]
#求和
tf.reduce_sum(x) ==> 6
#按列求和
tf.reduce_sum(x, 0) ==> [2, 2, 2]
#按行求和
tf.reduce_sum(x, 1) ==> [3, 3]
#按照行的维度求和
tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]
#行列求和
tf.reduce_sum(x, [0, 1]) ==> 6
其中,tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。
tf实现if 语句:
2)
numpy.argsort(a, axis=-1, kind=’quicksort’, order=None) 功能:
将矩阵a按照axis排序,并返回排序后的下标 参数:
a:输入矩阵, axis:需要排序的维度 返回值:
输出排序后的下标
>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])
等同于 从小到大排序:[1,2,3] 其对应索引为:[1,2,0]
>>> x = np.array([[1, 5, 7], [3, 2, 4]])
>>> np.argsort(x, axis=0)
array([[0, 1, 1],
[1, 0, 0]]) #列元素进行排序
等同于,按列排序:[[1,2,4][3,5,7]] 其对应索引为:[[0,1,1][1,0,0]]
>>> np.argsort(x, axis=1)
array([[0, 1, 2],
[1, 0, 2]]) #行元素进行排序
等同于,按行排序 : [[1,5,7][2,3,4]] 其对应索引为:[[0,1,2][1,0,2]]
3)
np.concatenate :拼接函数
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
In [25]: np.concatenate((a, b), axis=0) #按列拼接(拼接在每列的后面)
Out[25]: array([[1, 2], [3, 4], [5, 6]])
In [28]: np.concatenate((a,b.T),axis = 1) #按行拼接(拼接在每行后面)
Out[28]: array([[1, 2, 5], [3, 4, 6]])
4)
-
import numpy as np
-
a = np.array([3, 1, 2, 4, 6, 1])
-
b=np.argmax(a)#取出a中元素最大值所对应的索引,此时最大值位6,其对应的位置索引值为4,(索引值默认从0开始)
-
print(b) #4