033、生成数组的函数
生成数组的函数 arange arange 类似于Python中的 range 函数,只不过返回的不是列表,而是数组: arange(start, stop=None, step=1, dtype=None) 产生一个在区间 [start, stop) 之间,以 step 为间隔的数组,如果只输入一个参数,则默认从 0 开始,并以这个值为结束: import numpy as np np.aran…
2024-3-07 20:43
|
|
37
1010 字
|
21 分钟
032、对角线
对角线 这里,使用与之前不同的导入方法: import numpy as np 使用numpy中的函数前,需要加上 np.: a = np.array([11,21,31,12,22,32,13,23,33]) a.shape = 3,3 a array([[11, 21, 31], [12, 22, 32], [13, 23, 33]]) 查看它的对角线元素: a.diagonal() arra…
2024-3-07 20:43
|
|
35
141 字
|
4 分钟
031、数组形状
数组形状 %pylab Using matplotlib backend: Qt4Agg Populating the interactive namespace from numpy and matplotlib 修改数组的形状 a = arange(6) a array([0, 1, 2, 3, 4, 5]) 将形状修改为2乘3: a.shape = 2,3 a array([[0, 1, 2…
2024-3-07 20:42
|
|
53
937 字
|
22 分钟
030、数组排序
数组排序 %pylab Using matplotlib backend: Qt4Agg Populating the interactive namespace from numpy and matplotlib sort 函数 先看这个例子: names = array(['bob', 'sue', 'jan', 'ad…
2024-3-07 20:42
|
|
47
469 字
|
8 分钟
029、数组方法
数组方法 %pylab Using matplotlib backend: Qt4Agg Populating the interactive namespace from numpy and matplotlib 求和 a = array([[1,2,3], [4,5,6]]) 求所有元素的和: sum(a) 21 指定求和的维度: 沿着第一维求和: sum(a, axis=0) array([…
2024-3-07 20:42
|
|
46
372 字
|
9 分钟
028、数组类型
数组类型 from numpy import * 之前已经看过整数数组和布尔数组,除此之外还有浮点数数组和复数数组。 复数数组 产生一个复数数组: a = array([1 + 1j, 2, 3, 4]) Python会自动判断数组的类型: a.dtype dtype('complex128') 对于复数我们可以查看它的实部和虚部: a.real array([ 1., 2., 3., 4.]) …
2024-3-07 20:42
|
|
41
875 字
|
14 分钟
027、Numpy 数组及其索引
Numpy 数组及其索引 先导入numpy: from numpy import * 产生数组 从列表产生数组: lst = [0, 1, 2, 3] a = array(lst) a array([0, 1, 2, 3]) 或者直接将列表传入: a = array([1, 2, 3, 4]) a array([1, 2, 3, 4]) 数组属性 查看类型: type(a) numpy.ndarr…
2024-3-07 20:41
|
|
50
1679 字
|
32 分钟
026、Matplotlib 基础
Matplotlib 基础 在使用Numpy之前,需要了解一些画图的基础。 Matplotlib是一个类似Matlab的工具包,主页地址为 http://matplotlib.org 导入 matplotlib 和 numpy: %pylab Using matplotlib backend: Qt4Agg Populating the interactive namespace from num…
2024-3-07 20:41
|
|
50
817 字
|
31 分钟
025、Numpy 简介
Numpy 简介 导入numpy Numpy是Python的一个很重要的第三方库,很多其他科学计算的第三方库都是以Numpy为基础建立的。 Numpy的一个重要特性是它的数组计算。 在使用Numpy之前,我们需要导入numpy包: from numpy import * 使用前一定要先导入 Numpy 包,导入的方法有以下几种: import numpy import numpy as np fr…
2024-3-07 20:41
|
|
49
496 字
|
9 分钟
024、文件读写
文件读写 写入测试文件: %%writefile test.txt this is a test file. hello world! python is good! today is a good day. Writing test.txt 读文件 使用 open 函数或者 file 函数来读文件,使用文件名的字符串作为输入参数: f = open('test.txt') f…
2024-3-07 20:40
|
|
46
6654 字
|
9.9 小时