【晕头晕脑的Python】Python中Reshape函数解析

article/2025/11/6 3:28:07

Reshape函数解析

  • Reshape()作用:
  • Reshape()实例说明:
    • 一维reshape() 为 二维
    • 二维数组 reshape 切片,逆置
    • 三维Reshape情况

Reshape()作用:

Reshape(),函数的作用就是将数据的按照既定的维度进行整理。

  • reshape(M,N):可以将数据整理为M X N的大小。
  • reshape(M, N)[:,:,:] :”[ ]“ ,方括号可以对而外的 M x N 维度的数据进行顺序的排布。

Reshape()实例说明:

初期数据及库准备:

import numpy as np  # 调用numpy库
# 设置一个1-18的列表
anchors = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
#将anchors由列表转换为数组的形式
anchors = np.array(anchors)

一维reshape() 为 二维

18个元素一维度数组,可以转换为2 x 93 X 6的二维数组

print(anchors.reshape([3,6]))  # 生成一个(3,6)的二维数组print(anchors.reshape([2,9]))  # 生成一个(2,9)的二维数组# print(anchors.reshape([3,3]))   # error,显示维度不匹配,无法生成
  • -anchors.reshape([3,6]):
    3,6的输出结果
  • anchors.reshape([2,9]):
    ]
  • anchors.reshape([3,3]):
    在这里插入图片描述
    现在得到了一个由维转换到二维的矩阵,但是发现想对现在这个矩阵的一些排布进行调整,就用到了reshape( )[ ] 中的**[ ]** 进行排序,

二维数组 reshape 切片,逆置

list的[]中有三个参数,用冒号分割 list[A:B:C]
A:相当于start_index,可以为空,默认是0 ----开始位置
B:相当于end_index,可以为空,默认是list.size()-----结束位置
C:步长,默认为1。步长为-1时,返回倒序原序列------每一步多走多远
ps:另外AB构成的区间是[A,B)的左闭右开区间,即B的值是取不到的,只会取到B-1的数

print(anchors.reshape([3,6])[:-1,:])
"""
-1表示最后一行或者一列,根据python的切片规则,[A:B]的切片包含A而不包含B,即是[A, B)的取值
原始结果为:
[[ 1  2  3  4  5  6][ 7  8  9 10 11 12][13 14 15 16 17 18]]
结果为:
[[ 1  2  3  4  5  6][ 7  8  9 10 11 12]]
"""
  • 在两个维度上切片
print(anchors.reshape([3,6])[:-1,:-1])
"""
-1表示最后一行或者一列,根据python的切片规则,[A:B]的切片包含A而不包含B,即是[A, B)的取值
原始结果为:
[[ 1  2  3  4  5  6][ 7  8  9 10 11 12][13 14 15 16 17 18]]
结果为:
[[ 1  2  3  4  5][ 7  8  9 10 11]]
"""
  • 按行逆置
print(anchors.reshape([3,6])[::-1,::])
"""
------------------------------------------------------------
> list的[]中有三个参数,用冒号分割 list[A:B:C]
> A:相当于start_index,可以为空,默认是0 ----开始位置
> B:相当于end_index,可以为空,默认是list.size()-----结束位置
> C:步长,默认为1。步长为-1时,返回倒序原序列------每一步多走多远
> ps:另外AB构成的区间是[A,B)的左闭右开区间,即B的值是取不到的,只会取到B-1的数-----------------------------------------------------------
原始结果为:
[[ 1  2  3  4  5  6]
[ 7  8  9 10 11 12]
[13 14 15 16 17 18]]
结果为:
[[13 14 15 16 17 18]
[ 7  8  9 10 11 12]
[ 1  2  3  4  5  6]]
"""
  • 按列逆置
