一、Numpy介绍
![](https://img.php1.cn/3cd4a/1eebe/cd5/99b88427bc9ce0dc.webp)
NumPy提供了一个N维数组类型ndarray,它描述了相同类型的“items”的集合。
![](https://img.php1.cn/3cd4a/1eebe/cd5/c72d78d7317a9e8e.webp)
返回结果:
![](https://img.php1.cn/3cd4a/1e618/cd5/af17da15769ccb2e.jpeg)
提问:
使用Python列表可以存储一维数组,通过列表的嵌套可以实现多维数组,那么为什么还需要使用Numpy的ndarray呢?
三、ndarray与Python原生list运算效率对比
在这里我们通过一段代码运行来体会到ndarray的好处
![](https://img.php1.cn/3cd4a/1eebe/cd5/086aec93f5e1e9b2.webp)
其中第一个时间显示的是使用原生Python计算时间,第二个内容是使用numpy计算时间:
![](https://img.php1.cn/3cd4a/1eebe/cd5/02c379d60086f382.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/60405fda58cd0acd.webp)
从中我们看到ndarray的计算速度要快很多,节约了时间。
机器学习的最大特点就是大量的数据运算,那么如果没有一个快速的解决方案,那可能现在python也在机器学习领域达不到好的效果。
![](https://img.php1.cn/3cd4a/1e618/cd5/af17da15769ccb2e.jpeg)
2 ndarray的形状
首先创建一些数组。
![](https://img.php1.cn/3cd4a/1eebe/cd5/6789f68dabde0aed.png)
分别打印出形状
![](https://img.php1.cn/3cd4a/1eebe/cd5/e88efe5b0a13a7fa.webp)
如何理解数组的形状?
二维数组:
![](https://img.php1.cn/3cd4a/1eebe/cd5/617c1173853af4b6.webp)
三维数组:
![](https://img.php1.cn/3cd4a/1eebe/cd5/bff2716168d1ed7b.webp)
dtype是numpy.dtype类型,先看看对于数组来说都有哪些类型
![](https://img.php1.cn/3cd4a/1eebe/cd5/21e585a7e21fc7dc.png)
- 注意:若不指定,整数默认int64,小数默认float64
4 总结
数组的基本属性【知道】
![](https://img.php1.cn/3cd4a/1eebe/cd5/4fae50aeee651818.webp)
六、基本操作
1 生成数组的方法
1.1 生成0和1的数组
- np.ones(shape, dtype)
- np.ones_like(a, dtype)
- np.zeros(shape, dtype)
- np.zeros_like(a, dtype)
![](https://img.php1.cn/3cd4a/1e618/bdf/129913486c37ddf6.jpeg)
返回结果:
![](https://img.php1.cn/3cd4a/1eebe/cd5/8343fdbffb0056b5.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/21e585a7e21fc7dc.png)
返回结果:
![](https://img.php1.cn/3cd4a/1eebe/cd5/4fae50aeee651818.webp)
1.2 从现有数组生成
1.2.1 生成方式
- np.array(object, dtype)
- np.asarray(a, dtype)
![](https://img.php1.cn/3cd4a/1eebe/cd5/086aec93f5e1e9b2.webp)
1.2.2 关于array和asarray的不同
![](https://img.php1.cn/3cd4a/1eebe/cd5/45a090220e38e09d.webp)
返回结果:
![](https://img.php1.cn/3cd4a/1eebe/cd5/1113165c4904ecc5.webp)
1.3.2 np.arange(start,stop, step, dtype)
创建等差数组 — 指定步长
参数
![](https://img.php1.cn/3cd4a/1eebe/cd5/dc7ef30f57b727c7.jpeg)
返回结果:
![](https://img.php1.cn/3cd4a/1eebe/cd5/a1be7872e8d4934f.webp)
1.3.3 np.logspace(start,stop, num)
创建等比数列
参数:
- num:要生成的等比数列数量,默认为50
![](https://img.php1.cn/3cd4a/1eebe/cd5/8ad8f3bf8da691df.webp)
返回结果:
![](https://img.php1.cn/3cd4a/1eebe/cd5/857a46d091981bac.webp)
1.4 生成随机数组
1.4.1 使用模块介绍
1.4.2 正态分布
一、基础概念复习:正态分布(理解)
a. 什么是正态分布
正态分布是一种概率分布。正态分布是具有两个参数μ和σ的连续型随机变量的分布,第一参数μ是服从正态分布的随机变量的均值,第二个参
数σ是此随机变量的标准差,所以正态分布记作N(μ,σ )。
![](https://img.php1.cn/3cd4a/1eebe/cd5/443b30bb45e66690.webp)
其中M为平均值,n为数据总个数,σ 为标准差,σ ^2可以理解一个整体为方差
![](https://img.php1.cn/3cd4a/1eebe/cd5/a1be7872e8d4934f.webp)
可以理解成数据的一个离散程度的衡量
![](https://img.php1.cn/3cd4a/1eebe/cd5/dc7ef30f57b727c7.jpeg)
返回结果:
![](https://img.php1.cn/3cd4a/1eebe/cd5/45a090220e38e09d.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/7cccb7e4b6cb5cb8.webp)
![](https://img.php1.cn/3cd4a/1e618/cd5/af17da15769ccb2e.jpeg)
![](https://img.php1.cn/3cd4a/1eebe/cd5/a5d7215df572c386.webp)
返回结果:
![](https://img.php1.cn/3cd4a/1eebe/cd5/8ad8f3bf8da691df.webp)
1.4.2 均匀分布
- np.random.rand(d0, d1, ..., dn)
返回[0.0,1.0)内的一组均匀分布的数。
- np.random.uniform(low=0.0, high=1.0, size=None)
功能:从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high.
参数介绍:
- low: 采样下界,float类型,默认值为0;
- high: 采样上界,float类型,默认值为1;
- size: 输出样本数目,为int或元组(tuple)类型,例如,size=(m,n,k), 则输出mnk个样本,缺省时输出1个值。
返回值:ndarray类型,其形状和参数size中描述一致。
- np.random.randint(low, high=None, size=None, dtype='l')
从一个均匀分布中随机采样,生成一个整数或N维整数数组,
取数范围:若high不为None时,取[low,high)之间随机整数,否则取值[0,low)之间随机整数
![](https://img.php1.cn/3cd4a/1eebe/cd5/ed19db63ee478b98.png)
返回结果:
![](https://img.php1.cn/3cd4a/1eebe/cd5/a1be7872e8d4934f.webp)
画图看分布状况:
![](https://img.php1.cn/3cd4a/1eebe/cd5/5b97d3b808d031e2.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/857a46d091981bac.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/4283cd4bbba41b87.png)
返回结果:
![](https://img.php1.cn/3cd4a/1eebe/cd5/011ac27956d007f0.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/67cc2e96eddffff8.png)
3 形状修改
3.1 ndarray.reshape(shape, order)
- 返回一个具有相同数据域,但shape不一样的视图
- 行、列不进行互换
![](https://img.php1.cn/3cd4a/1eebe/cd5/67cc2e96eddffff8.png)
3.2 ndarray.resize(new_shape)
- 修改数组本身的形状(需要保持元素个数前后相同)
- 行、列不进行互换
![](https://img.php1.cn/3cd4a/1eebe/cd5/2d903861d5ad779c.png)
3.3 ndarray.T
![](https://img.php1.cn/3cd4a/1eebe/cd5/ddcc574beb16294e.jpeg)
4 类型修改
4.1 ndarray.astype(type)
![](https://img.php1.cn/3cd4a/1eebe/cd5/b428d8f746fb8d47.webp)
4.2 ndarray.tostring([order])或者ndarray.tobytes([order])
![](https://img.php1.cn/3cd4a/1eebe/cd5/086aec93f5e1e9b2.webp)
4.3 jupyter输出太大可能导致崩溃问题【了解】
如果遇到
![](https://img.php1.cn/3cd4a/1eebe/cd5/0ef126b5295c089b.webp)
这个问题是在jupyer当中对输出的字节数有限制,需要去修改配置文件
创建配置文件
![](https://img.php1.cn/3cd4a/1eebe/cd5/8343fdbffb0056b5.webp)
取消注释,多增加
![](https://img.php1.cn/3cd4a/1eebe/cd5/d05d9dfd09a56332.webp)
但是不建议这样去修改,jupyter输出太大会崩溃
5 数组的去重
5.1 np.unique()
![](https://img.php1.cn/3cd4a/1eebe/cd5/d84f9786330d9e41.png)