当前位置: 移动技术网 > IT编程>脚本编程>Python > 数据统计

数据统计

2019年05月13日  | 移动技术网IT编程  | 我要评论

青岛城市房产网,李伟希,李艳勤

outline

  • tf.norm

  • tf.reduce_min/max/mean

  • tf.argmax/argmin
  • tf.equal
  • tf.unique

vector norm

  • eukl. norm
    \[ ||x||_2=|\sum_{k}x_k^2|^{\frac{1}{2}} \]
  • max.norm
    \[ ||x||_{\infty}=max_k|x_k| \]
  • l1-norm
    \[ ||x||_1=\sum_{k}|x_k| \]

  • here talks about vector norm

eukl. norm

import tensorflow as tf
a = tf.ones([2, 2])
a
<tf.tensor: id=11, shape=(2, 2), dtype=float32, numpy=
array([[1., 1.],
       [1., 1.]], dtype=float32)>
tf.norm(a)
<tf.tensor: id=7, shape=(), dtype=float32, numpy=2.0>
tf.sqrt(tf.reduce_sum(tf.square(a)))
<tf.tensor: id=16, shape=(), dtype=float32, numpy=2.0>
a = tf.ones([4, 28, 28, 3])
a.shape
tensorshape([4, 28, 28, 3])
tf.norm(a)
<tf.tensor: id=25, shape=(), dtype=float32, numpy=96.99484>
tf.sqrt(tf.reduce_sum(tf.square(a)))
<tf.tensor: id=30, shape=(), dtype=float32, numpy=96.99484>

l1 norm

b = tf.ones([2, 2])
tf.norm(b)
<tf.tensor: id=45, shape=(), dtype=float32, numpy=2.0>
tf.norm(b, ord=2, axis=1)
<tf.tensor: id=51, shape=(2,), dtype=float32, numpy=array([1.4142135, 1.4142135], dtype=float32)>
tf.norm(b, ord=1)
<tf.tensor: id=56, shape=(), dtype=float32, numpy=4.0>
# 列为整体
tf.norm(b, ord=1, axis=0)
<tf.tensor: id=66, shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>
# 行为整体
tf.norm(b, ord=1, axis=1)
<tf.tensor: id=71, shape=(2,), dtype=float32, numpy=array([2., 2.], dtype=float32)>

reduce_min/max/mean

  • reduce,操作可能会有减维的功能,如[2,2],对行求max,会变成[2]
a = tf.random.normal([4, 10])
tf.reduce_min(a), tf.reduce_max(a), tf.reduce_mean(a)
(<tf.tensor: id=80, shape=(), dtype=float32, numpy=-2.215113>,
 <tf.tensor: id=82, shape=(), dtype=float32, numpy=1.9458845>,
 <tf.tensor: id=84, shape=(), dtype=float32, numpy=-0.045550883>)
# 对某一行求max
tf.reduce_min(a, axis=1), tf.reduce_max(a, axis=1), tf.reduce_mean(a, axis=1)
(<tf.tensor: id=98, shape=(4,), dtype=float32, numpy=array([-2.215113 , -1.5824796, -1.4861531, -1.3477703], dtype=float32)>,
 <tf.tensor: id=100, shape=(4,), dtype=float32, numpy=array([0.9380455, 1.1625607, 1.9458845, 1.492183 ], dtype=float32)>,
 <tf.tensor: id=102, shape=(4,), dtype=float32, numpy=array([-0.48791748,  0.25639585,  0.07420422, -0.02488617], dtype=float32)>)

argmax/argmin

a.shape
tensorshape([4, 10])
tf.argmax(a).shape
tensorshape([10])
# 返回index
tf.argmax(a)
<tf.tensor: id=112, shape=(10,), dtype=int64, numpy=array([1, 1, 2, 3, 2, 1, 3, 1, 2, 1])>
# 对第1维作用
tf.argmin(a).shape
tensorshape([10])
# 对第2维作用
tf.argmin(a, axis=1).shape
tensorshape([4])

tf.equal

a = tf.constant([1, 2, 3, 2, 5])
b = tf.range(5)
tf.equal(a, b)
<tf.tensor: id=186, shape=(5,), dtype=bool, numpy=array([false, false, false, false, false])>
res = tf.equal(a, b)
# 对true和false转换为1和0
tf.reduce_sum(tf.cast(res, dtype=tf.int32))
<tf.tensor: id=191, shape=(), dtype=int32, numpy=0>

accuracy

a = tf.random.normal([2, 3])
a
<tf.tensor: id=198, shape=(2, 3), dtype=float32, numpy=
array([[ 0.25201225, -1.3897187 ,  0.29240564],
       [-1.0671712 ,  2.1487093 ,  0.690736  ]], dtype=float32)>
pred = tf.cast(tf.argmax(a, axis=1), dtype=tf.int32)
pred.shape
tensorshape([2])
y = tf.constant([2, 1])
y
<tf.tensor: id=163, shape=(2,), dtype=int32, numpy=array([2, 1], dtype=int32)>
tf.equal(y, pred)
<tf.tensor: id=165, shape=(2,), dtype=bool, numpy=array([ true,  true])>
correct = tf.reduce_sum(tf.cast(tf.equal(y, pred), dtype=tf.int32))
correct
<tf.tensor: id=170, shape=(), dtype=int32, numpy=2>
correct / 2
<tf.tensor: id=175, shape=(), dtype=float64, numpy=1.0>

tf.unique

  • 用于去重
a = tf.range(5)
a
<tf.tensor: id=235, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>
# 返回索引
tf.unique(a)
unique(y=<tf.tensor: id=237, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>, idx=<tf.tensor: id=238, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>)
a = tf.constant([4, 2, 2, 4, 3])
a
<tf.tensor: id=226, shape=(5,), dtype=int32, numpy=array([4, 2, 2, 4, 3], dtype=int32)>
res = tf.unique(a)
unique(y=<tf.tensor: id=228, shape=(3,), dtype=int32, numpy=array([4, 2, 3], dtype=int32)>, idx=<tf.tensor: id=229, shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2], dtype=int32)>)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网