Python-NumPy ╰+攻爆jí腚メ 2022-06-04 07:56 187阅读 0赞 #### 网上查到的个人认为最简洁的大纲,如果你有Python基础,又有些数学基础,看这个就够了。看不懂的地方敲一遍,这篇文章能告诉我们numpy是什么、它能做什么、怎么做。 #### # NumPy # NumPy是高性能科学计算和数据分析的基础包。部分功能如下: * * ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组。 * 用于对整组数据进行快速运算的标准数学函数(无需编写循环)。 * 用于读写磁盘数据的工具以及用于操作内存映射文件的工具。 * 线性代数、随机数生成以及傅里叶变换功能。 * 用于集成C、C++、Fortran等语言编写的代码的工具。 首先要导入numpy库:import numpy as np #### A NumPy函数和属性: #### <table style="margin:0px; padding:0px; border:1px solid silver; border-collapse:collapse; word-break:break-word"> <tbody style="margin:0px; padding:0px"> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 类型</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 类型代码</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 说明</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> int8、uint8</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> i1、u1</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 有符号和无符号8位整型(1字节)</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> int16、uint16</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> i2、u2</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 有符号和无符号16位整型(2字节)</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> int32、uint32</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> i4、u4</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 有符号和无符号32位整型(4字节)</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> int64、uint64</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> i8、u8</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 有符号和无符号64位整型(8字节)</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> float16</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> f2</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 半精度浮点数</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> float32</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> f4、f</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 单精度浮点数</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> float64</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> f8、d</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 双精度浮点数</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> float128</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> f16、g</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 扩展精度浮点数</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> complex64</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> c8</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 分别用两个32位表示的复数</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> complex128</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> c16</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 分别用两个64位表示的复数</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> complex256</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> c32</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 分别用两个128位表示的复数</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> bool</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ?</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 布尔型</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> object</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> O</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> python对象</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> string</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> Sn</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 固定长度字符串,每个字符1字节,如S10</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> unicode</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> Un</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 固定长度Unicode,字节数由系统决定,如U10</td> </tr> </tbody> </table> 表2.1.A.1 NumPy类型 <table style="margin:0px; padding:0px; border:1px solid silver; border-collapse:collapse; word-break:break-word"> <tbody style="margin:0px; padding:0px"> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">生成函数</span></td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">作用</span></td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.array( x)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.array( x, dtype)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 将输入数据转化为一个ndarray</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 将输入数据转化为一个类型为type的ndarray</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> np.asarray( array )</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 将输入数据转化为一个新的(copy)ndarray</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.ones( N )</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.ones( N, dtype)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.ones_like( ndarray )</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 生成一个N长度的一维全一ndarray</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 生成一个N长度类型是dtype的一维全一ndarray</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 生成一个形状与参数相同的全一ndarray</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.zeros( N)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.zeros( N, dtype)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.zeros_like(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 生成一个N长度的一维全零ndarray</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 生成一个N长度类型位dtype的一维全零ndarray</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 类似np.ones_like( ndarray )</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.empty( N )</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.empty( N, dtype)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.empty(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 生成一个N长度的未初始化一维ndarray</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 生成一个N长度类型是dtype的未初始化一维ndarray</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 类似np.ones_like( ndarray )</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.eye( N )</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.identity( N )</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 创建一个N * N的单位矩阵(对角线为1,其余为0)</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.arange( num)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.arange( begin, end)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.arange( begin, end, step)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 生成一个从0到num-1步数为1的一维ndarray</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 生成一个从begin到end-1步数为1的一维ndarray</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 生成一个从begin到end-step的步数为step的一维ndarray</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.mershgrid(ndarray, ndarray,...)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 生成一个ndarray * ndarray * ...的多维ndarray</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.where(cond, ndarray1, ndarray2)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 根据条件cond,选取ndarray1或者ndarray2,返回一个新的ndarray</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.in1d(ndarray, [x,y,...])</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 检查ndarray中的元素是否等于[x,y,...]中的一个,返回bool数组</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">矩阵函数</span></td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">说明</span></td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.diag( ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.diag( [x,y,...])</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 以一维数组的形式返回方阵的对角线(或非对角线)元素</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 将一维数组转化为方阵(非对角线元素为0)</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> np.dot(ndarray, ndarray)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 矩阵乘法</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> np.trace( ndarray)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 计算对角线元素的和</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> <span style="margin:0px; padding:0px">排序函数</span></p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> <span style="margin:0px; padding:0px">说明</span></p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.sort( ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 排序,返回副本</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.unique(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 返回ndarray中的元素,排除重复元素之后,并进行排序</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.intersect1d( ndarray1, ndarray2)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.union1d( ndarray1, ndarray2)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.setdiff1d( ndarray1, ndarray2)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.setxor1d( ndarray1, ndarray2)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 返回二者的交集并排序。</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 返回二者的并集并排序。</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 返回二者的差。</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 返回二者的对称差</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">一元计算函数</span></td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">说明</span></td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.abs(ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.fabs(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算绝对值</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算绝对值(非复数)</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.mean(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 求平均值</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.sqrt(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算x^0.5</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.square(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算x^2</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.exp(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算e^x</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> log、log10、log2、log1p</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算自然对数、底为10的log、底为2的log、底为(1+x)的log</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.sign(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算正负号:1(正)、0(0)、-1(负)</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.ceil(ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.floor(ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.rint(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算大于等于改值的最小整数</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算小于等于该值的最大整数</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 四舍五入到最近的整数,保留dtype</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.modf(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 将数组的小数和整数部分以两个独立的数组方式返回</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.isnan(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 返回一个判断是否是NaN的bool型数组</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.isfinite(ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.isinf(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 返回一个判断是否是有穷(非inf,非NaN)的bool型数组</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 返回一个判断是否是无穷的bool型数组</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> cos、cosh、sin、sinh、tan、tanh</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 普通型和双曲型三角函数</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> arccos、arccosh、arcsin、arcsinh、arctan、arctanh</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 反三角函数和双曲型反三角函数</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.logical_not(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算各元素not x的真值,相当于-ndarray</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> <span style="margin:0px; padding:0px">多元计算函数</span></p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> <span style="margin:0px; padding:0px">说明</span></p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.add(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.subtract(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.multiply(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.divide(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.floor_divide(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.power(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.mod(ndarray, ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 相加</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 相减</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 乘法</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 除法</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 圆整除法(丢弃余数)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 次方</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 求模</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.maximum(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.fmax(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.minimun(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.fmin(ndarray, ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 求最大值</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 求最大值(忽略NaN)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 求最小值</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 求最小值(忽略NaN)</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.copysign(ndarray, ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 将参数2中的符号赋予参数1</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.greater(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.greater_equal(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.less(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.less_equal(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.equal(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> np.not_equal(ndarray, ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ></p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> >=</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> <</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> <=</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ==</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> !=</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> logical_and(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> logical_or(ndarray, ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> logical_xor(ndarray, ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> &</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> |</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ^</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> np.dot( ndarray, ndarray)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 计算两个ndarray的矩阵内积</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> np.ix_([x,y,m,n],...)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 生成一个索引器,用于Fancy indexing(花式索引)</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">文件读写</span></td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">说明</span></td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> np.save(string, ndarray)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 将ndarray保存到文件名为 [string].npy 的文件中(无压缩)</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> np.savez(string, ndarray1, ndarray2, ...)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 将所有的ndarray压缩保存到文件名为[string].npy的文件中</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> np.savetxt(sring, ndarray, fmt, newline=‘\n‘)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 将ndarray写入文件,格式为fmt</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> np.load(string)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 读取文件名string的文件内容并转化为ndarray对象(或字典对象)</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> np.loadtxt(string, delimiter)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 读取文件名string的文件内容,以delimiter为分隔符转化为ndarray</td> </tr> </tbody> </table> 表2.1.A.2 np常用函数 #### B NumPy.ndarray函数和属性: #### <table style="margin:0px; padding:0px; border:1px solid silver; border-collapse:collapse; word-break:break-word"> <tbody style="margin:0px; padding:0px"> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray.ndim</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 获取ndarray的维数</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray.shape</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 获取ndarray各个维度的长度</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray.dtype</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 获取ndarray中元素的数据类型</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray.T</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 简单转置矩阵ndarray</td> </tr> </tbody> </table> 表2.1.B.1 ndarray属性 <table style="margin:0px; padding:0px; border:1px solid silver; border-collapse:collapse; word-break:break-word"> <tbody style="margin:0px; padding:0px"> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">函数</span></td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">说明</span></td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray.astype(dtype)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 转换类型,若转换失败则会出现TypeError</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray.copy()</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 复制一份ndarray(新的内存空间)</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray.reshape((N,M,...))</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 将ndarray转化为N*M*...的多维ndarray(非copy)</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray.transpose((xIndex,yIndex,...))</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 根据维索引xIndex,yIndex...进行矩阵转置,依赖于shape,不能用于一维矩阵(非copy)</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray.swapaxes(xIndex,yIndex)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 交换维度(非copy)</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">计算函数</span></td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">说明</span></td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray.mean( axis=0 )</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 求平均值 </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray.sum( axis= 0)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 求和 </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.cumsum( axis=0)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.cumprod( axis=0)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 累加 </p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 累乘</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.std()</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.var()</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 方差</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 标准差</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.max()</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.min()</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 最大值</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 最小值</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.argmax()</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.argmin()</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 最大值索引</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 最小值索引</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.any()</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.all()</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 是否至少有一个True</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 是否全部为True</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.dot( ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算矩阵内积</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> <span style="margin:0px; padding:0px">排序函数</span></p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> <span style="margin:0px; padding:0px">说明</span></p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray.sort(axis=0)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 排序,返回源数据</p> </td> </tr> </tbody> </table> 表2.1.B.2 ndarray函数 <table style="margin:0px; padding:0px; border:1px solid silver; border-collapse:collapse; word-break:break-word"> <tbody style="margin:0px; padding:0px"> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray[n]</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 选取第n+1个元素</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray[n:m]</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 选取第n+1到第m个元素</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray[:]</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 选取全部元素</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray[n:]</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 选取第n+1到最后一个元素</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> ndarray[:n]</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 选取第0到第n个元素</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray[ bool_ndarray ]</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 注:bool_ndarray表示bool类型的ndarray</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 选取为true的元素</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray[[x,y,m,n]]...</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 选取顺序和序列为x、y、m、n的ndarray</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray[n,m]</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray[n][m]</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 选取第n+1行第m+1个元素</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray[n,m,...]</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> ndarray[n][m]....</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 选取n行n列....的元素</td> </tr> </tbody> </table> 表2.1.B.3 ndarray索引/切片方式 #### C NumPy.random函数和属性: #### <table style="margin:0px; padding:0px; border:1px solid silver; border-collapse:collapse; word-break:break-word"> <tbody style="margin:0px; padding:0px"> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">函数</span></td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">说明</span></td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> seed()</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> seed(int)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> seed(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 确定随机数生成种子</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> permutation(int)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> permutation(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 返回一个一维从0~9的序列的随机排列</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 返回一个序列的随机排列</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> shuffle(ndarray)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 对一个序列就地随机排列</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> rand(int)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> randint(begin,end,num=1)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 产生int个均匀分布的样本值</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 从给定的begin和end随机选取num个整数</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> randn(N, M, ...)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 生成一个N*M*...的正态分布(平均值为0,标准差为1)的ndarray</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> normal(size=(N,M,...))</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 生成一个N*M*...的正态(高斯)分布的ndarray </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> beta(ndarray1,ndarray2)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 产生beta分布的样本值,参数必须大于0 </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> chisquare()</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 产生卡方分布的样本值 </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> gamma()</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 产生gamma分布的样本值 </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> uniform()</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 产生在[0,1)中均匀分布的样本值 </td> </tr> </tbody> </table> 2.1.C.1 random常用函数 #### D NumPy.linalg函数和属性: #### <table style="margin:0px; padding:0px; border:1px solid silver; border-collapse:collapse; word-break:break-word"> <tbody style="margin:0px; padding:0px"> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">函数</span></td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <span style="margin:0px; padding:0px">说明</span></td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> det(ndarray)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 计算矩阵列式</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> eig(ndarray)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 计算方阵的本征值和本征向量</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> inv(ndarray)</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> pinv(ndarray)</p> </td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算方阵的逆</p> <p style="margin:10px auto; padding-top:0px; padding-bottom:0px; line-height:1.5; font-size:13px"> 计算方阵的Moore-Penrose伪逆</p> </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> qr(ndarray)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 计算qr分解 </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> svd(ndarray)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 计算奇异值分解svd</td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> solve(ndarray)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 解线性方程组Ax = b,其中A为方阵 </td> </tr> <tr style="margin:0px; padding:0px"> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> lstsq(ndarray)</td> <td style="margin:0px; padding:3px; border:1px solid silver; border-collapse:collapse"> 计算Ax=b的最小二乘解 <br> </td> </tr> </tbody> </table>
还没有评论,来说两句吧...