iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >np.repeat()函数的具体使用
  • 430
分享到

np.repeat()函数的具体使用

np.repeat()函数np.repeat 2023-03-13 11:03:58 430人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录使用np.repeat()展平二维数组np.repeat()函数的坐标轴问题二维三维在 numpy 模块中的 repeat 函数,总是会出现设置 axis 坐标轴的情况,这时的坐

在 numpy 模块中的 repeat 函数,总是会出现设置 axis 坐标轴的情况,这时的坐标轴有时候就显的十分混乱,每到此处就不知道该给 axis 什么值。特写一篇博客来详细说明这个问题。

使用np.repeat()展平二维数组

代码如下:

import numpy as np


class Debug:
    def __init__(self):
        self.array1 = np.array([[1, 2], [3, 4]])

    def mainProgram(self):
        print("The value of array1 is: ")
        print(self.array1)
        print("The repeated array is: ")
        array2 = np.repeat(self.array1, repeats=1)
        print(array2)


if __name__ == '__main__':
    main = Debug()
    main.mainProgram()
"""
The value of array1 is: 
[[1 2]
 [3 4]]
The repeated array is: 
[1 2 3 4]
"""    

我们可以看到我们输入的是一个二维数组,当我们设定 repeats 值为 1 时,输出结果变成了一个一维数组,因此这时的 np.repeats 函数类似numpy.ndarray.flatten()函数的功能。

np.repeat()函数的坐标轴问题

接下来我们研究一下关于 axis 坐标轴的问题。

二维

对于数组是二维的情况,代码如下:

import numpy as np


class Debug:
    def __init__(self):
        self.array1 = np.array([[1, 2], [3, 4]])

    def mainProgram(self):
        print("The value of array1 is: ")
        print(self.array1)
        print("The array2 is: ")
        array2 = np.repeat(self.array1, repeats=2, axis=0)
        print(array2)
        print("The array3 is: ")
        array3 = np.repeat(self.array1, repeats=2, axis=1)
        print(array3)


if __name__ == '__main__':
    main = Debug()
    main.mainProgram()
"""
The value of array1 is: 
[[1 2]
 [3 4]]
The array2 is: 
[[1 2]
 [1 2]
 [3 4]
 [3 4]]
The array3 is: 
[[1 1 2 2]
 [3 3 4 4]]
"""

我们可以看到,axis=0 时表示沿着y方向重复,axis=1 时表示沿着x方向重复。我们可以对比numpy数组的坐标轴表示,二维时,坐标轴为 (y, x),从左向右第一个参数 0 便代表 y 轴,1 代表 x轴。

三维

接下来我们研究一下数组是三维的情况,代码如下:

import numpy as np


class Debug:
    def __init__(self):
        self.array1 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

    def mainProgram(self):
        print("The value of array1 is: ")
        print(self.array1)
        print("The array2 is: ")
        array2 = np.repeat(self.array1, repeats=2, axis=0)
        print(array2)
        print("The array3 is: ")
        array3 = np.repeat(self.array1, repeats=2, axis=1)
        print(array3)
        print("The array4 is: ")
        array4 = np.repeat(self.array1, repeats=2, axis=2)
        print(array4)


if __name__ == '__main__':
    main = Debug()
    main.mainProgram()
"""
The value of array1 is: 
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]
The array2 is: 
[[[1 2]
  [3 4]]

 [[1 2]
  [3 4]]

 [[5 6]
  [7 8]]

 [[5 6]
  [7 8]]]
The array3 is: 
[[[1 2]
  [1 2]
  [3 4]
  [3 4]]

 [[5 6]
  [5 6]
  [7 8]
  [7 8]]]
The array4 is: 
[[[1 1 2 2]
  [3 3 4 4]]

 [[5 5 6 6]
  [7 7 8 8]]]
"""

我们可以看到,axis=0 对应与沿着z轴重复,axis=1 对应沿着y轴重复,axis=2 对应沿着x轴重复。对比numpy坐标轴的表示,我们知道三维坐标轴为 (z, y, x),所以从左向右,0 对应z轴,1 对应 y轴,2 对应 x 轴。

到此这篇关于np.repeat()函数的具体使用的文章就介绍到这了,更多相关np.repeat()函数内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: np.repeat()函数的具体使用

本文链接: https://www.lsjlt.com/news/199529.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作