print(anchors.reshape([3,6])[::,::-1])
"""
------------------------------------------------------------
> list的[]中有三个参数,用冒号分割 list[A:B:C]
> A:相当于start_index,可以为空,默认是0 ----开始位置
> B:相当于end_index,可以为空,默认是list.size()-----结束位置
> C:步长,默认为1。步长为-1时,返回倒序原序列------每一步多走多远
> ps:另外AB构成的区间是[A,B)的左闭右开区间,即B的值是取不到的,只会取到B-1的数-----------------------------------------------------------
原始结果为:
[[ 1  2  3  4  5  6]
[ 7  8  9 10 11 12]
[13 14 15 16 17 18]]
结果为:
[[ 6  5  4  3  2  1]
[12 11 10  9  8  7]
[18 17 16 15 14 13]]
"""

三维Reshape情况

三维情况与二维类似,可以创建验证。下面补充一种在reshape数值中有-1的情况,需要强调的一点就是:若reshape(-1,2),则会生成两列的数据,行数会由系统自动计算。

print(anchors.reshape([-1,2,3]))
"""
[[[ 1  2  3][ 4  5  6]][[ 7  8  9][10 11 12]][[13 14 15][16 17 18]]]
"""
print(anchors.reshape([-1,3]))
"""
[[ 1  2  3][ 4  5  6][ 7  8  9][10 11 12][13 14 15][16 17 18]]
"""

http://chatgpt.dhexx.cn/article/U8v3jAKQ.shtml

相关文章

python中reshape的用法

python中reshape的用法 reshape函数的使用: #reshape()是数组对象中的方法,用于改变数组的形状 arr [1,2,3,4,5,6,7,8,9] import numpy as np arrnp.array(arr) #一维 #变成一个3 * 3的二维矩阵: #方法一 arr.resha…

Python的reshape的用法

numpy中reshape函数的三种常见相关用法 reshape(1,-1)转化成1行: reshape(2,-1)转换成两行: reshape(-1,1)转换成1列: reshape(-1,2)转化成两列 numpy中reshape函数的三种常见相关用法 numpy.arange(n).reshape(a, b) 依次生成n个自然…

python中reshape函数用法详解

python中reshape函数用法详解 reshape函数 reshape函数是Numpy库中的一个函数,可以用于改变一个数组的形状,例如将一个二维数组转换成一个三维数组。 import numpy as np # 创建一个二维数组,形状为(4, 6) a np.array([[1, 2, 3, 4, 5, 6]…

ORA-12162 :TNS 指定的网络服务名不正确

原因:这台服务器有多个库 在环境变量文件/home/oracle/.bash_profile中也没有export ORACLE_SIDxxx 解决方法: 登录前先export ORACLE_SIDxxx 再确保查看一下echo $ORACLE_SID 再登录sqlplus / as sysdba

ORA-12162错误解决

新来的一个小伙儿,想学习Oracle我给了他文档,自己研究着,下午时段叫我过去,发现其在装oracle后,进行测试时报ORA-12162错误,正好本鸟之前也遇到过,这里面贴出了大家如有遇到不要惊慌。 报错图片…

ORA-12162: TNS:net service name is incorrectly specified

概述 因未设置系统环境变量ORACLE_SID导致ORA-12162错误 分析原因 首先登录数据库主机执行 oerr ora 12162 我们首先查看看下 tnsnames.ora文件 执行tnsping CC命令 检查下是否可以tnsping通,核对IP和端口以及实例名 数据库服务器端使用TNSNAMES.ORA中记录的…

Oracle“ ORA-12162:TNS:net服务名称指定不正确”错误和解决方案

During the connection to the Oracle Database server with the sqlplus we may get an error with the ORA-12162 specifier. This error will prevent to connect to the Oracle Database Server. 在使用sqlplus连接到Oracle数据库服务器的过程中,ORA-12162说明符可能会出错…

androidstudio自定义Dialog

新建一个XML文件&#xff0c;文件名为layout_custom_dialog <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"androi…

Kotlin中自定义dialog

文章目录 效果dialog样式dialog 背景 common_dialog_bgdialog布局 base_common_dialog_layout全局工具类 CommonDialog&#xff08;建造者模式&#xff09;使用 效果 dialog样式 <style name"custom_dialog2" parent"android:style/Theme.Dialog">&l…

