简介:Numpy:Numeric Python。
- 一个强大的N维数组对象Array
- 比较成熟的(广播)函数库
- 用于整合C/C++和Fortran代码的工具包
- 实用的线性代数、傅里叶变换和随机数生成函数
- numpy和稀疏矩阵运算包scipy配合使用更加强大
导入numpy库,并查看numpy版本
import numpy as np
np.__version__
01-创建ndarray
- 使用np.array()由python list创建
#一维
test = np.array([1,2,3,4,5])
#多维
test = np.array([[1,2,3],[4,5,6]])
注意:
numpy默认ndarray的所有元素的类型是相同的
如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int
02-使用np的routines函数创建
包含以下常见创建方法:
1.np.ones(shape, dtype=None, order=’C’)
np.ones([3,3])
输出结果:
array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])
np.ones([3,3],dtype=int)
输出结果:
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
2.np.zeros(shape, dtype=float, order=’C’)
3.np.full(shape, fill_value, dtype=None, order=’C’)
np.full([3,3],3.14)
输出结果:
array([[ 3.14, 3.14, 3.14],
[ 3.14, 3.14, 3.14],
[ 3.14, 3.14, 3.14]])
4.np.eye(N, M=None, k=0, dtype=float)
对角线为1其他的位置为0
np.eye(4)
输出结果:
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
np.eye(4,4,1)
输出结果:
array([[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.],
[ 0., 0., 0., 0.]])
np.eye(4,4,-1)
输出结果:
array([[ 0., 0., 0., 0.],
[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.]])
5.np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
6.np.arange([start, ]stop, [step, ]dtype=None)
等差数列
np.arange(0,10,2)
输出结果:
array([0, 2, 4, 6, 8])
7.np.random.randint(low, high=None, size=None, dtype=’l’)
np.random.randint(0,10,5)
输出结果:
array([0, 7, 2, 3, 7])
8.np.random.randn(d0, d1, …, dn)
np.random.randn(10)
# 每次每次都不一样
输出结果:
array([-1.74976547, 0.3426804 , 1.1530358 , -0.25243604, 0.98132079,
0.51421884, 0.22117967, -1.07004333, -0.18949583, 0.25500144])
//////////////////////////////////////////////////
np.random.seed(100)#随机的种子,有了种子,每次都一样
np.random.randn(10)
输出结果:
array([ 0.37332715, -0.2887605 , 0.04985088, -0.93815832, -0.4087037 ,
1.13352254, 0.52713526, -0.76014192, -0.97292788, 0.16290446])
标准正太分布
9.np.random.random(size=None)
生成0到1的随机数,左闭右开
03-ndarray的属性
4个必记参数:
- ndim:维度
- shape:形状(各维度的长度)
- size:总长度
- dtype:元素类型
04-ndarray的基本操作
- 索引
一维与列表完全一致 多维时同理
一维与列表完全一致 多维时同理
np.random.seed(1)
x = np.random.randint(10,size=[3,4,5])
print(x[2,0,0])
print(x)
5
[[[5 8 9 5 0]
[0 1 7 6 9]
[2 4 5 2 4]
[2 4 7 7 9]]
[[1 7 0 6 9]
[9 7 6 9 1]
[0 1 8 8 3]
[9 8 7 3 6]]
[[5 1 9 3 4]
[8 1 4 0 3]
[9 2 0 4 9]
[2 7 7 9 8]]]
- 切片
一维与列表完全一致 多维时同理
np.random.seed(0)
x = np.random.randint(100,size = (10,4))
x
输出结果:
array([[44, 47, 64, 67],
[67, 9, 83, 21],
[36, 87, 70, 88],
[88, 12, 58, 65],
[39, 87, 46, 88],
[81, 37, 25, 77],
[72, 9, 20, 80],
[69, 79, 47, 64],
[82, 99, 88, 49],
[29, 19, 19, 14]])
切片:
x[7:10]
切片结果:
array([[69, 79, 47, 64],
[82, 99, 88, 49],
[29, 19, 19, 14]
- 变形
使用reshape函数,注意参数是一个tuple!
使用reshape函数,注意参数是一个tuple!
x = np.arange(0,16).reshape(4,4)
x
执行结果:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
类型是:
type(x.shape)
tuple
级联
np.concatenate() 级联需要注意的点:
级联的参数是列表:一定要加中括号或小括号
维度必须相同
形状相符
【重点】级联的方向默认是shape这个tuple的第一个值所代表的维度方向
可通过axis参数改变级联的方向
np.hstack与np.vstack 水平级联与垂直级联切分
与级联类似,三个函数完成切分工作:- np.split
- np.vsplit
- np.hsplit
副本
- 所有赋值运算不会为ndarray的任何元素创建副本。对赋值后的对象的操作也对原来的对象生效。
- 可使用copy()函数创建副本
05-ndarray的聚合操作
- 求和np.sum
np.random.seed(0)
a = np.random.randint(1000,size = 100)
print(np.sum(a))
b = np.random.randint(1000,size = (3,4,5))
print(np.sum(b))
输出:
52397
32865
最大最小值:np.max/ np.min
同理其他聚合操作
Function Name NaN-safe Version Description
np.sum np.nansum Compute sum of elements
np.prod np.nanprod Compute product of elements
np.mean np.nanmean Compute mean of elements
np.std np.nanstd Compute standard deviation
np.var np.nanvar Compute variance
np.min np.nanmin Find minimum value
np.max np.nanmax Find maximum value
np.argmin np.nanargmin Find index of minimum value
np.argmax np.nanargmax Find index of maximum value
np.median np.nanmedian Compute median of elements
np.percentile np.nanpercentile Compute rank-based statistics of elements
np.any N/A Evaluate whether any elements are true
np.all N/A Evaluate whether all elements are true
np.power 幂运算
np.sum 和 np.nansum 的区别 nan not a number
操作文件
使用pandas打开文件president_heights.csv 获取文件中的数据
06-ndarray的矩阵操作
- 基本矩阵操作
-
1) 算术运算符:
加减乘除
a = np.array([[1,2,3],
[4,5,6]])
a
输出:
array([[1, 2, 3],
[4, 5, 6]])
a+1
输出:
array([[2, 3, 4],
[5, 6, 7]])
a*2
输出:
array([[ 2, 4, 6],
[ 8, 10, 12]])
a+[[1,4,9],[3,3,3]]
输出:
array([[ 2, 6, 12],
[ 7, 8, 9]])
a*2-2
输出:
array([[ 0, 2, 4],
[ 6, 8, 10]])
2) 矩阵积np.dot()
a = np.array([[1,2,3],
[4,5,6]])
a
输出:
array([[1, 2, 3],
[4, 5, 6]])
b = np.array([[1,1],
[1,1],
[1,1]])
b
输出:
array([[1, 1],
[1, 1],
[1, 1]])
np.dot(a,b)
输出:
array([[ 6, 6],
[15, 15]])
- 广播机制
【重要】ndarray广播机制的两条规则
规则一:为缺失的维度补1
规则二:假定缺失元素用已有值填充
07-ndarray的排序
小测验: 使用以上所学numpy的知识,对一个ndarray对象进行选择排序。 def selectSort(x): 代码越短越好
1. 快速排序
np.sort()与ndarray.sort()都可以,但有区别:
- np.sort()不改变输入
- ndarray.sort()本地处理,不占用空间,但改变输入
2. 部分排序
np.partition(a,k)
有的时候我们不是对全部数据感兴趣,我们可能只对最小或最大的一部分感兴趣。
- 当k为正时,我们想要得到最小的k个数
- 当k为负时,我们想要得到最大的k个数