NumPy(Numerical Python) 是 Python 语言中做科学计算的基础库。重在于数值计算,也是大部分Python科学计算库的基础,多用于在大型、多维数组上执行的数值运算。
import numpy as np
arr = np.array([1,2,3])
arr
array([1, 2, 3])
arr = np.array([[1,2,3],[4,5,6]])
arr
array([[1, 2, 3],
[4, 5, 6]])
数组中存储的数据元素类型必须是统一类型
优先级:字符串 > 浮点型 > 整型
arr = np.array([1,2.2,3])
arr
array([1. , 2.2, 3. ])
import matplotlib.pyplot as plt
img_arr = plt.imread('./1.jpg') # 返回的数组,数组中装载的就是图片内容
plt.imshow(img_arr) # 将numpy数组进行可视化展示
<matplotlib.image.AxesImage at 0x117fb1b38>
img_arr = img_arr - 100 # 将每一个数组元素都减去100
plt.imshow(img_arr)
<matplotlib.image.AxesImage at 0x1181a6b38>
np.ones(shape=(3,4))
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
np.linspace(0,100,num=20) # 一维的等差数列数组
array([ 0. , 5.26315789, 10.52631579, 15.78947368,
21.05263158, 26.31578947, 31.57894737, 36.84210526,
42.10526316, 47.36842105, 52.63157895, 57.89473684,
63.15789474, 68.42105263, 73.68421053, 78.94736842,
84.21052632, 89.47368421, 94.73684211, 100. ])
np.arange(10,50,step=2) # 一维等差数列
array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,
44, 46, 48])
np.random.randint(0,100,size=(5,3))
array([[19, 0, 17],
[72, 29, 13],
[69, 59, 68],
[63, 54, 87],
[70, 64, 0]])
arr = np.random.randint(0,100,size=(5,6))
arr
array([[43, 96, 75, 1, 34, 88],
[96, 2, 17, 34, 26, 57],
[71, 36, 11, 11, 10, 29],
[72, 46, 51, 4, 27, 75],
[80, 42, 27, 55, 19, 43]])
arr.shape # 返回的是数组的形状
(5, 6)
arr.ndim # 返回的是数组的维度
2
arr.size # 返回数组元素的个数
30
arr.dtype # 返回的是数组元素的类型
dtype('int64')
type(arr) # 数组的数据类型
numpy.ndarray
arr = np.array([1,2,3])
arr.dtype
dtype('int64')
# 创建一个数组,指定数组元素类型为int32
arr = np.array([1,2,3],dtype='int32')
arr.dtype
dtype('int32')
arr.dtype = 'uint8' #修改数组的元素类型
arr.dtype
dtype('uint8')
arr = np.random.randint(1,100,size=(5,6))
arr
array([[69, 80, 7, 90, 31, 44],
[37, 57, 26, 92, 91, 34],
[13, 16, 93, 54, 87, 34],
[ 5, 16, 47, 66, 51, 12],
[54, 63, 20, 11, 94, 88]])
arr[1] # 取出了numpy数组中的下标为1的行数据
array([37, 57, 26, 92, 91, 34])
arr[[1,3,4]] # 取出多行
array([[37, 57, 26, 92, 91, 34],
[ 5, 16, 47, 66, 51, 12],
[54, 63, 20, 11, 94, 88]])
# 切出arr数组的前两行的数据
arr[0:2] # arr[行切片]
array([[69, 80, 7, 90, 31, 44],
[37, 57, 26, 92, 91, 34]])
# 切出arr数组中的前两列
arr[:,0:2] # arr[行切片,列切片]
array([[69, 80],
[37, 57],
[13, 16],
[ 5, 16],
[54, 63]])
# 切出前两行的前两列的数据
arr[0:2,0:2]
array([[69, 80],
[37, 57]])
arr
array([[69, 80, 7, 90, 31, 44],
[37, 57, 26, 92, 91, 34],
[13, 16, 93, 54, 87, 34],
[ 5, 16, 47, 66, 51, 12],
[54, 63, 20, 11, 94, 88]])
# 将数组的行倒置
arr[::-1]
array([[54, 63, 20, 11, 94, 88],
[ 5, 16, 47, 66, 51, 12],
[13, 16, 93, 54, 87, 34],
[37, 57, 26, 92, 91, 34],
[69, 80, 7, 90, 31, 44]])
# 将数组的列倒置
arr[:,::-1]
array([[44, 31, 90, 7, 80, 69],
[34, 91, 92, 26, 57, 37],
[34, 87, 54, 93, 16, 13],
[12, 51, 66, 47, 16, 5],
[88, 94, 11, 20, 63, 54]])
# 所有元素倒置
arr[::-1,::-1]
array([[88, 94, 11, 20, 63, 54],
[12, 51, 66, 47, 16, 5],
[34, 87, 54, 93, 16, 13],
[34, 91, 92, 26, 57, 37],
[44, 31, 90, 7, 80, 69]])
# 将一张图片进行左右翻转
img_arr = plt.imread('./1.jpg')
plt.imshow(img_arr)
<matplotlib.image.AxesImage at 0x1182c3b00>
img_arr.shape
(300, 450, 3)
plt.imshow(img_arr[:,::-1,:]) # img_arr[行,列,颜色]
<matplotlib.image.AxesImage at 0x11835cb70>
# 图片上下翻转
plt.imshow(img_arr[::-1,:,:])
<matplotlib.image.AxesImage at 0x118437ef0>
# 图片裁剪的功能
plt.imshow(img_arr[66:200,78:300,:])
<matplotlib.image.AxesImage at 0x1187fee48>
arr # 是一个5行6列的二维数组
array([[69, 80, 7, 90, 31, 44],
[37, 57, 26, 92, 91, 34],
[13, 16, 93, 54, 87, 34],
[ 5, 16, 47, 66, 51, 12],
[54, 63, 20, 11, 94, 88]])
# 将二维的数组变形成1维
arr_1 = arr.reshape((30,))
arr_1
array([69,80,7,90,31,44,37,57,26,92,91,34,13,16,93,54,87,
34,5,16,47,66,51,12,54,63,20,11,94,88])
# 将一维变形成多维
arr_1.reshape((6,5))
array([[69, 80, 7, 90, 31],
[44, 37, 57, 26, 92],
[91, 34, 13, 16, 93],
[54, 87, 34, 5, 16],
[47, 66, 51, 12, 54],
[63, 20, 11, 94, 88]])
np.concatenate((arr,arr),axis=1)
array([[69, 80, 7, 90, 31, 44, 69, 80, 7, 90, 31, 44],
[37, 57, 26, 92, 91, 34, 37, 57, 26, 92, 91, 34],
[13, 16, 93, 54, 87, 34, 13, 16, 93, 54, 87, 34],
[ 5, 16, 47, 66, 51, 12, 5, 16, 47, 66, 51, 12],
[54, 63, 20, 11, 94, 88, 54, 63, 20, 11, 94, 88]])
arr_3 = np.concatenate((img_arr,img_arr,img_arr),axis=0)
plt.imshow(arr_3)
<matplotlib.image.AxesImage at 0x118f459b0>
arr
array([[69, 80, 7, 90, 31, 44],
[37, 57, 26, 92, 91, 34],
[13, 16, 93, 54, 87, 34],
[ 5, 16, 47, 66, 51, 12],
[54, 63, 20, 11, 94, 88]])
arr.sum(axis=1)
array([321, 337, 297, 197, 330])
arr.max(axis=1)
array([90, 92, 93, 66, 94])
np.sin(2.5)
0.5984721441039564
np.around(3.84,2)
3.84
arr[1].std()
26.66718749491384
arr[1].var()
711.138888888889
np.eye(6)
array([[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.]])
arr.T
array([[69, 37, 13, 5, 54],
[80, 57, 16, 16, 63],
[ 7, 26, 93, 47, 20],
[90, 92, 54, 66, 11],
[31, 91, 87, 51, 94],
[44, 34, 34, 12, 88]])
a1 = np.array([[2,1],[4,3]])
a2 = np.array([[1,2],[1,0]])
np.dot(a1,a2)
array([[3, 4],
[7, 8]])