Android 简单的自定义Dialog

效果图 Dialog的详细用法看这篇博客&#xff1a;http://blog.csdn.net/zhuwentao2150/article/details/51478053 我们自定义的CustomDialog是通过继承Dialog类并添加自定义的布局来实现的 /*** 自定义Dialog弹窗* Created by zhuwentao on 2016-08-19.*/ public class Cus…

实现自定义dialog样式

1定义弹出的dialog样式 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:orientation"vertical"android:layout_width"match_parent"a…

Android自定义Dialog+圆角处理

目录 一、自定义Dialog 二、Dialog添加圆角 一、自定义Dialog 自定义Dialog实现过程 本文的自定义dialog是在fragment中实现的&#xff0c;在Activity里面应该大同小异了。 android studio其实自带了很多种dialog 下面这个图是引用一位大佬的&#xff01; 这几天在学习自定…

Android 自定义Dialog实现(二)

在之前的文章中介绍了自定Dialog的实现方法之一&#xff1a;https://blog.csdn.net/m0_57487205/article/details/124775019?spm1001.2014.3001.5501https://blog.csdn.net/m0_57487205/article/details/124775019?spm1001.2014.3001.5501 这篇文章记录一下另外一种实现…

Android-自定义Dialog

Android-自定义Dialog 2014年4月27日 星期天 天气晴朗 心情平静 本篇博文来分享一个也是开发中经常需要用到的功能-自定义对话框&#xff0c;这里我用到了Android中的图形资源shape&#xff0c;具体使用方法&#xff0c;各位看代码吧&#xff0c;Android有多钟图形资源&#xf…

Flutter 自定义Dialog

我们项目开发中&#xff0c;有很多地方会用到dialog&#xff0c;虽然flutter自身也有&#xff0c;比如AboutDialog、AlertDialog、SimpleDialog、CupertinoAlertDialog等等之类的&#xff0c;但是这些满足不了我们的控制欲&#xff0c;我们想要的是它可以根据我们的想法而随改变…

安卓自定义dialog弹窗

1.先设置dialog样式&#xff0c;style.xml <!-- dialog样式 --><style name"DialogTheme" parent"android:style/Theme.Dialog"><!-- 边框 --><item name"android:windowFrame">null</item><!-- 是否浮现在act…

Android 自定义Dialog的实现

最新实现了一个自定义Dialog的需求&#xff0c;先看看效果图&#xff1a; 下面说说如何实现&#xff1a; 首先需要自定义一个Dialog类&#xff0c;继承自android.app.Dialog类。这个Dialog类就是要显示的对话框&#xff0c;包含双选按钮和单选按钮两种效果。本例中自定义Custo…

Android自定义Dialog对话框的几种方法(精简版)

自定义对话框是经常使用的功能&#xff0c;我们常用的弹窗操作&#xff0c;除了使用popwindow就是使用dialog来实现&#xff0c;这两种组件都支持之定义布局和功能来满足我们个性化的需求&#xff0c;也可以不采用自定义而直接使用系统封装好的api来实现功能。今天简单总结下在…

自定义Dialog的简单实现

自定义Dialog的详细步骤&#xff08;实现自定义样式一般原理&#xff09; 发表于2016/3/22 22:12:57 1410人阅读 分类&#xff1a; android开发 转载请标注转载http://blog.csdn.net/oqihaogongyuan/article/details/50958659 自定义Dialog的详细步骤&#xff08;实现自定义样…

安卓dialog的使用+如何自定义dialog

吐槽 哇哇哇&#xff0c;刚写一半win10给我蓝屏了&#xff0c;心塞塞&#xff0c;以后写一点保存一点。回到正题&#xff0c;看到产品给我的设计图&#xff0c;有辣么多的自定义的dialog&#xff0c;发现之前自己只会系统自带的dialog&#xff0c;但是这样根本满足不了产品的需…