NumPy 1-1

NumPy 1-1

Python的array模块不支持多维和运算函数,因此利用NumPy。

NumPy提供了ndarray和ufunc两个基本对象。

ndarray对象

数组的创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#创建数组
>>> import numpy as np
>>> a = np.array([1, 2, 3, 4])
>>> b = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]])
>>> a
array([1, 2, 3, 4])
>>> b
array([[ 1, 2, 3, 4],
[ 4, 5, 6, 7],
[ 7, 8, 9, 10]])
#dtype属性默认64bit长整型
>>> b.dtype
dtype('int64')
#返回数组大小
>>> a.shape
(4,)
>>> b.shape
(3, 4)
#改变轴的长度,数据在内存中的位置不发生改变,也就是交换shape的01轴的值,不会发生转置操作。
>>> b.shape = (4, 3)
>>> b
array([[ 1, 2, 3],
[ 4, 4, 5],
[ 6, 7, 7],
[ 8, 9, 10]])
#默认值-1代表自动根据元素个数计算轴的长度。
>>> b.shape = (2, -1)
>>> b
array([[ 1, 2, 3, 4, 4, 5],
[ 6, 7, 7, 8, 9, 10]])
#reshape可以创建一个改变了尺寸的新数组,并不改变原数组,两者共享数据存储内存区域,改变其中一个数组元素会修改另一个。
>>> d = a.reshape((2,2))
>>> a
array([ 1, 10, 3, 5])
>>> d
array([[ 1, 10],
[ 3, 5]])
>>> a[1] = 4
>>> a
array([1, 4, 3, 5])
>>> d
array([[1, 4],
[3, 5]])

可以在命名时决定dtype的类型

1
2
3
4
5
6
7
8
>>> e = np.array([1, 2, 3, 4], dtype=np.float)
>>> e
array([ 1., 2., 3., 4.])
>>> f = np.array([1, 2, 3, 4], dtype=np.complex)
>>> f
array([ 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j])
#直接命名时加入j会导致报错,因此复数数组可以利用更改的方式产生
#有直接的复数数组产生方式么?
  • 有直接的复数数组产生方式么? (待办)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> import numpy as np
#类似range,语句为arange(a, b, d),产生首项为a,公差为d,尾项为b-d的等差数列。
>>> np.arange(0, 1, 0.1)
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
#语句为linspace(a, b, n),产生首项为a,尾项为b共n项的等差数列(公差d为(b-a)/(n-1))
>>> np.linspace(0, 1, 10)
array([ 0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ])
#语句为logspace(a, b, n),产生首项为10^a,尾项为10^b,共n项的等比数列(q = 10^((b-a)/(n-1))
>>> np.logspace(0, 2, 20)
array([ 1. , 1.27427499, 1.62377674, 2.06913808,
2.6366509 , 3.35981829, 4.2813324 , 5.45559478,
6.95192796, 8.8586679 , 11.28837892, 14.38449888,
18.32980711, 23.35721469, 29.76351442, 37.92690191,
48.32930239, 61.58482111, 78.47599704, 100. ])

从字符串读取数字

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> str1 = "abcdefgh"
#int8对应ASCII码对应
>>> np.fromstring(str1, dtype=np.int8)
array([ 97, 98, 99, 100, 101, 102, 103, 104], dtype=int8)
#int16对应两项合并一项,第一项98*256+97
>>> np.fromstring(str1, dtype=np.int16)
array([25185, 25699, 26213, 26727], dtype=int16)
>>> np.fromstring(str1, dtype=np.int32)
array([1684234849, 1751606885], dtype=int32)
>>> np.fromstring(str1, dtype=np.int64)
array([7523094288207667809])
>>> np.fromstring(str1, dtype=np.float)
array([ 8.54088322e+194])

利用fromfunction函数创建列表fromfunction(func, (a,b)),其中func为计算数组元素的函数,(a, b)为数组的shape()

例子 : 构建九九乘法表的数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import numpy as np
def func1(i, j):
return (i+1) * (j+1)

a = np.fromfunction(func1, (9, 9))
print(a)

#运行结果:
[[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[ 2. 4. 6. 8. 10. 12. 14. 16. 18.]
[ 3. 6. 9. 12. 15. 18. 21. 24. 27.]
[ 4. 8. 12. 16. 20. 24. 28. 32. 36.]
[ 5. 10. 15. 20. 25. 30. 35. 40. 45.]
[ 6. 12. 18. 24. 30. 36. 42. 48. 54.]
[ 7. 14. 21. 28. 35. 42. 49. 56. 63.]
[ 8. 16. 24. 32. 40. 48. 56. 64. 72.]
[ 9. 18. 27. 36. 45. 54. 63. 72. 81.]]

数组支持切片操作 array[a: b :s] s为步长,a为开始,b为结束(不包含b)

与Python中的序列不同,切片操作的结果和原始数组存储空间相同,因此改变切片操作的结果会导致原始数组存储空间内的元素的改变。

整数数列可以避免共享数据空间(使用整数数列下标得到新数组)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>> x = np.arange(24,0,-3)
>>> x
array([24, 21, 18, 15, 12, 9, 6, 3])
>>> x[1]
21
>>> x[[1,3,-3,7]]
array([21, 15, 9, 3])
>>> b = x[[1,3,-3,7]]
#等同语句 : b = x[np.array([1,3,-3,7])]
>>> b
array([21, 15, 9, 3])
>>> b[2] = 0
>>> b
array([21, 15, 0, 3])
>>> x
array([24, 21, 18, 15, 12, 9, 6, 3])

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!