0
点赞
收藏
分享

微信扫一扫

【笔记】语言实例比较 2. 两数之和 C++ Rust Java Python

芥子书屋 2024-01-20 阅读 46

文章目录

1多维数组

a.shape=(3,2);既数组h=3,w=2
a.shape=(2,3,2);这里第一个2表示axis=0维度上的,三维数组中3,2)数组的个数,这里表示两个(3,2)数组。

压缩维度

  • 这里axis=0指代哪里是很重要的知识点。深度学习中经常压缩一个维度,axis=0。

语法:numpy.squeeze(a,axis = None);作用是将shape维度为1的去掉,但通常我们会指定axis=0,去除batchsize的维度。

扩充维度

  • np.expand_dims(a, axis=1)将得到shape为(m, 1, n, c)的新数组,新数组中的元素与原数组a完全相同。
    np.expand_dims(a, axis=2)将得到shape为(m, n, 1, c)的新数组,新数组中的元素与原数组a完全相同。
    np.expand_dims(a, axis=3)将得到shape为(m, n, c, 1)的新数组,新数组中的元素与原数组a完全相同。
    ————————————————

在这里插入图片描述
在这里插入图片描述

2numpy类型转换

深度学习常见的float32类型。

  • 函数
>>> a = np.random.random(4)
>>> a
array([ 0.0945377 ,  0.52199916,  0.62490646,  0.21260126])
>>> a.dtype
dtype('float64')
>>> a.shape
(4,)
>>> a.dtype = 'float32'
>>> a
array([  3.65532693e+20,   1.43907535e+00,  -3.31994873e-25,
         1.75549972e+00,  -2.75686653e+14,   1.78122652e+00,
        -1.03207532e-19,   1.58760118e+00], dtype=float32)
>>> a.shape
(8,)

3数组扁平化

假设C为三维数组
A = C.flatten()

4np.where()的用法

  • 一维数组,返回一个array
a = np.arange(8)
a
array([0, 1, 2, 3, 4, 5, 6, 7])
 
np.where(a>4)
(array([5, 6, 7], dtype=int64),)
  • 二维数组,返回两个array。返回的第一个array表示行坐标,第二个array表示纵坐标,两者一一对应。
b = np.arange(4*5).reshape(4,5)
 
b
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
 
np.where(b>14)
(array([3, 3, 3, 3, 3], dtype=int64), array([0, 1, 2, 3, 4], dtype=int64))

5np.argmax()

  • 语义分割中将多通道预测结果pred_mask转化为单通道mask
    np.argmax(pre_mask,axis=0)。即:在通道方向上找到哪个通道的置信度最大,比如1通道表示“汽车”,2“人”,3“猴子”,那么返回的索引值刚好对应label,将不同类别的像素点用不同颜色填充在原图上,这样就可以起到分割的效果。

6图像拼接

np.hstack(array1,array2)
np.vstack(array1,array2)

7生成同shape的图片,指定数据类型

# 以下是常用的两种类型
b = np.zeros(a.shape,dtype='float32')
dtype = np.int 
dtype = 'int8'
举报

相关推荐

0 条评论