python科学计算库numpy基础

Numpy是什么?

NumPyNumericalPython的缩写)是一个开源的Python科学计算库。使用NumPy,就可以很自然地使用数组和矩阵。NumPy包含很多实用的数学函数,涵盖线性代数运算、傅里叶变换和随机数生成等功能

Numpy基础

NumPy的主要对象是同种元素的多维数组。这是一个所有的元素都是一种类型。在NumPy中维度(dimensions)叫做轴(axis),轴的个数叫做秩(rank)NumPy的数组类被称作 ndarray(矩阵也叫数组)通常被称作数组。

常用的ndarray对象属性有:ndarray.ndim(数组轴的个数,轴的个数被称作秩)

ndarray.shape(数组的维度。这是一个指示数组在每个维度上大小的整数元组。例如一个nm列的矩阵,它的shape属性将是(2,3),这个元组的长度显然是秩,即维度或者ndim属性)

ndarray.size数组元素的总个数,等于shape属性中元组元素的乘积

ndarray.dtype一个用来描述数组中元素类型的对象,可以通过创造或指定dtype使用标准Python类型。另外NumPy提供它自己的数据类型)。

Numpy的数据类型:

Numpy内置的特征码:

#int8, int16, int32,int64 可以由字符串’i1’, ‘i2’,’i4’, ‘i8’代替

创建数组并查看其属性

两种方法创建

a = np.array([[1,2,3], [4, 5, 6]], dtype=int)
print(a.shape)       #  a.ndim, a.size, a.dtype
print(a.ndim)       #  a.ndim, a.size, a.dtype
print(a.dtype)       #  a.ndim, a.size, a.dtype
a = np.arange(10).reshape(2, 5) # 创建2行5列的二维数组,
# 也可以创建三维数组,
a = np.arange(12).reshape(2,3,2)
print(a)

 Numpy 随机数模块np.random.random, np.random.randn, np.random.rand的比较

  1. rand 生成均匀分布的伪随机数。分布在(0~1)之间
  2. randn 生成标准正态分布的伪随机数(均值为0,方差为1)
#=====随机函数=======
a=np.random.random(6)
b=np.random.rand(6)
c=np.random.randn(6)

常用函数

索引、切片和迭代

#一维数组索引、迭代、遍历
a = np.arange(10)**3
print(a)
#[开始:末尾:步长]
print(a[2:5])
#小于6以下的步长为2的变为-1000
a[:6:2] = -1000
print(a)
#逆序
print(a[ : :-1])
#遍历元素
for i in a:
    print(i)
print(a)

#二维数组索引、迭代、遍历
b=np.arange(20).reshape(5,4)
print(b)
print(b[2,3])#下面等价
print(b[2][3])
print(b[:,1])#下面等价
print(b[0:,1])#下面等价
print(b[0:5,1])
print(b[1:3,:])
print(b[-1])#下面等价
print(b[-1,:])#下面等价
print(b[-1,...])

# b[i] 中括号中的表达式被当作 i 和一系列 : ,来代表剩下的轴。NumPy也允许你使用“点”像 b[i,...] 。
# 点 (…)代表许多产生一个完整的索引元组必要的分号。如果x是
# 秩为5的数组(即它有5个轴),那么:x[1,2,…] 等同于 x[1,2,:,:,:],x[…,3] 等同于 x[:,:,:,:,3],x[4,…,5,:] 等同 x[4,:,:,5,:].


# 三维数组操作
c = np.arange(12).reshape(2,3,2)

print(c)
print(c[1])
print(c[0,1])
#c[2,1]    # 等价于c[2][1]
#c[2,1,1]  # 等价于c[2][1][1]
d = np.arange(10)**2
print(d)
e = np.array([3, 5, 6])
print(e)
print(d)
#花哨的索引NumPy比普通Python序列提供更多的索引功能。数组可以被整数数组和布尔数组索引。
print(d[e])


f = np.arange(12).reshape(3, 4)
g = f>4
print(g)
print(f[g])

#  np.flatten()返回一个折叠成一维的数组。但是该函数只能适用于numpy对象,即array或者mat,普通的list列表是不行的。
a = np.array([[1,2], [3, 4], [5, 6]])
print(a.flatten())
b = np.mat([[1,2,3], [4, 5, 6]])
print(b.flatten())

灵活切片

c = np.arange(20).reshape(4,5)
print(c[0,0])
print(c[0:3:2])
print(c[:-1])
#取第一行
print(c[0])
#取第一列
print(c[:,0])
print(c)
#取第一、四行
print(c[0:4:3])
print(c[[0,3]])
#取第一、三列
print(c[0:,0:3:2])
print(c[:,[0,2]])
#取二三行二三列
print(c[[1,2],1:3])

形状操作:

ravel(), vstack(),hstack(),column_stack,row_stack, stack, split, hsplit, vsplit

#合并
# a = np.arange(10).reshape(2,5)
# print(a.resize(5,2))
b = np.arange(6).reshape(2,3)
c = np.ones((2,3))
d = np.hstack((b,c))              # hstack:horizontal stack 左右合并
print(d)
e = np.vstack((b,c))              # vstack: vertical stack 上下合并
print(e)
f = np.column_stack((b,c))
print(f)
g = np.row_stack((b,c))
print(g)
h = np.stack((b, c), axis=1)      # 按行合并
print(h)
i = np.stack((b,c), axis=0)       # 按列合并
print(i)
j = np.concatenate ((b, c, c, b), axis=0)   #多个合并
print(j)
#分割
k = np.hsplit(i, 2)
print(k)
l = np.vsplit(i, 2)
print(l)
m = np.split(i, 2, axis=0)
print(m)
n = np.split(i, 2,axis=1)
print(n)
o = np.array_split(np.arange(10),3)   #不等量分割
print(o)

猜你喜欢

转载自blog.csdn.net/LuYi_WeiLin/article/details/85095713