1.2 NumPy数组基础

第一章 numpy入门

1.2 numpy数组基础

1.2.1 数组的属性

import numpy as np
np.random.seed(0)

x1 = np.random.randint(10,size=6)
x2 = np.random.randint(10,size=(3,4))
x3 = np.random.randint(10,size=(3,4,5))

print("x3 ndim:",x3.ndim)
print("x3 shape:",x3.shape)
print("x3 size:",x3.size)
print("x3 dtype:",x3.dtype)
print("x3 itemsize:",x3.itemsize,"bytes")
print("x3 nbytes:",x3.nbytes,"bytes")
x3 ndim: 3
x3 shape: (3, 4, 5)
x3 size: 60
x3 dtype: int32
x3 itemsize: 4 bytes
x3 nbytes: 240 bytes

1.2.2 数组的索引:获取单个元素

1.正向索引取值

print(x1)
print(x1[0])
print(x1[5])
[5 0 3 3 7 9]
5
9

2.反向索引取值

print(x1[-1])
print(x1[-3])
9
3

3.多维数组索引取值

print(x2)
print(x2[1,2])
print(x2[2,1])
print(x2[-1,-2])
[[3 5 2 4]
 [7 6 8 8]
 [1 6 7 7]]
8
6
7

4.索引修改值

x2[0,0] = 23
print(x2)
[[23  5  2  4]
 [ 7  6  8  8]
 [ 1  6  7  7]]

1.2.3 数组切片:获取子数组

1.一维子数组

# 切片格式:
#    x[start:stop:step]
x = np.arange(10)
print("原数组:\n",x)
print("前5个元素:\n",x[:5])
print("索引5之后元素:\n",x[5:])
print("中间的子数组:\n",x[4:7])
print("每隔一个元素:\n",x[::2])
print("每隔一个元素,从索引1开始:\n",x[1::2])
print("所有元素,逆序:\n",x[::-1])
print("从索引4开始每隔一个元素逆序:\n",x[4::-2])
原数组:
 [0 1 2 3 4 5 6 7 8 9]
前5个元素:
 [0 1 2 3 4]
索引5之后元素:
 [5 6 7 8 9]
中间的子数组:
 [4 5 6]
每隔一个元素:
 [0 2 4 6 8]
每隔一个元素,从索引1开始:
 [1 3 5 7 9]
所有元素,逆序:
 [9 8 7 6 5 4 3 2 1 0]
从索引4开始每隔一个元素逆序:
 [4 2 0]

2.多维子数组

print("原数组:\n",x2)
print("两行三列:\n",x2[:2,:3])
print("所有行,每隔一列:\n",x2[:,::2])
print("行逆序:\n",x2[:,::-1])
print("列逆序:\n",x2[::-1,:])
print("行列逆序:\n",x2[::-1,::-1])
原数组:
 [[23  5  2  4]
 [ 7  6  8  8]
 [ 1  6  7  7]]
两行三列:
 [[23  5  2]
 [ 7  6  8]]
所有行,每隔一列:
 [[23  2]
 [ 7  8]
 [ 1  7]]
行逆序:
 [[ 4  2  5 23]
 [ 8  8  6  7]
 [ 7  7  6  1]]
列逆序:
 [[ 1  6  7  7]
 [ 7  6  8  8]
 [23  5  2  4]]
行列逆序:
 [[ 7  7  6  1]
 [ 8  8  6  7]
 [ 4  2  5 23]]

3.获取数组的行和列

print("原数组:\n",x2)
print("获取指定行:\n",x2[2,:])
print("获取指定列:\n",x2[:,1])
print("获取行时,可以省略空的切片:\n",x2[0]) # 等价于 x2[0,:]
原数组:
 [[23  5  2  4]
 [ 7  6  8  8]
 [ 1  6  7  7]]
获取指定行:
 [1 6 7 7]
获取指定列:
 [5 6 6]
获取行时,可以省略空的切片:
 [23  5  2  4]

4.数组的视图(即切片)

print("原数组:\n",x2)
sub_arr = x2[:2,:2]
print("抽取2×2的子数组:\n",sub_arr)
sub_arr[0,0] = 88
print("修改后的子数组:\n",sub_arr)
print("原数组:\n",x2)
# 数组的视图修改值会修改数组本身,它意味着在处理非常大的数据集时,可以获取或处理这些数据集的片段,而不用复制底层的数据缓存。
原数组:
 [[88  5  2  4]
 [ 7  6  8  8]
 [ 1  6  7  7]]
抽取2×2的子数组:
 [[88  5]
 [ 7  6]]
修改后的子数组:
 [[88  5]
 [ 7  6]]
原数组:
 [[88  5  2  4]
 [ 7  6  8  8]
 [ 1  6  7  7]]

5.数组的副本(即复制)

print("原数组;\n",x2)
sub_arr_copy = x2[:2,:2].copy()
print("拷贝2×2的子数组:\n",sub_arr_copy)
sub_arr_copy[0,0] = 100
print("修改后的子数组:\n",sub_arr_copy)
print("原数组;\n",x2)
原数组;
 [[88  5  2  4]
 [ 7  6  8  8]
 [ 1  6  7  7]]
