Numpy系列之ndarray数组的维度变换,拼接,分割和复制
ndarray数组的维度变换,拼接,分隔和复制ndarray数组的维度变换将1维数组转换为2维数组将2维数组转换为1维数组交换二维数组的两个维度(矩阵的转置)ndarray数组的拼接给已有的数据添加多行,比如增添一些样本数据给已有的数据添加多列,比如增添一些特征进去ndarray数组的分隔将已有的数据按行分隔,比如将训练集分割成训练集和验证集将已有的数据按列分隔,比如将特征和标签分隔数组的赋值nd
文章共3,267字 · 阅读需要大约11分钟
一键AI生成摘要,助你高效阅读
问答
·
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]])
b = a.flatten()
c = a.ravel()
d = a.reshape(-1)
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]
交换二维数组的两个维度(矩阵的转置)
代码示例
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.transpose()
c = a.T
d = a.swapaxes(0,1)
e = np.transpose(a,(1,0))
print(a) # 输出[[1 2 3] [[4 5 6]]
print(b) # 输出[[1 4] [2 5] [3 6]]
print(c) # 输出[[1 4] [2 5] [3 6]]
print(d) # 输出[[1 4] [2 5] [3 6]]
print(e) # 输出[[1 4] [2 5] [3 6]]
给数组增加一个维度
import numpy as np
a = np.arange(5)
print(a.shape) # 输出(5,)
# a = np.reshape(a, (1,5))
a = np.reshape(a, (1,-1))
print(a.shape) # 输出(1,5)
b = np.arange(5)
print(b.shape) # 输出(5,)
b = np.expand_dims(b,axis=0)
print(b.shape) # 输出(1,5)
ndarray数组的拼接
给已有的数据添加多行,比如增添一些样本数据
代码展示:
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.arange(7,13).reshape(2,3)
c = np.concatenate([a,b],axis=0)
d = np.vstack([a,b])
print(a) # 输出[[1 2 3] [4 5 6]]
print(b) # 输出[[ 7 8 9] [10 11 12]]
print(c) # 输出[[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]
print(d) # 输出[[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]
给已有的数据添加多列,比如增添一些特征进去
代码展示:
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.arange(7,13).reshape(2,3)
c = np.concatenate([a,b],axis=1)
d = np.hstack([a,b])
print(a) # 输出[[1 2 3] [4 5 6]]
print(b) # 输出[[ 7 8 9] [10 11 12]]
print(c) # 输出[[ 1 2 3 7 8 9] [ 4 5 6 10 11 12]]
print(d) # 输出[[ 1 2 3 7 8 9] [ 4 5 6 10 11 12]]
ndarray数组的分割
将已有的数据按行分割,比如将训练集分割成训练集和验证集
import numpy as np
a = np.arange(1,19).reshape(6,3)
b,c = np.split(a,[4],axis=0)
d,e = np.vsplit(a,[4])
print(a) #输出为 [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12] [13 14 15] [16 17 18]]
print(b) #前4个样本为1个数组 [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]
print(c) #余下的样本为1个数组 [[13 14 15] [16 17 18]]
print(d) #前4个样本为1个数组 [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]]
print(e) #余下的样本为1个数组 [[13 14 15] [16 17 18]]
将已有的数据按列分割,比如将特征和标签分隔
import numpy as np
a = np.arange(1,13).reshape(2,6)
b,c = np.split(a,[-1],axis=1)
d,e = np.hsplit(a,[-1])
print(a) #输出为 [[ 1 2 3 4 5 6] [ 7 8 9 10 11 12]]
print(b) #前面的n-1列特征为1个数组 [[ 1 2 3 4 5] [ 7 8 9 10 11]]
print(c) #最后一列的特征为1个数组 [[ 6] [12]]
print(d) #前面的n-1列特征为1个数组 [[ 1 2 3 4 5] [ 7 8 9 10 11]]
print(e) #最后一列的特征为1个数组 [[ 6] [12]]
数组的复制
import numpy as np
a = np.array([1,2,3])
b = a
c = a[:]
d = np.copy(a)
print(b is a, c is a , d is a) # 输出 True False False
d[0] = 10
print(a,d) # 输出[1 2 3] [10 2 3] d变,a不变
c[0] = 100
print(a,c) #输出[100 2 3] [100 2 3],c变,a变
数组到列表的转换
np.random.seed(123)
arr = np.random.randint(1,10,(3,3))
arr = arr.tolist()
print(arr) # 输出 [[3, 3, 7], [2, 4, 7], [2, 1, 2]]
更多推荐
已为社区贡献4条内容
所有评论(0)