np.repeat()函数

2024-06-15 18:32
文章标签 函数 np repeat

本文主要是介绍np.repeat()函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

np.repeat函数

  • 使用np.repeat()展平二维数组
  • np.repeat()函数的坐标轴问题
    • 二维
    • 三维

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

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

代码如下:

import numpy as npclass 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]
"""    

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

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

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

二维

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

import numpy as npclass 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 npclass 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()函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1064240

相关文章

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

C++操作符重载实例(独立函数)

C++操作符重载实例,我们把坐标值CVector的加法进行重载,计算c3=c1+c2时,也就是计算x3=x1+x2,y3=y1+y2,今天我们以独立函数的方式重载操作符+(加号),以下是C++代码: c1802.cpp源代码: D:\YcjWork\CppTour>vim c1802.cpp #include <iostream>using namespace std;/*** 以独立函数

函数式编程思想

我们经常会用到各种各样的编程思想,例如面向过程、面向对象。不过笔者在该博客简单介绍一下函数式编程思想. 如果对函数式编程思想进行概括,就是f(x) = na(x) , y=uf(x)…至于其他的编程思想,可能是y=a(x)+b(x)+c(x)…,也有可能是y=f(x)=f(x)/a + f(x)/b+f(x)/c… 面向过程的指令式编程 面向过程,简单理解就是y=a(x)+b(x)+c(x)

利用matlab bar函数绘制较为复杂的柱状图,并在图中进行适当标注

示例代码和结果如下:小疑问:如何自动选择合适的坐标位置对柱状图的数值大小进行标注?😂 clear; close all;x = 1:3;aa=[28.6321521955954 26.2453660695847 21.69102348512086.93747104431360 6.25442246899816 3.342835958564245.51365061796319 4.87

OpenCV结构分析与形状描述符(11)椭圆拟合函数fitEllipse()的使用

操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C++11 算法描述 围绕一组2D点拟合一个椭圆。 该函数计算出一个椭圆,该椭圆在最小二乘意义上最好地拟合一组2D点。它返回一个内切椭圆的旋转矩形。使用了由[90]描述的第一个算法。开发者应该注意,由于数据点靠近包含的 Mat 元素的边界,返回的椭圆/旋转矩形数据

Unity3D 运动之Move函数和translate

CharacterController.Move 移动 function Move (motion : Vector3) : CollisionFlags Description描述 A more complex move function taking absolute movement deltas. 一个更加复杂的运动函数,每次都绝对运动。 Attempts to

✨机器学习笔记(二)—— 线性回归、代价函数、梯度下降

1️⃣线性回归(linear regression) f w , b ( x ) = w x + b f_{w,b}(x) = wx + b fw,b​(x)=wx+b 🎈A linear regression model predicting house prices: 如图是机器学习通过监督学习运用线性回归模型来预测房价的例子,当房屋大小为1250 f e e t 2 feet^

JavaSE(十三)——函数式编程(Lambda表达式、方法引用、Stream流)

函数式编程 函数式编程 是 Java 8 引入的一个重要特性,它允许开发者以函数作为一等公民(first-class citizens)的方式编程,即函数可以作为参数传递给其他函数,也可以作为返回值。 这极大地提高了代码的可读性、可维护性和复用性。函数式编程的核心概念包括高阶函数、Lambda 表达式、函数式接口、流(Streams)和 Optional 类等。 函数式编程的核心是Lambda

PHP APC缓存函数使用教程

APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”。它为我们提供了缓存和优化PHP的中间代码的框架。 APC的缓存分两部分:系统缓存和用户数据缓存。(Linux APC扩展安装) 系统缓存 它是指APC把PHP文件源码的编译结果缓存起来,然后在每次调用时先对比时间标记。如果未过期,则使用缓存的中间代码运行。默认缓存 3600s(一小时)。但是这样仍会浪费大量C

PHP7扩展开发之函数方式使用lib库

前言 首先说下什么是lib库。lib库就是一个提供特定功能的一个文件。可以把它看成是PHP的一个文件,这个文件提供一些函数方法。只是这个lib库是用c或者c++写的。 使用lib库的场景。一些软件已经提供了lib库,我们就没必要再重复实现一次。如,原先的mysql扩展,就是使用mysql官方的lib库进行的封装。 在本文,我们将建立一个简单的lib库,并在扩展中进行封装调用。 代码 基础