# /usr/env/python
import numpy as np
a_list = list(range(10))
print(a_list)
b = np.array(a_list)
print(b)
print(type(b))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0 1 2 3 4 5 6 7 8 9]
<class 'numpy.ndarray'>
a = np.zeros(10, dtype=int)
print(a)
print(a.dtype)
结果
[0 0 0 0 0 0 0 0 0 0]
int32
a = np.zeros(10)
print(a)
print(a.dtype)
结果为
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
float64
a = np.zeros((4, 4), dtype=int)
print(a)
print(a.dtype)
结果
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
int32
b = np.ones((4, 4), dtype=float)
print(b)
结果
c = np.full((3, 3), 3.14)
print(c)
结果
[[3.14 3.14 3.14]
[3.14 3.14 3.14]
[3.14 3.14 3.14]]
当你想要生成一个和矩阵c有同样格式的矩阵时,就可以用这些函数。
c = np.full((3, 3), 3.14)
# print(c)
d = np.zeros_like(c)
print(d)
结果
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
想要生成一个二维矩阵时,需要把两个数用小括号括起来作为参数传给random函数,如random((3,3)),因为random函数里边只接受一个数,默认生成一维数组,这个数表示生成的随机数的个数。
print(random.randint(0, 10))
print(np.random.randint(1, 10, (3, 3)))
print(np.random.random((2, 2)))
6
[[8 1 6]
[1 4 9]
[8 1 6]]
[[0.48495004 0.7943088 ]
[0.16865528 0.13987217]]
print(list(range(0, 10, 2)))
print(np.arange(0, 10, 2))
print(np.linspace(0, 3, 20)) #生成一个0-3之间,步长一样的一维(1*20)的数组
[0, 2, 4, 6, 8]
[0 2 4 6 8]
[0. 0.15789474 0.31578947 0.47368421 0.63157895 0.78947368
0.94736842 1.10526316 1.26315789 1.42105263 1.57894737 1.73684211
1.89473684 2.05263158 2.21052632 2.36842105 2.52631579 2.68421053
2.84210526 3. ]
print(np.eye(6))
[[1. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 1.]]
嵌套列表的元素访问
var = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(var[2][2])
9
列表和np访问列表元素对比
var = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
a = np.array(var)
print(var[2][2])
print(a[2][2])
print(a[2, 2]) #与上一行等价
print(a[:2][:2])#操作为,先取了两行,后来又取了两行
print(a[:2][:1])
print(a[:2, :2]) #相当于切片方式,先取了两行又取了两列,但是不等价于a[:2][:2]
9
9
9
[[1 2 3]
[4 5 6]]
[[1 2 3]]
[[1 2]
[4 5]]
var = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
a = np.array(var)
print(a.ndim) # 维度
print(a.shape) # shape
print(a.size) # size
print(a.dtype) # dtype 4*8
print(a.itemsize) # itemsize 每个占4个字节
print(a.nbytes) # 4个字节*9个数
2
(3, 3)
9
int32
4
36
a = np.array(list(range(10)))
print(a)
print(a + 10)
print(np.add(a, 10))
print(a - 10)
print(a * 10)
b = np.linspace(0,np.pi, 20)
print(np.sin(b))
[0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[10 11 12 13 14 15 16 17 18 19]
[-10 -9 -8 -7 -6 -5 -4 -3 -2 -1]
[ 0 10 20 30 40 50 60 70 80 90]
[0.00000000e+00 1.64594590e-01 3.24699469e-01 4.75947393e-01
6.14212713e-01 7.35723911e-01 8.37166478e-01 9.15773327e-01
9.69400266e-01 9.96584493e-01 9.96584493e-01 9.69400266e-01
9.15773327e-01 8.37166478e-01 7.35723911e-01 6.14212713e-01
4.75947393e-01 3.24699469e-01 1.64594590e-01 1.22464680e-16]
[0. 0.16534698 0.33069396 0.49604095 0.66138793 0.82673491
0.99208189 1.15742887 1.32277585 1.48812284 1.65346982 1.8188168
1.98416378 2.14951076 2.31485774 2.48020473 2.64555171 2.81089869
2.97624567 3.14159265]
a = np.array(list(range(10)))
b = np.array([[1, 2], [3, 4]])
print(a)
print(np.sum(a))
print(b)
print(np.sum(b)) # 所有数相加求和
print(np.sum(b, axis=0)) # 0 列相加求和
print(np.sum(b, axis=1)) # 1 行相加求和
print(np.max(b))
print(np.max(b, axis=0))
print(np.max(b, axis=1))
[0 1 2 3 4 5 6 7 8 9]
45
[[1 2]
[3 4]]
10
[4 6]
[3 7]
4
[3 4]
[2 4]
n = np.random.rand(10)
print(n)
print(n > 0.5)
print(n != 3)
print(np.all(n > -1))
print(np.any(n > 1))
a = np.full((2, 5), 1, dtype=float)
print(a)
print(a.reshape(5, 2))
var = [[1, 2, 3], [7, 5, 6], [4, 8, 9]]
print(np.sort(var))
print(np.sort(var, axis=0)) # 按列排序
print(np.sort(var, axis=1)) # 按行排序
[0.86278078 0.8039594 0.69086224 0.24137832 0.90497624 0.25532105
0.00698186 0.89320662 0.35044465 0.21689973]
[ True True True False True False False True False False]
[ True True True True True True True True True True]
True
False
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
[[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]]
[[1 2 3]
[5 6 7]
[4 8 9]]
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 2 3]
[5 6 7]
[4 8 9]]
np.concatenate([b,b,b])
print(np.concatenate([var, var], axis=0)) # 按行连接
print(np.concatenate([var, var], axis=1)) # 按列连接
[[1 2 3]
[7 5 6]
[4 8 9]
[1 2 3]
[7 5 6]
[4 8 9]]
[[1 2 3 1 2 3]
[7 5 6 7 5 6]
[4 8 9 4 8 9]]