一、简介
二、ndarray属性
三、ndarray创建方法
import numpy as np
# ndarray创建方式一
a = np.array((1,1,2,2,3,3))
print(a)
b = np.array([1,1,3,3,2,2])
print(b)
c = np.array([[1,3],[3,4],(1.0,2.0)])
print(c)
result:
[1 1 2 2 3 3]
[1 1 3 3 2 2]
[[1. 3.]
[3. 4.]
[1. 2.]]
二、ndarray创建方法
# 创建array方式二
d = np.arange(5)
print(d)
e = np.ones(a.shape)
print(e)
f = np.zeros((2,4))
print(f)
g = np.full((2,2),6)
print(g)
h = np.eye(5)
print(h)
result:
[0 1 2 3 4]
[1. 1. 1. 1. 1. 1.]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[6 6]
[6 6]]
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
c = np.full((3,4,5),6)
c
Out[29]:
array([[[6, 6, 6, 6, 6],
[6, 6, 6, 6, 6],
[6, 6, 6, 6, 6],
[6, 6, 6, 6, 6]],
[[6, 6, 6, 6, 6],
[6, 6, 6, 6, 6],
[6, 6, 6, 6, 6],
[6, 6, 6, 6, 6]],
[[6, 6, 6, 6, 6],
[6, 6, 6, 6, 6],
[6, 6, 6, 6, 6],
[6, 6, 6, 6, 6]]])
d = np.ones_like(c)
d
Out[31]:
array([[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]],
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]],
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]])
e = np.zeros_like(c)
e
Out[33]:
array([[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]])
f = np.full_like(c,8)
f
Out[36]:
array([[[8, 8, 8, 8, 8],
[8, 8, 8, 8, 8],
[8, 8, 8, 8, 8],
[8, 8, 8, 8, 8]],
[[8, 8, 8, 8, 8],
[8, 8, 8, 8, 8],
[8, 8, 8, 8, 8],
[8, 8, 8, 8, 8]],
[[8, 8, 8, 8, 8],
[8, 8, 8, 8, 8],
[8, 8, 8, 8, 8],
[8, 8, 8, 8, 8]]])
g = np.linspace(0,10,20)
g
Out[44]:
array([ 0. , 0.52631579, 1.05263158, 1.57894737, 2.10526316,
2.63157895, 3.15789474, 3.68421053, 4.21052632, 4.73684211,
5.26315789, 5.78947368, 6.31578947, 6.84210526, 7.36842105,
7.89473684, 8.42105263, 8.94736842, 9.47368421, 10. ])
h = np.full_like(g,6.6)
h
Out[49]:
array([6.6, 6.6, 6.6, 6.6, 6.6, 6.6, 6.6, 6.6, 6.6, 6.6, 6.6, 6.6, 6.6,
6.6, 6.6, 6.6, 6.6, 6.6, 6.6, 6.6])
i = np.concatenate((g,h))
i
Out[52]:
array([ 0. , 0.52631579, 1.05263158, 1.57894737, 2.10526316,
2.63157895, 3.15789474, 3.68421053, 4.21052632, 4.73684211,
5.26315789, 5.78947368, 6.31578947, 6.84210526, 7.36842105,
7.89473684, 8.42105263, 8.94736842, 9.47368421, 10. ,
6.6 , 6.6 , 6.6 , 6.6 , 6.6 ,
6.6 , 6.6 , 6.6 , 6.6 , 6.6 ,
6.6 , 6.6 , 6.6 , 6.6 , 6.6 ,
6.6 , 6.6 , 6.6 , 6.6 , 6.6 ])
三、ndarray变换
a
Out[62]:
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
a.reshape((2,12))
Out[63]:
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
a
Out[64]:
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
a.resize((2,12))
a
Out[66]:
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
a = np.ones((2,3,4),dtype = np.int32)
a
Out[68]:
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
#resize 返回一个NoneType并且修改原数组
c = a.resize((12,2))
c
print(c)
None
a
Out[77]:
array([[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1]])
a = np.ones((12,2),dtype=float32)
Out[88]:
array([[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.],
[1., 1.]], dtype=float32)
new_a = a.astype(np.int32)
new_a
Out[90]:
array([[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1]])
#该方法返回一个新数组,并且不影响原数组,相当于拷贝
c = np.full((2,3,4),99,dtype=np.int16)
c
Out[95]:
array([[[99, 99, 99, 99],
[99, 99, 99, 99],
[99, 99, 99, 99]],
[[99, 99, 99, 99],
[99, 99, 99, 99],
[99, 99, 99, 99]]], dtype=int16)
c.tolist()
Out[96]:
[[[99, 99, 99, 99], [99, 99, 99, 99], [99, 99, 99, 99]],
[[99, 99, 99, 99], [99, 99, 99, 99], [99, 99, 99, 99]]]
四、ndarray操作
a = np.array([1,2,3,4,5,6,7,8])
a[4]
Out[98]: 5
a[1:4:1]
Out[99]: array([2, 3, 4])
a = np.arange(30).reshape((5,6))
a
Out[101]:
array([[ 0, 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]])
a = np.arange(30).reshape((2,5,3))
a
Out[103]:
array([[[ 0, 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]]])
a[1,2,2]
Out[104]: 23
a[-1,-2,-1]
Out[105]: 26
a[-2,-3,-2]
Out[106]: 7
a[:,:,1]
Out[107]:
array([[ 1, 4, 7, 10, 13],
[16, 19, 22, 25, 28]])
a[:,:,::2]
Out[108]:
array([[[ 0, 2],
[ 3, 5],
[ 6, 8],
[ 9, 11],
[12, 14]],
[[15, 17],
[18, 20],
[21, 23],
[24, 26],
[27, 29]]])
a[:,1:4:2,0:4:2]
Out[109]:
array([[[ 3, 5],
[ 9, 11]],
[[18, 20],
[24, 26]]])
五、ndarray运算
a = np.arange(20).reshape((4,5))
a
Out[119]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
a = np.square(a)
a
Out[121]:
array([[ 0, 1, 4, 9, 16],
[ 25, 36, 49, 64, 81],
[100, 121, 144, 169, 196],
[225, 256, 289, 324, 361]])
a = np.log10(a)
<ipython-input-122-220883be9793>:1: RuntimeWarning: divide by zero encountered in log10
a = np.log10(a)
a
Out[123]:
array([[ -inf, 0. , 0.60205999, 0.95424251, 1.20411998],
[1.39794001, 1.5563025 , 1.69019608, 1.80617997, 1.90848502],
[2. , 2.08278537, 2.15836249, 2.2278867 , 2.29225607],
[2.35218252, 2.40823997, 2.46089784, 2.51054501, 2.5575072 ]])
a = np.arange(20).reshape((4,5))
b = a + a
b
Out[126]:
array([[ 0, 2, 4, 6, 8],
[10, 12, 14, 16, 18],
[20, 22, 24, 26, 28],
[30, 32, 34, 36, 38]])
c = a * a
c
Out[128]:
array([[ 0, 1, 4, 9, 16],
[ 25, 36, 49, 64, 81],
[100, 121, 144, 169, 196],
[225, 256, 289, 324, 361]])
np.sign(c)
Out[129]:
array([[0, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])
np.maximum(a,b)
Out[130]:
array([[ 0, 2, 4, 6, 8],
[10, 12, 14, 16, 18],
[20, 22, 24, 26, 28],
[30, 32, 34, 36, 38]])
a > b
Out[131]:
array([[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False]])
a < b
Out[132]:
array([[False, True, True, True, True],
[ True, True, True, True, True],
[ True, True, True, True, True],
[ True, True, True, True, True]])
六、总结
七、练习
1. 导入numpy库并取别名为np (★☆☆)
(提示: import … as …)
import numpy as np
2. 打印输出numpy的版本和配置信息 (★☆☆)
(提示: np.__verison__, np.show_config)
print (np.__version__)
np.show_config()
3. 创建长度为10的零向量 (★☆☆)
(提示: np.zeros)
Z = np.zeros(10)
print (Z)
4. 获取数组所占内存大小 (★☆☆)
(提示: size, itemsize)
Z = np.zeros((10, 10))
print (Z.size * Z.itemsize)
5. 怎么用命令行获取numpy add函数的文档说明? (★☆☆)
(提示: np.info)
np.info(np.add)
6. 创建一个长度为10的零向量,并把第五个值赋值为1 (★☆☆)
(提示: array[4])
Z = np.zeros(10)
Z[4] = 1
print (Z)
7. 创建一个值域为10到49的向量 (★☆☆)
(提示: np.arange)
Z = np.arange(10, 50)
print (Z)
8**. 将一个向量进行反转(第一个元素变为最后一个元素) (★☆☆)
(提示: array[::-1])
Z = np.arange(50)
Z = Z[::-1]
print (Z)
9. 创建一个3x3的矩阵,值域为0到8**(★☆☆)
(提示: reshape)
Z = np.arange(9).reshape(3, 3)
print (Z)
10. 从数组[1, 2, 0, 0, 4, 0]中找出非0元素的位置索引 (★☆☆)
(提示: np.nonzero)
nz = np.nonzero([1, 2, 0, 0, 4, 0])
print (NZ)
11. 创建一个3x3的单位矩阵 (★☆☆)
(提示: np.eye)
Z = np.eye(3)
print (Z)
12. 创建一个3x3x3的随机数组**(★☆☆)
(提示: np.random.random)
Z = np.random.random((3, 3, 3))
print (Z)
13. 创建一个10x10的随机数组,并找出该数组中的最大值与最小值**(★☆☆)
(提示: max, min)
Z = np.random.random((10, 10))
Zmax, Zmin = Z.max(), Z.min()
print (Z.max, Z.min)
14. 创建一个长度为30的随机向量,并求它的平均值 (★☆☆)
(提示: mean)
Z = np.random.random(30)
mean = Z.mean()
print (mean)