拷贝2×2的子数组:
 [[88  5]
 [ 7  6]]
修改后的子数组:
 [[100   5]
 [  7   6]]
原数组;
 [[88  5  2  4]
 [ 7  6  8  8]
 [ 1  6  7  7]]

1.2.4 数组的变形

# 数组的变形最灵活的方式是通过reshape() 函数来实现。
grid = np.arange(1,10).reshape(3,3)
print("将数字1-9放入一个3×3的矩阵中:\n",grid)

# 请注意,如果该方法可行,那么原数组的大小必须和变形后的数组大小一致。如果满足这个条件,
# reshape方法将会用到原数组的一个非副本视图。但实际情况是,在非连续的数据缓存的情况下,
# 返回非副本视图往往不可能实现。

# 另一种常见的变形模式时将一个一维数组变形为二维数组的行或列的矩阵。你也可以通过reshape
# 方法来实现,或者更简单的通过在一个切片操作中利用newaxis关键字:
x = np.array([1,2,3])
print("原数组:\n",x)
print("通过变形获得行向量:\n",x.reshape(1,3))
print("通过newaxis获取行向量:\n",x[np.newaxis,:])
print("通过变形获取列向量:\n",x.reshape((3,1)))
print("通过newaxis获取列向量:\n",x[:,np.newaxis])
将数字1-9放入一个3×3的矩阵中:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
原数组:
 [1 2 3]
通过变形获得行向量:
 [[1 2 3]]
通过newaxis获取行向量:
 [[1 2 3]]
通过变形获取列向量:
 [[1]
 [2]
 [3]]
通过newaxis获取列向量:
 [[1]
 [2]
 [3]]

1.2.5 数组的拼接和分裂

1.数组的拼接

# 拼接numpy数组主要由 np.concatenate、np.vstack和np.hstack函数实现。
x = np.array([1,2,3])
y = np.array((7,6,15))
z = np.array([43,3,53])
arr_2n = np.array([[1,2,3],
                   [4,5,6],
                   [7,8,9]])
# 一维数组的拼接
print("拼接x和y数组:\n",np.concatenate([x,y])) # concatenate传入元祖
print("拼接x和y和z数组:\n",np.concatenate((x,x,z))) # concatenate传入列表
# 二维数组的拼接
print("沿第一个轴拼接:\n",np.concatenate([arr_2n,arr_2n]))
print("沿第二个轴拼接:\n",np.concatenate((arr_2n,arr_2n),axis=1))
# 沿着固定维度处理数据时,使用np.vstack(垂直栈)和np.hstack(水平栈)函数会更简洁:
print("垂直栈数组:\n",np.vstack([x,arr_2n]))
print("水平栈数组:\n",np.hstack([arr_2n,x[:,np.newaxis]]))
拼接x和y数组:
 [ 1  2  3  7  6 15]
拼接x和y和z数组:
 [ 1  2  3  1  2  3 43  3 53]
沿第一个轴拼接:
 [[1 2 3]
 [4 5 6]
 [7 8 9]
 [1 2 3]
 [4 5 6]
 [7 8 9]]
沿第二个轴拼接:
 [[1 2 3 1 2 3]
 [4 5 6 4 5 6]
 [7 8 9 7 8 9]]
垂直栈数组:
 [[1 2 3]
 [1 2 3]
 [4 5 6]
 [7 8 9]]
水平栈数组:
 [[1 2 3 1]
 [4 5 6 2]
 [7 8 9 3]]

2.数组的分裂

# 分裂numpy数组主要由 np.split、np.vsplit和np.hsplit函数实现。
# 可以向以上函数传传入一个索引列表作为参数,索引列表记录的是分裂点的位置:
split_date = np.array([2,3,5,1,3,5,6,34,43,23,1])
grid = np.arange(16).reshape(4,4)
print("原数组split_date:\n",split_date,"\n原数组grid:\n",grid)
x1,x2,x3 = np.split(split_date,[3,5])
print("分裂后的数组:\n",x1,x2,x3)
upper,lower = np.vsplit(grid,[2])
print("垂直分裂upper:\n",upper,"\n垂直分裂lower:\n",lower)
left,right = np.hsplit(grid,[2])
print("水平分裂left:\n",left,"\n水平分裂right:\n",right)

原数组split_date:
 [ 2  3  5  1  3  5  6 34 43 23  1] 
原数组grid:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
分裂后的数组:
 [2 3 5] [1 3] [ 5  6 34 43 23  1]
垂直分裂upper:
 [[0 1 2 3]
 [4 5 6 7]] 
垂直分裂lower:
 [[ 8  9 10 11]
 [12 13 14 15]]
水平分裂left:
 [[ 0  1]
 [ 4  5]
 [ 8  9]
 [12 13]] 
水平分裂right:
 [[ 2  3]
 [ 6  7]
 [10 11]
 [14 15]]

本文地址:https://www.cnblogs.com/-xiaoyu-/p/12293645.html

posted on 2020-02-11 01:33  -小鱼-  阅读(321)  评论(0编辑  收藏  举报

导航