Numpy系列之ndarray数组的维度变换,拼接,分割和复制

ndarray数组的元素类型变换

arr = np.random.randint(1,10,(3,3))
# astype方法返回一个新数组
arr = arr.astype(float) # arr.dtype 是查看数据属性

ndarray数组的维度变换

方法 说明
.reshape(shape) 不改变数组元素,返回一个shape形状的数组,原数组不变
.resize(shape) 与.reshape( )功能一致,但修改原数组
.swapaxes(ax1,ax2) 将数组n个维度中两个维度进行调换
.flatten( ) 对数组进行降维,返回折叠后的一维数组,原数组不变

将1维数组转换为2维数组

import numpy as np
a=np.arange(6)
##修改原数组成2行3列
a.shape=2,3
a

代码示例:

import numpy as np
a = np.array([1,2,3,4,5,6])
b = a.reshape(2,3)
c = a.reshape((2,3))# 等价于 b = a.reshape(2,3)
d = np.reshape(a,(2,3))
print(a) # 输出[1 2 3 4 5 6]
print(b) # 输出[[1 2 3] [4 5 6]]
print(c) # 输出[[1 2 3] [4 5 6]]
print(d) # 输出[[1 2 3] [4 5 6]]

将2维数组转换为1维数组

代码示例:

import numpy as np
a = np.array([[1,2,3],[4,5,6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值