首页
随机页面
分类
Python教程
Python HOME]]
Python简介]]
Python入门]]
Python语法]]
Python注释]]
Python变量]]
Python数据类型]]
Python数字]]
Python类型转换]]
Python字符串]]
Python布尔值]]
Python运算符]]
Python列表]]
Python元组]]
Python集合]]
Python字典]]
Python If...Else]]
Python While循环]]
Python For循环]]
Python函数]]
Python Lambda]]
Python数组]]
Python类/对象]]
Python继承]]
Python迭代器]]
Python作用域]]
Python模块]]
Python日期]]
Python JSON]]
Python正则表达式]]
Python PIP包管理]]
Python Try...Except]]
Python输入交互]]
Python字符串格式化]]
文件处理
Python文件处理]]
Python文件读取]]
Python文件读写]]
Python文件删除]]
Python NumPy
NumPy简介]]
NumPy入门]]
NumPy数组创建]]
NumPy数组索引]]
NumPy数组切片]]
NumPy数据类型]]
NumPy数组复制与视图]]
NumPy数组形状]]
NumPy数组重塑]]
NumPy数组迭代]]
NumPy数组连接]]
NumPy数组拆分]]
NumPy数组搜索]]
NumPy数组排序]]
NumPy数组过滤]]
NumPy随机]]
Random简介]]
Data Distribution]]
Random Permutation]]
Seaborn Module]]
Normal Distribution]]
Binomial Distribution]]
Poisson Distribution]]
Uniform Distribution]]
Logistic Distribution]]
Multinomial Distribution]]
Exponential Distribution]]
Chi Square Distribution]]
Rayleigh Distribution]]
Pareto Distribution]]
Zipf Distribution]]
NumPy ufunc]]
ufunc Intro]]
ufunc Create Function]]
ufunc Simple Arithmetic]]
ufunc Rounding Decimals]]
ufunc Logs]]
ufunc Summations]]
ufunc Products]]
ufunc Differences]]
ufunc Finding LCM]]
ufunc Finding GCD]]
ufunc Trigonometric]]
ufunc Hyperbolic]]
ufunc Set Operations]]
机器学习
入门]]
Mean Median Mode]]
Standard Deviation]]
Percentile]]
Data Distribution]]
Normal Data Distribution]]
Scatter Plot]]
Linear Regression]]
Polynomial Regression]]
Multiple Regression]]
Scale]]
Train/Test]]
Decision Tree]]
Python MySQL
MySQL入门]]
MySQL创建数据库]]
MySQL创建表]]
MySQL插入]]
MySQL查询]]
MySQL Where]]
MySQL Order By]]
MySQL Delete]]
MySQL Drop Table]]
MySQL Update]]
MySQL Limit]]
MySQL Join]]
Python MongoDB
MongoDB入门]]
MongoDB创建数据库]]
MongoDB创建集合]]
MongoDB Insert]]
MongoDB Find]]
MongoDB Query]]
MongoDB Sort]]
MongoDB Delete]]
MongoDB Drop Collection]]
MongoDB Update]]
MongoDB Limit]]
Python参考
Python概述]]
Python内建函数]]
Python字符串方法]]
Python列表方法]]
Python字典方法]]
Python元组方法]]
Python集合方法]]
Python文件方法]]
Python关键字]]
Python异常]]
Python词汇表]]
模块参考
随机数模块]]
Requests模块]]
数学模块]]
CMath模块]]
Python How To
删除列表重复项]]
反转字符串]]
数字相加]]
查看“Python/numpy array reshape”的源代码
来自菜鸟教程
←
Python/numpy array reshape
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
<languages /> = NumPy数组重塑 = == 重塑数组 == 重塑意味着更改数组的形状。 数组的形状是每个维中元素的数量。 通过重塑,我们可以添加或删除维度或更改每个维度中的元素数量。 == 从一维变形为二维 == <div> === 例 === 将以下具有12个元素的1-D数组转换为2-D数组。 最外面的维度将具有4个数组,每个数组包含3个元素: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) newarr = arr.reshape(4, 3) print(newarr)</pre> </div> == 从1-D变形为3-D == <div> === 例 === 将以下具有12个元素的1-D数组转换为3-D数组。 最外面的维度将具有2个数组,其中包含3个数组,每个数组包含2个元素: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) newarr = arr.reshape(2, 3, 2) print(newarr)</pre> </div> == 我们可以重塑成任何形状吗? == 是的,只要重塑所需的元素在两种形状中均相等。 我们可以将2个2行2D数组中的8个元素1D数组重塑为4个元素,但是我们不能将其重塑为3个元素3行2D数组,因为这将需要3x3 = 9个元素。 <div> === 例 === 尝试将具有8个元素的1D数组转换为每个维度中具有3个元素的2D数组(将产生错误): <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) newarr = arr.reshape(3, 3) print(newarr)</pre> </div> == 返回复制还是查看? == <div> === 例 === 检查返回的数组是副本还是视图: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) print(arr.reshape(2, 4).base)</pre> </div> 上面的示例返回原始数组,因此它是一个视图。 == 未知尺寸 == 允许您使用一个“未知”维度。 这意味着您不必在整形方法中为尺寸之一指定确切的数字。 Pass <code> -1 </code> 作为值,NumPy将为您计算该数字。 <div> === 例 === 将8个元素的1D数组转换为2x2元素的3D数组: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) newarr = arr.reshape(2, 2, -1) print(newarr)</pre> </div> <div> '''注意:''' 我们不能通过 <code> -1 </code> 超过一个维度 </div> == 展平数组 == 展平数组意味着将多维数组转换为一维数组。 我们可以用 <code> reshape(-1) </code> 去做这个。 <div> === 例 === 将数组转换为一维数组: <pre class="notranslate pythonHigh"> import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) newarr = arr.reshape(-1) print(newarr)</pre> </div> <div> '''注意:''' 有很多函数可以更改numpy中数组的形状 <code> flatten </code> , <code> ravel </code> 并用于重新排列元素 <code> rot90 </code> , <code> flip </code> , <code> fliplr </code> , <code> flipud </code> etc.这些属于numpy的中级到高级部分。 </div> <br />
返回至“
Python/numpy array reshape
”。